|
| 1 | +// Copyright (c) Files Community |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using CommunityToolkit.WinUI; |
| 5 | +using Microsoft.UI.Xaml; |
| 6 | +using Microsoft.UI.Xaml.Controls; |
| 7 | +using Microsoft.UI.Xaml.Media; |
| 8 | +using Microsoft.UI.Xaml.Markup; |
| 9 | +using Microsoft.UI.Xaml.Shapes; |
| 10 | +using Microsoft.UI.Xaml.Input; |
| 11 | +using Microsoft.UI; |
| 12 | +using Windows.ApplicationModel.Contacts; |
| 13 | + |
| 14 | +namespace Files.App.Controls |
| 15 | +{ |
| 16 | + // Content |
| 17 | + [ContentProperty(Name = nameof(Modes))] |
| 18 | + // Template parts |
| 19 | + [TemplatePart(Name = "PART_ModesHostGrid", Type = typeof(Grid))] |
| 20 | + // Visual states |
| 21 | + [TemplateVisualState(Name = "Focused", GroupName = "FocusStates")] |
| 22 | + [TemplateVisualState(Name = "Normal", GroupName = "FocusStates")] |
| 23 | + public partial class Omnibar : Control |
| 24 | + { |
| 25 | + private const string ModesHostGrid = "PART_ModesHostGrid"; |
| 26 | + private const string AutoSuggestPopup = "PART_AutoSuggestPopup"; |
| 27 | + private const string AutoSuggestBoxBorder = "PART_AutoSuggestBoxBorder"; |
| 28 | + |
| 29 | + private Grid? _modesHostGrid; |
| 30 | + private Popup? _autoSuggestPopup; |
| 31 | + private Border? _autoSuggestBoxBorder; |
| 32 | + private bool _isFocused; |
| 33 | + private bool _stillHasFocus; |
| 34 | + |
| 35 | + public Omnibar() |
| 36 | + { |
| 37 | + DefaultStyleKey = typeof(Omnibar); |
| 38 | + |
| 39 | + Modes ??= []; |
| 40 | + } |
| 41 | + |
| 42 | + protected override void OnApplyTemplate() |
| 43 | + { |
| 44 | + _modesHostGrid = GetTemplateChild(ModesHostGrid) as Grid |
| 45 | + ?? throw new MissingFieldException($"Could not find {ModesHostGrid} in the given {nameof(Omnibar)}'s style."); |
| 46 | + _autoSuggestPopup = GetTemplateChild(AutoSuggestPopup) as Popup |
| 47 | + ?? throw new MissingFieldException($"Could not find {AutoSuggestPopup} in the given {nameof(Omnibar)}'s style."); |
| 48 | + _autoSuggestBoxBorder = GetTemplateChild(AutoSuggestBoxBorder) as Border |
| 49 | + ?? throw new MissingFieldException($"Could not find {AutoSuggestBoxBorder} in the given {nameof(Omnibar)}'s style."); |
| 50 | + |
| 51 | + if (Modes is null) |
| 52 | + return; |
| 53 | + |
| 54 | + // Add shadow to the popup and set the proper width |
| 55 | + _autoSuggestBoxBorder!.Translation = new(0, 0, 32); |
| 56 | + _autoSuggestBoxBorder!.Width = _modesHostGrid!.ActualWidth; |
| 57 | + |
| 58 | + // Populate the modes |
| 59 | + foreach (var mode in Modes) |
| 60 | + { |
| 61 | + // Insert a divider |
| 62 | + if (_modesHostGrid.Children.Count is not 0) |
| 63 | + { |
| 64 | + var divider = new Rectangle() |
| 65 | + { |
| 66 | + Fill = (SolidColorBrush)Application.Current.Resources["DividerStrokeColorDefaultBrush"], |
| 67 | + Height = 20, |
| 68 | + Margin = new(2,0,2,0), |
| 69 | + Width = 1, |
| 70 | + }; |
| 71 | + |
| 72 | + _modesHostGrid.ColumnDefinitions.Add(new() { Width = GridLength.Auto }); |
| 73 | + Grid.SetColumn(divider, _modesHostGrid.Children.Count); |
| 74 | + _modesHostGrid.Children.Add(divider); |
| 75 | + } |
| 76 | + |
| 77 | + // Insert the mode |
| 78 | + _modesHostGrid.ColumnDefinitions.Add(new() { Width = GridLength.Auto }); |
| 79 | + Grid.SetColumn(mode, _modesHostGrid.Children.Count); |
| 80 | + _modesHostGrid.Children.Add(mode); |
| 81 | + mode.Host = this; |
| 82 | + } |
| 83 | + |
| 84 | + _modesHostGrid.SizeChanged += ModesHostGrid_SizeChanged; |
| 85 | + |
| 86 | + GotFocus += Omnibar_GotFocus; |
| 87 | + LostFocus += Omnibar_LostFocus; |
| 88 | + LosingFocus += Omnibar_LosingFocus; |
| 89 | + |
| 90 | + UpdateVisualStates(); |
| 91 | + |
| 92 | + base.OnApplyTemplate(); |
| 93 | + } |
| 94 | + |
| 95 | + // Methods |
| 96 | + |
| 97 | + internal void ChangeMode(OmnibarMode modeToExpand) |
| 98 | + { |
| 99 | + if (_modesHostGrid is null || Modes is null) |
| 100 | + throw new NullReferenceException(); |
| 101 | + |
| 102 | + // Reset |
| 103 | + foreach (var column in _modesHostGrid.ColumnDefinitions) |
| 104 | + column.Width = GridLength.Auto; |
| 105 | + foreach (var mode in Modes) |
| 106 | + VisualStateManager.GoToState(mode, "Unfocused", true); |
| 107 | + |
| 108 | + // Expand the given mode |
| 109 | + VisualStateManager.GoToState(modeToExpand, "Focused", true); |
| 110 | + _modesHostGrid.ColumnDefinitions[_modesHostGrid.Children.IndexOf(modeToExpand)].Width = new(1, GridUnitType.Star); |
| 111 | + |
| 112 | + CurrentSelectedMode = modeToExpand; |
| 113 | + |
| 114 | + UpdateVisualStates(); |
| 115 | + } |
| 116 | + |
| 117 | + private void UpdateVisualStates() |
| 118 | + { |
| 119 | + VisualStateManager.GoToState(this, _isFocused ? "Focused" : "Normal", true); |
| 120 | + |
| 121 | + if (CurrentSelectedMode is not null && _autoSuggestPopup is not null) |
| 122 | + { |
| 123 | + // Close anyway |
| 124 | + if (_autoSuggestPopup.IsOpen && CurrentSelectedMode.SuggestionItemsSource is null) |
| 125 | + VisualStateManager.GoToState(this, "PopupClosed", true); |
| 126 | + |
| 127 | + // Decide open or close |
| 128 | + if (_isFocused != _autoSuggestPopup.IsOpen) |
| 129 | + VisualStateManager.GoToState(this, _isFocused && CurrentSelectedMode.SuggestionItemsSource is not null ? "PopupOpened" : "PopupClosed", true); |
| 130 | + } |
| 131 | + |
| 132 | + if (CurrentSelectedMode is not null) |
| 133 | + VisualStateManager.GoToState( |
| 134 | + CurrentSelectedMode, |
| 135 | + _isFocused |
| 136 | + ? "Focused" |
| 137 | + : CurrentSelectedMode.ContentOnInactive is null |
| 138 | + ? "CurrentUnfocusedWithoutInactiveMode" |
| 139 | + : "CurrentUnfocusedWithInactiveMode", |
| 140 | + true); |
| 141 | + } |
| 142 | + |
| 143 | + // Events |
| 144 | + |
| 145 | + private void ModesHostGrid_SizeChanged(object sender, SizeChangedEventArgs e) |
| 146 | + { |
| 147 | + _autoSuggestBoxBorder!.Width = _modesHostGrid!.ActualWidth; |
| 148 | + } |
| 149 | + |
| 150 | + private void Omnibar_GotFocus(object sender, RoutedEventArgs e) |
| 151 | + { |
| 152 | + _isFocused = true; |
| 153 | + UpdateVisualStates(); |
| 154 | + } |
| 155 | + |
| 156 | + private void Omnibar_LosingFocus(UIElement sender, LosingFocusEventArgs args) |
| 157 | + { |
| 158 | + // Ignore when user clicks on the TextBox or the button area of an OmnibarMode, Omnibar still has focus anyway |
| 159 | + if (args.NewFocusedElement?.GetType() is not { } focusedType || |
| 160 | + focusedType == typeof(TextBox) || |
| 161 | + focusedType == typeof(OmnibarMode) || |
| 162 | + focusedType == typeof(Omnibar)) |
| 163 | + { |
| 164 | + _stillHasFocus = true; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private void Omnibar_LostFocus(object sender, RoutedEventArgs e) |
| 169 | + { |
| 170 | + if (_stillHasFocus) |
| 171 | + { |
| 172 | + _stillHasFocus = false; |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + _isFocused = false; |
| 177 | + UpdateVisualStates(); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments