private Control GetControlForSetting(PropertyInfo property, PluginSetting setting) { if (property.PropertyType == typeof(string)) { var textbox = new TextBox { Text = setting.GetValue <string>() }; textbox.TextChanged += (sender, e) => setting.SetValue(textbox.Text); return(textbox); } else if (property.PropertyType == typeof(bool)) { string description = property.Name; if (property.GetCustomAttribute <BooleanPropertyAttribute>() is BooleanPropertyAttribute attribute) { description = attribute.Description; } var checkbox = new CheckBox { Text = description, Checked = setting.GetValue <bool>() }; checkbox.CheckedChanged += (sender, e) => setting.SetValue((bool)checkbox.Checked); return(checkbox); } else if (property.PropertyType == typeof(float)) { var tb = new TextBox { Text = $"{setting.GetValue<float>()}" }; tb.TextChanged += (sender, e) => setting.SetValue(float.TryParse(tb.Text, out var val) ? val : 0f); if (property.GetCustomAttribute <SliderPropertyAttribute>() is SliderPropertyAttribute sliderAttr) { // TODO: replace with slider when possible (https://github.com/picoe/Eto/issues/1772) tb.ToolTip = $"Minimum: {sliderAttr.Min}, Maximum: {sliderAttr.Max}"; tb.PlaceholderText = $"{sliderAttr.DefaultValue}"; if (setting.Value == null) { setting.SetValue(sliderAttr.DefaultValue); } } return(tb); } else if (GenericallyConvertableTypes.Contains(property.PropertyType)) { var tb = new TextBox { Text = $"{setting.GetValue(property.PropertyType)}" }; tb.TextChanged += (sender, e) => setting.SetValue(Convert.ChangeType(tb.Text, property.PropertyType) ?? 0); return(tb); } throw new NotSupportedException($"'{property.PropertyType}' is not supported by {nameof(PluginSettingStoreEditor)}"); }
private Control GetControlForSetting(PropertyInfo property, PluginSetting setting) { if (property.PropertyType == typeof(string)) { var textbox = new TextBox { Text = GetSetting <string>(property, setting) }; textbox.TextChanged += (sender, e) => setting.SetValue(textbox.Text); return(textbox); } else if (property.PropertyType == typeof(bool)) { string description = property.Name; if (property.GetCustomAttribute <BooleanPropertyAttribute>() is BooleanPropertyAttribute attribute) { description = attribute.Description; } var checkbox = new CheckBox { Text = description, Checked = GetSetting <bool>(property, setting) }; checkbox.CheckedChanged += (sender, e) => setting.SetValue((bool)checkbox.Checked); return(checkbox); } else if (property.PropertyType == typeof(float)) { var tb = GetNumericMaskedTextBox <float>(property, setting); if (property.GetCustomAttribute <SliderPropertyAttribute>() is SliderPropertyAttribute sliderAttr) { // TODO: replace with slider when possible (https://github.com/picoe/Eto/issues/1772) tb.ToolTip = $"Minimum: {sliderAttr.Min}, Maximum: {sliderAttr.Max}"; tb.PlaceholderText = $"{sliderAttr.DefaultValue}"; if (!setting.HasValue) { setting.SetValue(sliderAttr.DefaultValue); } } return(tb); } else if (genericControls.TryGetValue(property.PropertyType, out var getGenericTextBox)) { return(getGenericTextBox(property, setting)); } throw new NotSupportedException($"'{property.PropertyType}' is not supported by {nameof(PluginSettingStoreEditor)}"); }
private static MaskedTextBox <T> GetMaskedTextBox <T>(PropertyInfo property, PluginSetting setting) { var tb = new MaskedTextBox <T> { Value = GetSetting <T>(property, setting) }; tb.ValueChanged += (sender, e) => setting.SetValue(tb.Value); return(tb); }
private static NumericMaskedTextBox <T> GetNumericMaskedTextBox <T>(PropertyInfo property, PluginSetting setting) { var tb = new NumericMaskedTextBox <T> { Value = setting.GetValue <T>() }; tb.ValueChanged += (sender, e) => setting.SetValue(tb.Value); return(tb); }
private static T GetSetting <T>(PropertyInfo property, PluginSetting setting) { if (setting.HasValue) { return(setting.GetValue <T>()); } else { if (property.GetCustomAttribute <DefaultPropertyValueAttribute>() is DefaultPropertyValueAttribute defaults) { try { setting.SetValue(defaults.Value); return((T)defaults.Value); } catch (Exception e) { Log.Write("UX", $"Failed to get custom default of {property.Name}: {e.Message}"); } } return(default);
private static MaskedTextBox <T> BindNumberBox <T>(MaskedTextBox <T> textBox, PropertyInfo property, PluginSetting setting) { textBox.Value = GetSetting <T>(property, setting); textBox.ValueChanged += (sender, e) => setting.SetValue(textBox.Value); return(textBox); }