Skip to content

Commit bb05811

Browse files
committed
chore: Porting overlay code
1 parent ab07280 commit bb05811

File tree

4 files changed

+99
-88
lines changed

4 files changed

+99
-88
lines changed

src/Uno.UI/UI/Xaml/Controls/ComboBox/ComboBox.cs

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#nullable enable
22

33
using System;
4-
using Microsoft.UI.Input;
54
using Microsoft.UI.Xaml.Automation.Peers;
65
using Microsoft.UI.Xaml.Controls.Primitives;
76
using Microsoft.UI.Xaml.Data;
@@ -36,7 +35,6 @@ namespace Microsoft.UI.Xaml.Controls
3635
{
3736
public partial class ComboBox : Selector
3837
{
39-
4038
private bool _areItemTemplatesForwarded;
4139

4240
private TextBox? m_tpEditableTextPart;
@@ -48,6 +46,7 @@ public partial class ComboBox : Selector
4846
private ContentPresenter? _contentPresenter;
4947
private TextBlock? _placeholderTextBlock;
5048
private ContentPresenter? m_tpHeaderContentPresenterPart;
49+
private Border? m_tpDropDownOverlayPart;
5150

5251
private DateTime m_timeSinceLastCharacterReceived;
5352
private string m_searchString = "";
@@ -105,11 +104,8 @@ protected override void OnApplyTemplate()
105104
_contentPresenter = this.GetTemplateChild("ContentPresenter") as ContentPresenter;
106105
m_tpContentPresenterPart = _contentPresenter;
107106
_placeholderTextBlock = this.GetTemplateChild("PlaceholderTextBlock") as TextBlock;
108-
109-
if (IsEditable)
110-
{
111-
m_tpEditableTextPart = this.GetTemplateChild("EditableText") as TextBox;
112-
}
107+
m_tpDropDownOverlayPart = this.GetTemplateChild("DropDownOverlay") as Border;
108+
m_tpEditableTextPart = this.GetTemplateChild("EditableText") as TextBox;
113109

114110
if (_popup is Popup popup)
115111
{
@@ -176,72 +172,6 @@ protected override void OnApplyTemplate()
176172
ChangeVisualState(false);
177173
}
178174

179-
180-
// Selects the next item in the list.
181-
private void SelectNext(ref int index)
182-
{
183-
var count = Items.Count;
184-
if (count > 0)
185-
{
186-
int internalSelectedIndex = index + 1;
187-
if (internalSelectedIndex <= count - 1)
188-
{
189-
SelectItemHelper(ref internalSelectedIndex, 1);
190-
if (internalSelectedIndex != -1)
191-
{
192-
index = internalSelectedIndex;
193-
}
194-
}
195-
}
196-
}
197-
198-
// Selects the previous item in the list.
199-
private void SelectPrev(ref int index)
200-
{
201-
var count = Items.Count;
202-
if (count > 0)
203-
{
204-
int internalSelectedIndex = index - 1;
205-
if (internalSelectedIndex >= 0)
206-
{
207-
SelectItemHelper(ref internalSelectedIndex, -1);
208-
if (internalSelectedIndex != -1)
209-
{
210-
index = internalSelectedIndex;
211-
}
212-
}
213-
}
214-
}
215-
216-
// Given a direction, searches through list for next available item to select.
217-
private void SelectItemHelper(ref int index, int increment)
218-
{
219-
var items = Items;
220-
var count = items.Count;
221-
bool isSelectable = false;
222-
223-
for (; index > -1 && index < count; index += increment)
224-
{
225-
var item = items[index];
226-
isSelectable = IsSelectableHelper(item);
227-
if (isSelectable)
228-
{
229-
var container = ContainerFromIndex(index);
230-
isSelectable = IsSelectableHelper(container);
231-
if (isSelectable)
232-
{
233-
break;
234-
}
235-
}
236-
}
237-
238-
if (!isSelectable)
239-
{
240-
// If no selectable item was found, set index to -1 so selection will not be updated.
241-
index = -1;
242-
}
243-
}
244-
245175
private protected override void OnLoaded()
246176
{
247177
base.OnLoaded();

src/Uno.UI/UI/Xaml/Controls/ComboBox/ComboBox.partial.h.mux.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ partial class ComboBox
2323
private bool m_IsPointerOverMain;
2424
private bool m_IsPointerOverPopup;
2525
private bool m_bIsPressed;
26+
private bool m_isDropDownClosing;
27+
private bool m_bPopupHasBeenArrangedOnce;
2628
// Used to determine when to open the popup based on touch, we open the popup when TextBox gains
2729
// focus due to a pointer event.
2830
private bool m_openPopupOnTouch;
@@ -39,15 +41,19 @@ partial class ComboBox
3941
private bool m_isClosingDueToCancel;
4042
private bool m_restoreIndexSet;
4143

44+
private bool m_IsPointerOverDropDownOverlay;
45+
4246
private InputDeviceType m_inputDeviceTypeUsedToOpen;
4347

44-
private readonly SerialDisposable m_spEditableTextPreviewKeyDownHandler = new();
48+
private readonly SerialDisposable m_spEditableTextPointerPressedEventHandler = new();
49+
private readonly SerialDisposable m_spEditableTextTappedEventHandler = new();
4550
private readonly SerialDisposable m_spEditableTextKeyDownHandler = new();
51+
private readonly SerialDisposable m_spEditableTextPreviewKeyDownHandler = new();
4652
private readonly SerialDisposable m_spEditableTextTextChangedHandler = new();
4753
private readonly SerialDisposable m_spEditableTextCandidateWindowBoundsChangedEventHandler = new();
4854
private readonly SerialDisposable m_spEditableTextSizeChangedHandler = new();
49-
private readonly SerialDisposable m_spEditableTextPointerPressedEventHandler = new();
50-
private readonly SerialDisposable m_spEditableTextTappedEventHandler = new();
55+
private readonly SerialDisposable m_spDropDownOverlayPointerEnteredHandler = new();
56+
private readonly SerialDisposable m_spDropDownOverlayPointerExitedHandler = new();
5157

5258
private TextBlock m_tpEditableContentPresenterTextBlock;
5359

src/Uno.UI/UI/Xaml/Controls/ComboBox/ComboBox.partial.mux.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Linq;
66
using DirectUI;
77
using Microsoft.UI.Input;
8-
using Microsoft.UI.Xaml.Controls;
98
using Microsoft.UI.Xaml.Controls.Primitives;
109
using Microsoft.UI.Xaml.Input;
1110
using Microsoft.UI.Xaml.Media;
@@ -70,15 +69,13 @@ private void SetupEditableMode()
7069

7170
if (m_tpDropDownOverlayPart is not null)
7271
{
73-
m_spDropDownOverlayPointerEnteredHandler.AttachEventHandler(
74-
m_tpDropDownOverlayPart.Cast<Border>(),
75-
std::bind(&ComboBox::OnDropDownOverlayPointerEntered, this, _1, _2)));
72+
m_tpDropDownOverlayPart.PointerEntered += OnDropDownOverlayPointerEntered;
73+
m_spDropDownOverlayPointerEnteredHandler.Disposable = Disposable.Create(() => m_tpContentPresenterPart.PointerEntered -= OnDropDownOverlayPointerEntered);
7674

77-
m_spDropDownOverlayPointerExitedHandler.AttachEventHandler(
78-
m_tpDropDownOverlayPart.Cast<Border>(),
79-
std::bind(&ComboBox::OnDropDownOverlayPointerExited, this, _1, _2)));
75+
m_tpDropDownOverlayPart.PointerExited += OnDropDownOverlayPointerExited;
76+
m_spDropDownOverlayPointerExitedHandler.Disposable = Disposable.Create(() => m_tpContentPresenterPart.PointerExited -= OnDropDownOverlayPointerExited);
8077

81-
m_tpDropDownOverlayPart.Cast<Border>()->put_Visibility(xaml::Visibility_Visible));
78+
m_tpDropDownOverlayPart.Visibility = Visibility.Visible;
8279
}
8380

8481
// Tells the selector to allow Custom Values.
@@ -168,11 +165,11 @@ private void DisableEditableMode()
168165

169166
private protected override void ChangeVisualState(bool useTransitions)
170167
{
171-
bool isPointerOver = m_isPointerOverMain || m_isPointerOverPopup;
168+
bool isPointerOver = m_IsPointerOverMain || m_IsPointerOverPopup;
172169
bool isEnabled = IsEnabled;
173170
bool isSelectionActive = IsSelectionActive;
174171
bool isDropDownOpen = IsDropDownOpen;
175-
bool selectedIndex = SelectedIndex;
172+
int selectedIndex = SelectedIndex;
176173

177174
var focusManager = VisualTree.GetFocusManagerForElement(this);
178175

@@ -184,7 +181,7 @@ private protected override void ChangeVisualState(bool useTransitions)
184181
{
185182
bool editableTextHasFocus = EditableTextHasFocus();
186183

187-
if (m_IsPointerOverDropDownOverlay is not null)
184+
if (m_IsPointerOverDropDownOverlay)
188185
{
189186
if (m_bIsPressed)
190187
{
@@ -342,7 +339,7 @@ internal override void OnPropertyChanged2(DependencyPropertyChangedEventArgs arg
342339
}
343340
}
344341

345-
private void UpdateEditableTextBox(object item, bool selectText, bool selectAll)
342+
private void UpdateEditableTextBox(object? item, bool selectText, bool selectAll)
346343
{
347344
if (item is null)
348345
{
@@ -1173,6 +1170,19 @@ private void OnTextBoxPreviewKeyDown(object pSender, KeyRoutedEventArgs pArgs)
11731170
pArgs.Handled = true;
11741171
}
11751172
}
1173+
1174+
private void OnDropDownOverlayPointerEntered(object sender, PointerRoutedEventArgs args)
1175+
{
1176+
m_IsPointerOverDropDownOverlay = true;
1177+
UpdateVisualState();
1178+
}
1179+
1180+
private void OnDropDownOverlayPointerExited(object sender, PointerRoutedEventArgs args)
1181+
{
1182+
m_IsPointerOverDropDownOverlay = false;
1183+
UpdateVisualState();
1184+
}
1185+
11761186
private void OnTextBoxSizeChanged(object sender, SizeChangedEventArgs args) => ArrangePopup(false);
11771187

11781188
private void OnTextBoxCandidateWindowBoundsChanged(TextBox sender, CandidateWindowBoundsChangedEventArgs args)

src/Uno.UI/UI/Xaml/Controls/Primitives/Selector.partial.mux.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,71 @@ namespace Microsoft.UI.Xaml.Controls.Primitives;
88

99
partial class Selector
1010
{
11+
// Selects the next item in the list.
12+
private protected void SelectNext(ref int index)
13+
{
14+
var count = Items.Count;
15+
if (count > 0)
16+
{
17+
int internalSelectedIndex = index + 1;
18+
if (internalSelectedIndex <= count - 1)
19+
{
20+
SelectItemHelper(ref internalSelectedIndex, 1);
21+
if (internalSelectedIndex != -1)
22+
{
23+
index = internalSelectedIndex;
24+
}
25+
}
26+
}
27+
}
28+
29+
// Selects the previous item in the list.
30+
private protected void SelectPrev(ref int index)
31+
{
32+
var count = Items.Count;
33+
if (count > 0)
34+
{
35+
int internalSelectedIndex = index - 1;
36+
if (internalSelectedIndex >= 0)
37+
{
38+
SelectItemHelper(ref internalSelectedIndex, -1);
39+
if (internalSelectedIndex != -1)
40+
{
41+
index = internalSelectedIndex;
42+
}
43+
}
44+
}
45+
}
46+
47+
// Given a direction, searches through list for next available item to select.
48+
private void SelectItemHelper(ref int index, int increment)
49+
{
50+
var items = Items;
51+
var count = items.Count;
52+
bool isSelectable = false;
53+
54+
for (; index > -1 && index < count; index += increment)
55+
{
56+
var item = items[index];
57+
isSelectable = IsSelectableHelper(item);
58+
if (isSelectable)
59+
{
60+
var container = ContainerFromIndex(index);
61+
isSelectable = IsSelectableHelper(container);
62+
if (isSelectable)
63+
{
64+
break;
65+
}
66+
}
67+
}
68+
69+
if (!isSelectable)
70+
{
71+
// If no selectable item was found, set index to -1 so selection will not be updated.
72+
index = -1;
73+
}
74+
}
75+
1176
internal void SetAllowCustomValues(bool allow)
1277
{
1378
m_customValuesAllowed = allow;

0 commit comments

Comments
 (0)