mirror of
https://github.com/2dust/v2rayN.git
synced 2026-11-16 17:35:01 +03:00
Creating a new desktop app with avaloniaui
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Media.Imaging;
|
||||
using Avalonia.Platform;
|
||||
using System.Reflection;
|
||||
|
||||
namespace v2rayN.Desktop.Common
|
||||
{
|
||||
internal class AvaUtils
|
||||
{
|
||||
public static async Task<string?> GetClipboardData(Window owner)
|
||||
{
|
||||
try
|
||||
{
|
||||
var clipboard = TopLevel.GetTopLevel(owner)?.Clipboard;
|
||||
if (clipboard == null) return null;
|
||||
return await clipboard.GetTextAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task SetClipboardData(Visual? visual, string strData)
|
||||
{
|
||||
try
|
||||
{
|
||||
var clipboard = TopLevel.GetTopLevel(visual)?.Clipboard;
|
||||
if (clipboard == null) return;
|
||||
var dataObject = new DataObject();
|
||||
dataObject.Set(DataFormats.Text, strData);
|
||||
await clipboard.SetDataObjectAsync(dataObject);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public static WindowIcon GetAppIcon(ESysProxyType sysProxyType)
|
||||
{
|
||||
int index = 1;
|
||||
switch (sysProxyType)
|
||||
{
|
||||
case ESysProxyType.ForcedClear:
|
||||
index = 1;
|
||||
break;
|
||||
|
||||
case ESysProxyType.ForcedChange:
|
||||
case ESysProxyType.Pac:
|
||||
index = 2;
|
||||
break;
|
||||
|
||||
case ESysProxyType.Unchanged:
|
||||
index = 3;
|
||||
break;
|
||||
}
|
||||
var uri = new Uri($"avares://{Assembly.GetExecutingAssembly().GetName().Name}/Assets/NotifyIcon{index}.ico");
|
||||
using var bitmap = new Bitmap(AssetLoader.Open(uri));
|
||||
return new(bitmap);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Platform.Storage;
|
||||
using MsBox.Avalonia;
|
||||
using MsBox.Avalonia.Enums;
|
||||
|
||||
namespace v2rayN.Desktop.Common
|
||||
{
|
||||
internal class UI
|
||||
{
|
||||
private static readonly string caption = Global.AppName;
|
||||
|
||||
public static async Task<ButtonResult> ShowYesNo(Window owner, string msg)
|
||||
{
|
||||
var box = MessageBoxManager.GetMessageBoxStandard(caption, msg, ButtonEnum.YesNo);
|
||||
return await box.ShowWindowDialogAsync(owner);
|
||||
}
|
||||
|
||||
public static async Task<string?> OpenFileDialog(Window owner, FilePickerFileType? filter)
|
||||
{
|
||||
var topLevel = TopLevel.GetTopLevel(owner);
|
||||
if (topLevel == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
// Start async operation to open the dialog.
|
||||
var files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
|
||||
{
|
||||
AllowMultiple = false,
|
||||
FileTypeFilter = filter is null ? [FilePickerFileTypes.All, FilePickerFileTypes.ImagePng] : [filter]
|
||||
});
|
||||
|
||||
return files.FirstOrDefault()?.TryGetLocalPath();
|
||||
}
|
||||
|
||||
public static async Task<string?> SaveFileDialog(Window owner, string filter)
|
||||
{
|
||||
var topLevel = TopLevel.GetTopLevel(owner);
|
||||
if (topLevel == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
// Start async operation to open the dialog.
|
||||
var files = await topLevel.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions
|
||||
{
|
||||
});
|
||||
|
||||
return files?.TryGetLocalPath();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user