Skip to content

Commit 679bd8a

Browse files
committed
Persist DataGrid column layout for ClashConnectionsView
2dust#8893
1 parent 66e1aea commit 679bd8a

5 files changed

Lines changed: 136 additions & 33 deletions

File tree

v2rayN/ServiceLib/Handler/ConfigHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public static class ConfigHandler
154154
DownMbps = 100
155155
};
156156
config.ClashUIItem ??= new();
157+
config.ClashUIItem.ConnectionsColumnItem ??= new();
157158
config.SystemProxyItem ??= new();
158159
config.WebDavItem ??= new();
159160
config.CheckUpdateItem ??= new();

v2rayN/ServiceLib/Models/ConfigItems.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ public class ClashUIItem
208208
public int ProxiesAutoDelayTestInterval { get; set; } = 10;
209209
public bool ConnectionsAutoRefresh { get; set; }
210210
public int ConnectionsRefreshInterval { get; set; } = 2;
211+
public List<ColumnItem> ConnectionsColumnItem { get; set; }
211212
}
212213

213214
[Serializable]

v2rayN/v2rayN/Views/ClashConnectionsView.xaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
x:Class="v2rayN.Views.ClashConnectionsView"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:base="clr-namespace:v2rayN.Base"
56
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
67
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
78
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -78,25 +79,30 @@
7879
</ContextMenu>
7980
</DataGrid.ContextMenu>
8081
<DataGrid.Columns>
81-
<DataGridTextColumn
82+
<base:MyDGTextColumn
8283
Width="300"
8384
Binding="{Binding Host}"
85+
ExName="Host"
8486
Header="{x:Static resx:ResUI.TbSortingHost}" />
85-
<DataGridTextColumn
87+
<base:MyDGTextColumn
8688
Width="500"
8789
Binding="{Binding Chain}"
90+
ExName="Chain"
8891
Header="{x:Static resx:ResUI.TbSortingChain}" />
89-
<DataGridTextColumn
92+
<base:MyDGTextColumn
9093
Width="80"
9194
Binding="{Binding Network}"
95+
ExName="Network"
9296
Header="{x:Static resx:ResUI.TbSortingNetwork}" />
93-
<DataGridTextColumn
97+
<base:MyDGTextColumn
9498
Width="160"
9599
Binding="{Binding Type}"
100+
ExName="Type"
96101
Header="{x:Static resx:ResUI.TbSortingType}" />
97-
<DataGridTextColumn
102+
<base:MyDGTextColumn
98103
Width="100"
99104
Binding="{Binding Elapsed}"
105+
ExName="Elapsed"
100106
Header="{x:Static resx:ResUI.TbSortingTime}" />
101107
</DataGrid.Columns>
102108
</DataGrid>

v2rayN/v2rayN/Views/ClashConnectionsView.xaml.cs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Windows.Controls;
2+
using v2rayN.Base;
23

34
namespace v2rayN.Views;
45

@@ -7,9 +8,14 @@ namespace v2rayN.Views;
78
/// </summary>
89
public partial class ClashConnectionsView
910
{
11+
private static Config _config;
12+
private static readonly string _tag = "ClashConnectionsView";
13+
1014
public ClashConnectionsView()
1115
{
1216
InitializeComponent();
17+
_config = AppManager.Instance.Config;
18+
1319
ViewModel = new ClashConnectionsViewModel(UpdateViewHandler);
1420
btnAutofitColumnWidth.Click += BtnAutofitColumnWidth_Click;
1521

@@ -24,7 +30,15 @@ public ClashConnectionsView()
2430
this.Bind(ViewModel, vm => vm.HostFilter, v => v.txtHostFilter.Text).DisposeWith(disposables);
2531
this.BindCommand(ViewModel, vm => vm.ConnectionCloseAllCmd, v => v.btnConnectionCloseAll).DisposeWith(disposables);
2632
this.Bind(ViewModel, vm => vm.AutoRefresh, v => v.togAutoRefresh.IsChecked).DisposeWith(disposables);
33+
34+
AppEvents.AppExitRequested
35+
.AsObservable()
36+
.ObserveOn(RxApp.MainThreadScheduler)
37+
.Subscribe(_ => StorageUI())
38+
.DisposeWith(disposables);
2739
});
40+
41+
RestoreUI();
2842
}
2943

3044
private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
@@ -48,12 +62,79 @@ private void AutofitColumnWidth()
4862
}
4963
catch (Exception ex)
5064
{
51-
Logging.SaveLog("ClashConnectionsView", ex);
65+
Logging.SaveLog(_tag, ex);
5266
}
5367
}
5468

5569
private void btnClose_Click(object sender, System.Windows.RoutedEventArgs e)
5670
{
5771
ViewModel?.ClashConnectionClose(false);
5872
}
73+
74+
#region UI
75+
76+
private void RestoreUI()
77+
{
78+
try
79+
{
80+
var lvColumnItem = _config.ClashUIItem?.ConnectionsColumnItem?.OrderBy(t => t.Index).ToList();
81+
if (lvColumnItem == null)
82+
{
83+
return;
84+
}
85+
86+
var displayIndex = 0;
87+
foreach (var item in lvColumnItem)
88+
{
89+
foreach (var col in lstConnections.Columns.Cast<MyDGTextColumn>())
90+
{
91+
if (col.ExName == item.Name)
92+
{
93+
if (item.Width > 0)
94+
{
95+
col.Width = item.Width;
96+
}
97+
98+
col.DisplayIndex = displayIndex++;
99+
break;
100+
}
101+
}
102+
}
103+
}
104+
catch (Exception ex)
105+
{
106+
Logging.SaveLog(_tag, ex);
107+
}
108+
}
109+
110+
private void StorageUI()
111+
{
112+
try
113+
{
114+
List<ColumnItem> lvColumnItem = new();
115+
foreach (var col in lstConnections.Columns.Cast<MyDGTextColumn>())
116+
{
117+
var name = col.ExName;
118+
if (string.IsNullOrWhiteSpace(name))
119+
{
120+
continue;
121+
}
122+
123+
lvColumnItem.Add(new()
124+
{
125+
Name = name,
126+
Width = (int)col.ActualWidth,
127+
Index = col.DisplayIndex
128+
});
129+
}
130+
131+
_config.ClashUIItem.ConnectionsColumnItem = lvColumnItem;
132+
}
133+
catch (Exception ex)
134+
{
135+
Logging.SaveLog(_tag, ex);
136+
}
137+
}
138+
139+
#endregion UI
59140
}

v2rayN/v2rayN/Views/ProfilesView.xaml.cs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace v2rayN.Views;
1010
public partial class ProfilesView
1111
{
1212
private static Config _config;
13+
private static readonly string _tag = "ProfilesView";
1314

1415
public ProfilesView()
1516
{
@@ -339,7 +340,7 @@ private void AutofitColumnWidth()
339340
}
340341
catch (Exception ex)
341342
{
342-
Logging.SaveLog("ProfilesView", ex);
343+
Logging.SaveLog(_tag, ex);
343344
}
344345
}
345346

@@ -357,46 +358,59 @@ private void TxtServerFilter_PreviewKeyDown(object sender, KeyEventArgs e)
357358

358359
private void RestoreUI()
359360
{
360-
var lvColumnItem = _config.UiItem.MainColumnItem.OrderBy(t => t.Index).ToList();
361-
var displayIndex = 0;
362-
foreach (var item in lvColumnItem)
361+
try
363362
{
364-
foreach (MyDGTextColumn item2 in lstProfiles.Columns)
363+
var lvColumnItem = _config.UiItem.MainColumnItem.OrderBy(t => t.Index).ToList();
364+
var displayIndex = 0;
365+
foreach (var item in lvColumnItem)
365366
{
366-
if (item2.ExName == item.Name)
367+
foreach (var item2 in lstProfiles.Columns.Cast<MyDGTextColumn>())
367368
{
368-
if (item.Width < 0)
369-
{
370-
item2.Visibility = Visibility.Hidden;
371-
}
372-
else
369+
if (item2.ExName == item.Name)
373370
{
374-
item2.Width = item.Width;
375-
item2.DisplayIndex = displayIndex++;
376-
}
377-
if (item.Name.ToLower().StartsWith("to"))
378-
{
379-
item2.Visibility = _config.GuiItem.EnableStatistics ? Visibility.Visible : Visibility.Hidden;
371+
if (item.Width < 0)
372+
{
373+
item2.Visibility = Visibility.Hidden;
374+
}
375+
else
376+
{
377+
item2.Width = item.Width;
378+
item2.DisplayIndex = displayIndex++;
379+
}
380+
if (item.Name.ToLower().StartsWith("to"))
381+
{
382+
item2.Visibility = _config.GuiItem.EnableStatistics ? Visibility.Visible : Visibility.Hidden;
383+
}
380384
}
381385
}
382386
}
383387
}
388+
catch (Exception ex)
389+
{
390+
Logging.SaveLog(_tag, ex);
391+
}
384392
}
385393

386394
private void StorageUI()
387395
{
388-
List<ColumnItem> lvColumnItem = new();
389-
foreach (var t in lstProfiles.Columns)
396+
try
390397
{
391-
var item2 = (MyDGTextColumn)t;
392-
lvColumnItem.Add(new()
398+
List<ColumnItem> lvColumnItem = new();
399+
foreach (var item2 in lstProfiles.Columns.Cast<MyDGTextColumn>())
393400
{
394-
Name = item2.ExName,
395-
Width = (int)(item2.Visibility == Visibility.Visible ? item2.ActualWidth : -1),
396-
Index = item2.DisplayIndex
397-
});
401+
lvColumnItem.Add(new()
402+
{
403+
Name = item2.ExName,
404+
Width = (int)(item2.Visibility == Visibility.Visible ? item2.ActualWidth : -1),
405+
Index = item2.DisplayIndex
406+
});
407+
}
408+
_config.UiItem.MainColumnItem = lvColumnItem;
409+
}
410+
catch (Exception ex)
411+
{
412+
Logging.SaveLog(_tag, ex);
398413
}
399-
_config.UiItem.MainColumnItem = lvColumnItem;
400414
}
401415

402416
#endregion UI
@@ -405,7 +419,7 @@ private void StorageUI()
405419

406420
private Point startPoint = new();
407421
private int startIndex = -1;
408-
private string formatData = "ProfileItemModel";
422+
private readonly string formatData = "ProfileItemModel";
409423

410424
/// <summary>
411425
/// Helper to search up the VisualTree

0 commit comments

Comments
 (0)