v2rayN/v2rayN/v2rayN.Desktop/Views/StatusBarView.axaml.cs

125 lines
4.6 KiB
C#
Raw Normal View History

2025-01-30 09:10:05 +00:00
using System.Reactive.Disposables;
2024-10-12 07:45:21 +00:00
using Avalonia;
using Avalonia.Controls;
2024-10-12 07:45:21 +00:00
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.ReactiveUI;
using Avalonia.Threading;
using DialogHostAvalonia;
2024-10-12 07:45:21 +00:00
using ReactiveUI;
2025-08-17 08:52:51 +00:00
using ServiceLib.Manager;
2024-10-12 07:45:21 +00:00
using Splat;
using v2rayN.Desktop.Common;
namespace v2rayN.Desktop.Views;
public partial class StatusBarView : ReactiveUserControl<StatusBarViewModel>
2024-10-12 07:45:21 +00:00
{
private static Config _config;
2024-10-12 07:45:21 +00:00
public StatusBarView()
{
InitializeComponent();
2024-10-12 07:45:21 +00:00
_config = AppHandler.Instance.Config;
//ViewModel = new StatusBarViewModel(UpdateViewHandler);
//Locator.CurrentMutable.RegisterLazySingleton(() => ViewModel, typeof(StatusBarViewModel));
ViewModel = Locator.Current.GetService<StatusBarViewModel>();
ViewModel?.InitUpdateView(UpdateViewHandler);
2024-10-12 07:45:21 +00:00
txtRunningServerDisplay.Tapped += TxtRunningServerDisplay_Tapped;
txtRunningInfoDisplay.Tapped += TxtRunningServerDisplay_Tapped;
2024-10-12 07:45:21 +00:00
this.WhenActivated(disposables =>
{
//status bar
this.OneWayBind(ViewModel, vm => vm.InboundDisplay, v => v.txtInboundDisplay.Text).DisposeWith(disposables);
this.OneWayBind(ViewModel, vm => vm.InboundLanDisplay, v => v.txtInboundLanDisplay.Text).DisposeWith(disposables);
this.OneWayBind(ViewModel, vm => vm.RunningServerDisplay, v => v.txtRunningServerDisplay.Text).DisposeWith(disposables);
this.OneWayBind(ViewModel, vm => vm.RunningInfoDisplay, v => v.txtRunningInfoDisplay.Text).DisposeWith(disposables);
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);
2024-10-12 07:45:21 +00:00
this.Bind(ViewModel, vm => vm.SystemProxySelected, v => v.cmbSystemProxy.SelectedIndex).DisposeWith(disposables);
this.Bind(ViewModel, vm => vm.SelectedRouting, v => v.cmbRoutings2.SelectedItem).DisposeWith(disposables);
});
//spEnableTun.IsVisible = (Utils.IsWindows() || AppHandler.Instance.IsAdministrator);
if (Utils.IsNonWindows() && cmbSystemProxy.Items.IsReadOnly == false)
{
cmbSystemProxy.Items.RemoveAt(cmbSystemProxy.Items.Count - 1);
2024-10-12 07:45:21 +00:00
}
}
2024-10-12 07:45:21 +00:00
private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
{
switch (action)
2024-10-12 07:45:21 +00:00
{
case EViewAction.DispatcherServerAvailability:
if (obj is null)
return false;
Dispatcher.UIThread.Post(() =>
ViewModel?.TestServerAvailabilityResult((string)obj),
DispatcherPriority.Default);
break;
2024-10-12 07:45:21 +00:00
case EViewAction.DispatcherRefreshServersBiz:
Dispatcher.UIThread.Post(() =>
ViewModel?.RefreshServersBiz(),
DispatcherPriority.Default);
break;
2024-10-12 07:45:21 +00:00
case EViewAction.DispatcherRefreshIcon:
Dispatcher.UIThread.Post(() =>
{
RefreshIcon();
},
DispatcherPriority.Default);
break;
case EViewAction.SetClipboardData:
if (obj is null)
return false;
await AvaUtils.SetClipboardData(this, (string)obj);
break;
case EViewAction.PasswordInput:
return await PasswordInputAsync();
2024-10-12 07:45:21 +00:00
}
return await Task.FromResult(true);
}
2024-10-12 07:45:21 +00:00
private void RefreshIcon()
{
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
2024-10-12 07:45:21 +00:00
{
desktop.MainWindow.Icon = AvaUtils.GetAppIcon(_config.SystemProxyItem.SysProxyType);
var iconslist = TrayIcon.GetIcons(Application.Current);
iconslist[0].Icon = desktop.MainWindow.Icon;
TrayIcon.SetIcons(Application.Current, iconslist);
2024-10-12 07:45:21 +00:00
}
}
2024-10-12 07:45:21 +00:00
private async Task<bool> PasswordInputAsync()
{
var dialog = new SudoPasswordInputView();
var obj = await DialogHost.Show(dialog);
var password = obj?.ToString();
if (password.IsNullOrEmpty())
{
togEnableTun.IsChecked = false;
return false;
}
AppHandler.Instance.LinuxSudoPwd = password;
return true;
}
private void TxtRunningServerDisplay_Tapped(object? sender, Avalonia.Input.TappedEventArgs e)
{
ViewModel?.TestServerAvailability();
2024-10-12 07:45:21 +00:00
}
2025-01-30 09:10:05 +00:00
}