mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/PCK-Studio.git
synced 2026-05-26 20:46:52 +00:00
AppSettingsForm - Update API to be more flexible
This commit is contained in:
1
PCK-Studio/Forms/AppSettingsForm.Designer.cs
generated
1
PCK-Studio/Forms/AppSettingsForm.Designer.cs
generated
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user