Skip to content

Commit df800a6

Browse files
committed
Persist DataGrid column layout for ClashConnectionsView
1 parent 679bd8a commit df800a6

3 files changed

Lines changed: 139 additions & 35 deletions

File tree

v2rayN/v2rayN.Desktop/Views/ClashConnectionsView.axaml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,28 @@
7474
<DataGridTextColumn
7575
Width="300"
7676
Binding="{Binding Host}"
77-
Header="{x:Static resx:ResUI.TbSortingHost}" />
77+
Header="{x:Static resx:ResUI.TbSortingHost}"
78+
Tag="Host" />
7879
<DataGridTextColumn
7980
Width="500"
8081
Binding="{Binding Chain}"
81-
Header="{x:Static resx:ResUI.TbSortingChain}" />
82+
Header="{x:Static resx:ResUI.TbSortingChain}"
83+
Tag="Chain" />
8284
<DataGridTextColumn
8385
Width="80"
8486
Binding="{Binding Network}"
85-
Header="{x:Static resx:ResUI.TbSortingNetwork}" />
87+
Header="{x:Static resx:ResUI.TbSortingNetwork}"
88+
Tag="Network" />
8689
<DataGridTextColumn
8790
Width="160"
8891
Binding="{Binding Type}"
89-
Header="{x:Static resx:ResUI.TbSortingType}" />
92+
Header="{x:Static resx:ResUI.TbSortingType}"
93+
Tag="Type" />
9094
<DataGridTextColumn
9195
Width="100"
9296
Binding="{Binding Elapsed}"
93-
Header="{x:Static resx:ResUI.TbSortingTime}" />
97+
Header="{x:Static resx:ResUI.TbSortingTime}"
98+
Tag="Elapsed" />
9499
</DataGrid.Columns>
95100
</DataGrid>
96101
</DockPanel>

v2rayN/v2rayN.Desktop/Views/ClashConnectionsView.axaml.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ namespace v2rayN.Desktop.Views;
22

33
public partial class ClashConnectionsView : ReactiveUserControl<ClashConnectionsViewModel>
44
{
5+
private static Config _config;
6+
private static readonly string _tag = "ClashConnectionsView";
7+
58
public ClashConnectionsView()
69
{
710
InitializeComponent();
11+
12+
_config = AppManager.Instance.Config;
13+
814
ViewModel = new ClashConnectionsViewModel(UpdateViewHandler);
915
btnAutofitColumnWidth.Click += BtnAutofitColumnWidth_Click;
1016

@@ -19,7 +25,15 @@ public ClashConnectionsView()
1925
this.Bind(ViewModel, vm => vm.HostFilter, v => v.txtHostFilter.Text).DisposeWith(disposables);
2026
this.BindCommand(ViewModel, vm => vm.ConnectionCloseAllCmd, v => v.btnConnectionCloseAll).DisposeWith(disposables);
2127
this.Bind(ViewModel, vm => vm.AutoRefresh, v => v.togAutoRefresh.IsChecked).DisposeWith(disposables);
28+
29+
AppEvents.AppExitRequested
30+
.AsObservable()
31+
.ObserveOn(RxApp.MainThreadScheduler)
32+
.Subscribe(_ => StorageUI())
33+
.DisposeWith(disposables);
2234
});
35+
36+
RestoreUI();
2337
}
2438

2539
private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
@@ -51,4 +65,74 @@ private void btnClose_Click(object? sender, RoutedEventArgs e)
5165
{
5266
ViewModel?.ClashConnectionClose(false);
5367
}
68+
69+
#region UI
70+
71+
private void RestoreUI()
72+
{
73+
try
74+
{
75+
var lvColumnItem = _config.ClashUIItem?.ConnectionsColumnItem?.OrderBy(t => t.Index).ToList();
76+
if (lvColumnItem == null)
77+
{
78+
return;
79+
}
80+
81+
var displayIndex = 0;
82+
foreach (var item in lvColumnItem)
83+
{
84+
foreach (var item2 in lstConnections.Columns)
85+
{
86+
if (item2.Tag == null)
87+
{
88+
continue;
89+
}
90+
if (item2.Tag.Equals(item.Name))
91+
{
92+
if (item.Width < 0)
93+
{
94+
item2.IsVisible = false;
95+
}
96+
else
97+
{
98+
item2.Width = new DataGridLength(item.Width, DataGridLengthUnitType.Pixel);
99+
item2.DisplayIndex = displayIndex++;
100+
}
101+
}
102+
}
103+
}
104+
}
105+
catch (Exception ex)
106+
{
107+
Logging.SaveLog(_tag, ex);
108+
}
109+
}
110+
111+
private void StorageUI()
112+
{
113+
try
114+
{
115+
List<ColumnItem> lvColumnItem = new();
116+
foreach (var item2 in lstConnections.Columns)
117+
{
118+
if (item2.Tag == null)
119+
{
120+
continue;
121+
}
122+
lvColumnItem.Add(new()
123+
{
124+
Name = (string)item2.Tag,
125+
Width = (int)(item2.IsVisible == true ? item2.ActualWidth : -1),
126+
Index = item2.DisplayIndex
127+
});
128+
}
129+
_config.ClashUIItem.ConnectionsColumnItem = lvColumnItem;
130+
}
131+
catch (Exception ex)
132+
{
133+
Logging.SaveLog(_tag, ex);
134+
}
135+
}
136+
137+
#endregion UI
54138
}

v2rayN/v2rayN.Desktop/Views/ProfilesView.axaml.cs

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public partial class ProfilesView : ReactiveUserControl<ProfilesViewModel>
77
{
88
private static Config _config;
99
private Window? _window;
10+
private static readonly string _tag = "ProfilesView";
1011

1112
public ProfilesView()
1213
{
@@ -381,7 +382,7 @@ private void AutofitColumnWidth()
381382
}
382383
catch (Exception ex)
383384
{
384-
Logging.SaveLog("ProfilesView", ex);
385+
Logging.SaveLog(_tag, ex);
385386
}
386387
}
387388

@@ -399,53 +400,67 @@ private void TxtServerFilter_KeyDown(object? sender, KeyEventArgs e)
399400

400401
private void RestoreUI()
401402
{
402-
var lvColumnItem = _config.UiItem.MainColumnItem.OrderBy(t => t.Index).ToList();
403-
var displayIndex = 0;
404-
foreach (var item in lvColumnItem)
403+
try
405404
{
406-
foreach (var item2 in lstProfiles.Columns)
405+
var lvColumnItem = _config.UiItem.MainColumnItem.OrderBy(t => t.Index).ToList();
406+
var displayIndex = 0;
407+
foreach (var item in lvColumnItem)
407408
{
408-
if (item2.Tag == null)
409-
{
410-
continue;
411-
}
412-
if (item2.Tag.Equals(item.Name))
409+
foreach (var item2 in lstProfiles.Columns)
413410
{
414-
if (item.Width < 0)
411+
if (item2.Tag == null)
415412
{
416-
item2.IsVisible = false;
413+
continue;
417414
}
418-
else
415+
if (item2.Tag.Equals(item.Name))
419416
{
420-
item2.Width = new DataGridLength(item.Width, DataGridLengthUnitType.Pixel);
421-
item2.DisplayIndex = displayIndex++;
422-
}
423-
if (item.Name.ToLower().StartsWith("to"))
424-
{
425-
item2.IsVisible = _config.GuiItem.EnableStatistics;
417+
if (item.Width < 0)
418+
{
419+
item2.IsVisible = false;
420+
}
421+
else
422+
{
423+
item2.Width = new DataGridLength(item.Width, DataGridLengthUnitType.Pixel);
424+
item2.DisplayIndex = displayIndex++;
425+
}
426+
if (item.Name.ToLower().StartsWith("to"))
427+
{
428+
item2.IsVisible = _config.GuiItem.EnableStatistics;
429+
}
426430
}
427431
}
428432
}
429433
}
434+
catch (Exception ex)
435+
{
436+
Logging.SaveLog(_tag, ex);
437+
}
430438
}
431439

432440
private void StorageUI()
433441
{
434-
List<ColumnItem> lvColumnItem = new();
435-
foreach (var item2 in lstProfiles.Columns)
442+
try
436443
{
437-
if (item2.Tag == null)
444+
List<ColumnItem> lvColumnItem = new();
445+
foreach (var item2 in lstProfiles.Columns)
438446
{
439-
continue;
447+
if (item2.Tag == null)
448+
{
449+
continue;
450+
}
451+
lvColumnItem.Add(new()
452+
{
453+
Name = (string)item2.Tag,
454+
Width = (int)(item2.IsVisible == true ? item2.ActualWidth : -1),
455+
Index = item2.DisplayIndex
456+
});
440457
}
441-
lvColumnItem.Add(new()
442-
{
443-
Name = (string)item2.Tag,
444-
Width = (int)(item2.IsVisible == true ? item2.ActualWidth : -1),
445-
Index = item2.DisplayIndex
446-
});
458+
_config.UiItem.MainColumnItem = lvColumnItem;
459+
}
460+
catch (Exception ex)
461+
{
462+
Logging.SaveLog(_tag, ex);
447463
}
448-
_config.UiItem.MainColumnItem = lvColumnItem;
449464
}
450465

451466
#endregion UI

0 commit comments

Comments
 (0)