Browse Source

Add AppViewModel for Desktop

pull/5829/head
2dust 2 months ago
parent
commit
f40eb724d7
  1. 11
      v2rayN/v2rayN.Desktop/App.axaml
  2. 37
      v2rayN/v2rayN.Desktop/App.axaml.cs
  3. 51
      v2rayN/v2rayN.Desktop/ViewModels/AppViewModel.cs

11
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">
<Application.Styles>
<StyleInclude Source="Styles/GlobalStyles.axaml" />
<StyleInclude Source="avares://Semi.Avalonia/Themes/Index.axaml" />
@ -32,13 +33,13 @@
ToolTipText="v2rayN Desktop">
<TrayIcon.Menu>
<NativeMenu>
<NativeMenuItem Click="MenuAddServerViaClipboardClick" Header="{x:Static resx:ResUI.menuAddServerViaClipboard}" />
<NativeMenuItem Command="{Binding AddServerViaClipboardCmd}" Header="{x:Static resx:ResUI.menuAddServerViaClipboard}" />
<NativeMenuItem Header="{x:Static resx:ResUI.menuAddServerViaScan}" IsVisible="False" />
<NativeMenuItem Click="MenuSubUpdate_Click" Header="{x:Static resx:ResUI.menuSubUpdate}" />
<NativeMenuItem Click="MenuSubUpdateViaProxy_Click" Header="{x:Static resx:ResUI.menuSubUpdateViaProxy}" />
<NativeMenuItem Command="{Binding SubUpdateCmd}" Header="{x:Static resx:ResUI.menuSubUpdate}" />
<NativeMenuItem Command="{Binding SubUpdateViaProxyCmd}" Header="{x:Static resx:ResUI.menuSubUpdateViaProxy}" />
<NativeMenuItemSeparator />
<NativeMenuItem Click="TrayIcon_Clicked" Header="{x:Static resx:ResUI.menuShowOrHideMainWindow}" />
<NativeMenuItem Click="MenuExit_Click" Header="{x:Static resx:ResUI.menuExit}" />
<NativeMenuItem Command="{Binding ExitCmd}" Header="{x:Static resx:ResUI.menuExit}" />
</NativeMenu>
</TrayIcon.Menu>
</TrayIcon>

37
v2rayN/v2rayN.Desktop/App.axaml.cs

@ -2,14 +2,14 @@ using Avalonia;
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 partial class App : Application
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
this.DataContext = new AppViewModel();
}
public override void OnFrameworkInitializationCompleted()
@ -65,7 +67,7 @@ public partial class App : Application
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 @@ public partial class App : Application
}
}
}
private void MenuAddServerViaClipboardClick(object? sender, EventArgs e)
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
var clipboardData = AvaUtils.GetClipboardData(desktop.MainWindow).Result;
Locator.Current.GetService<MainWindowViewModel>()?.AddServerViaClipboardAsync(clipboardData);
}
}
private void MenuSubUpdate_Click(object? sender, EventArgs e)
{
Locator.Current.GetService<MainWindowViewModel>()?.UpdateSubscriptionProcess("", false);
}
private void MenuSubUpdateViaProxy_Click(object? sender, EventArgs e)
{
Locator.Current.GetService<MainWindowViewModel>()?.UpdateSubscriptionProcess("", true);
}
private void MenuExit_Click(object? sender, EventArgs e)
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
Locator.Current.GetService<MainWindowViewModel>()?.MyAppExitAsync(false);
desktop.Shutdown();
}
}
}

51
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<Unit, Unit> AddServerViaClipboardCmd { get; }
public ReactiveCommand<Unit, Unit> SubUpdateCmd { get; }
public ReactiveCommand<Unit, Unit> SubUpdateViaProxyCmd { get; }
public ReactiveCommand<Unit, Unit> ExitCmd { get; }
public AppViewModel()
{
_config = LazyConfig.Instance.Config;
_noticeHandler = Locator.Current.GetService<NoticeHandler>();
AddServerViaClipboardCmd = ReactiveCommand.Create(() =>
{
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
var clipboardData = AvaUtils.GetClipboardData(desktop.MainWindow).Result;
Locator.Current.GetService<MainWindowViewModel>()?.AddServerViaClipboardAsync(clipboardData);
}
});
SubUpdateCmd = ReactiveCommand.Create(() =>
{
Locator.Current.GetService<MainWindowViewModel>()?.UpdateSubscriptionProcess("", false);
});
SubUpdateViaProxyCmd = ReactiveCommand.Create(() =>
{
Locator.Current.GetService<MainWindowViewModel>()?.UpdateSubscriptionProcess("", true);
});
ExitCmd = ReactiveCommand.Create(() =>
{
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
Locator.Current.GetService<MainWindowViewModel>()?.MyAppExitAsync(false);
desktop.Shutdown();
}
});
}
}
}
Loading…
Cancel
Save