|
7 | 7 | using Microsoft.UI.Xaml.Media; |
8 | 8 | using Microsoft.UI.Xaml.Markup; |
9 | 9 | using Microsoft.UI.Xaml.Shapes; |
10 | | -using System.Linq; |
11 | | -using System.Collections.Generic; |
12 | 10 |
|
13 | 11 | namespace Files.App.Controls |
14 | 12 | { |
15 | 13 | public partial class OmnibarMode : Control |
16 | 14 | { |
| 15 | + private const string ModeClickBorder = "PART_ModeClickBorder"; |
| 16 | + private const string InputTextBox = "PART_InputTextBox"; |
| 17 | + |
| 18 | + private Border? _modeClickArea; |
| 19 | + private TextBox? _inputTextBox; |
| 20 | + |
| 21 | + private bool _isHoveredOver; |
| 22 | + private bool _isPressed; |
| 23 | + |
17 | 24 | public OmnibarMode() |
18 | 25 | { |
19 | 26 | DefaultStyleKey = typeof(OmnibarMode); |
20 | 27 | } |
21 | 28 |
|
22 | 29 | protected override void OnApplyTemplate() |
23 | 30 | { |
24 | | - PointerEntered += OmnibarMode_PointerEntered; |
25 | | - PointerPressed += OmnibarMode_PointerPressed; |
26 | | - PointerReleased += OmnibarMode_PointerReleased; |
27 | | - PointerExited += OmnibarMode_PointerExited; |
| 31 | + _modeClickArea = GetTemplateChild(ModeClickBorder) as Border |
| 32 | + ?? throw new MissingFieldException($"Could not find {ModeClickBorder} in the given {nameof(OmnibarMode)}'s style."); |
| 33 | + _inputTextBox = GetTemplateChild(InputTextBox) as TextBox |
| 34 | + ?? throw new MissingFieldException($"Could not find {InputTextBox} in the given {nameof(OmnibarMode)}'s style."); |
| 35 | + |
| 36 | + UpdateVisualStates(); |
| 37 | + |
| 38 | + _modeClickArea.PointerEntered += OmnibarMode_PointerEntered; |
| 39 | + _modeClickArea.PointerPressed += OmnibarMode_PointerPressed; |
| 40 | + _modeClickArea.PointerReleased += OmnibarMode_PointerReleased; |
| 41 | + _modeClickArea.PointerExited += OmnibarMode_PointerExited; |
28 | 42 |
|
29 | 43 | base.OnApplyTemplate(); |
30 | 44 | } |
31 | 45 |
|
32 | | - private void OmnibarMode_PointerExited(object sender, PointerRoutedEventArgs e) |
| 46 | + private void UpdateVisualStates() |
33 | 47 | { |
34 | | - VisualStateManager.GoToState(this, "Normal", true); |
| 48 | + VisualStateManager.GoToState( |
| 49 | + this, |
| 50 | + _isPressed ? "PointerPressed" : _isHoveredOver ? "PointerOver" : "PointerNormal", |
| 51 | + true); |
| 52 | + |
| 53 | + if (IsDefault && Host is not null) |
| 54 | + { |
| 55 | + VisualStateManager.GoToState(this, "Visible",true); |
| 56 | + Host.ColumnDefinitions[Host.Children.IndexOf(this)].Width = new(1, GridUnitType.Star); |
| 57 | + } |
35 | 58 | } |
36 | 59 |
|
37 | | - private void OmnibarMode_PointerReleased(object sender, PointerRoutedEventArgs e) |
| 60 | + // Events |
| 61 | + |
| 62 | + private void OmnibarMode_PointerEntered(object sender, PointerRoutedEventArgs e) |
38 | 63 | { |
39 | | - VisualStateManager.GoToState(this, "PointerOver", true); |
| 64 | + _isHoveredOver = true; |
| 65 | + _isPressed = false; |
| 66 | + UpdateVisualStates(); |
40 | 67 | } |
41 | 68 |
|
42 | 69 | private void OmnibarMode_PointerPressed(object sender, PointerRoutedEventArgs e) |
43 | 70 | { |
44 | | - VisualStateManager.GoToState(this, "Pressed", true); |
| 71 | + _isHoveredOver = false; |
| 72 | + _isPressed = true; |
| 73 | + UpdateVisualStates(); |
45 | 74 | } |
46 | 75 |
|
47 | | - private void OmnibarMode_PointerEntered(object sender, PointerRoutedEventArgs e) |
| 76 | + private void OmnibarMode_PointerReleased(object sender, PointerRoutedEventArgs e) |
| 77 | + { |
| 78 | + _isHoveredOver = true; |
| 79 | + _isPressed = false; |
| 80 | + UpdateVisualStates(); |
| 81 | + } |
| 82 | + |
| 83 | + private void OmnibarMode_PointerExited(object sender, PointerRoutedEventArgs e) |
48 | 84 | { |
49 | | - VisualStateManager.GoToState(this, "PointerOver", true); |
| 85 | + _isHoveredOver = _isPressed = false; |
| 86 | + UpdateVisualStates(); |
50 | 87 | } |
51 | 88 | } |
52 | 89 | } |
0 commit comments