From 3286e8e24de3d079a185b167f9ea1cd64a340aee Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 15 Aug 2024 13:51:30 +0800 Subject: [PATCH] Refactor code to decouple view and viewmodel --- v2rayN/v2rayN/Common/Utils.cs | 67 ------------------- v2rayN/v2rayN/Common/WindowsUtils.cs | 62 ++++++++++++++++- .../v2rayN/ViewModels/MainWindowViewModel.cs | 10 ++- .../ViewModels/OptionSettingViewModel.cs | 13 +--- v2rayN/v2rayN/ViewModels/ProfilesViewModel.cs | 5 ++ v2rayN/v2rayN/Views/AddServerWindow.xaml.cs | 1 + v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs | 1 + v2rayN/v2rayN/Views/MainWindow.xaml.cs | 2 +- .../v2rayN/Views/OptionSettingWindow.xaml.cs | 2 + v2rayN/v2rayN/Views/ProfilesView.xaml.cs | 7 ++ .../Views/RoutingRuleDetailsWindow.xaml.cs | 2 + .../Views/RoutingRuleSettingWindow.xaml.cs | 2 + .../v2rayN/Views/RoutingSettingWindow.xaml.cs | 2 + v2rayN/v2rayN/Views/SubEditWindow.xaml.cs | 2 + v2rayN/v2rayN/Views/SubSettingWindow.xaml.cs | 2 + 15 files changed, 94 insertions(+), 86 deletions(-) diff --git a/v2rayN/v2rayN/Common/Utils.cs b/v2rayN/v2rayN/Common/Utils.cs index 77169165..c4fb551e 100644 --- a/v2rayN/v2rayN/Common/Utils.cs +++ b/v2rayN/v2rayN/Common/Utils.cs @@ -7,7 +7,6 @@ using System.Net.NetworkInformation; using System.Net.Sockets; using System.Reflection; using System.Security.Cryptography; -using System.Security.Principal; using System.Text; using System.Text.RegularExpressions; @@ -404,7 +403,6 @@ namespace v2rayN } } - public static bool IsNullOrEmpty(string? text) { if (string.IsNullOrWhiteSpace(text)) @@ -611,7 +609,6 @@ namespace v2rayN } } - /// /// 取得GUID /// @@ -636,26 +633,6 @@ namespace v2rayN return string.Empty; } - /// - /// IsAdministrator - /// - /// - public static bool IsAdministrator() - { - try - { - WindowsIdentity current = WindowsIdentity.GetCurrent(); - WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current); - //WindowsBuiltInRole可以枚举出很多权限,例如系统用户、User、Guest等等 - return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator); - } - catch (Exception ex) - { - Logging.SaveLog(ex.Message, ex); - return false; - } - } - public static string GetDownloadFileName(string url) { var fileName = Path.GetFileName(url); @@ -871,49 +848,5 @@ namespace v2rayN } #endregion TempPath - - #region 开机自动启动等 - - /// - /// 开机自动启动 - /// - /// - /// - public static void SetAutoRun(string AutoRunRegPath, string AutoRunName, bool run) - { - try - { - var autoRunName = $"{AutoRunName}_{GetMD5(StartupPath())}"; - WindowsUtils. - - //delete first - RegWriteValue(AutoRunRegPath, autoRunName, ""); - if (IsAdministrator()) - { - WindowsUtils.AutoStart(autoRunName, "", ""); - } - - if (run) - { - string exePath = GetExePath(); - if (IsAdministrator()) - { - WindowsUtils.AutoStart(autoRunName, exePath, ""); - } - else - { - WindowsUtils.RegWriteValue(AutoRunRegPath, autoRunName, exePath.AppendQuotes()); - } - } - } - catch (Exception ex) - { - Logging.SaveLog(ex.Message, ex); - } - } - - #endregion 开机自动启动等 - - } } \ No newline at end of file diff --git a/v2rayN/v2rayN/Common/WindowsUtils.cs b/v2rayN/v2rayN/Common/WindowsUtils.cs index 09f13cc1..6e02c995 100644 --- a/v2rayN/v2rayN/Common/WindowsUtils.cs +++ b/v2rayN/v2rayN/Common/WindowsUtils.cs @@ -17,7 +17,6 @@ namespace v2rayN { internal static class WindowsUtils { - /// /// 获取剪贴板数 /// @@ -55,6 +54,7 @@ namespace v2rayN { } } + /// /// Auto Start via TaskService /// @@ -199,16 +199,72 @@ namespace v2rayN } } - public static void SetDarkBorder(System.Windows.Window window, bool dark) + public static void SetDarkBorder(Window window, bool dark) { // Make sure the handle is created before the window is shown - IntPtr hWnd = new System.Windows.Interop.WindowInteropHelper(window).EnsureHandle(); + IntPtr hWnd = new WindowInteropHelper(window).EnsureHandle(); int attribute = dark ? 1 : 0; uint attributeSize = (uint)Marshal.SizeOf(attribute); DwmSetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, ref attribute, attributeSize); DwmSetWindowAttribute(hWnd, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, ref attribute, attributeSize); } + /// + /// IsAdministrator + /// + /// + public static bool IsAdministrator() + { + try + { + WindowsIdentity current = WindowsIdentity.GetCurrent(); + WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current); + //WindowsBuiltInRole可以枚举出很多权限,例如系统用户、User、Guest等等 + return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator); + } + catch (Exception ex) + { + Logging.SaveLog(ex.Message, ex); + return false; + } + } + + /// + /// 开机自动启动 + /// + /// + /// + public static void SetAutoRun(string AutoRunRegPath, string AutoRunName, bool run) + { + try + { + var autoRunName = $"{AutoRunName}_{Utils.GetMD5(Utils.StartupPath())}"; + //delete first + RegWriteValue(AutoRunRegPath, autoRunName, ""); + if (IsAdministrator()) + { + AutoStart(autoRunName, "", ""); + } + + if (run) + { + string exePath = Utils.GetExePath(); + if (IsAdministrator()) + { + AutoStart(autoRunName, exePath, ""); + } + else + { + RegWriteValue(AutoRunRegPath, autoRunName, exePath.AppendQuotes()); + } + } + } + catch (Exception ex) + { + Logging.SaveLog(ex.Message, ex); + } + } + #region Windows API [Flags] diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index 50afd9b5..38584acc 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -170,7 +170,7 @@ namespace v2rayN.ViewModels SelectedServer = new(); if (_config.tunModeItem.enableTun) { - if (Utils.IsAdministrator()) + if (WindowsUtils.IsAdministrator()) { EnableTun = true; } @@ -422,6 +422,10 @@ namespace v2rayN.ViewModels { Reload(); } + if (_config.uiItem.enableAutoAdjustMainLvColWidth) + { + Locator.Current.GetService()?.AutofitColumnWidth(); + } } } @@ -825,7 +829,7 @@ namespace v2rayN.ViewModels if (_config.tunModeItem.enableTun) { Thread.Sleep(1000); - WindowsUtils.RemoveTunDevice(); + //WindowsUtils.RemoveTunDevice(); } var node = ConfigHandler.GetDefaultServer(_config); @@ -947,7 +951,7 @@ namespace v2rayN.ViewModels { _config.tunModeItem.enableTun = EnableTun; // When running as a non-administrator, reboot to administrator mode - if (EnableTun && !Utils.IsAdministrator()) + if (EnableTun && !WindowsUtils.IsAdministrator()) { _config.tunModeItem.enableTun = false; RebootAsAdmin(); diff --git a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs index d9e6591e..ffb35a7d 100644 --- a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs @@ -301,18 +301,7 @@ namespace v2rayN.ViewModels _config.hysteriaItem.up_mbps = hyUpMbps; _config.hysteriaItem.down_mbps = hyDownMbps; _config.coreBasicItem.enableFragment = enableFragment; - - //Kcp - //_config.kcpItem.mtu = Kcpmtu; - //_config.kcpItem.tti = Kcptti; - //_config.kcpItem.uplinkCapacity = KcpuplinkCapacity; - //_config.kcpItem.downlinkCapacity = KcpdownlinkCapacity; - //_config.kcpItem.readBufferSize = KcpreadBufferSize; - //_config.kcpItem.writeBufferSize = KcpwriteBufferSize; - //_config.kcpItem.congestion = Kcpcongestion; - - //UI - Utils.SetAutoRun(Global.AutoRunRegPath, Global.AutoRunName, AutoRun); + _config.guiItem.autoRun = AutoRun; _config.guiItem.enableStatistics = EnableStatistics; _config.guiItem.keepOlderDedupl = KeepOlderDedupl; diff --git a/v2rayN/v2rayN/ViewModels/ProfilesViewModel.cs b/v2rayN/v2rayN/ViewModels/ProfilesViewModel.cs index 68f17613..7bfe9047 100644 --- a/v2rayN/v2rayN/ViewModels/ProfilesViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/ProfilesViewModel.cs @@ -304,6 +304,11 @@ namespace v2rayN.ViewModels } } + public void AutofitColumnWidth() + { + _updateView?.Invoke(EViewAction.AdjustMainLvColWidth, null); + } + #endregion Actions #region Servers && Groups diff --git a/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs b/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs index a04625b9..61fbe2be 100644 --- a/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/AddServerWindow.xaml.cs @@ -222,6 +222,7 @@ namespace v2rayN.Views }); this.Title = $"{profileItem.configType}"; + WindowsUtils.SetDarkBorder(this, LazyConfig.Instance.GetConfig().uiItem.followSystemTheme ? !WindowsUtils.IsLightTheme() : LazyConfig.Instance.GetConfig().uiItem.colorModeDark); } private bool UpdateViewHandler(EViewAction action, object? obj) diff --git a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs index 77067c77..48124dfb 100644 --- a/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs @@ -54,6 +54,7 @@ namespace v2rayN.Views this.BindCommand(ViewModel, vm => vm.ImportDefConfig4V2rayCmd, v => v.btnImportDefConfig4V2ray).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.ImportDefConfig4SingboxCmd, v => v.btnImportDefConfig4Singbox).DisposeWith(disposables); }); + WindowsUtils.SetDarkBorder(this, LazyConfig.Instance.GetConfig().uiItem.followSystemTheme ? !WindowsUtils.IsLightTheme() : LazyConfig.Instance.GetConfig().uiItem.colorModeDark); } private bool UpdateViewHandler(EViewAction action, object? obj) diff --git a/v2rayN/v2rayN/Views/MainWindow.xaml.cs b/v2rayN/v2rayN/Views/MainWindow.xaml.cs index bfe29129..957cfd8b 100644 --- a/v2rayN/v2rayN/Views/MainWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/MainWindow.xaml.cs @@ -148,7 +148,7 @@ namespace v2rayN.Views } }); - var IsAdministrator = Utils.IsAdministrator(); + var IsAdministrator = WindowsUtils.IsAdministrator(); this.Title = $"{Utils.GetVersion()} - {(IsAdministrator ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}"; if (!_config.guiItem.enableHWA) diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs index 19b705d1..d44fe6f3 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs @@ -169,12 +169,14 @@ namespace v2rayN.Views this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables); }); + WindowsUtils.SetDarkBorder(this, LazyConfig.Instance.GetConfig().uiItem.followSystemTheme ? !WindowsUtils.IsLightTheme() : LazyConfig.Instance.GetConfig().uiItem.colorModeDark); } private bool UpdateViewHandler(EViewAction action, object? obj) { if (action == EViewAction.CloseWindow) { + WindowsUtils.SetAutoRun(Global.AutoRunRegPath, Global.AutoRunName, togAutoRun.IsChecked ?? false); this.DialogResult = true; } return true; diff --git a/v2rayN/v2rayN/Views/ProfilesView.xaml.cs b/v2rayN/v2rayN/Views/ProfilesView.xaml.cs index 2c70f8cd..e8b4f495 100644 --- a/v2rayN/v2rayN/Views/ProfilesView.xaml.cs +++ b/v2rayN/v2rayN/Views/ProfilesView.xaml.cs @@ -104,6 +104,13 @@ namespace v2rayN.Views { switch (action) { + case EViewAction.AdjustMainLvColWidth: + Application.Current?.Dispatcher.Invoke((() => + { + AutofitColumnWidth(); + }), DispatcherPriority.Normal); + break; + case EViewAction.ProfilesFocus: lstProfiles.Focus(); break; diff --git a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs index 0df1db34..2d1a16bc 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs @@ -2,6 +2,7 @@ using System.Reactive.Disposables; using System.Windows; using v2rayN.Enums; +using v2rayN.Handler; using v2rayN.Models; using v2rayN.ViewModels; @@ -60,6 +61,7 @@ namespace v2rayN.Views this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables); }); + WindowsUtils.SetDarkBorder(this, LazyConfig.Instance.GetConfig().uiItem.followSystemTheme ? !WindowsUtils.IsLightTheme() : LazyConfig.Instance.GetConfig().uiItem.colorModeDark); } private bool UpdateViewHandler(EViewAction action, object? obj) diff --git a/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs index 108dc087..ff6340b4 100644 --- a/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs @@ -3,6 +3,7 @@ using System.Reactive.Disposables; using System.Windows; using System.Windows.Input; using v2rayN.Enums; +using v2rayN.Handler; using v2rayN.Models; using v2rayN.Resx; using v2rayN.ViewModels; @@ -61,6 +62,7 @@ namespace v2rayN.Views this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables); }); + WindowsUtils.SetDarkBorder(this, LazyConfig.Instance.GetConfig().uiItem.followSystemTheme ? !WindowsUtils.IsLightTheme() : LazyConfig.Instance.GetConfig().uiItem.colorModeDark); } private bool UpdateViewHandler(EViewAction action, object? obj) diff --git a/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs index f1d348c2..68560429 100644 --- a/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs @@ -3,6 +3,7 @@ using System.Reactive.Disposables; using System.Windows; using System.Windows.Input; using v2rayN.Enums; +using v2rayN.Handler; using v2rayN.Models; using v2rayN.Resx; using v2rayN.ViewModels; @@ -68,6 +69,7 @@ namespace v2rayN.Views this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables); }); + WindowsUtils.SetDarkBorder(this, LazyConfig.Instance.GetConfig().uiItem.followSystemTheme ? !WindowsUtils.IsLightTheme() : LazyConfig.Instance.GetConfig().uiItem.colorModeDark); } private bool UpdateViewHandler(EViewAction action, object? obj) diff --git a/v2rayN/v2rayN/Views/SubEditWindow.xaml.cs b/v2rayN/v2rayN/Views/SubEditWindow.xaml.cs index c97f3f1e..4c6e7b74 100644 --- a/v2rayN/v2rayN/Views/SubEditWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/SubEditWindow.xaml.cs @@ -2,6 +2,7 @@ using System.Reactive.Disposables; using System.Windows; using v2rayN.Enums; +using v2rayN.Handler; using v2rayN.Models; using v2rayN.ViewModels; @@ -39,6 +40,7 @@ namespace v2rayN.Views this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables); }); + WindowsUtils.SetDarkBorder(this, LazyConfig.Instance.GetConfig().uiItem.followSystemTheme ? !WindowsUtils.IsLightTheme() : LazyConfig.Instance.GetConfig().uiItem.colorModeDark); } private bool UpdateViewHandler(EViewAction action, object? obj) diff --git a/v2rayN/v2rayN/Views/SubSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/SubSettingWindow.xaml.cs index 44df4bed..63836f5a 100644 --- a/v2rayN/v2rayN/Views/SubSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/SubSettingWindow.xaml.cs @@ -5,6 +5,7 @@ using System.Reactive.Disposables; using System.Windows; using System.Windows.Input; using v2rayN.Enums; +using v2rayN.Handler; using v2rayN.Models; using v2rayN.Resx; using v2rayN.ViewModels; @@ -34,6 +35,7 @@ namespace v2rayN.Views this.BindCommand(ViewModel, vm => vm.SubEditCmd, v => v.menuSubEdit).DisposeWith(disposables); this.BindCommand(ViewModel, vm => vm.SubShareCmd, v => v.menuSubShare).DisposeWith(disposables); }); + WindowsUtils.SetDarkBorder(this, LazyConfig.Instance.GetConfig().uiItem.followSystemTheme ? !WindowsUtils.IsLightTheme() : LazyConfig.Instance.GetConfig().uiItem.colorModeDark); } private bool UpdateViewHandler(EViewAction action, object? obj)