From a28cef5b988a9ac0929eaf8d304d4ee472b4e025 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sat, 7 Dec 2024 21:13:57 +0800 Subject: [PATCH] Fixed the issue of save window size when exit https://github.com/2dust/v2rayN/issues/6251 --- v2rayN/ServiceLib/Enums/EMsgCommand.cs | 3 ++- .../ViewModels/BackupAndRestoreViewModel.cs | 6 ++--- .../ViewModels/MainWindowViewModel.cs | 1 + .../v2rayN.Desktop/Views/MainWindow.axaml.cs | 4 ++-- .../v2rayN.Desktop/Views/ProfilesView.axaml | 23 ++++++++----------- .../Views/ProfilesView.axaml.cs | 12 +++------- v2rayN/v2rayN/Views/MainWindow.xaml.cs | 9 ++------ v2rayN/v2rayN/Views/ProfilesView.xaml.cs | 11 +++------ 8 files changed, 25 insertions(+), 44 deletions(-) diff --git a/v2rayN/ServiceLib/Enums/EMsgCommand.cs b/v2rayN/ServiceLib/Enums/EMsgCommand.cs index c955e830..86d5213e 100644 --- a/v2rayN/ServiceLib/Enums/EMsgCommand.cs +++ b/v2rayN/ServiceLib/Enums/EMsgCommand.cs @@ -6,6 +6,7 @@ SendMsgView, SendSnackMsg, RefreshProfiles, - StopSpeedtest + StopSpeedtest, + AppExit } } \ No newline at end of file diff --git a/v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs b/v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs index 35c3d60d..804d46eb 100644 --- a/v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/BackupAndRestoreViewModel.cs @@ -160,9 +160,9 @@ namespace ServiceLib.ViewModels var configDirZipTemp = Utils.GetTempPath($"v2rayN_{DateTime.Now:yyyyMMddHHmmss}"); var configDirTemp = Path.Combine(configDirZipTemp, _guiConfigs); - await Task.Run(() => FileManager.CopyDirectory(configDir, configDirTemp, false, "cache.db")); - var ret = await Task.Run(() => FileManager.CreateFromDirectory(configDirZipTemp, fileName)); - await Task.Run(() => Directory.Delete(configDirZipTemp, true)); + FileManager.CopyDirectory(configDir, configDirTemp, false, "cache.db"); + var ret = FileManager.CreateFromDirectory(configDirZipTemp, fileName); + Directory.Delete(configDirZipTemp, true); return ret; } } diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index ff680c3d..2de9b453 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -283,6 +283,7 @@ namespace ServiceLib.ViewModels try { Logging.SaveLog("MyAppExitAsync Begin"); + MessageBus.Current.SendMessage("", EMsgCommand.AppExit.ToString()); await ConfigHandler.SaveConfig(_config); await SysProxyHandler.UpdateSysProxy(_config, true); diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs index 9bd228af..279b6063 100644 --- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs @@ -156,6 +156,7 @@ namespace v2rayN.Desktop.Views RestoreUI(); AddHelpMenuItem(); //WindowsHandler.Instance.RegisterGlobalHotkey(_config, OnHotkeyHandler, null); + MessageBus.Current.Listen(EMsgCommand.AppExit.ToString()).Subscribe(StorageUI); } #region Event @@ -441,7 +442,7 @@ namespace v2rayN.Desktop.Views } } - private void StorageUI() + private void StorageUI(string? n = null) { _config.UiItem.MainWidth = Utils.ToInt(this.Width); _config.UiItem.MainHeight = Utils.ToInt(this.Height); @@ -456,7 +457,6 @@ namespace v2rayN.Desktop.Views _config.UiItem.MainGirdHeight1 = Math.Ceiling(gridMain1.RowDefinitions[0].ActualHeight + 0.1); _config.UiItem.MainGirdHeight2 = Math.Ceiling(gridMain1.RowDefinitions[2].ActualHeight + 0.1); } - ConfigHandler.SaveConfig(_config); } private void AddHelpMenuItem() diff --git a/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml b/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml index de532782..2985c633 100644 --- a/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml +++ b/v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml @@ -56,28 +56,23 @@ Foreground="{DynamicResource ButtonDefaultTertiaryForeground}" /> - - + - - - - - - - + + (EMsgCommand.AppExit.ToString()).Subscribe(StorageUI); } private async void LstProfiles_Sorting(object? sender, DataGridColumnEventArgs e) @@ -323,12 +323,7 @@ namespace v2rayN.Desktop.Views ViewModel?.RefreshServers(); } } - - private void MenuStorageUI_Click(object? sender, RoutedEventArgs e) - { - StorageUI(); - } - + //#endregion Event //#region UI @@ -368,7 +363,7 @@ namespace v2rayN.Desktop.Views } } - private void StorageUI() + private void StorageUI(string? n = null) { List lvColumnItem = new(); for (int k = 0; k < lstProfiles.Columns.Count; k++) @@ -386,7 +381,6 @@ namespace v2rayN.Desktop.Views }); } _config.UiItem.MainColumnItem = lvColumnItem; - ConfigHandler.SaveConfig(_config); } //#endregion UI diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs index cd467d67..5ad3c04a 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs @@ -26,7 +26,6 @@ namespace v2rayN.Views _config = AppHandler.Instance.Config; ThreadPool.RegisterWaitForSingleObject(App.ProgramStarted, OnProgramStarted, null, -1, false); - Application.Current.Exit += Current_Exit; App.Current.SessionEnding += Current_SessionEnding; this.Closing += MainWindow_Closing; this.PreviewKeyDown += MainWindow_PreviewKeyDown; @@ -142,6 +141,7 @@ namespace v2rayN.Views RestoreUI(); AddHelpMenuItem(); WindowsHandler.Instance.RegisterGlobalHotkey(_config, OnHotkeyHandler, null); + MessageBus.Current.Listen(EMsgCommand.AppExit.ToString()).Subscribe(StorageUI); } #region Event @@ -265,11 +265,6 @@ namespace v2rayN.Views ShowHideWindow(false); } - private void Current_Exit(object sender, ExitEventArgs e) - { - StorageUI(); - } - private async void Current_SessionEnding(object sender, SessionEndingCancelEventArgs e) { Logging.SaveLog("Current_SessionEnding"); @@ -402,7 +397,7 @@ namespace v2rayN.Views } } - private void StorageUI() + private void StorageUI(string? n = null) { _config.UiItem.MainWidth = Utils.ToInt(this.Width); _config.UiItem.MainHeight = Utils.ToInt(this.Height); diff --git a/v2rayN/v2rayN/Views/ProfilesView.xaml.cs b/v2rayN/v2rayN/Views/ProfilesView.xaml.cs index 36101b41..e0671eb0 100644 --- a/v2rayN/v2rayN/Views/ProfilesView.xaml.cs +++ b/v2rayN/v2rayN/Views/ProfilesView.xaml.cs @@ -1,6 +1,7 @@ using MaterialDesignThemes.Wpf; using ReactiveUI; using Splat; +using System; using System.Reactive.Disposables; using System.Windows; using System.Windows.Controls; @@ -24,7 +25,6 @@ namespace v2rayN.Views _config = AppHandler.Instance.Config; - Application.Current.Exit += Current_Exit; btnAutofitColumnWidth.Click += BtnAutofitColumnWidth_Click; txtServerFilter.PreviewKeyDown += TxtServerFilter_PreviewKeyDown; lstProfiles.PreviewKeyDown += LstProfiles_PreviewKeyDown; @@ -90,15 +90,11 @@ namespace v2rayN.Views RestoreUI(); ViewModel?.RefreshServers(); + MessageBus.Current.Listen(EMsgCommand.AppExit.ToString()).Subscribe(StorageUI); } #region Event - private void Current_Exit(object sender, ExitEventArgs e) - { - StorageUI(); - } - private async Task UpdateViewHandler(EViewAction action, object? obj) { switch (action) @@ -356,7 +352,7 @@ namespace v2rayN.Views } } - private void StorageUI() + private void StorageUI(string? n = null) { List lvColumnItem = new(); for (int k = 0; k < lstProfiles.Columns.Count; k++) @@ -370,7 +366,6 @@ namespace v2rayN.Views }); } _config.UiItem.MainColumnItem = lvColumnItem; - ConfigHandler.SaveConfig(_config); } #endregion UI