Skip to content

Commit e963f9e

Browse files
committed
Add clash display in the main interface
1 parent e3c2a4b commit e963f9e

10 files changed

+297
-264
lines changed

v2rayN/v2rayN/Handler/ClashApiHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public sealed class ClashApiHandler
1010

1111
private Dictionary<String, ProxiesItem> _proxies;
1212
public Dictionary<string, object> ProfileContent { get; set; }
13-
public bool ShowInTaskbar { get; set; } = true;
1413

1514
public void SetProxies(Dictionary<String, ProxiesItem> proxies)
1615
{

v2rayN/v2rayN/Models/ConfigItems.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,12 @@ public class HysteriaItem
216216
public class ClashUIItem
217217
{
218218
public ERuleMode ruleMode { get; set; }
219+
public bool showInTaskbar { get; set; }
219220
public int proxiesSorting { get; set; }
220221
public bool proxiesAutoRefresh { get; set; }
221-
public int AutoDelayTestInterval { get; set; } = 10;
222+
public int proxiesAutoDelayTestInterval { get; set; } = 10;
222223
public int connectionsSorting { get; set; }
223224
public bool connectionsAutoRefresh { get; set; }
224-
225+
public int connectionsRefreshInterval { get; set; } = 2;
225226
}
226227
}

v2rayN/v2rayN/ViewModels/ClashConnectionsViewModel.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ static ClashConnectionsViewModel()
3636
[Reactive]
3737
public bool AutoRefresh { get; set; }
3838

39-
private int AutoRefreshInterval;
40-
4139
public ClashConnectionsViewModel()
4240
{
43-
AutoRefreshInterval = 10;
4441
SortingSelected = _config.clashUIItem.connectionsSorting;
4542
AutoRefresh = _config.clashUIItem.connectionsAutoRefresh;
4643

@@ -87,15 +84,26 @@ private void DoSortingSelected(bool c)
8784

8885
private void Init()
8986
{
90-
Observable.Interval(TimeSpan.FromSeconds(AutoRefreshInterval))
91-
.Subscribe(x =>
92-
{
93-
if (!(AutoRefresh && ClashApiHandler.Instance.ShowInTaskbar))
94-
{
95-
return;
96-
}
97-
GetClashConnections();
98-
});
87+
var lastTime = DateTime.Now;
88+
89+
Observable.Interval(TimeSpan.FromSeconds(10))
90+
.Subscribe(x =>
91+
{
92+
if (!(AutoRefresh && _config.clashUIItem.showInTaskbar))
93+
{
94+
return;
95+
}
96+
var dtNow = DateTime.Now;
97+
if (_config.clashUIItem.connectionsRefreshInterval > 0)
98+
{
99+
if ((dtNow - lastTime).Minutes % _config.clashUIItem.connectionsRefreshInterval == 0)
100+
{
101+
GetClashConnections();
102+
lastTime = dtNow;
103+
}
104+
Thread.Sleep(1000);
105+
}
106+
});
99107
}
100108

101109
private void GetClashConnections()

v2rayN/v2rayN/ViewModels/ClashProxiesViewModel.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,8 @@ private void ProxiesDelayTest(bool blAll)
434434
var dicResult = JsonUtils.Deserialize<Dictionary<string, object>>(result);
435435
if (dicResult != null && dicResult.ContainsKey("delay"))
436436
{
437-
detail.delay = Convert.ToInt32(dicResult["delay"]);
438-
detail.delayName = $"{dicResult["delay"]}ms";
437+
detail.delay = Convert.ToInt32(dicResult["delay"].ToString());
438+
detail.delayName = $"{detail.delay}ms";
439439
}
440440
else if (dicResult != null && dicResult.ContainsKey("message"))
441441
{
@@ -459,27 +459,26 @@ private void ProxiesDelayTest(bool blAll)
459459

460460
public void DelayTestTask()
461461
{
462-
var autoDelayTestTime = DateTime.Now;
462+
var lastTime = DateTime.Now;
463463

464464
Observable.Interval(TimeSpan.FromSeconds(60))
465465
.Subscribe(x =>
466466
{
467-
if (!(AutoRefresh && ClashApiHandler.Instance.ShowInTaskbar))
467+
if (!(AutoRefresh && _config.clashUIItem.showInTaskbar))
468468
{
469469
return;
470470
}
471471
var dtNow = DateTime.Now;
472-
473-
if (_config.clashUIItem.AutoDelayTestInterval > 0)
472+
if (_config.clashUIItem.proxiesAutoDelayTestInterval > 0)
474473
{
475-
if ((dtNow - autoDelayTestTime).Minutes % _config.clashUIItem.AutoDelayTestInterval == 0)
474+
if ((dtNow - lastTime).Minutes % _config.clashUIItem.proxiesAutoDelayTestInterval == 0)
476475
{
477476
ProxiesDelayTest();
478-
autoDelayTestTime = dtNow;
477+
lastTime = dtNow;
479478
}
480479
Thread.Sleep(1000);
481480
}
482-
});
481+
});
483482
}
484483

485484
#endregion task

v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ public class MainWindowViewModel : ReactiveObject
243243
[Reactive]
244244
public string CurrentLanguage { get; set; }
245245

246+
[Reactive]
247+
public bool ShowCalshUI { get; set; }
248+
246249
#endregion UI
247250

248251
#region Init
@@ -569,6 +572,7 @@ public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue, Action<EV
569572
AutoHideStartup();
570573

571574
_showInTaskbar = true;
575+
_config.clashUIItem.showInTaskbar = _showInTaskbar;
572576
}
573577

574578
private void Init()
@@ -1509,7 +1513,12 @@ public void Reload()
15091513
Application.Current?.Dispatcher.Invoke((Action)(() =>
15101514
{
15111515
BlReloadEnabled = true;
1516+
ShowCalshUI = (_config.runningCoreType is ECoreType.clash or ECoreType.clash_meta or ECoreType.mihomo);
1517+
if (ShowCalshUI) {
1518+
Locator.Current.GetService<ClashProxiesViewModel>()?.ProxiesReload();
1519+
}
15121520
}));
1521+
15131522
});
15141523
}
15151524

@@ -1680,6 +1689,7 @@ public void ShowHideWindow(bool? blShow)
16801689
//Utile.RegWriteValue(Global.MyRegPath, Utile.WindowHwndKey, Convert.ToString((long)windowHandle));
16811690
}
16821691
_showInTaskbar = bl;
1692+
_config.clashUIItem.showInTaskbar = _showInTaskbar;
16831693
}
16841694

16851695
private void RestoreUI()

v2rayN/v2rayN/Views/ClashConnectionsView.xaml

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,7 @@
1313
d:DesignWidth="800"
1414
x:TypeArguments="vms:ClashConnectionsViewModel"
1515
mc:Ignorable="d">
16-
<DockPanel Margin="8">
17-
<StackPanel
18-
Margin="8,0,8,8"
19-
HorizontalAlignment="Left"
20-
DockPanel.Dock="Top"
21-
Orientation="Horizontal">
22-
<TextBlock Style="{StaticResource ModuleTitle}" Text="{x:Static resx:ResUI.TbConnections}" />
23-
<materialDesign:Chip
24-
x:Name="chipCount"
25-
Height="20"
26-
IsEnabled="False"
27-
Style="{StaticResource ListItemChip}" />
28-
</StackPanel>
16+
<DockPanel>
2917
<ToolBarTray Margin="0,8,0,8" DockPanel.Dock="Top">
3018
<ToolBar ClipToBounds="True" Style="{StaticResource MaterialDesignToolBar}">
3119
<Button Width="1" Visibility="Hidden">

v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public ClashConnectionsView()
1818
{
1919
this.OneWayBind(ViewModel, vm => vm.ConnectionItems, v => v.lstConnections.ItemsSource).DisposeWith(disposables);
2020
this.Bind(ViewModel, vm => vm.SelectedSource, v => v.lstConnections.SelectedItem).DisposeWith(disposables);
21-
this.OneWayBind(ViewModel, vm => vm.ConnectionItems.Count, v => v.chipCount.Content).DisposeWith(disposables);
2221

2322
this.BindCommand(ViewModel, vm => vm.ConnectionCloseCmd, v => v.menuConnectionClose).DisposeWith(disposables);
2423
this.BindCommand(ViewModel, vm => vm.ConnectionCloseAllCmd, v => v.menuConnectionCloseAll).DisposeWith(disposables);

v2rayN/v2rayN/Views/ClashProxiesView.xaml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@
2020
<converters:DelayColorConverter x:Key="DelayColorConverter" />
2121
</UserControl.Resources>
2222

23-
<DockPanel Margin="8">
24-
<TextBlock
25-
Margin="8,0,8,8"
26-
DockPanel.Dock="Top"
27-
Style="{StaticResource ModuleTitle}"
28-
Text="{x:Static resx:ResUI.TbProxies}" />
29-
23+
<DockPanel>
3024
<ToolBarTray DockPanel.Dock="Top">
3125
<ToolBar
3226
HorizontalAlignment="Center"

0 commit comments

Comments
 (0)