From f40eb724d795d308eb4dfc98ae64306bb9a0aa29 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 6 Oct 2024 21:16:17 +0800 Subject: [PATCH] Add AppViewModel for Desktop --- v2rayN/v2rayN.Desktop/App.axaml | 11 ++-- v2rayN/v2rayN.Desktop/App.axaml.cs | 37 ++------------ .../v2rayN.Desktop/ViewModels/AppViewModel.cs | 51 +++++++++++++++++++ 3 files changed, 62 insertions(+), 37 deletions(-) create mode 100644 v2rayN/v2rayN.Desktop/ViewModels/AppViewModel.cs diff --git a/v2rayN/v2rayN.Desktop/App.axaml b/v2rayN/v2rayN.Desktop/App.axaml index 365b648e11..edfa18e6dd 100644 --- a/v2rayN/v2rayN.Desktop/App.axaml +++ b/v2rayN/v2rayN.Desktop/App.axaml @@ -2,9 +2,10 @@ x:Class="v2rayN.Desktop.App" xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:local="using:v2rayN.Desktop.ViewModels" xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib" + x:DataType="local:AppViewModel" RequestedThemeVariant="Default"> - @@ -32,13 +33,13 @@ ToolTipText="v2rayN Desktop"> - + - - + + - + diff --git a/v2rayN/v2rayN.Desktop/App.axaml.cs b/v2rayN/v2rayN.Desktop/App.axaml.cs index 7743a403ba..dbd26f537c 100644 --- a/v2rayN/v2rayN.Desktop/App.axaml.cs +++ b/v2rayN/v2rayN.Desktop/App.axaml.cs @@ -2,14 +2,14 @@ using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; using Splat; -using v2rayN.Desktop.Common; +using v2rayN.Desktop.ViewModels; using v2rayN.Desktop.Views; namespace v2rayN.Desktop; public partial class App : Application { - public static EventWaitHandle ProgramStarted; + //public static EventWaitHandle ProgramStarted; private static Config _config; public override void Initialize() @@ -19,6 +19,8 @@ public override void Initialize() AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; + + this.DataContext = new AppViewModel(); } public override void OnFrameworkInitializationCompleted() @@ -65,7 +67,7 @@ private void Init() LazyConfig.Instance.SetConfig(_config); Locator.CurrentMutable.RegisterLazySingleton(() => new NoticeHandler(), typeof(NoticeHandler)); Thread.CurrentThread.CurrentUICulture = new(_config.uiItem.currentLanguage); - + //Under Win10 if (Utils.IsWindows() && Environment.OSVersion.Version.Major < 10) { @@ -104,33 +106,4 @@ private void TrayIcon_Clicked(object? sender, EventArgs e) } } } - - private void MenuAddServerViaClipboardClick(object? sender, EventArgs e) - { - if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) - { - var clipboardData = AvaUtils.GetClipboardData(desktop.MainWindow).Result; - Locator.Current.GetService()?.AddServerViaClipboardAsync(clipboardData); - } - } - - private void MenuSubUpdate_Click(object? sender, EventArgs e) - { - Locator.Current.GetService()?.UpdateSubscriptionProcess("", false); - } - - private void MenuSubUpdateViaProxy_Click(object? sender, EventArgs e) - { - Locator.Current.GetService()?.UpdateSubscriptionProcess("", true); - } - - private void MenuExit_Click(object? sender, EventArgs e) - { - if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) - { - Locator.Current.GetService()?.MyAppExitAsync(false); - - desktop.Shutdown(); - } - } } \ No newline at end of file diff --git a/v2rayN/v2rayN.Desktop/ViewModels/AppViewModel.cs b/v2rayN/v2rayN.Desktop/ViewModels/AppViewModel.cs new file mode 100644 index 0000000000..e38132295b --- /dev/null +++ b/v2rayN/v2rayN.Desktop/ViewModels/AppViewModel.cs @@ -0,0 +1,51 @@ +using Avalonia; +using Avalonia.Controls.ApplicationLifetimes; +using ReactiveUI; +using Splat; +using System.Reactive; +using v2rayN.Desktop.Common; + +namespace v2rayN.Desktop.ViewModels +{ + public class AppViewModel : MyReactiveObject + { + public ReactiveCommand AddServerViaClipboardCmd { get; } + public ReactiveCommand SubUpdateCmd { get; } + public ReactiveCommand SubUpdateViaProxyCmd { get; } + public ReactiveCommand ExitCmd { get; } + + public AppViewModel() + { + _config = LazyConfig.Instance.Config; + _noticeHandler = Locator.Current.GetService(); + + AddServerViaClipboardCmd = ReactiveCommand.Create(() => + { + if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + { + var clipboardData = AvaUtils.GetClipboardData(desktop.MainWindow).Result; + Locator.Current.GetService()?.AddServerViaClipboardAsync(clipboardData); + } + }); + + SubUpdateCmd = ReactiveCommand.Create(() => + { + Locator.Current.GetService()?.UpdateSubscriptionProcess("", false); + }); + SubUpdateViaProxyCmd = ReactiveCommand.Create(() => + { + Locator.Current.GetService()?.UpdateSubscriptionProcess("", true); + }); + + ExitCmd = ReactiveCommand.Create(() => + { + if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + { + Locator.Current.GetService()?.MyAppExitAsync(false); + + desktop.Shutdown(); + } + }); + } + } +} \ No newline at end of file