Skip to content

Commit

Permalink
Add clash display in the main interface
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Jun 28, 2024
1 parent e3c2a4b commit e963f9e
Show file tree
Hide file tree
Showing 10 changed files with 297 additions and 264 deletions.
1 change: 0 additions & 1 deletion v2rayN/v2rayN/Handler/ClashApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public sealed class ClashApiHandler

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

public void SetProxies(Dictionary<String, ProxiesItem> proxies)
{
Expand Down
5 changes: 3 additions & 2 deletions v2rayN/v2rayN/Models/ConfigItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,12 @@ public class HysteriaItem
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;
}
}
32 changes: 20 additions & 12 deletions v2rayN/v2rayN/ViewModels/ClashConnectionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ static ClashConnectionsViewModel()
[Reactive]
public bool AutoRefresh { get; set; }

private int AutoRefreshInterval;

public ClashConnectionsViewModel()
{
AutoRefreshInterval = 10;
SortingSelected = _config.clashUIItem.connectionsSorting;
AutoRefresh = _config.clashUIItem.connectionsAutoRefresh;

Expand Down Expand Up @@ -87,15 +84,26 @@ private void DoSortingSelected(bool c)

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()
Expand Down
17 changes: 8 additions & 9 deletions v2rayN/v2rayN/ViewModels/ClashProxiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ private void ProxiesDelayTest(bool blAll)
var dicResult = JsonUtils.Deserialize<Dictionary<string, object>>(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"))
{
Expand All @@ -459,27 +459,26 @@ private void ProxiesDelayTest(bool blAll)

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
Expand Down
10 changes: 10 additions & 0 deletions v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ public class MainWindowViewModel : ReactiveObject
[Reactive]
public string CurrentLanguage { get; set; }

[Reactive]
public bool ShowCalshUI { get; set; }

#endregion UI

#region Init
Expand Down Expand Up @@ -569,6 +572,7 @@ public MainWindowViewModel(ISnackbarMessageQueue snackbarMessageQueue, Action<EV
AutoHideStartup();

_showInTaskbar = true;
_config.clashUIItem.showInTaskbar = _showInTaskbar;
}

private void Init()
Expand Down Expand Up @@ -1509,7 +1513,12 @@ public void Reload()
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<ClashProxiesViewModel>()?.ProxiesReload();
}
}));
});
}

Expand Down Expand Up @@ -1680,6 +1689,7 @@ public void ShowHideWindow(bool? blShow)
//Utile.RegWriteValue(Global.MyRegPath, Utile.WindowHwndKey, Convert.ToString((long)windowHandle));
}
_showInTaskbar = bl;
_config.clashUIItem.showInTaskbar = _showInTaskbar;
}

private void RestoreUI()
Expand Down
14 changes: 1 addition & 13 deletions v2rayN/v2rayN/Views/ClashConnectionsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,7 @@
d:DesignWidth="800"
x:TypeArguments="vms:ClashConnectionsViewModel"
mc:Ignorable="d">
<DockPanel Margin="8">
<StackPanel
Margin="8,0,8,8"
HorizontalAlignment="Left"
DockPanel.Dock="Top"
Orientation="Horizontal">
<TextBlock Style="{StaticResource ModuleTitle}" Text="{x:Static resx:ResUI.TbConnections}" />
<materialDesign:Chip
x:Name="chipCount"
Height="20"
IsEnabled="False"
Style="{StaticResource ListItemChip}" />
</StackPanel>
<DockPanel>
<ToolBarTray Margin="0,8,0,8" DockPanel.Dock="Top">
<ToolBar ClipToBounds="True" Style="{StaticResource MaterialDesignToolBar}">
<Button Width="1" Visibility="Hidden">
Expand Down
1 change: 0 additions & 1 deletion v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public ClashConnectionsView()
{
this.OneWayBind(ViewModel, vm => vm.ConnectionItems, v => v.lstConnections.ItemsSource).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedSource, v => v.lstConnections.SelectedItem).DisposeWith(disposables);
this.OneWayBind(ViewModel, vm => vm.ConnectionItems.Count, v => v.chipCount.Content).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.ConnectionCloseCmd, v => v.menuConnectionClose).DisposeWith(disposables);
this.BindCommand(ViewModel, vm => vm.ConnectionCloseAllCmd, v => v.menuConnectionCloseAll).DisposeWith(disposables);
Expand Down
8 changes: 1 addition & 7 deletions v2rayN/v2rayN/Views/ClashProxiesView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@
<converters:DelayColorConverter x:Key="DelayColorConverter" />
</UserControl.Resources>

<DockPanel Margin="8">
<TextBlock
Margin="8,0,8,8"
DockPanel.Dock="Top"
Style="{StaticResource ModuleTitle}"
Text="{x:Static resx:ResUI.TbProxies}" />

<DockPanel>
<ToolBarTray DockPanel.Dock="Top">
<ToolBar
HorizontalAlignment="Center"
Expand Down
Loading

0 comments on commit e963f9e

Please sign in to comment.