| 1 | using Avalonia.Controls; |
| 2 | using Avalonia.Interactivity; |
| 3 | using CascadeIDE.Features.Settings; |
| 4 | using CascadeIDE.Services; |
| 5 | using CascadeIDE.Views.Settings; |
| 6 | |
| 7 | namespace CascadeIDE.Views; |
| 8 | |
| 9 | public partial class SettingsShellView : UserControl |
| 10 | { |
| 11 | private readonly Dictionary<string, Control> _panelCache = new(StringComparer.OrdinalIgnoreCase); |
| 12 | |
| 13 | public SettingsShellView() |
| 14 | { |
| 15 | InitializeComponent(); |
| 16 | Loaded += OnLoaded; |
| 17 | CategoryList.SelectionChanged += OnCategorySelectionChanged; |
| 18 | } |
| 19 | |
| 20 | private void OnLoaded(object? sender, RoutedEventArgs e) |
| 21 | { |
| 22 | var nav = BuildNavigation(SettingsPanelRegistry.LoadOrdered()); |
| 23 | CategoryList.ItemsSource = nav; |
| 24 | var first = nav.OfType<SettingsNavigationCategory>().FirstOrDefault(); |
| 25 | if (first is not null) |
| 26 | CategoryList.SelectedItem = first; |
| 27 | else |
| 28 | ShowCategory(null); |
| 29 | } |
| 30 | |
| 31 | internal static IReadOnlyList<SettingsNavigationItem> BuildNavigation( |
| 32 | IReadOnlyList<Models.SettingsCategoryDefinition> categories) |
| 33 | { |
| 34 | var items = new List<SettingsNavigationItem>(); |
| 35 | string? lastGroup = null; |
| 36 | foreach (var cat in categories.OrderBy(c => c.Order).ThenBy(c => c.Title, StringComparer.OrdinalIgnoreCase)) |
| 37 | { |
| 38 | var group = cat.Group.Trim(); |
| 39 | if (!string.IsNullOrEmpty(group) |
| 40 | && !string.Equals(group, lastGroup, StringComparison.Ordinal)) |
| 41 | { |
| 42 | items.Add(new SettingsNavigationGroupHeader(group)); |
| 43 | lastGroup = group; |
| 44 | } |
| 45 | |
| 46 | items.Add(new SettingsNavigationCategory( |
| 47 | cat.Id, |
| 48 | group, |
| 49 | cat.Title, |
| 50 | cat.Panel, |
| 51 | ShowGroupHeader: false)); |
| 52 | } |
| 53 | |
| 54 | return items; |
| 55 | } |
| 56 | |
| 57 | private void OnCategorySelectionChanged(object? sender, SelectionChangedEventArgs e) |
| 58 | { |
| 59 | if (CategoryList.SelectedItem is SettingsNavigationGroupHeader) |
| 60 | { |
| 61 | SelectFirstCategoryAfterHeader(); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | if (CategoryList.SelectedItem is SettingsNavigationCategory cat) |
| 66 | ShowCategory(cat); |
| 67 | } |
| 68 | |
| 69 | private void SelectFirstCategoryAfterHeader() |
| 70 | { |
| 71 | if (CategoryList.ItemsSource is not IEnumerable<SettingsNavigationItem> items) |
| 72 | return; |
| 73 | |
| 74 | var list = items.ToList(); |
| 75 | var idx = CategoryList.SelectedIndex; |
| 76 | if (idx < 0) |
| 77 | idx = 0; |
| 78 | for (var i = idx; i < list.Count; i++) |
| 79 | { |
| 80 | if (list[i] is SettingsNavigationCategory cat) |
| 81 | { |
| 82 | CategoryList.SelectedItem = cat; |
| 83 | return; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private void ShowCategory(SettingsNavigationCategory? category) |
| 89 | { |
| 90 | if (category is null) |
| 91 | { |
| 92 | PageTitle.Text = ""; |
| 93 | PageHost.Content = null; |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | PageTitle.Text = category.Title; |
| 98 | PageHost.Content = ResolvePanel(category.Panel); |
| 99 | } |
| 100 | |
| 101 | private Control? ResolvePanel(string panelId) |
| 102 | { |
| 103 | if (_panelCache.TryGetValue(panelId, out var cached)) |
| 104 | return cached; |
| 105 | |
| 106 | var created = SettingsPanelFactory.TryCreate(panelId); |
| 107 | if (created is null) |
| 108 | return null; |
| 109 | |
| 110 | created.DataContext = DataContext; |
| 111 | _panelCache[panelId] = created; |
| 112 | return created; |
| 113 | } |
| 114 | } |
| 115 | |