Finished Navigation system

This commit is contained in:
2026-01-03 15:12:11 -06:00
parent dffd35543a
commit b9a2a0127e
9 changed files with 161 additions and 8 deletions

View File

@@ -6,7 +6,12 @@
x:Class="CritterFolio.Navigation">
<Grid HorizontalAlignment="Stretch" Height="50" Background="{DynamicResource AccentBackground}" ColumnDefinitions="Auto, *, Auto">
<Button Grid.Column="0" x:Name="BackButton" Classes="headerBttn" Content="&#xe058;" HorizontalAlignment="Center"/>
<Label Grid.Column="1" x:Name="PageTitle" VerticalAlignment="Center" Foreground="{DynamicResource PrimaryForeground}" Margin="20, 0, 0, 0"/>
<Label Grid.Column="1"
x:Name="PageTitle"
VerticalAlignment="Center"
Foreground="{DynamicResource PrimaryForeground}"
Margin="20, 0, 0, 0"
FontSize="20"/>
<StackPanel Grid.Column="2" x:Name="HeaderButtons" Orientation="Horizontal" HorizontalAlignment="Center"/>
</Grid>
</UserControl>

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Avalonia;
@@ -18,16 +19,21 @@ public partial class Navigation : UserControl
}
}
private Panel? _pageContainer;
private ContentControl? _pageContainer;
private Stack<Page> _pageStack = [];
public Navigation()
{
InitializeComponent();
BackButton.Click += (sender, args) =>
{
PopPage();
};
}
public void Init(Panel pageContainer)
public void Init(ContentControl pageContainer)
{
_pageContainer = pageContainer;
}
@@ -58,22 +64,54 @@ public partial class Navigation : UserControl
HeaderButtons.Children.Remove(button);
}
public void ClearHeaderButtons()
{
HeaderButtons.Children.Clear();
}
public void PushPage(Page page)
{
Console.WriteLine($"Adding page {page.Title} to the page stack");
_pageStack.Push(page);
RefreshPage();
PrintPageStack();
}
public void PopPage()
{
Console.WriteLine($"Removing page {CurrentPage?.Title ?? "??"} from the page stack");
_pageStack.Pop();
RefreshPage();
PrintPageStack();
}
private void PrintPageStack()
{
var i = 0;
foreach (var page in _pageStack)
{
Console.WriteLine($"{i} - {page.Title}");
i++;
}
}
private void RefreshPage()
{
_pageContainer?.Children.Clear();
if (CurrentPage is null) return;
_pageContainer?.Children.Add(CurrentPage);
if (_pageContainer == null || CurrentPage is null) return;
ClearHeaderButtons();
_pageContainer.Content = CurrentPage;
CurrentPage.Refresh();
if (_pageStack.Count > 1)
{
ShowBack();
}
else
{
HideBack();
}
}
}

View File

@@ -0,0 +1,11 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="CritterFolio.Pages.HomePage">
<StackPanel>
<Label>Home</Label>
<Button x:Name="TestBttn">Test</Button>
</StackPanel>
</UserControl>

View File

@@ -0,0 +1,51 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
namespace CritterFolio.Pages;
public partial class HomePage : Page
{
public override string Title { get; } = "Home";
public HomePage()
{
InitializeComponent();
}
public override void Refresh()
{
base.Refresh();
Sys.Navigation?.SetTitle(Title);
TestBttn.Click += TestPage;
var headBtn = new Button()
{
Classes = { "headerBttn" },
Content = "\ue248"
};
headBtn.Click += (sender, args) =>
{
Console.WriteLine("Save button clicked");
};
Sys.Navigation?.AddHeaderButton(headBtn);
}
private void TestPage(object? sender, RoutedEventArgs args)
{
Sys.Navigation?.PushPage(new TestPage());
}
protected override void ClearConnections()
{
TestBttn.Click -= TestPage;
}
}

View File

@@ -2,7 +2,16 @@ using Avalonia.Controls;
namespace CritterFolio.Pages;
public abstract class Page : UserControl
public class Page : UserControl
{
public abstract void Refresh();
public virtual string? Title { get; }
public virtual void Refresh()
{
ClearConnections();
}
protected virtual void ClearConnections() { }
}

View File

@@ -0,0 +1,8 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="CritterFolio.Pages.TestPage">
Test page!
</UserControl>

View File

@@ -0,0 +1,27 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace CritterFolio.Pages;
public partial class TestPage : Page
{
public override string Title { get; } = "Test";
public TestPage()
{
InitializeComponent();
}
public override void Refresh()
{
ClearConnections();
Sys.Navigation?.SetTitle(Title);
}
protected override void ClearConnections()
{
}
}

View File

@@ -16,6 +16,7 @@
<Grid RowDefinitions="Auto, *" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="{DynamicResource PrimaryBackground}">
<critterFolio:Navigation Grid.Row="0" x:Name="Nav"/>
<ScrollViewer Grid.Row="1" x:Name="ScrollView" />
</Grid>

View File

@@ -1,4 +1,5 @@
using Avalonia.Controls;
using CritterFolio.Pages;
namespace CritterFolio.Views;
@@ -8,5 +9,7 @@ public partial class MainView : UserControl
{
InitializeComponent();
Sys.Navigation = Nav;
Nav.Init(ScrollView);
Nav.PushPage(new HomePage());
}
}