Skip to content

Commit 429d4fb

Browse files
committed
fix: remove Save/Cancel from routing settings, save edits immediately
1 parent 4524784 commit 429d4fb

5 files changed

Lines changed: 33 additions & 92 deletions

File tree

v2rayN/ServiceLib/ViewModels/RoutingSettingViewModel.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class RoutingSettingViewModel : MyReactiveObject
2222
public ReactiveCommand<Unit, Unit> RoutingAdvancedSetDefaultCmd { get; }
2323
public ReactiveCommand<Unit, Unit> RoutingAdvancedImportRulesCmd { get; }
2424

25-
public ReactiveCommand<Unit, Unit> SaveCmd { get; }
2625
public bool IsModified { get; set; }
2726

2827
#endregion Reactive
@@ -53,12 +52,14 @@ public RoutingSettingViewModel(Func<EViewAction, object?, Task<bool>>? updateVie
5352
await RoutingAdvancedImportRules();
5453
});
5554

56-
SaveCmd = ReactiveCommand.CreateFromTask(async () =>
57-
{
58-
await SaveRoutingAsync();
59-
});
60-
6155
_ = Init();
56+
57+
// Auto-save DomainStrategy when changed
58+
this.WhenAnyValue(
59+
x => x.DomainStrategy,
60+
x => x.DomainStrategy4Singbox)
61+
.Skip(1)
62+
.Subscribe(_ => SaveSettingsAsync());
6263
}
6364

6465
private async Task Init()
@@ -96,20 +97,14 @@ public async Task RefreshRoutingItems()
9697
}
9798
}
9899

99-
private async Task SaveRoutingAsync()
100+
/// <summary>
101+
/// Save DomainStrategy settings on window close
102+
/// </summary>
103+
public async Task SaveSettingsAsync()
100104
{
101105
_config.RoutingBasicItem.DomainStrategy = DomainStrategy;
102106
_config.RoutingBasicItem.DomainStrategy4Singbox = DomainStrategy4Singbox;
103-
104-
if (await ConfigHandler.SaveConfig(_config) == 0)
105-
{
106-
NoticeManager.Instance.Enqueue(ResUI.OperationSuccess);
107-
_updateView?.Invoke(EViewAction.CloseWindow, null);
108-
}
109-
else
110-
{
111-
NoticeManager.Instance.Enqueue(ResUI.OperationFailed);
112-
}
107+
await ConfigHandler.SaveConfig(_config);
113108
}
114109

115110
#endregion Refresh Save

v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,7 @@
2020
<MenuItem x:Name="menuRoutingAdvancedImportRules2" Header="{x:Static resx:ResUI.menuRoutingAdvancedImportRules}" />
2121
</Menu>
2222

23-
<StackPanel
24-
Margin="{StaticResource Margin4}"
25-
HorizontalAlignment="Right"
26-
DockPanel.Dock="Bottom"
27-
Orientation="Horizontal">
28-
<Button
29-
x:Name="btnSave"
30-
Width="100"
31-
Content="{x:Static resx:ResUI.TbConfirm}"
32-
IsDefault="True" />
33-
<Button
34-
x:Name="btnCancel"
35-
Width="100"
36-
Margin="{StaticResource MarginLr8}"
37-
Content="{x:Static resx:ResUI.TbCancel}"
38-
IsCancel="True" />
39-
</StackPanel>
23+
4024

4125
<Grid
4226
Margin="{StaticResource Margin4}"

v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ namespace v2rayN.Desktop.Views;
55

66
public partial class RoutingSettingWindow : WindowBase<RoutingSettingViewModel>
77
{
8-
private bool _manualClose = false;
9-
108
public RoutingSettingWindow()
119
{
1210
InitializeComponent();
1311

1412
Loaded += Window_Loaded;
1513
Closing += RoutingSettingWindow_Closing;
16-
btnCancel.Click += (s, e) => Close();
1714
KeyDown += RoutingSettingWindow_KeyDown;
1815
lstRoutings.SelectionChanged += lstRoutings_SelectionChanged;
1916
lstRoutings.DoubleTapped += LstRoutings_DoubleTapped;
@@ -38,8 +35,6 @@ public RoutingSettingWindow()
3835
this.BindCommand(ViewModel, vm => vm.RoutingAdvancedSetDefaultCmd, v => v.menuRoutingAdvancedSetDefault).DisposeWith(disposables);
3936
this.BindCommand(ViewModel, vm => vm.RoutingAdvancedImportRulesCmd, v => v.menuRoutingAdvancedImportRules).DisposeWith(disposables);
4037
this.BindCommand(ViewModel, vm => vm.RoutingAdvancedImportRulesCmd, v => v.menuRoutingAdvancedImportRules2).DisposeWith(disposables);
41-
42-
this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables);
4338
});
4439
}
4540

@@ -69,6 +64,19 @@ private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
6964
return await Task.FromResult(true);
7065
}
7166

67+
private async void RoutingSettingWindow_Closing(object? sender, WindowClosingEventArgs e)
68+
{
69+
// Save DomainStrategy settings on close
70+
if (ViewModel != null)
71+
{
72+
await ViewModel.SaveSettingsAsync();
73+
}
74+
if (ViewModel?.IsModified == true)
75+
{
76+
Close(true);
77+
}
78+
}
79+
7280
private void RoutingSettingWindow_KeyDown(object? sender, KeyEventArgs e)
7381
{
7482
if (e.KeyModifiers is KeyModifiers.Control or KeyModifiers.Meta)
@@ -125,25 +133,7 @@ private void linkdomainStrategy4Singbox_Click(object? sender, RoutedEventArgs e)
125133
ProcUtils.ProcessStart("https://sing-box.sagernet.org/zh/configuration/route/rule_action/#strategy");
126134
}
127135

128-
private void btnCancel_Click(object? sender, RoutedEventArgs e)
129-
{
130-
_manualClose = true;
131-
Close(ViewModel?.IsModified);
132-
}
133-
134-
private void RoutingSettingWindow_Closing(object? sender, WindowClosingEventArgs e)
135-
{
136-
if (ViewModel?.IsModified == true)
137-
{
138-
if (!_manualClose)
139-
{
140-
btnCancel_Click(null, null);
141-
}
142-
}
143-
}
144-
145136
private void Window_Loaded(object? sender, RoutedEventArgs e)
146137
{
147-
btnCancel.Focus();
148138
}
149139
}

v2rayN/v2rayN/Views/RoutingSettingWindow.xaml

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,7 @@
5252
</ToolBar>
5353
</ToolBarTray>
5454

55-
<StackPanel
56-
Margin="{StaticResource Margin8}"
57-
HorizontalAlignment="Right"
58-
DockPanel.Dock="Bottom"
59-
Orientation="Horizontal">
60-
<Button
61-
x:Name="btnSave"
62-
Width="100"
63-
Content="{x:Static resx:ResUI.TbConfirm}"
64-
IsDefault="True"
65-
Style="{StaticResource DefButton}" />
66-
<Button
67-
x:Name="btnCancel"
68-
Width="100"
69-
Margin="{StaticResource MarginLeftRight8}"
70-
Content="{x:Static resx:ResUI.TbCancel}"
71-
IsCancel="true"
72-
Style="{StaticResource DefButton}" />
73-
</StackPanel>
55+
7456

7557
<Grid Margin="{StaticResource Margin8}" DockPanel.Dock="Top">
7658
<Grid.RowDefinitions>

v2rayN/v2rayN/Views/RoutingSettingWindow.xaml.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public RoutingSettingWindow()
1212
lstRoutings.SelectionChanged += lstRoutings_SelectionChanged;
1313
lstRoutings.MouseDoubleClick += LstRoutings_MouseDoubleClick;
1414
menuRoutingAdvancedSelectAll.Click += menuRoutingAdvancedSelectAll_Click;
15-
btnCancel.Click += btnCancel_Click;
1615

1716
ViewModel = new RoutingSettingViewModel(UpdateViewHandler);
1817

@@ -33,8 +32,6 @@ public RoutingSettingWindow()
3332
this.BindCommand(ViewModel, vm => vm.RoutingAdvancedSetDefaultCmd, v => v.menuRoutingAdvancedSetDefault).DisposeWith(disposables);
3433
this.BindCommand(ViewModel, vm => vm.RoutingAdvancedImportRulesCmd, v => v.menuRoutingAdvancedImportRules).DisposeWith(disposables);
3534
this.BindCommand(ViewModel, vm => vm.RoutingAdvancedImportRulesCmd, v => v.menuRoutingAdvancedImportRules2).DisposeWith(disposables);
36-
37-
this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables);
3835
});
3936
WindowsUtils.SetDarkBorder(this, AppManager.Instance.Config.UiItem.CurrentTheme);
4037
}
@@ -66,8 +63,13 @@ private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
6663
return await Task.FromResult(true);
6764
}
6865

69-
private void RoutingSettingWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
66+
private async void RoutingSettingWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
7067
{
68+
// Save DomainStrategy settings on close
69+
if (ViewModel != null)
70+
{
71+
await ViewModel.SaveSettingsAsync();
72+
}
7173
if (ViewModel?.IsModified == true)
7274
{
7375
DialogResult = true;
@@ -129,16 +131,4 @@ private void linkdomainStrategy4Singbox_Click(object sender, RoutedEventArgs e)
129131
{
130132
ProcUtils.ProcessStart("https://sing-box.sagernet.org/zh/configuration/route/rule_action/#strategy");
131133
}
132-
133-
private void btnCancel_Click(object sender, System.Windows.RoutedEventArgs e)
134-
{
135-
if (ViewModel?.IsModified == true)
136-
{
137-
DialogResult = true;
138-
}
139-
else
140-
{
141-
Close();
142-
}
143-
}
144134
}

0 commit comments

Comments
 (0)