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"> x:Class="CritterFolio.Navigation">
<Grid HorizontalAlignment="Stretch" Height="50" Background="{DynamicResource AccentBackground}" ColumnDefinitions="Auto, *, Auto"> <Grid HorizontalAlignment="Stretch" Height="50" Background="{DynamicResource AccentBackground}" ColumnDefinitions="Auto, *, Auto">
<Button Grid.Column="0" x:Name="BackButton" Classes="headerBttn" Content="&#xe058;" HorizontalAlignment="Center"/> <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"/> <StackPanel Grid.Column="2" x:Name="HeaderButtons" Orientation="Horizontal" HorizontalAlignment="Center"/>
</Grid> </Grid>
</UserControl> </UserControl>

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using Avalonia; using Avalonia;
@@ -18,16 +19,21 @@ public partial class Navigation : UserControl
} }
} }
private Panel? _pageContainer; private ContentControl? _pageContainer;
private Stack<Page> _pageStack = []; private Stack<Page> _pageStack = [];
public Navigation() public Navigation()
{ {
InitializeComponent(); InitializeComponent();
BackButton.Click += (sender, args) =>
{
PopPage();
};
} }
public void Init(Panel pageContainer) public void Init(ContentControl pageContainer)
{ {
_pageContainer = pageContainer; _pageContainer = pageContainer;
} }
@@ -58,22 +64,54 @@ public partial class Navigation : UserControl
HeaderButtons.Children.Remove(button); HeaderButtons.Children.Remove(button);
} }
public void ClearHeaderButtons()
{
HeaderButtons.Children.Clear();
}
public void PushPage(Page page) public void PushPage(Page page)
{ {
Console.WriteLine($"Adding page {page.Title} to the page stack");
_pageStack.Push(page); _pageStack.Push(page);
RefreshPage();
PrintPageStack();
} }
public void PopPage() public void PopPage()
{ {
Console.WriteLine($"Removing page {CurrentPage?.Title ?? "??"} from the page stack");
_pageStack.Pop(); _pageStack.Pop();
RefreshPage();
PrintPageStack();
}
private void PrintPageStack()
{
var i = 0;
foreach (var page in _pageStack)
{
Console.WriteLine($"{i} - {page.Title}");
i++;
}
} }
private void RefreshPage() private void RefreshPage()
{ {
_pageContainer?.Children.Clear(); if (_pageContainer == null || CurrentPage is null) return;
if (CurrentPage is null) return;
_pageContainer?.Children.Add(CurrentPage); ClearHeaderButtons();
_pageContainer.Content = CurrentPage;
CurrentPage.Refresh(); 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; 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}"> <Grid RowDefinitions="Auto, *" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="{DynamicResource PrimaryBackground}">
<critterFolio:Navigation Grid.Row="0" x:Name="Nav"/> <critterFolio:Navigation Grid.Row="0" x:Name="Nav"/>
<ScrollViewer Grid.Row="1" x:Name="ScrollView" />
</Grid> </Grid>

View File

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