diff --git a/PCK-Studio/Forms/AppSettingsForm.Designer.cs b/PCK-Studio/Forms/AppSettingsForm.Designer.cs index 00ed901a..4fc1d0f8 100644 --- a/PCK-Studio/Forms/AppSettingsForm.Designer.cs +++ b/PCK-Studio/Forms/AppSettingsForm.Designer.cs @@ -63,7 +63,6 @@ this.Resizable = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Style = MetroFramework.MetroColorStyle.Black; - this.Text = "Application Settings"; this.Theme = MetroFramework.MetroThemeStyle.Dark; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.AppBehaviorSettingsForm_FormClosing); this.ResumeLayout(false); diff --git a/PCK-Studio/Forms/AppSettingsForm.cs b/PCK-Studio/Forms/AppSettingsForm.cs index f9aa41d5..25401a01 100644 --- a/PCK-Studio/Forms/AppSettingsForm.cs +++ b/PCK-Studio/Forms/AppSettingsForm.cs @@ -12,57 +12,83 @@ namespace PckStudio.Forms public partial class AppSettingsForm : MetroForm { private ApplicationSettingsBase _applicationSettings; + internal const string keyToStringContextKey = "keyToString"; public AppSettingsForm() - : this(Settings.Default) + : this("Application Settings", Settings.Default, new Dictionary() + { + ["ShowRichPresence"] = "Show Rich Presence", + ["AutoSaveChanges"] = "Auto Save", + ["UseLittleEndianAsDefault"] = "Open as Little Endian", + ["AutoUpdate"] = "Auto Update", + ["LoadSubPcks"] = "Load Sub Pcks", + ["UsePrerelease"] = "Use Prerelease", + }) { } - public AppSettingsForm(ApplicationSettingsBase applicationSettings) + public AppSettingsForm(string title, ApplicationSettingsBase applicationSettings, Dictionary keyToStringMap = null) { InitializeComponent(); + Text = title; _applicationSettings = applicationSettings; + if (keyToStringMap is not null) + _applicationSettings.Context.Add(keyToStringContextKey, keyToStringMap); LoadSettings(); } - private static Dictionary CheckBoxText = new Dictionary() + static bool ContextTryGetKeyToString(SettingsContext context, string key, out string value) { - ["ShowRichPresence"] = "Show Rich Presence", - ["AutoSaveChanges"] = "Auto Save", - ["UseLittleEndianAsDefault"] = "Open as Little Endian", - ["AutoUpdate"] = "Auto Update", - ["LoadSubPcks"] = "Load Sub Pcks", - ["UsePrerelease"] = "Use Prerelease", - }; - - private void CheckBox_CheckedChanged(object sender, EventArgs e) - { - if (sender is CheckBox checkBox && checkBox.Tag is string settingsKey && _applicationSettings[settingsKey] is bool) - { - _applicationSettings[settingsKey] = checkBox.Checked; - } + value = default; + return + context.ContainsKey(keyToStringContextKey) && + context[keyToStringContextKey] is Dictionary keyToString && + keyToString.TryGetValue(key, out value); } + static Control CreateCheckBox(SettingsPropertyValue propertyValue, SettingsBase settings) + { + var control = new MetroCheckBox + { + Name = propertyValue.Name, + Tag = propertyValue.Name, + Text = ContextTryGetKeyToString(settings.Context, propertyValue.Name, out string name) ? name : propertyValue.Name, + Checked = (bool)propertyValue.PropertyValue, + + AutoSize = true, + Theme = MetroFramework.MetroThemeStyle.Dark, + Style = MetroFramework.MetroColorStyle.White, + }; + + void CheckBox_CheckedChanged(object sender, EventArgs e) + { + if (sender is CheckBox checkBox && checkBox.Tag is string settingsKey && settings[settingsKey] is bool) + { + settings[settingsKey] = checkBox.Checked; + } + } + + control.CheckedChanged += CheckBox_CheckedChanged; + return control; + } + + delegate Control ControlCreateDelegate(SettingsPropertyValue propertyValue, SettingsBase settings); + + Dictionary _typeToControl = new Dictionary() + { + [typeof(bool)] = CreateCheckBox, + }; + private void LoadSettings() { foreach (SettingsPropertyValue item in _applicationSettings.PropertyValues) { Debug.WriteLine($"{item.Property.Name}: {item.Property.PropertyType}"); - if (!item.Property.Attributes.ContainsKey(typeof(UserScopedSettingAttribute)) || item.Property.PropertyType != typeof(bool)) + bool isUserScoped = item.Property.Attributes?.ContainsKey(typeof(UserScopedSettingAttribute)) ?? true; + if (!isUserScoped || !_typeToControl.ContainsKey(item.Property.PropertyType) || item.Property.IsReadOnly) continue; - var checkBox = new MetroCheckBox - { - Name = item.Name, - Tag = item.Name, - Text = CheckBoxText.ContainsKey(item.Name) ? CheckBoxText[item.Name] : item.Name, - Checked = (bool)item.PropertyValue, - - AutoSize = true, - Theme = MetroFramework.MetroThemeStyle.Dark, - Style = MetroFramework.MetroColorStyle.White, - }; - checkBox.CheckedChanged += CheckBox_CheckedChanged; - flowLayoutPanel.Controls.Add(checkBox); + Control control = _typeToControl[item.Property.PropertyType](item, _applicationSettings); + flowLayoutPanel.Controls.Add(control); } }