refactor: rename Models.CustomColorSchema to Models.ThemeOverrides because it do NOT contains only colors currently.

This commit is contained in:
leo 2024-07-08 16:21:57 +08:00
parent af388bad60
commit 16d9b627f0
No known key found for this signature in database
12 changed files with 49 additions and 74 deletions

View file

@ -6,8 +6,8 @@ namespace SourceGit
[JsonSourceGenerationOptions(WriteIndented = true, IgnoreReadOnlyFields = true, IgnoreReadOnlyProperties = true)] [JsonSourceGenerationOptions(WriteIndented = true, IgnoreReadOnlyFields = true, IgnoreReadOnlyProperties = true)]
[JsonSerializable(typeof(List<Models.InteractiveRebaseJob>))] [JsonSerializable(typeof(List<Models.InteractiveRebaseJob>))]
[JsonSerializable(typeof(Models.JetBrainsState))] [JsonSerializable(typeof(Models.JetBrainsState))]
[JsonSerializable(typeof(Models.ThemeOverrides))]
[JsonSerializable(typeof(Models.Version))] [JsonSerializable(typeof(Models.Version))]
[JsonSerializable(typeof(Models.CustomColorSchema))]
[JsonSerializable(typeof(ViewModels.Preference))] [JsonSerializable(typeof(ViewModels.Preference))]
[JsonSerializable(typeof(ViewModels.RepositorySettings))] [JsonSerializable(typeof(ViewModels.RepositorySettings))]
internal partial class JsonCodeGen : JsonSerializerContext { } internal partial class JsonCodeGen : JsonSerializerContext { }

View file

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.IO; using System.IO;
using System.Net.Http; using System.Net.Http;
using System.Reflection; using System.Reflection;
@ -144,7 +143,7 @@ namespace SourceGit
app._activeLocale = targetLocale; app._activeLocale = targetLocale;
} }
public static void SetTheme(string theme, string colorsFile) public static void SetTheme(string theme, string themeOverridesFile)
{ {
var app = Current as App; var app = Current as App;
@ -155,22 +154,19 @@ namespace SourceGit
else else
app.RequestedThemeVariant = ThemeVariant.Default; app.RequestedThemeVariant = ThemeVariant.Default;
if (app._colorOverrides != null) if (app._themeOverrides != null)
{ {
app.Resources.MergedDictionaries.Remove(app._colorOverrides); app.Resources.MergedDictionaries.Remove(app._themeOverrides);
app._colorOverrides = null; app._themeOverrides = null;
} }
Models.CommitGraph.SetDefaultPens(); if (!string.IsNullOrEmpty(themeOverridesFile) && File.Exists(themeOverridesFile))
if (!string.IsNullOrEmpty(colorsFile) && File.Exists(colorsFile))
{ {
try try
{ {
var resDic = new ResourceDictionary(); var resDic = new ResourceDictionary();
var overrides = JsonSerializer.Deserialize(File.ReadAllText(themeOverridesFile), JsonCodeGen.Default.ThemeOverrides);
var schema = JsonSerializer.Deserialize(File.ReadAllText(colorsFile), JsonCodeGen.Default.CustomColorSchema); foreach (var kv in overrides.BasicColors)
foreach (var kv in schema.Basic)
{ {
if (kv.Key.Equals("SystemAccentColor", StringComparison.Ordinal)) if (kv.Key.Equals("SystemAccentColor", StringComparison.Ordinal))
resDic["SystemAccentColor"] = Color.Parse(kv.Value); resDic["SystemAccentColor"] = Color.Parse(kv.Value);
@ -178,40 +174,30 @@ namespace SourceGit
resDic[$"Color.{kv.Key}"] = Color.Parse(kv.Value); resDic[$"Color.{kv.Key}"] = Color.Parse(kv.Value);
} }
if (overrides.GraphColors.Count > 0)
if (schema.Graph.Count > 0)
{ {
var penColors = new List<Color>(); var penColors = new List<Color>();
foreach (var c in overrides.GraphColors)
foreach (var c in schema.Graph)
penColors.Add(Color.Parse(c)); penColors.Add(Color.Parse(c));
Models.CommitGraph.SetPenColors(penColors); Models.CommitGraph.SetPens(penColors, overrides.GraphPenThickness);
} }
else
foreach (var kv in schema.General)
{ {
if (kv.Key.Equals("Pen.Thickness", StringComparison.Ordinal)) Models.CommitGraph.SetDefaultPens(overrides.GraphPenThickness);
{
double thick = Models.CommitGraph.GetPenThickness();
try
{
thick = double.Parse(kv.Value, CultureInfo.InvariantCulture);
}
catch
{
}
Models.CommitGraph.SetPenThickness(thick);
}
} }
app.Resources.MergedDictionaries.Add(resDic); app.Resources.MergedDictionaries.Add(resDic);
app._colorOverrides = resDic; app._themeOverrides = resDic;
} }
catch catch
{ {
} }
} }
else
{
Models.CommitGraph.SetDefaultPens();
}
} }
public static async void CopyText(string data) public static async void CopyText(string data)
@ -338,7 +324,7 @@ namespace SourceGit
var pref = ViewModels.Preference.Instance; var pref = ViewModels.Preference.Instance;
SetLocale(pref.Locale); SetLocale(pref.Locale);
SetTheme(pref.Theme, pref.ColorOverrides); SetTheme(pref.Theme, pref.ThemeOverrides);
} }
public override void OnFrameworkInitializationCompleted() public override void OnFrameworkInitializationCompleted()
@ -383,6 +369,6 @@ namespace SourceGit
private ViewModels.Launcher _launcher = null; private ViewModels.Launcher _launcher = null;
private ResourceDictionary _activeLocale = null; private ResourceDictionary _activeLocale = null;
private ResourceDictionary _colorOverrides = null; private ResourceDictionary _themeOverrides = null;
} }
} }

View file

@ -108,30 +108,21 @@ namespace SourceGit.Models
private set; private set;
} = new List<Pen>(); } = new List<Pen>();
public static void SetDefaultPens() public static void SetDefaultPens(double thickness = 1.5)
{ {
SetPenColors(_defaultPenColors); SetPens(_defaultPenColors, thickness);
} }
public static void SetPenColors(List<Color> colors) public static void SetPens(List<Color> colors, double thickness)
{ {
Pens.Clear(); Pens.Clear();
foreach (var c in colors) foreach (var c in colors)
Pens.Add(new Pen(c.ToUInt32(), 2)); Pens.Add(new Pen(c.ToUInt32(), thickness));
_penCount = colors.Count; _penCount = colors.Count;
} }
public static void SetPenThickness(double value)
{
_penThickness = value;
}
public static double GetPenThickness()
{
return _penThickness;
}
public static CommitGraph Parse(List<Commit> commits) public static CommitGraph Parse(List<Commit> commits)
{ {
double UNIT_WIDTH = 12; double UNIT_WIDTH = 12;
@ -277,7 +268,6 @@ namespace SourceGit.Models
return temp; return temp;
} }
private static double _penThickness = 1;
private static int _penCount = 0; private static int _penCount = 0;
private static readonly List<Color> _defaultPenColors = [ private static readonly List<Color> _defaultPenColors = [
Colors.Orange, Colors.Orange,

View file

@ -1,11 +0,0 @@
using System.Collections.Generic;
namespace SourceGit.Models
{
public class CustomColorSchema
{
public Dictionary<string, string> Basic { get; set; } = new Dictionary<string, string>();
public List<string> Graph { get; set; } = new List<string>();
public Dictionary<string,string> General { get; set; } = new Dictionary<string, string>();
}
}

View file

@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace SourceGit.Models
{
public class ThemeOverrides
{
public Dictionary<string, string> BasicColors { get; set; } = new Dictionary<string, string>();
public double GraphPenThickness { get; set; } = 1.5;
public List<string> GraphColors { get; set; } = new List<string>();
}
}

View file

@ -337,11 +337,11 @@
<x:String x:Key="Text.Paste" xml:space="preserve">Paste</x:String> <x:String x:Key="Text.Paste" xml:space="preserve">Paste</x:String>
<x:String x:Key="Text.Preference" xml:space="preserve">Preference</x:String> <x:String x:Key="Text.Preference" xml:space="preserve">Preference</x:String>
<x:String x:Key="Text.Preference.Appearance" xml:space="preserve">APPEARANCE</x:String> <x:String x:Key="Text.Preference.Appearance" xml:space="preserve">APPEARANCE</x:String>
<x:String x:Key="Text.Preference.Appearance.ColorOverrides" xml:space="preserve">Custom Color Schema</x:String>
<x:String x:Key="Text.Preference.Appearance.DefaultFont" xml:space="preserve">Default Font</x:String> <x:String x:Key="Text.Preference.Appearance.DefaultFont" xml:space="preserve">Default Font</x:String>
<x:String x:Key="Text.Preference.Appearance.DefaultFontSize" xml:space="preserve">Default Font Size</x:String> <x:String x:Key="Text.Preference.Appearance.DefaultFontSize" xml:space="preserve">Default Font Size</x:String>
<x:String x:Key="Text.Preference.Appearance.MonospaceFont" xml:space="preserve">Monospace Font</x:String> <x:String x:Key="Text.Preference.Appearance.MonospaceFont" xml:space="preserve">Monospace Font</x:String>
<x:String x:Key="Text.Preference.Appearance.Theme" xml:space="preserve">Theme</x:String> <x:String x:Key="Text.Preference.Appearance.Theme" xml:space="preserve">Theme</x:String>
<x:String x:Key="Text.Preference.Appearance.ThemeOverrides" xml:space="preserve">Theme Overrides</x:String>
<x:String x:Key="Text.Preference.General" xml:space="preserve">GENERAL</x:String> <x:String x:Key="Text.Preference.General" xml:space="preserve">GENERAL</x:String>
<x:String x:Key="Text.Preference.General.AvatarServer" xml:space="preserve">Avatar Server</x:String> <x:String x:Key="Text.Preference.General.AvatarServer" xml:space="preserve">Avatar Server</x:String>
<x:String x:Key="Text.Preference.General.Check4UpdatesOnStartup" xml:space="preserve">Check for updates on startup</x:String> <x:String x:Key="Text.Preference.General.Check4UpdatesOnStartup" xml:space="preserve">Check for updates on startup</x:String>

View file

@ -340,11 +340,11 @@
<x:String x:Key="Text.Paste" xml:space="preserve">粘贴</x:String> <x:String x:Key="Text.Paste" xml:space="preserve">粘贴</x:String>
<x:String x:Key="Text.Preference" xml:space="preserve">偏好设置</x:String> <x:String x:Key="Text.Preference" xml:space="preserve">偏好设置</x:String>
<x:String x:Key="Text.Preference.Appearance" xml:space="preserve">外观配置</x:String> <x:String x:Key="Text.Preference.Appearance" xml:space="preserve">外观配置</x:String>
<x:String x:Key="Text.Preference.Appearance.ColorOverrides" xml:space="preserve">自定义配色文件</x:String>
<x:String x:Key="Text.Preference.Appearance.DefaultFont" xml:space="preserve">缺省字体</x:String> <x:String x:Key="Text.Preference.Appearance.DefaultFont" xml:space="preserve">缺省字体</x:String>
<x:String x:Key="Text.Preference.Appearance.DefaultFontSize" xml:space="preserve">默认字体大小</x:String> <x:String x:Key="Text.Preference.Appearance.DefaultFontSize" xml:space="preserve">默认字体大小</x:String>
<x:String x:Key="Text.Preference.Appearance.MonospaceFont" xml:space="preserve">等宽字体</x:String> <x:String x:Key="Text.Preference.Appearance.MonospaceFont" xml:space="preserve">等宽字体</x:String>
<x:String x:Key="Text.Preference.Appearance.Theme" xml:space="preserve">主题</x:String> <x:String x:Key="Text.Preference.Appearance.Theme" xml:space="preserve">主题</x:String>
<x:String x:Key="Text.Preference.Appearance.ThemeOverrides" xml:space="preserve">主题自定义</x:String>
<x:String x:Key="Text.Preference.General" xml:space="preserve">通用配置</x:String> <x:String x:Key="Text.Preference.General" xml:space="preserve">通用配置</x:String>
<x:String x:Key="Text.Preference.General.AvatarServer" xml:space="preserve">头像服务</x:String> <x:String x:Key="Text.Preference.General.AvatarServer" xml:space="preserve">头像服务</x:String>
<x:String x:Key="Text.Preference.General.Check4UpdatesOnStartup" xml:space="preserve">启动时检测软件更新</x:String> <x:String x:Key="Text.Preference.General.Check4UpdatesOnStartup" xml:space="preserve">启动时检测软件更新</x:String>

View file

@ -340,11 +340,11 @@
<x:String x:Key="Text.Paste" xml:space="preserve">貼上</x:String> <x:String x:Key="Text.Paste" xml:space="preserve">貼上</x:String>
<x:String x:Key="Text.Preference" xml:space="preserve">偏好設定</x:String> <x:String x:Key="Text.Preference" xml:space="preserve">偏好設定</x:String>
<x:String x:Key="Text.Preference.Appearance" xml:space="preserve">外觀配置</x:String> <x:String x:Key="Text.Preference.Appearance" xml:space="preserve">外觀配置</x:String>
<x:String x:Key="Text.Preference.Appearance.ColorOverrides" xml:space="preserve">自訂配色檔</x:String>
<x:String x:Key="Text.Preference.Appearance.DefaultFont" xml:space="preserve">預設字型</x:String> <x:String x:Key="Text.Preference.Appearance.DefaultFont" xml:space="preserve">預設字型</x:String>
<x:String x:Key="Text.Preference.Appearance.DefaultFontSize" xml:space="preserve">預設字型大小</x:String> <x:String x:Key="Text.Preference.Appearance.DefaultFontSize" xml:space="preserve">預設字型大小</x:String>
<x:String x:Key="Text.Preference.Appearance.MonospaceFont" xml:space="preserve">等寬字型</x:String> <x:String x:Key="Text.Preference.Appearance.MonospaceFont" xml:space="preserve">等寬字型</x:String>
<x:String x:Key="Text.Preference.Appearance.Theme" xml:space="preserve">主題</x:String> <x:String x:Key="Text.Preference.Appearance.Theme" xml:space="preserve">主題</x:String>
<x:String x:Key="Text.Preference.Appearance.ThemeOverrides" xml:space="preserve">主題自訂</x:String>
<x:String x:Key="Text.Preference.General" xml:space="preserve">通用配置</x:String> <x:String x:Key="Text.Preference.General" xml:space="preserve">通用配置</x:String>
<x:String x:Key="Text.Preference.General.AvatarServer" xml:space="preserve">頭像服務</x:String> <x:String x:Key="Text.Preference.General.AvatarServer" xml:space="preserve">頭像服務</x:String>
<x:String x:Key="Text.Preference.General.Check4UpdatesOnStartup" xml:space="preserve">啟動時檢測軟體更新</x:String> <x:String x:Key="Text.Preference.General.Check4UpdatesOnStartup" xml:space="preserve">啟動時檢測軟體更新</x:String>

View file

@ -66,16 +66,16 @@ namespace SourceGit.ViewModels
set set
{ {
if (SetProperty(ref _theme, value)) if (SetProperty(ref _theme, value))
App.SetTheme(_theme, _colorOverrides); App.SetTheme(_theme, _themeOverrides);
} }
} }
public string ColorOverrides public string ThemeOverrides
{ {
get => _colorOverrides; get => _themeOverrides;
set set
{ {
if (SetProperty(ref _colorOverrides, value)) if (SetProperty(ref _themeOverrides, value))
App.SetTheme(_theme, value); App.SetTheme(_theme, value);
} }
} }
@ -487,7 +487,7 @@ namespace SourceGit.ViewModels
private string _locale = "en_US"; private string _locale = "en_US";
private string _theme = "Default"; private string _theme = "Default";
private string _colorOverrides = string.Empty; private string _themeOverrides = string.Empty;
private FontFamily _defaultFont = null; private FontFamily _defaultFont = null;
private FontFamily _monospaceFont = null; private FontFamily _monospaceFont = null;
private double _defaultFontSize = 13; private double _defaultFontSize = 13;

View file

@ -158,7 +158,6 @@ namespace SourceGit.Views
var geo = new StreamGeometry(); var geo = new StreamGeometry();
var pen = Models.CommitGraph.Pens[line.Color]; var pen = Models.CommitGraph.Pens[line.Color];
pen.Thickness = Models.CommitGraph.GetPenThickness();
using (var ctx = geo.Open()) using (var ctx = geo.Open())
{ {

View file

@ -229,15 +229,15 @@
Value="{Binding DefaultFontSize, Mode=TwoWay}"/> Value="{Binding DefaultFontSize, Mode=TwoWay}"/>
<TextBlock Grid.Row="4" Grid.Column="0" <TextBlock Grid.Row="4" Grid.Column="0"
Text="{DynamicResource Text.Preference.Appearance.ColorOverrides}" Text="{DynamicResource Text.Preference.Appearance.ThemeOverrides}"
HorizontalAlignment="Right" HorizontalAlignment="Right"
Margin="0,0,16,0"/> Margin="0,0,16,0"/>
<TextBox Grid.Row="4" Grid.Column="1" <TextBox Grid.Row="4" Grid.Column="1"
Height="28" Height="28"
CornerRadius="3" CornerRadius="3"
Text="{Binding ColorOverrides, Mode=TwoWay}"> Text="{Binding ThemeOverrides, Mode=TwoWay}">
<TextBox.InnerRightContent> <TextBox.InnerRightContent>
<Button Classes="icon_button" Width="30" Height="30" Click="SelectColorSchemaFile"> <Button Classes="icon_button" Width="30" Height="30" Click="SelectThemeOverrideFile">
<Path Data="{StaticResource Icons.Folder.Open}" Fill="{DynamicResource Brush.FG1}"/> <Path Data="{StaticResource Icons.Folder.Open}" Fill="{DynamicResource Brush.FG1}"/>
</Button> </Button>
</TextBox.InnerRightContent> </TextBox.InnerRightContent>

View file

@ -210,18 +210,18 @@ namespace SourceGit.Views
Close(); Close();
} }
private async void SelectColorSchemaFile(object sender, RoutedEventArgs e) private async void SelectThemeOverrideFile(object sender, RoutedEventArgs e)
{ {
var options = new FilePickerOpenOptions() var options = new FilePickerOpenOptions()
{ {
FileTypeFilter = [new FilePickerFileType("Theme Color Schema File") { Patterns = ["*.json"] }], FileTypeFilter = [new FilePickerFileType("Theme Overrides File") { Patterns = ["*.json"] }],
AllowMultiple = false, AllowMultiple = false,
}; };
var selected = await StorageProvider.OpenFilePickerAsync(options); var selected = await StorageProvider.OpenFilePickerAsync(options);
if (selected.Count == 1) if (selected.Count == 1)
{ {
ViewModels.Preference.Instance.ColorOverrides = selected[0].Path.LocalPath; ViewModels.Preference.Instance.ThemeOverrides = selected[0].Path.LocalPath;
} }
e.Handled = true; e.Handled = true;