about summary refs log tree commit diff
path: root/MatrixRoomUtils.Desktop/Components/NavigationStack.axaml.cs
blob: d6343e2975ed8998773cc6fd9989f19668a07c38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace MatrixRoomUtils.Desktop.Components;

public partial class NavigationStack : UserControl {
    public NavigationStack() {
        InitializeComponent();
    }

    private void InitializeComponent() {
        AvaloniaXamlLoader.Load(this);
        buildView();
    }

    private void buildView() {
        navPanel.Children.Clear();
        foreach (var item in _stack) {
            Button btn = new() {
                Content = item.Name
            };
            btn.Click += (_, _) => {
                PopTo(_stack.IndexOf(item));
                buildView();
            };
            navPanel.Children.Add(btn);
        }
        content = Current?.View ?? new UserControl();
    }


    public class NavigationStackItem {
        public string Name { get; set; }
        public string Description { get; set; } = "";
        public UserControl View { get; set; }
    }

    private List<NavigationStackItem> _stack = new();

    public NavigationStackItem? Current => _stack.LastOrDefault();

    public void Push(string name, UserControl view) {
        _stack.Add(new NavigationStackItem {
            Name = name,
            View = view
        });
    }

    public void Pop() {
        _stack.RemoveAt(_stack.Count - 1);
    }

    public void PopTo(int index) {
        _stack.RemoveRange(index, _stack.Count - index);
    }
}