mirror of
https://github.com/2dust/v2rayN.git
synced 2026-11-03 19:15:04 +03:00
Prevent duplicate UI style injections
This commit is contained in:
@@ -7,6 +7,10 @@ namespace v2rayN.Desktop.ViewModels;
|
||||
|
||||
public partial class ThemeSettingViewModel : MyReactiveObject
|
||||
{
|
||||
private Style? _fontSizeStyle;
|
||||
private Style? _fontSizeExStyle;
|
||||
private Style? _fontFamilyStyle;
|
||||
|
||||
[Reactive] public partial string CurrentTheme { get; set; }
|
||||
|
||||
[Reactive] public partial int CurrentFontSize { get; set; }
|
||||
@@ -34,7 +38,7 @@ public partial class ThemeSettingViewModel : MyReactiveObject
|
||||
CurrentFontSize = _config.UiItem.CurrentFontSize;
|
||||
CurrentLanguage = _config.UiItem.CurrentLanguage;
|
||||
|
||||
this.WhenAnyValue(x => x.CurrentTheme)
|
||||
this.WhenAnyValueUnsafe(x => x.CurrentTheme)
|
||||
.Where(y => y.IsNotEmpty())
|
||||
.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)
|
||||
.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())
|
||||
.SubscribeAsync(async _ =>
|
||||
{
|
||||
@@ -75,19 +79,20 @@ public partial class ThemeSettingViewModel : MyReactiveObject
|
||||
private void ModifyTheme()
|
||||
{
|
||||
var app = Application.Current;
|
||||
if (app is not null)
|
||||
if (app is 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,
|
||||
};
|
||||
return;
|
||||
}
|
||||
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()
|
||||
@@ -98,7 +103,7 @@ public partial class ThemeSettingViewModel : MyReactiveObject
|
||||
return;
|
||||
}
|
||||
|
||||
Style style = new(x => Selectors.Or(
|
||||
var style = new Style(x => Selectors.Or(
|
||||
x.OfType<Button>(),
|
||||
x.OfType<TextBox>(),
|
||||
x.OfType<TextBlock>(),
|
||||
@@ -110,12 +115,12 @@ public partial class ThemeSettingViewModel : MyReactiveObject
|
||||
x.OfType<HeaderedContentControl>(),
|
||||
x.OfType<TextEditor>()
|
||||
));
|
||||
style.Add(new Setter()
|
||||
style.Add(new Setter
|
||||
{
|
||||
Property = TemplatedControl.FontSizeProperty,
|
||||
Value = size,
|
||||
});
|
||||
Application.Current?.Styles.Add(style);
|
||||
ReplaceStyle(ref _fontSizeStyle, style);
|
||||
|
||||
ModifyFontSizeEx(size);
|
||||
}
|
||||
@@ -124,9 +129,10 @@ public partial class ThemeSettingViewModel : MyReactiveObject
|
||||
{
|
||||
//DataGrid
|
||||
var rowHeight = 20 + (size / 2);
|
||||
|
||||
var style = new Style(x => x.OfType<DataGrid>());
|
||||
style.Add(new Setter(DataGrid.RowHeightProperty, rowHeight));
|
||||
Application.Current?.Styles.Add(style);
|
||||
ReplaceStyle(ref _fontSizeExStyle, style);
|
||||
}
|
||||
|
||||
private void ModifyFontFamily()
|
||||
@@ -139,7 +145,7 @@ public partial class ThemeSettingViewModel : MyReactiveObject
|
||||
|
||||
try
|
||||
{
|
||||
Style style = new(x => Selectors.Or(
|
||||
var style = new Style(x => Selectors.Or(
|
||||
x.OfType<Button>(),
|
||||
x.OfType<TextBox>(),
|
||||
x.OfType<TextBlock>(),
|
||||
@@ -152,16 +158,33 @@ public partial class ThemeSettingViewModel : MyReactiveObject
|
||||
x.OfType<WindowNotificationManager>(),
|
||||
x.OfType<TextEditor>()
|
||||
));
|
||||
style.Add(new Setter()
|
||||
style.Add(new Setter
|
||||
{
|
||||
Property = TemplatedControl.FontFamilyProperty,
|
||||
Value = new FontFamily(currentFontFamily),
|
||||
});
|
||||
Application.Current?.Styles.Add(style);
|
||||
ReplaceStyle(ref _fontFamilyStyle, style);
|
||||
}
|
||||
catch (Exception 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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user