@using MudBlazor @using System.ComponentModel @using System.Reflection @using Inspectron.Settings
@if (_selectedCategory != null && GetSettingsForCategory(_selectedCategory) is { Count: > 0 } settings) { @foreach (var setting in settings) {
@if (setting.Property.PropertyType == typeof(bool)) { var boolVal = (bool)(setting.Value ?? false); } else if (setting.Property.PropertyType == typeof(int)) { var intVal = (int)(setting.Value ?? 0); } else if (setting.Property.PropertyType == typeof(double)) { var dblVal = (double)(setting.Value ?? 0.0); } else if (setting.Property.PropertyType.IsEnum) { @foreach (var enumVal in Enum.GetValues(setting.Property.PropertyType)) { @enumVal.ToString() } } else { var strVal = setting.Value?.ToString() ?? ""; }
} } else { Select a category to view settings. }
Cancel Save
@code { [CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = null!; [Parameter] public InspectronSettings? Settings { get; set; } private Dictionary> _categories = new(); private string? _selectedCategory; private List _treeRoot = new(); protected override void OnParametersSet() { if (Settings == null) return; LoadSettings(); } private void LoadSettings() { _categories.Clear(); _treeRoot = new List(); var treeView = Settings!.UserSettings; var root = treeView.Root as Tree; if (root != null) Traverse(root, ""); // Build the UI tree from category paths foreach (var categoryPath in _categories.Keys) { var parts = categoryPath.Split('/'); var currentList = _treeRoot; for (int i = 0; i < parts.Length; i++) { var existing = currentList.FirstOrDefault(n => n.Label == parts[i]); if (existing == null) { existing = new CategoryNode { Label = parts[i], FullPath = string.Join("/", parts.Take(i + 1)) }; currentList.Add(existing); } currentList = existing.ChildNodes; } } } private void Traverse(Tree node, string path) { object value = node.Value; if (value is string folderName) { string newPath = string.IsNullOrEmpty(path) ? folderName : $"{path}/{folderName}"; foreach (Tree child in node.Children) Traverse(child, newPath); } else if (value is InspectronSettings.UserSettingsInfo info) { string groupName = info.Name; string groupPath = string.IsNullOrEmpty(path) ? groupName : $"{path}/{groupName}"; foreach (PropertyDescriptor pd in info.Settings) { if (pd is BoundPropertyDescriptor bpd) { var owner = bpd.Owner; var description = string.IsNullOrEmpty(pd.Description) ? bpd.DisplayName : pd.Description; var entry = new SettingEntry { Owner = owner, Property = bpd.PropertyInfo, Description = description, Value = bpd.PropertyInfo.GetValue(owner) }; if (!_categories.TryGetValue(groupPath, out var list)) { list = new List(); _categories[groupPath] = list; } list.Add(entry); } } } } private void OnCategorySelected(string? category) { _selectedCategory = category; } private List GetSettingsForCategory(string category) { // Exact match first if (_categories.TryGetValue(category, out var exact)) return exact; // Otherwise aggregate all sub-categories var prefix = category + "/"; return _categories .Where(kvp => kvp.Key.StartsWith(prefix)) .SelectMany(kvp => kvp.Value) .ToList(); } private void Save() { foreach (var list in _categories.Values) { foreach (var setting in list) { var currentValue = setting.Property.GetValue(setting.Owner); if (!Equals(currentValue, setting.Value)) { try { if (setting.Value != null && setting.Value.GetType() != setting.Property.PropertyType) { var converted = Convert.ChangeType(setting.Value, setting.Property.PropertyType); setting.Property.SetValue(setting.Owner, converted); } else { setting.Property.SetValue(setting.Owner, setting.Value); } } catch { // Skip properties that fail to convert } } } } Settings?.SaveSettings(); MudDialog.Close(DialogResult.Ok(true)); } private void Cancel() => MudDialog.Cancel(); private class SettingEntry { public object Owner { get; set; } = null!; public PropertyInfo Property { get; set; } = null!; public string Description { get; set; } = ""; public object? Value { get; set; } } public class CategoryNode { public string Label { get; set; } = ""; public string FullPath { get; set; } = ""; public List ChildNodes { get; set; } = new(); } }