tesses-cms/Tesses.CMS.Avalonia/Tesses.CMS.Avalonia.Desktop/DesktopPlatform.cs

109 lines
3.4 KiB
C#
Raw Normal View History

2024-08-10 18:32:16 +00:00
using System;
using System.Diagnostics;
2024-07-28 22:59:28 +00:00
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<Configuration>(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;
2024-08-10 18:32:16 +00:00
public bool CanTakeScreenShots => true;
public string ScreenshotPath => DownloadFilesystem == null ? Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) : Path.Combine(Configuration.DownloadPath,"Screenshots");
public bool MustMoveScreenshot => false;
2024-07-28 22:59:28 +00:00
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<Configuration>(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()
{
2024-08-10 18:32:16 +00:00
return new VideoView();
2024-07-28 22:59:28 +00:00
}
public void SetMediaPlayer(Control control, MediaPlayer? mediaPlayer)
{
2024-08-10 18:32:16 +00:00
var ctrl = control as VideoView;
2024-07-28 22:59:28 +00:00
if(ctrl != null)
ctrl.MediaPlayer = mediaPlayer;
}
public MediaPlayer? GetMediaPlayer(Control control)
{
2024-08-10 18:32:16 +00:00
var ctrl = control as VideoView;
2024-07-28 22:59:28 +00:00
if(ctrl != null)
return ctrl.MediaPlayer;
return null;
}
2024-08-10 18:32:16 +00:00
public void LaunchUrl(string url)
{
Process p = new Process();
p.StartInfo.UseShellExecute=true;
p.StartInfo.FileName = url;
p.Start();
}
2024-07-28 22:59:28 +00:00
}