Repository navigation
Expand file tree
/
Copy pathMultiPairInputDialog.cs
More file actions
208 lines (177 loc) · 6.86 KB
/
Copy pathMultiPairInputDialog.cs
File metadata and controls
208 lines (177 loc) · 6.86 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using OneSignalDemo.Services;
using OneSignalDemo.UI;
using UnityEngine;
using UnityEngine.UIElements;
namespace OneSignalDemo.UI.Dialogs
{
public class MultiPairInputDialog : DialogBase
{
private readonly string _title;
private readonly string _keyLabel;
private readonly string _valueLabel;
private readonly string _confirmText;
private readonly Action<Dictionary<string, string>> _onConfirm;
private readonly List<(TextField key, TextField value, VisualElement row)> _rows = new();
private VisualElement _rowsContainer;
private Button _confirmButton;
private bool _submitted;
private double _lastAddRowMs = -1.0;
private const double AddRowDedupeMs = 500.0;
// Monotonic — we cannot derive ids from _rows.Count because RemoveRow
// does not renumber survivors, so a new row would collide with an
// existing name and AccessibilityBridge.WalkAndUpsert (first-wins by
// name) would silently drop it from the a11y tree.
private int _nextRowIndex;
public MultiPairInputDialog(
string title,
string keyLabel,
string valueLabel,
string confirmText,
Action<Dictionary<string, string>> onConfirm
)
{
_title = title;
_keyLabel = keyLabel;
_valueLabel = valueLabel;
_confirmText = confirmText;
_onConfirm = onConfirm;
}
protected override void BuildContent(VisualElement container)
{
var title = new Label(_title);
title.AddToClassList("dialog-title");
title.AddToClassList("text-dialog-title");
container.Add(title);
_rowsContainer = new VisualElement();
container.Add(_rowsContainer);
AddRow();
var addRowButton = new Button(InvokeAddRow);
addRowButton.name = "multipair_add_row_button";
addRowButton.AddToClassList("dialog-add-row-button");
AccessibilityBridge.RegisterE2ETapTarget(
addRowButton,
() => addRowButton.enabledInHierarchy,
InvokeAddRow
);
var addIcon = new Label(MaterialIcons.Add);
addIcon.AddToClassList("dialog-add-row-icon");
addRowButton.Add(addIcon);
var addLabel = new Label("Add Row");
addLabel.AddToClassList("dialog-add-row-label");
addRowButton.Add(addLabel);
container.Add(addRowButton);
var actions = new VisualElement();
actions.AddToClassList("dialog-actions");
actions.Add(CreateCancelButton());
_confirmButton = CreateConfirmButton(_confirmText, OnConfirm);
_confirmButton.name = "multipair_confirm_button";
_confirmButton.SetEnabled(false);
actions.Add(_confirmButton);
container.Add(actions);
}
private void AddRow()
{
if (_rows.Count > 0)
{
var divider = new VisualElement();
divider.AddToClassList("divider");
_rowsContainer.Add(divider);
}
var row = new VisualElement();
row.AddToClassList("dialog-row");
var rowIndex = _nextRowIndex++;
var keyField = new TextField();
keyField.name = $"multipair_key_{rowIndex}";
keyField.AddToClassList("input-field");
keyField.AddToClassList("dialog-field-group-left");
SetPlaceholder(keyField, _keyLabel);
keyField.RegisterValueChangedCallback(_ => ValidateAll());
var valueField = new TextField();
valueField.name = $"multipair_value_{rowIndex}";
valueField.AddToClassList("input-field");
valueField.AddToClassList("dialog-field-group");
SetPlaceholder(valueField, _valueLabel);
valueField.RegisterValueChangedCallback(_ => ValidateAll());
row.Add(keyField);
row.Add(valueField);
var entry = (keyField, valueField, row);
var deleteBtn = new Button(() => RemoveRow(entry));
deleteBtn.text = MaterialIcons.Close;
deleteBtn.AddToClassList("dialog-row-delete");
row.Add(deleteBtn);
_rows.Add(entry);
_rowsContainer.Add(row);
UpdateDeleteVisibility();
ValidateAll();
}
private void InvokeAddRow()
{
double now = Time.realtimeSinceStartupAsDouble * 1000.0;
if (_lastAddRowMs >= 0.0 && now - _lastAddRowMs < AddRowDedupeMs)
return;
_lastAddRowMs = now;
AddRow();
AccessibilityBridge.RequestImmediateResync();
}
private void RemoveRow((TextField key, TextField value, VisualElement row) entry)
{
_rows.Remove(entry);
_rowsContainer.Clear();
for (int i = 0; i < _rows.Count; i++)
{
if (i > 0)
{
var divider = new VisualElement();
divider.AddToClassList("divider");
_rowsContainer.Add(divider);
}
_rowsContainer.Add(_rows[i].row);
}
UpdateDeleteVisibility();
ValidateAll();
}
private void UpdateDeleteVisibility()
{
foreach (var (_, _, row) in _rows)
{
var del = row.Q<Button>(className: "dialog-row-delete");
if (del != null)
del.style.display = _rows.Count > 1 ? DisplayStyle.Flex : DisplayStyle.None;
}
}
private void ValidateAll()
{
bool allValid = _rows.Count > 0;
foreach (var (key, value, _) in _rows)
{
if (string.IsNullOrWhiteSpace(key.value) || string.IsNullOrWhiteSpace(value.value))
{
allValid = false;
break;
}
}
_confirmButton?.SetEnabled(allValid);
}
private void OnConfirm()
{
if (_submitted)
return;
var dict = new Dictionary<string, string>();
foreach (var (key, value, _) in _rows)
{
var trimmedKey = key.value?.Trim();
var trimmedValue = value.value?.Trim();
if (!string.IsNullOrEmpty(trimmedKey) && !string.IsNullOrEmpty(trimmedValue))
dict[trimmedKey] = trimmedValue;
}
if (dict.Count == 0)
return;
_submitted = true;
_confirmButton?.SetEnabled(false);
_onConfirm?.Invoke(dict);
Dismiss();
}
}
}