Skip to content

Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic

Fixed Switch Profile

AnoF-Cyber0f0f485d23e4
diff --git a/Windows/gui/ViewModels/MainWindowViewModel.cs b/Windows/gui/ViewModels/MainWindowViewModel.cs
index a9516b5..4976793 100644
--- a/Windows/gui/ViewModels/MainWindowViewModel.cs
+++ b/Windows/gui/ViewModels/MainWindowViewModel.cs
@@ -41,7 +41,7 @@ public class MainWindowViewModel : ViewModelBase
public ObservableCollection<ProxyConfig> ProxyConfigs { get; } = new();
public ObservableCollection<ProxyRule> ProxyRules { get; } = new();
- public ObservableCollection<ProfileSwitchItem> SwitchProfileItems { get; } = new();
+ public ObservableCollection<string> SwitchProfileItems { get; } = new();
private readonly List<string> _pendingConnectionLogs = new(128);
private readonly List<string> _pendingActivityLogs = new(64);
@@ -400,9 +400,17 @@ public class MainWindowViewModel : ViewModelBase
public ICommand DeleteProfileCommand { get; }
public ICommand ImportProfileCommand { get; }
public ICommand ExportProfileCommand { get; }
+ public ICommand SwitchProfileCommand { get; }
public MainWindowViewModel()
{
+ SwitchProfileCommand = new RelayCommandWithParameter<string>(name =>
+ {
+ if (string.IsNullOrEmpty(name) || name == _activeProfileName) return;
+ SaveCurrentProfile();
+ SwitchToProfile(name);
+ });
+
ShowProxySettingsCommand = new RelayCommand(async () =>
{
var window = new ProxySettingsWindow();
@@ -1090,20 +1098,7 @@ public class MainWindowViewModel : ViewModelBase
{
SwitchProfileItems.Clear();
foreach (var name in ProfileManager.GetProfileNames())
- {
- var capName = name;
- SwitchProfileItems.Add(new ProfileSwitchItem
- {
- Name = name,
- IsActive = name == _activeProfileName,
- Command = new RelayCommand(() =>
- {
- if (capName == _activeProfileName) return;
- SaveCurrentProfile();
- SwitchToProfile(capName);
- })
- });
- }
+ SwitchProfileItems.Add(name);
}
private void UpdateSettingsProfile(string name)
diff --git a/Windows/gui/ViewModels/ProfileSwitchItem.cs b/Windows/gui/ViewModels/ProfileSwitchItem.cs
deleted file mode 100644
index 871ee70..0000000
--- a/Windows/gui/ViewModels/ProfileSwitchItem.cs
+++ /dev/null
@@ -1,10 +0,0 @@
-using System.Windows.Input;
-
-namespace ProxyBridge.GUI.ViewModels;
-
-public class ProfileSwitchItem
-{
- public string Name { get; init; } = "";
- public bool IsActive { get; init; }
- public ICommand Command { get; init; } = default!;
-}
diff --git a/Windows/gui/Views/MainWindow.axaml b/Windows/gui/Views/MainWindow.axaml
index b91bec2..9ea2853 100644
--- a/Windows/gui/Views/MainWindow.axaml
+++ b/Windows/gui/Views/MainWindow.axaml
@@ -266,14 +266,7 @@
<MenuItem Header="{Binding Loc.MenuRenameProfile}" Command="{Binding RenameProfileCommand}"/>
<MenuItem Header="{Binding Loc.MenuDeleteProfile}" Command="{Binding DeleteProfileCommand}"/>
<Separator/>
- <MenuItem Header="{Binding Loc.MenuSwitchProfile}" ItemsSource="{Binding SwitchProfileItems}">
- <MenuItem.ItemContainerTheme>
- <ControlTheme TargetType="MenuItem" x:DataType="vm:ProfileSwitchItem">
- <Setter Property="Header" Value="{Binding Name}"/>
- <Setter Property="Command" Value="{Binding Command}"/>
- </ControlTheme>
- </MenuItem.ItemContainerTheme>
- </MenuItem>
+ <MenuItem x:Name="SwitchProfileMenuItem" Header="{Binding Loc.MenuSwitchProfile}"/>
<Separator/>
<MenuItem Header="{Binding Loc.MenuImportProfile}" Command="{Binding ImportProfileCommand}"/>
<MenuItem Header="{Binding Loc.MenuExportProfile}" Command="{Binding ExportProfileCommand}"/>
diff --git a/Windows/gui/Views/MainWindow.axaml.cs b/Windows/gui/Views/MainWindow.axaml.cs
index ce0ec08..ed38983 100644
--- a/Windows/gui/Views/MainWindow.axaml.cs
+++ b/Windows/gui/Views/MainWindow.axaml.cs
@@ -1,3 +1,5 @@
+using System.Collections.Specialized;
+using System.Linq;
using Avalonia.Controls;
using ProxyBridge.GUI.ViewModels;
using System;
@@ -12,16 +14,36 @@ public partial class MainWindow : Window
{
InitializeComponent();
- // Set window reference in ViewModel
this.Opened += (s, e) =>
{
if (DataContext is MainWindowViewModel vm)
{
vm.SetMainWindow(this);
+ SetupSwitchProfileMenu(vm);
}
};
}
+ private void SetupSwitchProfileMenu(MainWindowViewModel vm)
+ {
+ var switchMenu = this.FindControl<MenuItem>("SwitchProfileMenuItem");
+ if (switchMenu == null) return;
+
+ void Rebuild()
+ {
+ var items = vm.SwitchProfileItems.Select(name =>
+ {
+ var item = new MenuItem { Header = name };
+ item.Click += (_, _) => vm.SwitchProfileCommand.Execute(name);
+ return item;
+ }).Cast<object>().ToList();
+ switchMenu.ItemsSource = items;
+ }
+
+ Rebuild();
+ vm.SwitchProfileItems.CollectionChanged += (_, _) => Rebuild();
+ }
+
private void OnChangeLanguageEnglish(object? sender, RoutedEventArgs e)
{
if (DataContext is MainWindowViewModel vm)