Prevent duplicate UI style injections

This commit is contained in:
2dust
2026-11-03 20:47:33 +08:00
parent ff9fd7b988
commit 749a7312d7
@@ -7,6 +7,10 @@ namespace v2rayN.Desktop.ViewModels;
public partial class ThemeSettingViewModel : MyReactiveObject public partial class ThemeSettingViewModel : MyReactiveObject
{ {
private Style? _fontSizeStyle;
private Style? _fontSizeExStyle;
private Style? _fontFamilyStyle;
[Reactive] public partial string CurrentTheme { get; set; } [Reactive] public partial string CurrentTheme { get; set; }
[Reactive] public partial int CurrentFontSize { get; set; } [Reactive] public partial int CurrentFontSize { get; set; }
@@ -34,7 +38,7 @@ public partial class ThemeSettingViewModel : MyReactiveObject
CurrentFontSize = _config.UiItem.CurrentFontSize; CurrentFontSize = _config.UiItem.CurrentFontSize;
CurrentLanguage = _config.UiItem.CurrentLanguage; CurrentLanguage = _config.UiItem.CurrentLanguage;
this.WhenAnyValue(x => x.CurrentTheme) this.WhenAnyValueUnsafe(x => x.CurrentTheme)
.Where(y => y.IsNotEmpty()) .Where(y => y.IsNotEmpty())
.SubscribeAsync(async _ => .SubscribeAsync(async _ =>
{ {
@@ -46,7 +50,7 @@ public partial class ThemeSettingViewModel : MyReactiveObject
} }
}); });
this.WhenAnyValue(x => x.CurrentFontSize) this.WhenAnyValueUnsafe(x => x.CurrentFontSize)
.Where(y => y > 0) .Where(y => y > 0)
.SubscribeAsync(async _ => .SubscribeAsync(async _ =>
{ {
@@ -58,7 +62,7 @@ public partial class ThemeSettingViewModel : MyReactiveObject
} }
}); });
this.WhenAnyValue(x => x.CurrentLanguage) this.WhenAnyValueUnsafe(x => x.CurrentLanguage)
.Where(y => !y.IsNullOrEmpty()) .Where(y => !y.IsNullOrEmpty())
.SubscribeAsync(async _ => .SubscribeAsync(async _ =>
{ {
@@ -75,19 +79,20 @@ public partial class ThemeSettingViewModel : MyReactiveObject
private void ModifyTheme() private void ModifyTheme()
{ {
var app = Application.Current; var app = Application.Current;
if (app is not null) if (app is null)
{ {
app.RequestedThemeVariant = CurrentTheme switch return;
{
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,
};
} }
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() private void ModifyFontSize()
@@ -98,7 +103,7 @@ public partial class ThemeSettingViewModel : MyReactiveObject
return; return;
} }
Style style = new(x => Selectors.Or( var style = new Style(x => Selectors.Or(
x.OfType<Button>(), x.OfType<Button>(),
x.OfType<TextBox>(), x.OfType<TextBox>(),
x.OfType<TextBlock>(), x.OfType<TextBlock>(),
@@ -110,12 +115,12 @@ public partial class ThemeSettingViewModel : MyReactiveObject
x.OfType<HeaderedContentControl>(), x.OfType<HeaderedContentControl>(),
x.OfType<TextEditor>() x.OfType<TextEditor>()
)); ));
style.Add(new Setter() style.Add(new Setter
{ {
Property = TemplatedControl.FontSizeProperty, Property = TemplatedControl.FontSizeProperty,
Value = size, Value = size,
}); });
Application.Current?.Styles.Add(style); ReplaceStyle(ref _fontSizeStyle, style);
ModifyFontSizeEx(size); ModifyFontSizeEx(size);
} }
@@ -124,9 +129,10 @@ public partial class ThemeSettingViewModel : MyReactiveObject
{ {
//DataGrid //DataGrid
var rowHeight = 20 + (size / 2); var rowHeight = 20 + (size / 2);
var style = new Style(x => x.OfType<DataGrid>()); var style = new Style(x => x.OfType<DataGrid>());
style.Add(new Setter(DataGrid.RowHeightProperty, rowHeight)); style.Add(new Setter(DataGrid.RowHeightProperty, rowHeight));
Application.Current?.Styles.Add(style); ReplaceStyle(ref _fontSizeExStyle, style);
} }
private void ModifyFontFamily() private void ModifyFontFamily()
@@ -139,7 +145,7 @@ public partial class ThemeSettingViewModel : MyReactiveObject
try try
{ {
Style style = new(x => Selectors.Or( var style = new Style(x => Selectors.Or(
x.OfType<Button>(), x.OfType<Button>(),
x.OfType<TextBox>(), x.OfType<TextBox>(),
x.OfType<TextBlock>(), x.OfType<TextBlock>(),
@@ -152,16 +158,33 @@ public partial class ThemeSettingViewModel : MyReactiveObject
x.OfType<WindowNotificationManager>(), x.OfType<WindowNotificationManager>(),
x.OfType<TextEditor>() x.OfType<TextEditor>()
)); ));
style.Add(new Setter() style.Add(new Setter
{ {
Property = TemplatedControl.FontFamilyProperty, Property = TemplatedControl.FontFamilyProperty,
Value = new FontFamily(currentFontFamily), Value = new FontFamily(currentFontFamily),
}); });
Application.Current?.Styles.Add(style); ReplaceStyle(ref _fontFamilyStyle, style);
} }
catch (Exception ex) catch (Exception ex)
{ {
Logging.SaveLog("ModifyFontFamily", ex); Logging.SaveLog("ModifyFontFamily", ex);
} }
} }
private static void ReplaceStyle(ref Style? oldStyle, Style newStyle)
{
var app = Application.Current;
if (app is null)
{
return;
}
if (oldStyle is not null)
{
app.Styles.Remove(oldStyle);
}
oldStyle = newStyle;
app.Styles.Add(newStyle);
}
} }