using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Platform.Storage; using LibVLCSharp.Avalonia; using LibVLCSharp.Shared; using Newtonsoft.Json; using Tesses.VirtualFilesystem; using Tesses.VirtualFilesystem.Extensions; using Tesses.VirtualFilesystem.Filesystems; namespace Tesses.CMS.Avalonia.Desktop; internal class DesktopPlatform : IPlatform { public DesktopPlatform() { if(rootfs.FileExists(Special.LocalAppData / "tesses_cms_app.json")) { var conf=JsonConvert.DeserializeObject(rootfs.ReadAllText(Special.LocalAppData/"tesses_cms_app.json")); if(conf != null) _conf = conf; var path=UnixPath.FromLocal(_conf.DownloadPath); virtualFilesystem = rootfs.GetSubdirFilesystem(path); } } LocalFileSystem rootfs=new LocalFileSystem(); Configuration _conf = new Configuration(); public Configuration Configuration => _conf; public bool PlatformUsesNormalPathsForDownload => true; IVirtualFilesystem? virtualFilesystem; public IVirtualFilesystem? DownloadFilesystem => virtualFilesystem; public bool CanTakeScreenShots => true; public string ScreenshotPath => DownloadFilesystem == null ? Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) : Path.Combine(Configuration.DownloadPath,"Screenshots"); public bool MustMoveScreenshot => false; public async Task BrowseForDownloadDirectoryAsync() { var sp=App.Window?.StorageProvider; if(sp != null) { var res=await sp.OpenFolderPickerAsync(new global::Avalonia.Platform.Storage.FolderPickerOpenOptions{Title="Browse for a downloads folder"}); if(res.Count > 0) { var first=res.First(); string? path=first.TryGetLocalPath(); if(!string.IsNullOrWhiteSpace(path)) { _conf.DownloadPath = path; await WriteConfigurationAsync(); } } } } public async Task ReadConfigurationAsync() { if(await rootfs.FileExistsAsync(Special.LocalAppData / "tesses_cms_app.json")) { var conf=JsonConvert.DeserializeObject(await rootfs.ReadAllTextAsync(Special.LocalAppData/"tesses_cms_app.json")); if(conf != null) _conf = conf; } } public async Task WriteConfigurationAsync() { await rootfs.WriteAllTextAsync(Special.LocalAppData / "tesses_cms_app.json",JsonConvert.SerializeObject(_conf)); var path=UnixPath.FromLocal(_conf.DownloadPath); virtualFilesystem = rootfs.GetSubdirFilesystem(path); } public Control CreatePlayer() { return new VideoView(); } public void SetMediaPlayer(Control control, MediaPlayer? mediaPlayer) { var ctrl = control as VideoView; if(ctrl != null) ctrl.MediaPlayer = mediaPlayer; } public MediaPlayer? GetMediaPlayer(Control control) { var ctrl = control as VideoView; if(ctrl != null) return ctrl.MediaPlayer; return null; } public void LaunchUrl(string url) { Process p = new Process(); p.StartInfo.UseShellExecute=true; p.StartInfo.FileName = url; p.Start(); } }