AppSettingsForm - Update API to be more flexible

This commit is contained in:
miku-666
2024-08-05 22:01:53 +02:00
parent a1ef8c7378
commit 685c3e5ad6
2 changed files with 57 additions and 32 deletions

View File

@@ -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);

View File

@@ -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<string, string>()
{
["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<string, string> keyToStringMap = null)
{
InitializeComponent();
Text = title;
_applicationSettings = applicationSettings;
if (keyToStringMap is not null)
_applicationSettings.Context.Add(keyToStringContextKey, keyToStringMap);
LoadSettings();
}
private static Dictionary<string, string> CheckBoxText = new Dictionary<string, string>()
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<string, string> 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<Type, ControlCreateDelegate> _typeToControl = new Dictionary<Type, ControlCreateDelegate>()
{
[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);
}
}