about summary refs log tree commit diff
path: root/MatrixUtils.Desktop/Components/NavigationStack.axaml.cs
blob: 4e962a731790091cdf415a8705062b413c0b3f00 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;

namespace MatrixUtils.Desktop.Components;

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

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

    protected override void OnLoaded(RoutedEventArgs e) {
        base.OnLoaded(e);
        buildView();
    }

    private void buildView() {
        if (navPanel is null) {
            Console.WriteLine("NavigationStack buildView called while navpanel is null!");
            // await Task.Delay(100);
            // if (navPanel is null)
            // await buildView();
            // else Console.WriteLine("navpanel is not null!");
        }

        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.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
        });
        buildView();
    }

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

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