Repository navigation
Expand file tree
/
Copy pathDialogBase.cs
More file actions
136 lines (119 loc) · 4.7 KB
/
Copy pathDialogBase.cs
File metadata and controls
136 lines (119 loc) · 4.7 KB
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
128
129
130
131
132
133
134
135
136
using System;
using System.Runtime.InteropServices;
using OneSignalDemo.Services;
using UnityEngine.UIElements;
namespace OneSignalDemo.UI.Dialogs
{
public abstract class DialogBase
{
#if UNITY_IOS && !UNITY_EDITOR
// Native bridge in Assets/Plugins/iOS/OneSignalDemoKeyboard.mm.
// Calls [keyWindow endEditing:YES] to dismiss the iOS keyboard view
// immediately. UIToolkit's TextField.Blur() and TouchScreenKeyboard.
// Open("").active = false don't reliably tear down the UIKit keyboard
// when a UIToolkit-owned modal dismisses, leaving it floating over the
// app and blocking taps on controls behind it.
[DllImport("__Internal")]
private static extern void OneSignalDemoEndEditing();
#endif
protected VisualElement Overlay { get; private set; }
protected VisualElement Container { get; private set; }
private VisualElement _parent;
public void Show(VisualElement parent)
{
if (parent?.Q(className: "dialog-overlay") != null)
return;
_parent = parent;
Overlay = new VisualElement();
Overlay.AddToClassList("dialog-overlay");
Overlay.RegisterCallback<ClickEvent>(e =>
{
if (e.target == Overlay)
Dismiss();
});
Container = new VisualElement();
Container.AddToClassList("dialog-container");
BuildContent(Container);
Overlay.Add(Container);
parent.Add(Overlay);
AccessibilityBridge.RequestImmediateResync();
}
public void Dismiss()
{
var textField = Container?.Q<TextField>();
textField?.Blur();
#if UNITY_IOS && !UNITY_EDITOR
// Force UIKit to resign first responder so the keyboard view tears
// down immediately. Without this the keyboard stays floating over
// the app after the modal closes and blocks taps on controls
// behind it (e.g. the logout button after login).
OneSignalDemoEndEditing();
#endif
// Release any pointer capture the dismissed Overlay still holds
// before removing it. Without this UIToolkit can keep delivering
// the in-flight pointer sequence to the captured target after
// RemoveFromHierarchy, which swallows the next synthetic tap.
var overlay = Overlay;
if (overlay != null)
{
var panel = overlay.panel;
if (panel != null)
{
for (int id = 0; id < PointerId.maxPointers; id++)
{
if (panel.GetCapturingElement(id) == overlay)
overlay.ReleasePointer(id);
}
}
overlay.RemoveFromHierarchy();
AccessibilityBridge.RequestImmediateResync();
}
Overlay = null;
}
protected abstract void BuildContent(VisualElement container);
protected TextField CreateTextField(string label, string name, string placeholder = "")
{
if (!string.IsNullOrEmpty(label))
{
var lbl = new Label(label);
lbl.AddToClassList("input-label");
lbl.AddToClassList("text-body-small");
Container.Add(lbl);
}
var field = new TextField();
field.name = name;
field.AddToClassList("input-field");
if (!string.IsNullOrEmpty(placeholder))
SetPlaceholder(field, placeholder);
return field;
}
protected static void SetPlaceholder(TextField field, string placeholder)
{
#if UNITY_2023_1_OR_NEWER
field.textEdition.placeholder = placeholder;
#endif
}
protected Button CreateConfirmButton(string text, Action onClick)
{
var btn = new Button(onClick);
btn.text = text;
btn.AddToClassList("dialog-confirm-button");
btn.AddToClassList("text-dialog-action");
AccessibilityBridge.RegisterE2ETapTarget(
btn,
() => btn.enabledInHierarchy,
onClick
);
return btn;
}
protected Button CreateCancelButton(string text = "Cancel")
{
var btn = new Button(Dismiss);
btn.text = text;
btn.AddToClassList("dialog-cancel-button");
btn.AddToClassList("text-dialog-action");
AccessibilityBridge.RegisterE2ETapTarget(btn, () => btn.enabledInHierarchy, Dismiss);
return btn;
}
}
}