Add ColorPicker and FileSelector settings controls and API classes

This commit is contained in:
Stone_Red
2026-01-17 00:15:57 +01:00
parent c4517fb8ac
commit 990793fb01
4 changed files with 276 additions and 3 deletions
@@ -0,0 +1,62 @@
using System.Drawing;
using System.Globalization;
namespace DesktopMagic.Api.Settings;
/// <summary>
/// Represents a color picker control.
/// </summary>
public sealed class ColorPicker : Setting
{
private Color _value;
/// <summary>
/// Gets or sets the color value assigned to the <see cref="ColorPicker"/> element.
/// </summary>
public Color Value
{
get => _value;
set
{
if (_value != value)
{
_value = value;
ValueChanged();
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ColorPicker"/> class with the provided <paramref name="defaultColor"/>.
/// </summary>
/// <param name="defaultColor">The default color value.</param>
public ColorPicker(Color defaultColor)
{
_value = defaultColor;
}
/// <summary>
/// Initializes a new instance of the <see cref="ColorPicker"/> class with a default color of White.
/// </summary>
public ColorPicker() : this(Color.White)
{
}
internal override string GetJsonValue()
{
return $"{Value.A},{Value.R},{Value.G},{Value.B}";
}
internal override void SetJsonValue(string value)
{
string[] parts = value.Split(',');
if (parts.Length == 4 &&
byte.TryParse(parts[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out byte a) &&
byte.TryParse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out byte r) &&
byte.TryParse(parts[2], NumberStyles.Integer, CultureInfo.InvariantCulture, out byte g) &&
byte.TryParse(parts[3], NumberStyles.Integer, CultureInfo.InvariantCulture, out byte b))
{
Value = Color.FromArgb(a, r, g, b);
}
}
}
@@ -0,0 +1,65 @@
namespace DesktopMagic.Api.Settings;
/// <summary>
/// Represents a file selector control that allows the user to browse for a file.
/// </summary>
public sealed class FileSelector : Setting
{
private string _value = string.Empty;
/// <summary>
/// Gets or sets the selected file path.
/// </summary>
public string Value
{
get => _value;
set
{
if (_value != value)
{
_value = value;
ValueChanged();
}
}
}
/// <summary>
/// Gets or sets the file filter for the file dialog (e.g., "Image Files|*.png;*.jpg|All Files|*.*").
/// </summary>
public string Filter { get; set; }
/// <summary>
/// Gets or sets the title of the file dialog.
/// </summary>
public string Title { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the file selector should select folders instead of files.
/// </summary>
public bool SelectFolder { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="FileSelector"/> class.
/// </summary>
/// <param name="defaultPath">The default file path.</param>
/// <param name="filter">The file filter for the file dialog.</param>
/// <param name="title">The title of the file dialog.</param>
/// <param name="selectFolder">If true, selects folders instead of files.</param>
public FileSelector(string defaultPath = "", string filter = "All Files|*.*", string title = "Select File", bool selectFolder = false)
{
_value = defaultPath;
Filter = filter;
Title = title;
SelectFolder = selectFolder;
}
internal override string GetJsonValue()
{
return Value;
}
internal override void SetJsonValue(string value)
{
Value = value;
}
}