|
| 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_RootItem = "PART_RootBreadcrumbBarItem"; |
| 24 | + private const string TemplatePartName_MainItemsRepeater = "PART_MainItemsRepeater"; |
| 25 | + |
| 26 | + // Fields |
| 27 | + |
| 28 | + private BreadcrumbBarLayout _itemsRepeaterLayout = null!; |
| 29 | + |
| 30 | + private BreadcrumbBarItem? _rootItem; |
| 31 | + private ItemsRepeater? _itemsRepeater; |
| 32 | + |
| 33 | + // Events |
| 34 | + |
| 35 | + public event TypedEventHandler<BreadcrumbBar, BreadcrumbBarItemClickedEventArgs>? ItemClicked; |
| 36 | + public event EventHandler<BreadcrumbBarItemDropDownFlyoutEventArgs>? ItemDropDownFlyoutOpening; |
| 37 | + public event EventHandler<BreadcrumbBarItemDropDownFlyoutEventArgs>? ItemDropDownFlyoutClosed; |
| 38 | + |
| 39 | + public event EventHandler? RootItemClicked; |
| 40 | + public event EventHandler<BreadcrumbBarItemDropDownFlyoutEventArgs>? RootItemDropDownFlyoutOpening; |
| 41 | + public event EventHandler<BreadcrumbBarItemDropDownFlyoutEventArgs>? RootItemDropDownFlyoutClosed; |
| 42 | + |
| 43 | + // Constructor |
| 44 | + |
| 45 | + public BreadcrumbBar() |
| 46 | + { |
| 47 | + DefaultStyleKey = typeof(BreadcrumbBar); |
| 48 | + |
| 49 | + _itemsRepeaterLayout = new(this, 2d); |
| 50 | + } |
| 51 | + |
| 52 | + // Methods |
| 53 | + |
| 54 | + protected override void OnApplyTemplate() |
| 55 | + { |
| 56 | + _rootItem = GetTemplateChild(TemplatePartName_RootItem) as BreadcrumbBarItem |
| 57 | + ?? throw new MissingFieldException($"Could not find {TemplatePartName_RootItem} in the given {nameof(BreadcrumbBarItem)}'s style."); |
| 58 | + _itemsRepeater = GetTemplateChild(TemplatePartName_MainItemsRepeater) as ItemsRepeater |
| 59 | + ?? throw new MissingFieldException($"Could not find {TemplatePartName_MainItemsRepeater} in the given {nameof(BreadcrumbBar)}'s style."); |
| 60 | + |
| 61 | + _rootItem.SetOwner(this); |
| 62 | + _itemsRepeater.Layout = _itemsRepeaterLayout; |
| 63 | + |
| 64 | + _itemsRepeater.ElementPrepared += ItemsRepeater_ElementPrepared; |
| 65 | + |
| 66 | + base.OnApplyTemplate(); |
| 67 | + } |
| 68 | + |
| 69 | + internal void RaiseItemClickedEvent(BreadcrumbBarItem item) |
| 70 | + { |
| 71 | + var index = _itemsRepeater?.GetElementIndex(item) ?? throw new ArgumentNullException($"{_itemsRepeater} is null."); |
| 72 | + var eventArgs = new BreadcrumbBarItemClickedEventArgs(item, index); |
| 73 | + ItemClicked?.Invoke(this, eventArgs); |
| 74 | + } |
| 75 | + |
| 76 | + internal void RaiseItemDropDownFlyoutOpening(BreadcrumbBarItem item, MenuFlyout flyout) |
| 77 | + { |
| 78 | + var index = _itemsRepeater?.GetElementIndex(item) ?? throw new ArgumentNullException($"{_itemsRepeater} is null."); |
| 79 | + ItemDropDownFlyoutOpening?.Invoke(this, new(flyout, item, index)); |
| 80 | + } |
| 81 | + |
| 82 | + internal void RaiseItemDropDownFlyoutClosed(BreadcrumbBarItem item, MenuFlyout flyout) |
| 83 | + { |
| 84 | + var index = _itemsRepeater?.GetElementIndex(item) ?? throw new ArgumentNullException($"{_itemsRepeater} is null."); |
| 85 | + ItemDropDownFlyoutClosed?.Invoke(this, new(flyout, item, index)); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments