From e963f9e349e1f1da576b4b206d31a6c05ab49cb8 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Fri, 28 Jun 2024 16:14:19 +0800 Subject: [PATCH] Add clash display in the main interface --- v2rayN/v2rayN/Handler/ClashApiHandler.cs | 1 - v2rayN/v2rayN/Models/ConfigItems.cs | 5 +- .../ViewModels/ClashConnectionsViewModel.cs | 32 +- .../ViewModels/ClashProxiesViewModel.cs | 17 +- .../v2rayN/ViewModels/MainWindowViewModel.cs | 10 + v2rayN/v2rayN/Views/ClashConnectionsView.xaml | 14 +- .../v2rayN/Views/ClashConnectionsView.xaml.cs | 1 - v2rayN/v2rayN/Views/ClashProxiesView.xaml | 8 +- v2rayN/v2rayN/Views/MainWindow.xaml | 449 +++++++++--------- v2rayN/v2rayN/Views/MainWindow.xaml.cs | 24 + 10 files changed, 297 insertions(+), 264 deletions(-) diff --git a/v2rayN/v2rayN/Handler/ClashApiHandler.cs b/v2rayN/v2rayN/Handler/ClashApiHandler.cs index 45efb8e6..1e04bf1e 100644 --- a/v2rayN/v2rayN/Handler/ClashApiHandler.cs +++ b/v2rayN/v2rayN/Handler/ClashApiHandler.cs @@ -10,7 +10,6 @@ namespace v2rayN.Handler private Dictionary _proxies; public Dictionary ProfileContent { get; set; } - public bool ShowInTaskbar { get; set; } = true; public void SetProxies(Dictionary proxies) { diff --git a/v2rayN/v2rayN/Models/ConfigItems.cs b/v2rayN/v2rayN/Models/ConfigItems.cs index 5e4fd795..e88806c3 100644 --- a/v2rayN/v2rayN/Models/ConfigItems.cs +++ b/v2rayN/v2rayN/Models/ConfigItems.cs @@ -216,11 +216,12 @@ namespace v2rayN.Models public class ClashUIItem { public ERuleMode ruleMode { get; set; } + public bool showInTaskbar { get; set; } public int proxiesSorting { get; set; } public bool proxiesAutoRefresh { get; set; } - public int AutoDelayTestInterval { get; set; } = 10; + public int proxiesAutoDelayTestInterval { get; set; } = 10; public int connectionsSorting { get; set; } public bool connectionsAutoRefresh { get; set; } - + public int connectionsRefreshInterval { get; set; } = 2; } } \ No newline at end of file diff --git a/v2rayN/v2rayN/ViewModels/ClashConnectionsViewModel.cs b/v2rayN/v2rayN/ViewModels/ClashConnectionsViewModel.cs index 046c8c50..78697400 100644 --- a/v2rayN/v2rayN/ViewModels/ClashConnectionsViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/ClashConnectionsViewModel.cs @@ -36,11 +36,8 @@ namespace v2rayN.ViewModels [Reactive] public bool AutoRefresh { get; set; } - private int AutoRefreshInterval; - public ClashConnectionsViewModel() { - AutoRefreshInterval = 10; SortingSelected = _config.clashUIItem.connectionsSorting; AutoRefresh = _config.clashUIItem.connectionsAutoRefresh; @@ -87,15 +84,26 @@ namespace v2rayN.ViewModels private void Init() { - Observable.Interval(TimeSpan.FromSeconds(AutoRefreshInterval)) - .Subscribe(x => - { - if (!(AutoRefresh && ClashApiHandler.Instance.ShowInTaskbar)) - { - return; - } - GetClashConnections(); - }); + var lastTime = DateTime.Now; + + Observable.Interval(TimeSpan.FromSeconds(10)) + .Subscribe(x => + { + if (!(AutoRefresh && _config.clashUIItem.showInTaskbar)) + { + return; + } + var dtNow = DateTime.Now; + if (_config.clashUIItem.connectionsRefreshInterval > 0) + { + if ((dtNow - lastTime).Minutes % _config.clashUIItem.connectionsRefreshInterval == 0) + { + GetClashConnections(); + lastTime = dtNow; + } + Thread.Sleep(1000); + } + }); } private void GetClashConnections() diff --git a/v2rayN/v2rayN/ViewModels/ClashProxiesViewModel.cs b/v2rayN/v2rayN/ViewModels/ClashProxiesViewModel.cs index 2813c742..255dc1bb 100644 --- a/v2rayN/v2rayN/ViewModels/ClashProxiesViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/ClashProxiesViewModel.cs @@ -434,8 +434,8 @@ namespace v2rayN.ViewModels var dicResult = JsonUtils.Deserialize>(result); if (dicResult != null && dicResult.ContainsKey("delay")) { - detail.delay = Convert.ToInt32(dicResult["delay"]); - detail.delayName = $"{dicResult["delay"]}ms"; + detail.delay = Convert.ToInt32(dicResult["delay"].ToString()); + detail.delayName = $"{detail.delay}ms"; } else if (dicResult != null && dicResult.ContainsKey("message")) { @@ -459,27 +459,26 @@ namespace v2rayN.ViewModels public void DelayTestTask() { - var autoDelayTestTime = DateTime.Now; + var lastTime = DateTime.Now; Observable.Interval(TimeSpan.FromSeconds(60)) .Subscribe(x => { - if (!(AutoRefresh && ClashApiHandler.Instance.ShowInTaskbar)) + if (!(AutoRefresh && _config.clashUIItem.showInTaskbar)) { return; } var dtNow = DateTime.Now; - - if (_config.clashUIItem.AutoDelayTestInterval > 0) + if (_config.clashUIItem.proxiesAutoDelayTestInterval > 0) { - if ((dtNow - autoDelayTestTime).Minutes % _config.clashUIItem.AutoDelayTestInterval == 0) + if ((dtNow - lastTime).Minutes % _config.clashUIItem.proxiesAutoDelayTestInterval == 0) { ProxiesDelayTest(); - autoDelayTestTime = dtNow; + lastTime = dtNow; } Thread.Sleep(1000); } - }); + }); } #endregion task diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index 805d0a29..ca93bf05 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -243,6 +243,9 @@ namespace v2rayN.ViewModels [Reactive] public string CurrentLanguage { get; set; } + [Reactive] + public bool ShowCalshUI { get; set; } + #endregion UI #region Init @@ -569,6 +572,7 @@ namespace v2rayN.ViewModels AutoHideStartup(); _showInTaskbar = true; + _config.clashUIItem.showInTaskbar = _showInTaskbar; } private void Init() @@ -1509,7 +1513,12 @@ namespace v2rayN.ViewModels Application.Current?.Dispatcher.Invoke((Action)(() => { BlReloadEnabled = true; + ShowCalshUI = (_config.runningCoreType is ECoreType.clash or ECoreType.clash_meta or ECoreType.mihomo); + if (ShowCalshUI) { + Locator.Current.GetService()?.ProxiesReload(); + } })); + }); } @@ -1680,6 +1689,7 @@ namespace v2rayN.ViewModels //Utile.RegWriteValue(Global.MyRegPath, Utile.WindowHwndKey, Convert.ToString((long)windowHandle)); } _showInTaskbar = bl; + _config.clashUIItem.showInTaskbar = _showInTaskbar; } private void RestoreUI() diff --git a/v2rayN/v2rayN/Views/ClashConnectionsView.xaml b/v2rayN/v2rayN/Views/ClashConnectionsView.xaml index 97168ab4..58bdcbdb 100644 --- a/v2rayN/v2rayN/Views/ClashConnectionsView.xaml +++ b/v2rayN/v2rayN/Views/ClashConnectionsView.xaml @@ -13,19 +13,7 @@ d:DesignWidth="800" x:TypeArguments="vms:ClashConnectionsViewModel" mc:Ignorable="d"> - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + Click="menuSelectAll_Click" + Header="{x:Static resx:ResUI.menuSelectAll}" /> + + Header="{x:Static resx:ResUI.menuExport2ClientConfig}" /> + Header="{x:Static resx:ResUI.menuExport2ShareUrl}" /> - - - - - - - - - - - + Header="{x:Static resx:ResUI.menuExport2SubContent}" /> + + + + + - - - + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - vm.SelectedSwatch, v => v.cmbSwatches.SelectedItem).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.CurrentFontSize, v => v.cmbCurrentFontSize.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.CurrentLanguage, v => v.cmbCurrentLanguage.Text).DisposeWith(disposables); + this.OneWayBind(ViewModel, vm => vm.ShowCalshUI, v => v.btnShowCalshUI.Visibility).DisposeWith(disposables); + this.OneWayBind(ViewModel, vm => vm.ShowCalshUI, v => v.tabClashUI.Visibility).DisposeWith(disposables); }); RestoreUI(); @@ -455,6 +458,27 @@ namespace v2rayN.Views } } + private bool blShowClashUI = false; + + private void BtnShowCalshUI_Click(object sender, RoutedEventArgs e) + { + if (blShowClashUI) + { + tabClashUI.Visibility = Visibility.Hidden; + tabClashUI.Width = 0; + } + else + { + tabClashUI.Visibility = Visibility.Visible; + if (tabClashUI.Content is null) + { + tabClashUI.Content = new ClashProxiesView(); + } + tabClashUI.Width = this.ActualWidth * 5 / 6; + } + blShowClashUI = !blShowClashUI; + } + #endregion Event #region UI