Skip to content

Commit

Permalink
tweak(Studio): Convert Theme into a class
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Oct 27, 2024
1 parent 3abc1bc commit 03b7a91
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 90 deletions.
2 changes: 1 addition & 1 deletion Studio/CelesteStudio/Editing/Theme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Dispose() {
}
}

public struct Theme() {
public class Theme {
// Editor
public Color Background;
public Color Caret;
Expand Down
82 changes: 2 additions & 80 deletions Studio/CelesteStudio/Editing/ThemeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,83 +334,6 @@ private void InitializeThemeParameters() {

styleLayout.EndHorizontal();
styleLayout.EndVertical();

/*
colorsLayout.Add(new Label { Text = field.Name });
colorsLayout.BeginVertical();
colorsLayout.BeginHorizontal();
colorsLayout.Add(new Label { Text = "Foreground" });
var fgPicker = new ColorPicker { Value = style.ForegroundColor, AllowAlpha = true };
fgPicker.ValueChanged += (_, _) => {
style.ForegroundColor = fgPicker.Value;
UpdateField(field, style);
};
colorsLayout.Add(fgPicker);
pickers.Add(fgPicker);
colorsLayout.EndBeginHorizontal();
if (style.BackgroundColor is Color bgColor) {
colorsLayout.Add(new Label { Text = "Background" });
// var enable = new CheckBox { Checked = true };
// enable.CheckedChanged += (_, _) => {
// style.BackgroundColor = null;
// UpdateField(field, style);
// };
// colorsLayout.Add(enable);
var bgPicker = new ColorPicker { Value = bgColor, AllowAlpha = true };
bgPicker.ValueChanged += (_, _) => {
style.BackgroundColor = bgPicker.Value;
UpdateField(field, style);
};
colorsLayout.Add(bgPicker);
pickers.Add(bgPicker);
} else {
// colorsLayout.Add(new Label { Text = "Background" });
// var enable = new CheckBox { Checked = false };
// enable.CheckedChanged += (_, _) => {
// style.BackgroundColor = Color.FromRgb(0);
// UpdateField(field, style);
// };
// colorsLayout.Add(enable);
}
colorsLayout.EndBeginHorizontal();
colorsLayout.Add(new Label { Text = "Italic" });
var italic = new CheckBox { Checked = (style.FontStyle & FontStyle.Italic) != 0 };
italic.CheckedChanged += (_, _) => {
if (italic.Checked is bool it) {
if (it) {
style.FontStyle |= FontStyle.Italic;
} else {
style.FontStyle &= ~FontStyle.Italic;
}
}
UpdateField(field, style);
};
colorsLayout.Add(italic);
colorsLayout.EndBeginHorizontal();
colorsLayout.Add(new Label { Text = "Bold" });
var bold = new CheckBox { Checked = (style.FontStyle & FontStyle.Bold) != 0 };
bold.CheckedChanged += (_, _) => {
if (bold.Checked is bool b) {
if (b) {
style.FontStyle |= FontStyle.Bold;
} else {
style.FontStyle &= ~FontStyle.Bold;
}
}
UpdateField(field, style);
};
colorsLayout.Add(bold);
colorsLayout.EndHorizontal();
colorsLayout.EndVertical();
*/
}

fieldsLayout.EndHorizontal();
Expand All @@ -422,9 +345,8 @@ private void UpdateField(FieldInfo field, object value) {
if (IsBuiltin()) {
return;
}
object theme = Settings.Instance.Theme;
field.SetValue(theme, value);
Settings.Instance.CustomThemes[Settings.Instance.ThemeName] = (Theme) theme;

field.SetValue(Settings.Instance.Theme, value);
Settings.OnThemeChanged();
Settings.Save();
}
Expand Down
12 changes: 3 additions & 9 deletions Studio/CelesteStudio/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Eto.Forms;
using StudioCommunication.Util;
using System.Diagnostics;
using System.Net;
using Tomlet;
using Tomlet.Attributes;
using Tomlet.Exceptions;
Expand Down Expand Up @@ -65,10 +64,10 @@ public static string BaseConfigPath {
[TomlNonSerialized]
public Theme Theme {
get {
if (Theme.BuiltinThemes.TryGetValue(ThemeName, out Theme builtinTheme)) {
if (Theme.BuiltinThemes.TryGetValue(ThemeName, out var builtinTheme)) {
return builtinTheme;
}
if (CustomThemes.TryGetValue(ThemeName, out Theme customTheme)) {
if (CustomThemes.TryGetValue(ThemeName, out var customTheme)) {
return customTheme;
}
// Fall back to light theme
Expand Down Expand Up @@ -223,19 +222,14 @@ public static void Load() {
? Theme.BuiltinThemes[Theme.BuiltinDark]
: Theme.BuiltinThemes[Theme.BuiltinLight];

// Need to box the theme, since it's a struct
var currentThemeBox = (object)currentTheme;

foreach (var field in typeof(Theme).GetFields(BindingFlags.Public | BindingFlags.Instance)) {
if (!themeTable.ContainsKey(field.Name)) {
var fallbackValue = field.GetValue(fallbackTheme);
field.SetValue(currentThemeBox, fallbackValue);
field.SetValue(currentTheme, fallbackValue);

Console.WriteLine($"Warning: Custom theme '{themeName}' is missing field '{field.Name}'! Defaulting to {fallbackValue}");
}
}

Instance.CustomThemes[themeName] = (Theme)currentThemeBox;
}
}

Expand Down

0 comments on commit 03b7a91

Please sign in to comment.