add autoswitch toggle button

This commit is contained in:
lazycat75246 2023-12-17 17:26:21 +08:00
parent a0c6b39f5a
commit 5986853801
10 changed files with 100 additions and 6 deletions

View file

@ -133,6 +133,13 @@ namespace v2rayN.Handler
mtu = 9000,
};
}
if (config.autoSwitchItem == null)
{
config.autoSwitchItem = new AutoSwitchItem
{
EnableAutoSwitch = false,
};
}
if (config.guiItem == null)
{
config.guiItem = new()

View file

@ -16,7 +16,7 @@ namespace v2rayN.Handler
public string indexId { get; set; }
public long latency { get; set; }
}
internal class ServerAutoSwitch
public class ServerAutoSwitch
{
private List<TestResultItem> testResultItems= new List<TestResultItem>();
private static readonly object objLock = new();
@ -70,6 +70,11 @@ namespace v2rayN.Handler
if (taskmain != null)
return;
var profiles = LazyConfig.Instance.ProfileItemsAutoSwitch();
if (profiles.Count < 2)
return;
bStop = false;
taskmain = Task.Run(() =>
{
int iFailTimeMax = 30;

View file

@ -20,6 +20,7 @@
public CoreBasicItem coreBasicItem { get; set; }
public TunModeItem tunModeItem { get; set; }
public AutoSwitchItem autoSwitchItem { get; set; }
public KcpItem kcpItem { get; set; }
public GrpcItem grpcItem { get; set; }
public RoutingBasicItem routingBasicItem { get; set; }

View file

@ -167,6 +167,15 @@ namespace v2rayN.Mode
public bool enableIPv6Address { get; set; } = true;
}
[Serializable]
public class AutoSwitchItem
{
public bool EnableAutoSwitch { get; set; }
public int FailTimeMax { get; set; } = 60;
public int ServerSelectMode { get; set; } = 1;
public int mode { get; set; } = 0;
}
[Serializable]
public class SpeedTestItem
{

View file

@ -2041,6 +2041,15 @@ namespace v2rayN.Resx {
}
}
/// <summary>
/// 查找类似 AutoSwitch 的本地化字符串。
/// </summary>
public static string TbEnableAutoSwitchAs {
get {
return ResourceManager.GetString("TbEnableAutoSwitchAs", resourceCulture);
}
}
/// <summary>
/// 查找类似 Enable advanced function 的本地化字符串。
/// </summary>

View file

@ -988,6 +988,9 @@
<data name="TbEnableTunAs" xml:space="preserve">
<value>Enable Tun</value>
</data>
<data name="TbEnableAutoSwitchAs" xml:space="preserve">
<value>AutoSwitch</value>
</data>
<data name="TbSettingsNewPort4LAN" xml:space="preserve">
<value>New Port for LAN</value>
</data>

View file

@ -988,6 +988,9 @@
<data name="TbEnableTunAs" xml:space="preserve">
<value>启用Tun模式</value>
</data>
<data name="TbEnableAutoSwitchAs" xml:space="preserve">
<value>自动切换</value>
</data>
<data name="TbSettingsNewPort4LAN" xml:space="preserve">
<value>为局域网开启新的端口</value>
</data>

View file

@ -21,6 +21,7 @@ using v2rayN.Views;
namespace v2rayN.ViewModels
{
public delegate void SetAutoSwitchTogDelegate(bool b);
public class MainWindowViewModel : ReactiveObject
{
#region private prop
@ -35,7 +36,11 @@ namespace v2rayN.ViewModels
private readonly PaletteHelper _paletteHelper = new();
private Dictionary<string, bool> _dicHeaderSort = new();
private Action<EViewAction> _updateView;
private SetAutoSwitchTogDelegate _setAutoSwitchTog;
public void SetDelegate(SetAutoSwitchTogDelegate s = null)
{
this._setAutoSwitchTog = s;
}
#endregion private prop
#region ObservableCollection
@ -228,6 +233,9 @@ namespace v2rayN.ViewModels
[Reactive]
public bool EnableTun { get; set; }
[Reactive]
public bool EnableAutoSwitch { get; set; }
[Reactive]
public bool ColorModeDark { get; set; }
@ -250,7 +258,7 @@ namespace v2rayN.ViewModels
ServerAutoSwitch ServerAutoSwitchs= new ServerAutoSwitch();
public ServerAutoSwitch ServerAutoSwitchs= new ServerAutoSwitch();
#region Init
@ -276,6 +284,8 @@ namespace v2rayN.ViewModels
}
_subId = _config.subIndexId;
EnableAutoSwitch = _config.autoSwitchItem.EnableAutoSwitch;
InitSubscriptionView();
RefreshRoutingsMenu();
RefreshServers();
@ -319,6 +329,8 @@ namespace v2rayN.ViewModels
y => y == true)
.Subscribe(c => DoEnableTun(c));
this.WhenAnyValue(x => x.EnableAutoSwitch).Subscribe(c => DoEnableAutoSwitch(c));
BindingUI();
RestoreUI();
AutoHideStartup();
@ -593,7 +605,8 @@ namespace v2rayN.ViewModels
Reload();
ChangeSystemProxyStatus(_config.sysProxyType, true);
ServerAutoSwitchs.SetDelegate(SetDefaultServer);
ServerAutoSwitchs.Start();
if(_config.autoSwitchItem.EnableAutoSwitch)
ServerAutoSwitchs.Start();
}
private void OnProgramStarted(object state, bool timeout)
@ -1644,6 +1657,37 @@ namespace v2rayN.ViewModels
}
}
private void DoEnableAutoSwitch(bool c)
{
if (_config.autoSwitchItem.EnableAutoSwitch != c)
{
_config.autoSwitchItem.EnableAutoSwitch = c;
if (c)
{
var profiles = LazyConfig.Instance.ProfileItemsAutoSwitch();
if (profiles.Count < 2)
{
MessageBox.Show("选择的切换服务器必须大于等于2才能启动切换!");
_setAutoSwitchTog(false);
_config.autoSwitchItem.EnableAutoSwitch = false;
EnableAutoSwitch = false;
ConfigHandler.SaveConfig(ref _config);
return;
}
ServerAutoSwitchs.Start();
}
else
{
Task.Run(() =>
{
ServerAutoSwitchs.Stop();
});
}
ConfigHandler.SaveConfig(ref _config);
}
}
#endregion System proxy and Routings
#region UI

View file

@ -431,6 +431,12 @@
materialDesign:HintAssist.Hint="{x:Static resx:ResUI.MsgServerTitle}"
materialDesign:TextFieldAssist.HasClearButton="True"
Style="{StaticResource DefTextBox}" />
<TextBlock
Text="{x:Static resx:ResUI.TbEnableAutoSwitchAs}" VerticalAlignment="Center" />
<ToggleButton
x:Name="togEnableAutoSwitch"
Margin="4"
HorizontalAlignment="Left" />
</WrapPanel>
<materialDesign:ColorZone
@ -785,7 +791,7 @@
x:Name="tbNotify"
IconSource="/v2rayN.ico"
NoLeftClickDelay="True"
ToolTipText="v2rayN">
ToolTipText="v2rayN" d:IsHidden="True">
<tb:TaskbarIcon.ContextMenu>
<ContextMenu Style="{StaticResource DefContextMenu}">
<MenuItem x:Name="menuSystemProxyClear" Height="{StaticResource MenuItemHeight}">

View file

@ -60,6 +60,8 @@ namespace v2rayN.Views
ViewModel = new MainWindowViewModel(MainSnackbar.MessageQueue!, UpdateViewHandler);
Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(MainWindowViewModel));
ViewModel.SetDelegate((bool b)=> togEnableAutoSwitch.IsChecked = b);
for (int i = Global.MinFontSize; i <= Global.MinFontSize + 8; i++)
{
cmbCurrentFontSize.Items.Add(i.ToString());
@ -192,6 +194,7 @@ namespace v2rayN.Views
this.OneWayBind(ViewModel, vm => vm.SpeedProxyDisplay, v => v.txtSpeedProxyDisplay.Text).DisposeWith(disposables);
this.OneWayBind(ViewModel, vm => vm.SpeedDirectDisplay, v => v.txtSpeedDirectDisplay.Text).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.EnableTun, v => v.togEnableTun.IsChecked).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.EnableAutoSwitch, v => v.togEnableAutoSwitch.IsChecked).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SystemProxySelected, v => v.cmbSystemProxy.SelectedIndex).DisposeWith(disposables);
this.OneWayBind(ViewModel, vm => vm.RoutingItems, v => v.cmbRoutings2.ItemsSource).DisposeWith(disposables);
@ -466,7 +469,7 @@ namespace v2rayN.Views
Width = _config.uiItem.mainWidth;
Height = _config.uiItem.mainHeight;
}
var maxWidth = SystemParameters.WorkArea.Width;
var maxHeight = SystemParameters.WorkArea.Height;
if (Width > maxWidth) Width = maxWidth;
@ -569,6 +572,10 @@ namespace v2rayN.Views
var item = LazyConfig.Instance.GetProfileItem(ViewModel.SelectedProfile.indexId);
item.autoSwitch = ViewModel.SelectedProfile.autoSwitch;
SqliteHelper.Instance.Update(item);
//if(ViewModel.EnableAutoSwitch)
//{
// ViewModel.ServerAutoSwitchs.Start();
//}
}
#endregion UI