Skip to content

Commit

Permalink
Add AppViewModel for Desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 6, 2024
1 parent 4d84eed commit f40eb72
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 37 deletions.
11 changes: 6 additions & 5 deletions v2rayN/v2rayN.Desktop/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down Expand Up @@ -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>
Expand Down
37 changes: 5 additions & 32 deletions v2rayN/v2rayN.Desktop/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<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 changes: 51 additions & 0 deletions v2rayN/v2rayN.Desktop/ViewModels/AppViewModel.cs
Original file line number Diff line number Diff line change
@@ -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();
}
});
}
}
}

0 comments on commit f40eb72

Please sign in to comment.