|
| 1 | +// Copyright (c) Files Community |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using CommunityToolkit.WinUI; |
| 5 | +using Microsoft.UI; |
| 6 | +using Microsoft.UI.Xaml; |
| 7 | +using Microsoft.UI.Xaml.Controls; |
| 8 | +using Microsoft.UI.Xaml.Input; |
| 9 | +using Microsoft.UI.Xaml.Markup; |
| 10 | +using Microsoft.UI.Xaml.Media; |
| 11 | +using Microsoft.UI.Xaml.Shapes; |
| 12 | +using System; |
| 13 | +using Windows.Foundation; |
| 14 | + |
| 15 | +namespace Files.App.Controls |
| 16 | +{ |
| 17 | + // Template parts |
| 18 | + [TemplatePart(Name = "PART_ItemsRepeater", Type = typeof(ItemsRepeater))] |
| 19 | + public partial class BreadcrumbBar : Control |
| 20 | + { |
| 21 | + // Constants |
| 22 | + |
| 23 | + private const string TemplatePartName_RootItemButton = "PART_RootItemButton"; |
| 24 | + private const string TemplatePartName_RootItemChevronButton = "PART_RootItemChevronButton"; |
| 25 | + private const string TemplatePartName_RootElementChevronDropDownMenuFlyout = "PART_RootElementChevronDropDownMenuFlyout"; |
| 26 | + private const string TemplatePartName_MainItemsRepeater = "PART_MainItemsRepeater"; |
| 27 | + |
| 28 | + // Fields |
| 29 | + |
| 30 | + private BreadcrumbBarLayout _itemsRepeaterLayout = null!; |
| 31 | + |
| 32 | + private Border? _rootItemButton; |
| 33 | + private Border? _rootItemChevronButton; |
| 34 | + private MenuFlyout? _rootItemChevronDropDownMenuFlyout; |
| 35 | + private ItemsRepeater? _itemsRepeater; |
| 36 | + |
| 37 | + // Events |
| 38 | + |
| 39 | + public event TypedEventHandler<BreadcrumbBar, BreadcrumbBarItemClickedEventArgs>? ItemClicked; |
| 40 | + public event EventHandler<BreadcrumbBarItemDropDownFlyoutEventArgs>? ItemDropDownFlyoutOpening; |
| 41 | + public event EventHandler<BreadcrumbBarItemDropDownFlyoutEventArgs>? ItemDropDownFlyoutClosed; |
| 42 | + |
| 43 | + public event EventHandler? RootItemClicked; |
| 44 | + public event EventHandler<BreadcrumbBarItemDropDownFlyoutEventArgs>? RootItemDropDownFlyoutOpening; |
| 45 | + public event EventHandler<BreadcrumbBarItemDropDownFlyoutEventArgs>? RootItemDropDownFlyoutClosed; |
| 46 | + |
| 47 | + // Constructor |
| 48 | + |
| 49 | + public BreadcrumbBar() |
| 50 | + { |
| 51 | + DefaultStyleKey = typeof(BreadcrumbBar); |
| 52 | + |
| 53 | + _itemsRepeaterLayout = new(this, 2d); |
| 54 | + } |
| 55 | + |
| 56 | + // Methods |
| 57 | + |
| 58 | + protected override void OnApplyTemplate() |
| 59 | + { |
| 60 | + _rootItemButton = GetTemplateChild(TemplatePartName_RootItemButton) as Border |
| 61 | + ?? throw new MissingFieldException($"Could not find {TemplatePartName_RootItemButton} in the given {nameof(BreadcrumbBar)}'s style."); |
| 62 | + _rootItemChevronButton = GetTemplateChild(TemplatePartName_RootItemChevronButton) as Border |
| 63 | + ?? throw new MissingFieldException($"Could not find {TemplatePartName_RootItemChevronButton} in the given {nameof(BreadcrumbBar)}'s style."); |
| 64 | + _rootItemChevronDropDownMenuFlyout = GetTemplateChild(TemplatePartName_RootElementChevronDropDownMenuFlyout) as MenuFlyout |
| 65 | + ?? throw new MissingFieldException($"Could not find {TemplatePartName_RootElementChevronDropDownMenuFlyout} in the given {nameof(BreadcrumbBar)}'s style."); |
| 66 | + _itemsRepeater = GetTemplateChild(TemplatePartName_MainItemsRepeater) as ItemsRepeater |
| 67 | + ?? throw new MissingFieldException($"Could not find {TemplatePartName_MainItemsRepeater} in the given {nameof(BreadcrumbBar)}'s style."); |
| 68 | + |
| 69 | + _itemsRepeater.Layout = _itemsRepeaterLayout; |
| 70 | + |
| 71 | + _rootItemButton.PointerEntered += RootItemButton_PointerEntered; |
| 72 | + _rootItemButton.PointerPressed += RootItemButton_PointerPressed; |
| 73 | + _rootItemButton.PointerReleased += RootItemButton_PointerReleased; |
| 74 | + _rootItemButton.PointerExited += RootItemButton_PointerExited; |
| 75 | + |
| 76 | + _rootItemChevronButton.PointerEntered += RootChevronItemButton_PointerEntered; |
| 77 | + _rootItemChevronButton.PointerPressed += RootChevronItemButton_PointerPressed; |
| 78 | + _rootItemChevronButton.PointerReleased += RootChevronItemButton_PointerReleased; |
| 79 | + _rootItemChevronButton.PointerExited += RootChevronItemButton_PointerExited; |
| 80 | + |
| 81 | + _rootItemChevronDropDownMenuFlyout.Opening += RootItemChevronDropDownMenuFlyout_Opening; |
| 82 | + _rootItemChevronDropDownMenuFlyout.Opened += RootItemChevronDropDownMenuFlyout_Opened; |
| 83 | + _rootItemChevronDropDownMenuFlyout.Closed += RootItemChevronDropDownMenuFlyout_Closed; |
| 84 | + |
| 85 | + _itemsRepeater.ElementPrepared += ItemsRepeater_ElementPrepared; |
| 86 | + |
| 87 | + base.OnApplyTemplate(); |
| 88 | + } |
| 89 | + |
| 90 | + internal void RaiseItemClickedEvent(BreadcrumbBarItem item) |
| 91 | + { |
| 92 | + var index = _itemsRepeater?.GetElementIndex(item) ?? throw new ArgumentNullException($"{_itemsRepeater} is null."); |
| 93 | + var eventArgs = new BreadcrumbBarItemClickedEventArgs(item, index); |
| 94 | + ItemClicked?.Invoke(this, eventArgs); |
| 95 | + } |
| 96 | + |
| 97 | + internal void RaiseItemDropDownFlyoutOpening(BreadcrumbBarItem item, MenuFlyout flyout) |
| 98 | + { |
| 99 | + var index = _itemsRepeater?.GetElementIndex(item) ?? throw new ArgumentNullException($"{_itemsRepeater} is null."); |
| 100 | + ItemDropDownFlyoutOpening?.Invoke(this, new(flyout, item, index)); |
| 101 | + } |
| 102 | + |
| 103 | + internal void RaiseItemDropDownFlyoutClosed(BreadcrumbBarItem item, MenuFlyout flyout) |
| 104 | + { |
| 105 | + var index = _itemsRepeater?.GetElementIndex(item) ?? throw new ArgumentNullException($"{_itemsRepeater} is null."); |
| 106 | + ItemDropDownFlyoutClosed?.Invoke(this, new(flyout, item, index)); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments