|
| 1 | +// Copyright (c) 2019-2025 ReactiveUI Association Incorporated. All rights reserved. |
| 2 | +// ReactiveUI Association Incorporated licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +using System.Collections.ObjectModel; |
| 6 | +using Avalonia; |
| 7 | +using Avalonia.Animation; |
| 8 | +using Avalonia.Animation.Easings; |
| 9 | +using Avalonia.Controls; |
| 10 | +using Avalonia.Controls.Primitives; |
| 11 | +using Avalonia.Input; |
| 12 | +using Avalonia.Media; |
| 13 | + |
| 14 | +namespace CrissCross.Avalonia.UI.Controls; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// A bottom application bar that can show and hide with animation, containing left and right content areas. |
| 18 | +/// </summary> |
| 19 | +public class AppBar : TemplatedControl |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Property for <see cref="AppBarEnabled"/>. |
| 23 | + /// </summary> |
| 24 | + public static readonly StyledProperty<bool> AppBarEnabledProperty = |
| 25 | + AvaloniaProperty.Register<AppBar, bool>(nameof(AppBarEnabled), true); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Property for <see cref="AppBarIsSticky"/>. |
| 29 | + /// </summary> |
| 30 | + public static readonly StyledProperty<bool> AppBarIsStickyProperty = |
| 31 | + AvaloniaProperty.Register<AppBar, bool>(nameof(AppBarIsSticky)); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Property for <see cref="AppBarLeft"/>. |
| 35 | + /// </summary> |
| 36 | + public static readonly StyledProperty<ObservableCollection<Control>> AppBarLeftProperty = |
| 37 | + AvaloniaProperty.Register<AppBar, ObservableCollection<Control>>(nameof(AppBarLeft)); |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Property for <see cref="AppBarRight"/>. |
| 41 | + /// </summary> |
| 42 | + public static readonly StyledProperty<ObservableCollection<Control>> AppBarRightProperty = |
| 43 | + AvaloniaProperty.Register<AppBar, ObservableCollection<Control>>(nameof(AppBarRight)); |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Property for <see cref="AppBarHeight"/>. |
| 47 | + /// </summary> |
| 48 | + public static readonly StyledProperty<double> AppBarHeightProperty = |
| 49 | + AvaloniaProperty.Register<AppBar, double>(nameof(AppBarHeight), 88.0); |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Property for <see cref="IsOpen"/>. |
| 53 | + /// </summary> |
| 54 | + public static readonly StyledProperty<bool> IsOpenProperty = |
| 55 | + AvaloniaProperty.Register<AppBar, bool>(nameof(IsOpen)); |
| 56 | + |
| 57 | + private Border? _rootBorder; |
| 58 | + private bool _isPointerOver; |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Initializes a new instance of the <see cref="AppBar"/> class. |
| 62 | + /// </summary> |
| 63 | + public AppBar() |
| 64 | + { |
| 65 | + AppBarLeft = []; |
| 66 | + AppBarRight = []; |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Gets or sets a value indicating whether the application bar is enabled. |
| 71 | + /// </summary> |
| 72 | + public bool AppBarEnabled |
| 73 | + { |
| 74 | + get => GetValue(AppBarEnabledProperty); |
| 75 | + set => SetValue(AppBarEnabledProperty, value); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Gets or sets a value indicating whether the application bar stays open until explicitly closed. |
| 80 | + /// </summary> |
| 81 | + public bool AppBarIsSticky |
| 82 | + { |
| 83 | + get => GetValue(AppBarIsStickyProperty); |
| 84 | + set => SetValue(AppBarIsStickyProperty, value); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Gets or sets the left-aligned content items. |
| 89 | + /// </summary> |
| 90 | + public ObservableCollection<Control> AppBarLeft |
| 91 | + { |
| 92 | + get => GetValue(AppBarLeftProperty); |
| 93 | + set => SetValue(AppBarLeftProperty, value); |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Gets or sets the right-aligned content items. |
| 98 | + /// </summary> |
| 99 | + public ObservableCollection<Control> AppBarRight |
| 100 | + { |
| 101 | + get => GetValue(AppBarRightProperty); |
| 102 | + set => SetValue(AppBarRightProperty, value); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Gets or sets the height of the application bar when expanded. |
| 107 | + /// </summary> |
| 108 | + public double AppBarHeight |
| 109 | + { |
| 110 | + get => GetValue(AppBarHeightProperty); |
| 111 | + set => SetValue(AppBarHeightProperty, value); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Gets or sets a value indicating whether the application bar is open. |
| 116 | + /// </summary> |
| 117 | + public bool IsOpen |
| 118 | + { |
| 119 | + get => GetValue(IsOpenProperty); |
| 120 | + set => SetValue(IsOpenProperty, value); |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Shows the application bar. |
| 125 | + /// </summary> |
| 126 | + /// <param name="isSticky">If set to <c>true</c>, the bar stays open until explicitly closed.</param> |
| 127 | + public void ShowAppBar(bool isSticky = false) |
| 128 | + { |
| 129 | + if (!AppBarEnabled) |
| 130 | + { |
| 131 | + HideAppBar(); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + AppBarIsSticky = isSticky; |
| 136 | + IsOpen = true; |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Hides the application bar. |
| 141 | + /// </summary> |
| 142 | + public void HideAppBar() => IsOpen = false; |
| 143 | + |
| 144 | + /// <inheritdoc/> |
| 145 | + protected override void OnApplyTemplate(TemplateAppliedEventArgs e) |
| 146 | + { |
| 147 | + ArgumentNullException.ThrowIfNull(e); |
| 148 | + base.OnApplyTemplate(e); |
| 149 | + _rootBorder = e.NameScope.Find<Border>("PART_RootBorder"); |
| 150 | + } |
| 151 | + |
| 152 | + /// <inheritdoc/> |
| 153 | + protected override void OnPointerEntered(PointerEventArgs e) |
| 154 | + { |
| 155 | + base.OnPointerEntered(e); |
| 156 | + _isPointerOver = true; |
| 157 | + } |
| 158 | + |
| 159 | + /// <inheritdoc/> |
| 160 | + protected override void OnPointerExited(PointerEventArgs e) |
| 161 | + { |
| 162 | + base.OnPointerExited(e); |
| 163 | + _isPointerOver = false; |
| 164 | + } |
| 165 | + |
| 166 | + /// <inheritdoc/> |
| 167 | + protected override void OnPointerPressed(PointerPressedEventArgs e) |
| 168 | + { |
| 169 | + base.OnPointerPressed(e); |
| 170 | + |
| 171 | + if (!_isPointerOver && IsOpen && !AppBarIsSticky) |
| 172 | + { |
| 173 | + HideAppBar(); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments