-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathOmnibar.Events.cs
127 lines (103 loc) · 3.39 KB
/
Omnibar.Events.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) Files Community
// Licensed under the MIT License.
using Microsoft.UI.Xaml.Input;
using Windows.System;
namespace Files.App.Controls
{
public partial class Omnibar
{
private void Omnibar_SizeChanged(object sender, SizeChangedEventArgs e)
{
// Popup width has to be set manually because it doesn't stretch with the parent
_textBoxSuggestionsContainerBorder.Width = ActualWidth;
}
private void AutoSuggestBox_GotFocus(object sender, RoutedEventArgs e)
{
IsFocused = true;
VisualStateManager.GoToState(CurrentSelectedMode, "Focused", true);
VisualStateManager.GoToState(_textBox, "InputAreaVisible", true);
TryToggleIsSuggestionsPopupOpen(true);
}
private void AutoSuggestBox_LostFocus(object sender, RoutedEventArgs e)
{
// TextBox still has focus if the context menu for selected text is open
if (_textBox.ContextFlyout.IsOpen)
return;
IsFocused = false;
if (CurrentSelectedMode?.ContentOnInactive is not null)
{
VisualStateManager.GoToState(CurrentSelectedMode, "CurrentUnfocused", true);
VisualStateManager.GoToState(_textBox, "InputAreaCollapsed", true);
}
TryToggleIsSuggestionsPopupOpen(false);
}
private void AutoSuggestBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key is VirtualKey.Enter)
{
e.Handled = true;
SubmitQuery(_textBoxSuggestionsPopup.IsOpen && _textBoxSuggestionsListView.SelectedIndex is not -1 ? _textBoxSuggestionsListView.SelectedItem : null);
}
else if ((e.Key == VirtualKey.Up || e.Key == VirtualKey.Down) && _textBoxSuggestionsPopup.IsOpen)
{
e.Handled = true;
var currentIndex = _textBoxSuggestionsListView.SelectedIndex;
var nextIndex = currentIndex;
var suggestionsCount = _textBoxSuggestionsListView.Items.Count;
if (e.Key is VirtualKey.Up)
{
nextIndex--;
}
else if (e.Key is VirtualKey.Down)
{
nextIndex++;
}
if (0 > nextIndex || nextIndex >= suggestionsCount)
{
RevertTextToUserInput();
}
else
{
_textBoxSuggestionsListView.SelectedIndex = nextIndex;
ChooseSuggestionItem(_textBoxSuggestionsListView.SelectedItem);
}
}
else if (e.Key == VirtualKey.Escape && _textBoxSuggestionsPopup.IsOpen)
{
e.Handled = true;
RevertTextToUserInput();
_textBoxSuggestionsPopup.IsOpen = false;
}
else
{
_textChangeReason = OmnibarTextChangeReason.UserInput;
}
}
private void AutoSuggestBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (string.Compare(_textBox.Text, CurrentSelectedMode!.Text, StringComparison.OrdinalIgnoreCase) is not 0)
CurrentSelectedMode!.Text = _textBox.Text;
// UpdateSuggestionListView();
if (_textChangeReason is not OmnibarTextChangeReason.SuggestionChosen and
not OmnibarTextChangeReason.ProgrammaticChange)
{
_textChangeReason = OmnibarTextChangeReason.UserInput;
_userInput = _textBox.Text;
}
TextChanged?.Invoke(this, new(CurrentSelectedMode, _textChangeReason));
// Reset
_textChangeReason = OmnibarTextChangeReason.None;
}
private void AutoSuggestBoxSuggestionsPopup_GettingFocus(UIElement sender, GettingFocusEventArgs args)
{
args.TryCancel();
}
private void AutoSuggestBoxSuggestionsListView_ItemClick(object sender, ItemClickEventArgs e)
{
if (CurrentSelectedMode is null)
return;
ChooseSuggestionItem(e.ClickedItem);
SubmitQuery(e.ClickedItem);
}
}
}