Skip to content

Commit

Permalink
Refactor code to decouple view and viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Aug 15, 2024
1 parent 372f399 commit 3286e8e
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 86 deletions.
67 changes: 0 additions & 67 deletions v2rayN/v2rayN/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Net.Sockets;
using System.Reflection;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -404,7 +403,6 @@ public static bool IsNumeric(string oText)
}
}


public static bool IsNullOrEmpty(string? text)
{
if (string.IsNullOrWhiteSpace(text))
Expand Down Expand Up @@ -611,7 +609,6 @@ public static string GetVersion(bool blFull = true)
}
}


/// <summary>
/// 取得GUID
/// </summary>
Expand All @@ -636,26 +633,6 @@ public static string GetGUID(bool full = true)
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);
Expand Down Expand Up @@ -871,49 +848,5 @@ public static string GetFontsPath(string filename = "")
}

#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 开机自动启动等


}
}
62 changes: 59 additions & 3 deletions v2rayN/v2rayN/Common/WindowsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace v2rayN
{
internal static class WindowsUtils
{

/// <summary>
/// 获取剪贴板数
/// </summary>
Expand Down Expand Up @@ -55,6 +54,7 @@ public static void SetClipboardData(string strData)
{
}
}

/// <summary>
/// Auto Start via TaskService
/// </summary>
Expand Down Expand Up @@ -199,16 +199,72 @@ public static void RemoveTunDevice()
}
}

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]
Expand Down
10 changes: 7 additions & 3 deletions v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public MainWindowViewModel(Func<EViewAction, object?, bool>? updateView)
SelectedServer = new();
if (_config.tunModeItem.enableTun)
{
if (Utils.IsAdministrator())
if (WindowsUtils.IsAdministrator())
{
EnableTun = true;
}
Expand Down Expand Up @@ -422,6 +422,10 @@ private void UpdateTaskHandler(bool success, string msg)
{
Reload();
}
if (_config.uiItem.enableAutoAdjustMainLvColWidth)
{
Locator.Current.GetService<ProfilesViewModel>()?.AutofitColumnWidth();
}
}
}

Expand Down Expand Up @@ -825,7 +829,7 @@ await Task.Run(() =>
if (_config.tunModeItem.enableTun)
{
Thread.Sleep(1000);
WindowsUtils.RemoveTunDevice();
//WindowsUtils.RemoveTunDevice();
}

var node = ConfigHandler.GetDefaultServer(_config);
Expand Down Expand Up @@ -947,7 +951,7 @@ private void DoEnableTun(bool c)
{
_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();
Expand Down
13 changes: 1 addition & 12 deletions v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,7 @@ private void SaveSetting()
_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;
Expand Down
5 changes: 5 additions & 0 deletions v2rayN/v2rayN/ViewModels/ProfilesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ public void UpdateStatistics(ServerSpeedItem update)
}
}

public void AutofitColumnWidth()
{
_updateView?.Invoke(EViewAction.AdjustMainLvColWidth, null);
}

#endregion Actions

#region Servers && Groups
Expand Down
1 change: 1 addition & 0 deletions v2rayN/v2rayN/Views/AddServerWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public AddServerWindow(ProfileItem profileItem)
});

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)
Expand Down
1 change: 1 addition & 0 deletions v2rayN/v2rayN/Views/DNSSettingWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public DNSSettingWindow()
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)
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public MainWindow()
}
});

var IsAdministrator = Utils.IsAdministrator();
var IsAdministrator = WindowsUtils.IsAdministrator();
this.Title = $"{Utils.GetVersion()} - {(IsAdministrator ? ResUI.RunAsAdmin : ResUI.NotRunAsAdmin)}";

if (!_config.guiItem.enableHWA)
Expand Down
2 changes: 2 additions & 0 deletions v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,14 @@ public OptionSettingWindow()

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;
Expand Down
7 changes: 7 additions & 0 deletions v2rayN/v2rayN/Views/ProfilesView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ private bool UpdateViewHandler(EViewAction action, object? obj)
{
switch (action)
{
case EViewAction.AdjustMainLvColWidth:
Application.Current?.Dispatcher.Invoke((() =>
{
AutofitColumnWidth();
}), DispatcherPriority.Normal);
break;

case EViewAction.ProfilesFocus:
lstProfiles.Focus();
break;
Expand Down
2 changes: 2 additions & 0 deletions v2rayN/v2rayN/Views/RoutingRuleDetailsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Reactive.Disposables;
using System.Windows;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Models;
using v2rayN.ViewModels;

Expand Down Expand Up @@ -60,6 +61,7 @@ public RoutingRuleDetailsWindow(RulesItem rulesItem)

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)
Expand Down
2 changes: 2 additions & 0 deletions v2rayN/v2rayN/Views/RoutingRuleSettingWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Windows;
using System.Windows.Input;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Models;
using v2rayN.Resx;
using v2rayN.ViewModels;
Expand Down Expand Up @@ -61,6 +62,7 @@ public RoutingRuleSettingWindow(RoutingItem routingItem)

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)
Expand Down
2 changes: 2 additions & 0 deletions v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Windows;
using System.Windows.Input;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Models;
using v2rayN.Resx;
using v2rayN.ViewModels;
Expand Down Expand Up @@ -68,6 +69,7 @@ public RoutingSettingWindow()

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)
Expand Down
2 changes: 2 additions & 0 deletions v2rayN/v2rayN/Views/SubEditWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Reactive.Disposables;
using System.Windows;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Models;
using v2rayN.ViewModels;

Expand Down Expand Up @@ -39,6 +40,7 @@ public SubEditWindow(SubItem subItem)

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)
Expand Down
2 changes: 2 additions & 0 deletions v2rayN/v2rayN/Views/SubSettingWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Windows;
using System.Windows.Input;
using v2rayN.Enums;
using v2rayN.Handler;
using v2rayN.Models;
using v2rayN.Resx;
using v2rayN.ViewModels;
Expand Down Expand Up @@ -34,6 +35,7 @@ public SubSettingWindow()
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)
Expand Down

0 comments on commit 3286e8e

Please sign in to comment.