|
@@ -0,0 +1,83 @@
|
|
|
+using System.Collections.ObjectModel;
|
|
|
+using System.Collections.Specialized;
|
|
|
+using Avalonia;
|
|
|
+using Avalonia.Controls;
|
|
|
+using Avalonia.Controls.Primitives;
|
|
|
+using Avalonia.Metadata;
|
|
|
+using Avalonia.Threading;
|
|
|
+
|
|
|
+namespace InABox.Avalonia.Components;
|
|
|
+
|
|
|
+public class PageStack : ContentControl
|
|
|
+{
|
|
|
+
|
|
|
+ private static readonly StyledProperty<PageStackItem?> SelectedItemProperty =
|
|
|
+ AvaloniaProperty.Register<PageStack,PageStackItem?>(nameof(SelectedItem));
|
|
|
+
|
|
|
+ private static readonly StyledProperty<int> SelectedIndexProperty =
|
|
|
+ AvaloniaProperty.Register<PageStack, int>(nameof(SelectedIndex), -1);
|
|
|
+
|
|
|
+ static PageStack()
|
|
|
+ {
|
|
|
+
|
|
|
+ SelectedIndexProperty.Changed.AddClassHandler<PageStack>((stack,args) =>
|
|
|
+ stack?.SelectPage((int)(args.NewValue ?? -1)));
|
|
|
+
|
|
|
+ SelectedItemProperty.Changed.AddClassHandler<PageStack>((stack,args) =>
|
|
|
+ stack?.SelectPage(args.NewValue as PageStackItem));
|
|
|
+ }
|
|
|
+
|
|
|
+ public PageStack()
|
|
|
+ {
|
|
|
+ var items = new ObservableCollection<PageStackItem>();
|
|
|
+ items.CollectionChanged += ItemsChanged;
|
|
|
+ Items = items;
|
|
|
+ }
|
|
|
+
|
|
|
+ [Content]
|
|
|
+ public IList<PageStackItem> Items { get; private set; }
|
|
|
+
|
|
|
+ public void SelectPage(int page)
|
|
|
+ {
|
|
|
+ SelectPage((page > -1) && (page < Items.Count) ? Items[page] : null);
|
|
|
+ }
|
|
|
+
|
|
|
+ private PageStackItem? _current;
|
|
|
+ public void SelectPage(PageStackItem? page)
|
|
|
+ {
|
|
|
+ if (_current != null)
|
|
|
+ _current.DoDisappearing();
|
|
|
+ _current = page;
|
|
|
+ Content = _current?.Content;
|
|
|
+ _current?.DoAppearing();
|
|
|
+ SelectionChanged?.Invoke(this, EventArgs.Empty);
|
|
|
+ }
|
|
|
+
|
|
|
+ public int SelectedIndex
|
|
|
+ {
|
|
|
+ get => GetValue(SelectedIndexProperty);
|
|
|
+ set => SetValue(SelectedIndexProperty, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public PageStackItem? SelectedItem
|
|
|
+ {
|
|
|
+ get => GetValue(SelectedItemProperty);
|
|
|
+ set => SetValue(SelectedItemProperty, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ public event EventHandler SelectionChanged;
|
|
|
+
|
|
|
+ public void DoSelectionChanged()
|
|
|
+ {
|
|
|
+ SelectionChanged?.Invoke(this, EventArgs.Empty);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ItemsChanged(object? sender, NotifyCollectionChangedEventArgs? e)
|
|
|
+ {
|
|
|
+ Dispatcher.UIThread.Post(() =>
|
|
|
+ {
|
|
|
+ SelectedItem ??= Items.FirstOrDefault();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+}
|