Repository navigation
Expand file tree
/
Copy pathServerList.qml
More file actions
310 lines (282 loc) · 10.5 KB
/
Copy pathServerList.qml
File metadata and controls
310 lines (282 loc) · 10.5 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import QtQuick
import QtQuick.Controls
import qs.Commons
import qs.Ui
// The one scrolling server surface, shared by the bar panel and the app
// window's Locations: a ListView so only visible rows own delegates (a
// hidden surface binds `active: false`, empties the model and owns none).
// The panel's pre-server content scrolls as `preamble` above the rows, its
// menu/key hints as `tail` in the footer — one continuous scroll either way.
// State and actions stay on `host` (the Widget), so both surfaces show the
// same list, cursor and pings.
//
// The header content (preamble, Quick Connect, SERVERS/Ping, filter) is a
// DIRECT CHILD of the view, not the `header:` Component: the view recreates
// its header item on every model rebuild, which destroyed the filter's
// focus mid-keystroke (the rest of the input landed on the key catcher and
// fired actions). A direct child is never recreated; the empty `header:`
// spacer below just reserves its height so delegates start underneath.
ListView {
id: list
property var host: null
// false while the panel/window is hidden: the model is [] and no
// ServerRow delegates exist
property bool active: false
property Component preamble: null
property Component tail: null
readonly property alias filterField: filterInput
readonly property bool filterFocused: filterInput.activeFocus
// where Esc in the filter hands the keyboard back to
property Item keyTarget: null
// the app window's list: "+ Import" and right-click profiles
property bool inWindow: false
// tabs/sidebar with one protocol picked: its "+ Import" sits by Ping
readonly property var pickedProvider: inWindow && host.rowsLayout !== "tree" && host.providerFilter !== ""
? host.providerInfo(host.providerFilter) : null
model: active ? host.serverRows.length : 0
delegate: ServerRow {
required property int index
width: list.width
// ListView (unlike the old Column) doesn't fall back to implicitHeight
height: implicitHeight
host: list.host
gate: pointerGate
inWindow: list.inWindow
row: list.host.serverRows[index]
rowIndex: index
}
spacing: Style.space(2)
clip: true
boundsBehavior: Flickable.StopAtBounds
interactive: contentHeight > height
ScrollBar.vertical: ScrollBar { policy: ScrollBar.AsNeeded }
// Typing stays local to the field; Enter applies the filter (the only
// model rebuild while the field is focused — no more typed characters
// need it), a second Enter activates the row. Esc clears and hands focus
// back. reopening resyncs a field whose binding was broken by typing
onActiveChanged: if (active) Qt.callLater(function() {
filterInput.text = list.host.serverFilter
})
// Editing breaks the initial text binding; mirror only committed changes
// so simultaneously visible fields follow the shared applied filter.
Connections {
target: list.host
function onServerFilterChanged() { filterInput.text = list.host.serverFilter }
}
function resetPointer() { pointerGate.reset() }
function focusFilter() { filterInput.forceActiveFocus() }
function scrollToRow(index) {
if (index < 0) return
// callLater: the model may just have changed, delegates still settling
Qt.callLater(function() { positionViewAtIndex(index, ListView.Contain) })
}
// Replacing a JavaScript-array model resets ListView to its origin. Keep the
// first visible row (or a clicked header) at the same viewport offset while
// folding groups or applying a refreshed --servers response.
property var savedViewport: null
property bool viewportRestorePending: false
function rememberViewport(preferredKey) {
if (!active || count === 0) {
savedViewport = null
return
}
var key = String(preferredKey || "")
var item = null
if (key !== "") {
var preferred = host.rowIndex(key)
if (preferred >= 0) item = itemAtIndex(preferred)
}
if (!item) {
var bestY = Infinity
for (var i = 0; i < count; i++) {
var candidate = itemAtIndex(i)
if (!candidate || candidate.y + candidate.height <= contentY || candidate.y >= contentY + height)
continue
if (candidate.y < bestY) {
item = candidate
key = host.rowIdentity(host.serverRows[i])
bestY = candidate.y
}
}
}
savedViewport = item
? { key: key, offset: item.y - contentY, contentY: contentY }
: { key: "", offset: 0, contentY: contentY }
}
function boundedContentY(value) {
return Math.max(originY, Math.min(originY + Math.max(0, contentHeight - height), value))
}
function restoreViewport() {
if (!active) {
savedViewport = null
return
}
if (!savedViewport || viewportRestorePending) return
viewportRestorePending = true
Qt.callLater(function() {
viewportRestorePending = false
if (!active) {
savedViewport = null
return
}
var saved = savedViewport
if (!saved) return
var index = saved.key !== "" ? host.rowIndex(saved.key) : -1
if (index < 0) {
contentY = boundedContentY(saved.contentY)
savedViewport = null
return
}
positionViewAtIndex(index, ListView.Visible)
Qt.callLater(function() {
if (savedViewport !== saved) return
var item = itemAtIndex(index)
contentY = boundedContentY(item ? item.y - saved.offset : saved.contentY)
savedViewport = null
})
})
}
// Hover moves the cursor only when the pointer itself moved: a rebuilt or
// scrolled list slides rows under a still pointer, and each one used to
// grab the cursor (the filter jumped it to row 27). Measured against the
// viewport, so scrolling isn't movement.
PointerMoveGate {
id: pointerGate
referenceItem: list
}
// Empty spacer: reserves exactly the stable header's height so delegates
// begin below it. Gap math: the view adds `spacing` (2) after the header;
// the old Column had 12 around the list.
header: Item {
width: list.width
height: head.implicitHeight + Style.space(12) - list.spacing
}
// The stable header follows the scroll header's content position; parenting
// alone leaves it at y=0 when ListView changes its non-zero origin.
// It survives model rebuilds, keeping filter focus.
Column {
id: head
parent: list.contentItem
y: list.headerItem ? list.headerItem.y : 0
z: 1 // above the spacer and freshly created delegates
width: list.width
spacing: Style.space(12)
Loader {
// follows the loaded Column's height (its root binds height:
// implicitHeight), so a late-appearing setup card still grows
width: parent.width
sourceComponent: list.preamble
}
QuickConnect {
width: parent.width
host: list.host
}
Item {
width: parent.width
implicitHeight: Math.max(serversHeader.implicitHeight, pingButton.implicitHeight)
PanelSectionHeader {
id: serversHeader
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
text: "SERVERS"
foreground: list.host.foreground
fontFamily: list.host.fontFamily
}
Text {
id: headerImport
visible: !!list.pickedProvider && !!list.pickedProvider.importer
anchors.right: pingButton.left
anchors.rightMargin: Style.space(12)
anchors.verticalCenter: parent.verticalCenter
textFormat: Text.PlainText
text: "+ Import"
color: headerImportMouse.containsMouse ? list.host.foreground : list.host.dim
font.family: list.host.fontFamily
font.pixelSize: Style.font.caption
MouseArea {
id: headerImportMouse
anchors.fill: parent
enabled: headerImport.visible
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: list.host.startImport(list.pickedProvider)
}
}
Text {
id: pingButton
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
textFormat: Text.PlainText
text: "\uf021" // nf-fa-refresh
color: pingMouse.containsMouse ? list.host.foreground : list.host.dim
font.family: list.host.fontFamily
font.pixelSize: Style.font.caption
}
MouseArea {
id: pingMouse
anchors.fill: pingButton
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: list.host.run("--ping-now")
}
PanelToolTip {
visible: pingMouse.containsMouse
text: list.host.pingActivity !== "" ? "Measuring: " + list.host.pingActivity
: "Measure every server's delay through xray"
fontFamily: list.host.fontFamily
}
}
TextField {
id: filterInput
width: parent.width
foreground: list.host.foreground
placeholderText: "Filter servers — press /, Enter to apply"
text: list.host.serverFilter
onAccepted: {
if (text !== list.host.serverFilter) {
list.host.keyboardCursor = true
list.host.serverCursor = 0
list.host.serverFilter = text // final op: rebuilds the model
return
}
list.host.activateServerRow(list.host.serverRows[Math.max(0, list.host.serverCursor)])
}
Keys.onPressed: function(event) {
if (event.key === Qt.Key_Down) { list.host.moveServerCursor(1); event.accepted = true }
else if (event.key === Qt.Key_Up) { list.host.moveServerCursor(-1); event.accepted = true }
else if (event.key === Qt.Key_Escape) {
text = "" // local first…
if (list.keyTarget) list.keyTarget.forceActiveFocus() // …then focus…
list.host.keyboardCursor = false
list.host.serverCursor = -1
list.host.serverFilter = "" // …then the rebuild-triggering write
event.accepted = true
}
}
}
Text {
visible: list.host.serverRows.length === 0
width: parent.width
textFormat: Text.PlainText
text: list.host.serverFilter !== "" ? "No servers match."
: (list.host.serversLoading ? "Loading servers…" : "No servers.")
color: list.host.dim
font.family: list.host.fontFamily
font.pixelSize: Style.font.bodySmall
horizontalAlignment: Text.AlignHCenter
}
}
footer: Item {
width: list.width
height: list.tail ? foot.implicitHeight + Style.space(12) - list.spacing : 0
Column {
id: foot
width: parent.width
spacing: Style.space(12)
Loader {
width: parent.width
sourceComponent: list.tail
}
}
}
}