Skip to content

Commit

Permalink
Add exit function to the main interface for Desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Nov 3, 2024
1 parent 4f05b93 commit 258e822
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 16 deletions.
9 changes: 9 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1363,4 +1363,7 @@
<data name="TbSettingsCurrentFontFamilyLinuxTip" xml:space="preserve">
<value>Install the font to the system and restart the settings</value>
</data>
<data name="menuExitTips" xml:space="preserve">
<value>Are you sure to exit?</value>
</data>
</root>
3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.zh-Hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1360,4 +1360,7 @@
<data name="TbSettingsCurrentFontFamilyLinuxTip" xml:space="preserve">
<value>安装字体到系统中,重启设置</value>
</data>
<data name="menuExitTips" xml:space="preserve">
<value>是否确定退出?</value>
</data>
</root>
3 changes: 3 additions & 0 deletions v2rayN/ServiceLib/Resx/ResUI.zh-Hant.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1240,4 +1240,7 @@
<data name="TbSettingsCurrentFontFamilyLinuxTip" xml:space="preserve">
<value>安裝字體到系統中,重新啟動設定</value>
</data>
<data name="menuExitTips" xml:space="preserve">
<value>是否確定退出?</value>
</data>
</root>
17 changes: 17 additions & 0 deletions v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class MainWindowViewModel : MyReactiveObject

public ReactiveCommand<Unit, Unit> ReloadCmd { get; }

public ReactiveCommand<Unit, Unit> ExitCmd { get; }

[Reactive]
public bool BlReloadEnabled { get; set; }

Expand Down Expand Up @@ -187,6 +189,11 @@ public MainWindowViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
await Reload();
});

ExitCmd = ReactiveCommand.CreateFromTask(async () =>
{
await Exit();
});

RegionalPresetDefaultCmd = ReactiveCommand.CreateFromTask(async () =>
{
await ApplyRegionalPreset(EPresetType.Default);
Expand Down Expand Up @@ -588,6 +595,16 @@ private async Task AutoHideStartup()
}
}

private async Task Exit()
{
if (await _updateView?.Invoke(EViewAction.ShowYesNo, null) == false)
{
return;
}

await MyAppExitAsync(false);
}

#endregion core job

#region Presets
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN.Desktop/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
<MenuItem x:Name="menuClose" Padding="8,0">
<MenuItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Static resx:ResUI.menuClose}" />
<TextBlock Text="{x:Static resx:ResUI.menuExit}" />
</StackPanel>
</MenuItem.Header>
</MenuItem>
Expand Down
29 changes: 20 additions & 9 deletions v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Avalonia.ReactiveUI;
using Avalonia.Threading;
using DialogHostAvalonia;
using MsBox.Avalonia.Enums;
using ReactiveUI;
using Splat;
using System.ComponentModel;
Expand All @@ -28,12 +29,11 @@ public MainWindow()

_config = AppHandler.Instance.Config;
_manager = new WindowNotificationManager(TopLevel.GetTopLevel(this)) { MaxItems = 3, Position = NotificationPosition.BottomRight };

this.Closing += MainWindow_Closing;
this.KeyDown += MainWindow_KeyDown;
menuSettingsSetUWP.Click += menuSettingsSetUWP_Click;
menuPromotion.Click += menuPromotion_Click;
menuClose.Click += menuClose_Click;
menuCheckUpdate.Click += MenuCheckUpdate_Click;
menuBackupAndRestore.Click += MenuBackupAndRestore_Click;

Expand Down Expand Up @@ -80,6 +80,7 @@ public MainWindow()
this.BindCommand(ViewModel, vm => vm.ReloadCmd, v => v.menuReload).DisposeWith(disposables);
this.OneWayBind(ViewModel, vm => vm.BlReloadEnabled, v => v.menuReload.IsEnabled).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.ExitCmd, v => v.menuClose).DisposeWith(disposables);
switch (_config.UiItem.MainGirdOrientation)
{
Expand Down Expand Up @@ -242,6 +243,14 @@ private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
Locator.Current.GetService<ProfilesViewModel>()?.AutofitColumnWidthAsync(),
DispatcherPriority.Default);
break;

case EViewAction.ShowYesNo:
if (await UI.ShowYesNo(this, ResUI.menuExitTips) == ButtonResult.No)
{
return false;
}
StorageUI();
break;
}

return await Task.FromResult(true);
Expand Down Expand Up @@ -304,12 +313,6 @@ private async void MainWindow_KeyDown(object? sender, KeyEventArgs e)
}
}

private void menuClose_Click(object? sender, RoutedEventArgs e)
{
StorageUI();
ShowHideWindow(false);
}

private void menuPromotion_Click(object? sender, RoutedEventArgs e)
{
Utils.ProcessStart($"{Utils.Base64Decode(Global.PromotionUrl)}?t={DateTime.Now.Ticks}");
Expand Down Expand Up @@ -376,8 +379,16 @@ public void ShowHideWindow(bool? blShow)
}
else
{
this.Hide();
if (Utils.IsWindows())
{
this.Hide();
}
else
{
this.WindowState = WindowState.Minimized;
}
}

_config.UiItem.ShowInTaskbar = bl;
}

Expand Down
12 changes: 6 additions & 6 deletions v2rayN/v2rayN/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,17 @@ public void ShowHideWindow(bool? blShow)
var bl = blShow ?? !_config.UiItem.ShowInTaskbar;
if (bl)
{
Application.Current.MainWindow.Show();
if (Application.Current.MainWindow.WindowState == WindowState.Minimized)
this?.Show();
if (this?.WindowState == WindowState.Minimized)
{
Application.Current.MainWindow.WindowState = WindowState.Normal;
this.WindowState = WindowState.Normal;
}
Application.Current.MainWindow.Activate();
Application.Current.MainWindow.Focus();
this?.Activate();
this?.Focus();
}
else
{
Application.Current.MainWindow.Hide();
this?.Hide();
}
_config.UiItem.ShowInTaskbar = bl;
}
Expand Down

0 comments on commit 258e822

Please sign in to comment.