using Avalonia.Controls.Notifications; using Avalonia.Controls.Primitives; using AvaloniaEdit; using Semi.Avalonia; namespace v2rayN.Desktop.ViewModels; public class ThemeSettingViewModel : MyReactiveObject { [Reactive] public string CurrentTheme { get; set; } [Reactive] public int CurrentFontSize { get; set; } [Reactive] public string CurrentLanguage { get; set; } public ThemeSettingViewModel() { _config = AppManager.Instance.Config; BindingUI(); RestoreUI(); } private void RestoreUI() { ModifyTheme(); ModifyFontFamily(); ModifyFontSize(); } private void BindingUI() { CurrentTheme = _config.UiItem.CurrentTheme; CurrentFontSize = _config.UiItem.CurrentFontSize; CurrentLanguage = _config.UiItem.CurrentLanguage; this.WhenAnyValue(x => x.CurrentTheme) .Subscribe(c => { if (_config.UiItem.CurrentTheme != CurrentTheme) { _config.UiItem.CurrentTheme = CurrentTheme; ModifyTheme(); ConfigHandler.SaveConfig(_config); } }); this.WhenAnyValue( x => x.CurrentFontSize, y => y > 0) .Subscribe(c => { if (_config.UiItem.CurrentFontSize != CurrentFontSize && CurrentFontSize >= Global.MinFontSize) { _config.UiItem.CurrentFontSize = CurrentFontSize; ModifyFontSize(); ConfigHandler.SaveConfig(_config); } }); this.WhenAnyValue( x => x.CurrentLanguage, y => y != null && !y.IsNullOrEmpty()) .Subscribe(c => { if (CurrentLanguage.IsNotEmpty() && _config.UiItem.CurrentLanguage != CurrentLanguage) { _config.UiItem.CurrentLanguage = CurrentLanguage; Thread.CurrentThread.CurrentUICulture = new(CurrentLanguage); ConfigHandler.SaveConfig(_config); NoticeManager.Instance.Enqueue(ResUI.NeedRebootTips); } }); } private void ModifyTheme() { var app = Application.Current; if (app is not null) { app.RequestedThemeVariant = CurrentTheme switch { nameof(ETheme.Dark) => ThemeVariant.Dark, nameof(ETheme.Light) => ThemeVariant.Light, nameof(ETheme.Aquatic) => SemiTheme.Aquatic, nameof(ETheme.Desert) => SemiTheme.Desert, nameof(ETheme.Dusk) => SemiTheme.Dusk, nameof(ETheme.NightSky) => SemiTheme.NightSky, _ => ThemeVariant.Default, }; } } private void ModifyFontSize() { double size = CurrentFontSize; if (size < Global.MinFontSize) { return; } Style style = new(x => Selectors.Or( x.OfType