Refactor code to decouple view and viewmodel

pull/5550/head
2dust 2024-08-15 13:51:30 +08:00
parent 372f3991e1
commit 3286e8e24d
15 changed files with 94 additions and 86 deletions

View File

@ -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
}
}
/// <summary>
/// 取得GUID
/// </summary>
@ -636,26 +633,6 @@ namespace v2rayN
return string.Empty;
}
/// <summary>
/// IsAdministrator
/// </summary>
/// <returns></returns>
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 开机自动启动等
/// <summary>
/// 开机自动启动
/// </summary>
/// <param name="run"></param>
/// <returns></returns>
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 开机自动启动等
}
}

View File

@ -17,7 +17,6 @@ namespace v2rayN
{
internal static class WindowsUtils
{
/// <summary>
/// 获取剪贴板数
/// </summary>
@ -55,6 +54,7 @@ namespace v2rayN
{
}
}
/// <summary>
/// Auto Start via TaskService
/// </summary>
@ -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);
}
/// <summary>
/// IsAdministrator
/// </summary>
/// <returns></returns>
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;
}
}
/// <summary>
/// 开机自动启动
/// </summary>
/// <param name="run"></param>
/// <returns></returns>
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]

View File

@ -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<ProfilesViewModel>()?.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();

View File

@ -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;

View File

@ -304,6 +304,11 @@ namespace v2rayN.ViewModels
}
}
public void AutofitColumnWidth()
{
_updateView?.Invoke(EViewAction.AdjustMainLvColWidth, null);
}
#endregion Actions
#region Servers && Groups

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)