From 62e5ed8a42f871c02b07b23f24de5753fe09014a Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 5 Jun 2024 18:23:28 +0800 Subject: [PATCH] enhance: supports user overrides the default color schema --- src/App.JsonCodeGen.cs | 4 +++- src/App.axaml.cs | 36 ++++++++++++++++++++++++++----- src/Resources/Locales/en_US.axaml | 1 + src/Resources/Locales/zh_CN.axaml | 1 + src/Resources/Themes.axaml | 2 +- src/ViewModels/Preference.cs | 17 ++++++++++----- src/Views/Preference.axaml | 29 +++++++++++++++++++------ src/Views/Preference.axaml.cs | 17 +++++++++++++++ src/Views/TextDiffView.axaml.cs | 6 +++--- 9 files changed, 91 insertions(+), 22 deletions(-) diff --git a/src/App.JsonCodeGen.cs b/src/App.JsonCodeGen.cs index 7269b252..af2e9913 100644 --- a/src/App.JsonCodeGen.cs +++ b/src/App.JsonCodeGen.cs @@ -1,10 +1,12 @@ -using System.Text.Json.Serialization; +using System.Collections.Generic; +using System.Text.Json.Serialization; namespace SourceGit { [JsonSourceGenerationOptions(WriteIndented = true, IgnoreReadOnlyFields = true, IgnoreReadOnlyProperties = true)] [JsonSerializable(typeof(Models.Version))] [JsonSerializable(typeof(Models.JetBrainsState))] + [JsonSerializable(typeof(Dictionary))] [JsonSerializable(typeof(ViewModels.Preference))] internal partial class JsonCodeGen : JsonSerializerContext { } } diff --git a/src/App.axaml.cs b/src/App.axaml.cs index df491dd0..3dee174f 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -145,19 +145,44 @@ namespace SourceGit app._activeLocale = targetLocale; } - public static void SetTheme(string theme) + public static void SetTheme(string theme, string colorsFile) { + var app = Current as App; + if (theme.Equals("Light", StringComparison.OrdinalIgnoreCase)) { - Current.RequestedThemeVariant = ThemeVariant.Light; + app.RequestedThemeVariant = ThemeVariant.Light; } else if (theme.Equals("Dark", StringComparison.OrdinalIgnoreCase)) { - Current.RequestedThemeVariant = ThemeVariant.Dark; + app.RequestedThemeVariant = ThemeVariant.Dark; } else { - Current.RequestedThemeVariant = ThemeVariant.Default; + app.RequestedThemeVariant = ThemeVariant.Default; + } + + if (app._colorOverrides != null) + { + app.Resources.MergedDictionaries.Remove(app._colorOverrides); + app._colorOverrides = null; + } + + if (!string.IsNullOrEmpty(colorsFile) && File.Exists(colorsFile)) + { + try + { + var resDic = new ResourceDictionary(); + var schema = JsonSerializer.Deserialize(File.ReadAllText(colorsFile), JsonCodeGen.Default.DictionaryStringString); + foreach (var kv in schema) + resDic[kv.Key] = Color.Parse(kv.Value); + + app.Resources.MergedDictionaries.Add(resDic); + app._colorOverrides = resDic; + } + catch + { + } } } @@ -256,7 +281,7 @@ namespace SourceGit var pref = ViewModels.Preference.Instance; SetLocale(pref.Locale); - SetTheme(pref.Theme); + SetTheme(pref.Theme, pref.ColorOverrides); } public override void OnFrameworkInitializationCompleted() @@ -300,6 +325,7 @@ namespace SourceGit } private ResourceDictionary _activeLocale = null; + private ResourceDictionary _colorOverrides = null; private Models.INotificationReceiver _notificationReceiver = null; } } diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index a7f470da..830f736a 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -286,6 +286,7 @@ Paste Preference APPEARANCE + Custom Color Schema Default Font Default Font Size Monospace Font diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index 6851458b..14f6085d 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -289,6 +289,7 @@ 粘贴 偏好设置 外观配置 + 自定义配色文件 缺省字体 默认字体大小 等宽字体 diff --git a/src/Resources/Themes.axaml b/src/Resources/Themes.axaml index f102b5f7..cf56f42a 100644 --- a/src/Resources/Themes.axaml +++ b/src/Resources/Themes.axaml @@ -35,7 +35,7 @@ #FF1F1F1F #FF2C2C2C #FF2B2B2B - #FF181818 + #FF1B1B1B #FF8F8F8F #FF505050 #FFF8F8F8 diff --git a/src/ViewModels/Preference.cs b/src/ViewModels/Preference.cs index 7b07b4a7..917349e0 100644 --- a/src/ViewModels/Preference.cs +++ b/src/ViewModels/Preference.cs @@ -65,9 +65,7 @@ namespace SourceGit.ViewModels set { if (SetProperty(ref _locale, value)) - { App.SetLocale(value); - } } } @@ -77,9 +75,17 @@ namespace SourceGit.ViewModels set { if (SetProperty(ref _theme, value)) - { - App.SetTheme(value); - } + App.SetTheme(_theme, _colorOverrides); + } + } + + public string ColorOverrides + { + get => _colorOverrides; + set + { + if (SetProperty(ref _colorOverrides, value)) + App.SetTheme(_theme, value); } } @@ -521,6 +527,7 @@ namespace SourceGit.ViewModels private string _locale = "en_US"; private string _theme = "Default"; + private string _colorOverrides = string.Empty; private FontFamily _defaultFont = null; private FontFamily _monospaceFont = null; private double _defaultFontSize = 13; diff --git a/src/Views/Preference.axaml b/src/Views/Preference.axaml index ab11cca3..ce3e809b 100644 --- a/src/Views/Preference.axaml +++ b/src/Views/Preference.axaml @@ -144,7 +144,7 @@ - + Light - + + + + + + + + - - - - - KnownLayer.Background; @@ -174,7 +174,7 @@ namespace SourceGit.Views public class LineStyleTransformer : DocumentColorizingTransformer { - private static readonly Brush HL_ADDED = new SolidColorBrush(Color.FromArgb(90, 0, 255, 0)); + private static readonly Brush HL_ADDED = new SolidColorBrush(Color.FromArgb(128, 0, 190, 0)); private static readonly Brush HL_DELETED = new SolidColorBrush(Color.FromArgb(80, 255, 0, 0)); public LineStyleTransformer(CombinedTextDiffPresenter editor)