Repository navigation
Expand file tree
/
Copy pathPanel.qml
More file actions
419 lines (373 loc) · 13 KB
/
Copy pathPanel.qml
File metadata and controls
419 lines (373 loc) · 13 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Quickshell
import qs.Commons
import qs.Ui
import "Model.js" as Model
Panel {
id: root
moduleName: "dingyi.omaportless"
manageIpc: false
property var anchorItem: null
property var hostWidget: null
property var omaportless: null
readonly property var barIdentity: hostWidget || root
property bool cursorActive: false
property int cursorIndex: 0
property int nameFocusCount: 0
readonly property color foreground: bar ? bar.foreground : Color.foreground
readonly property color urgent: bar ? bar.urgent : Color.urgent
readonly property color dim: Qt.darker(foreground, 1.55)
readonly property string fontFamily: bar ? bar.fontFamily : Style.font.family
readonly property var services: omaportless ? (omaportless.services || []) : []
readonly property bool proxyRunning: omaportless ? omaportless.proxyRunning : false
readonly property var targets: {
var list = ["power"]
for (var i = 0; i < services.length; i++) {
list.push("name" + i)
list.push("open" + i)
list.push("close" + i)
}
list.push("index")
return list
}
readonly property string cursorTarget: {
if (!cursorActive) return ""
if (cursorIndex < 0 || cursorIndex >= targets.length) return ""
return targets[cursorIndex]
}
function open() {
if (omaportless) omaportless.refresh()
root.controller.show()
}
function close() {
root.controller.hide()
}
function toggle() {
if (root.opened) root.close()
else root.open()
}
function switchPanel(direction) {
if (root.bar && typeof root.bar.switchPanelFrom === "function")
return root.bar.switchPanelFrom(root.barIdentity, direction)
return false
}
function clampCursor() {
if (targets.length === 0) { cursorIndex = 0; return }
if (cursorIndex < 0) cursorIndex = 0
if (cursorIndex > targets.length - 1) cursorIndex = targets.length - 1
}
function moveCursor(dx, dy) {
cursorActive = true
clampCursor()
if (dy !== 0) cursorIndex = Math.max(0, Math.min(targets.length - 1, cursorIndex + dy))
}
function activateCursor() {
clampCursor()
var target = cursorTarget
if (!omaportless) return
if (target === "power") omaportless.toggleProxy()
else if (target === "index") omaportless.openIndex()
else if (target.indexOf("open") === 0) {
var index = parseInt(target.slice(4), 10)
if (services[index]) omaportless.openUrl(services[index].url)
} else if (target.indexOf("close") === 0) {
var index = parseInt(target.slice(5), 10)
if (services[index]) omaportless.closeService(services[index].id)
}
}
function commitName(service, hostname) {
if (!omaportless || !service) return
var next = String(hostname || "").trim().toLowerCase()
if (next === service.hostname && service.pinned) return
if (next === "") omaportless.unsetName(service.id)
else omaportless.setName(service.id, next)
}
onOpenedChanged: if (opened) {
cursorActive = false
cursorIndex = 0
if (panelFlick) panelFlick.contentY = 0
if (omaportless) omaportless.refresh()
Qt.callLater(function() { keyCatcher.forceActiveFocus() })
}
onTargetsChanged: clampCursor()
KeyboardPanel {
id: panel
anchorItem: root.anchorItem
owner: root.hostWidget || root
bar: root.bar
open: root.opened
focusTarget: keyCatcher
contentWidth: panel.fittedContentWidth(Style.space(420))
contentHeight: panel.fittedContentHeight(column.implicitHeight + Style.space(8), Style.space(640))
PanelKeyCatcher {
id: keyCatcher
anchors.fill: parent
blocked: root.nameFocusCount > 0
onMoveRequested: function(dx, dy) {
if (!root.cursorActive) { root.cursorActive = true; return }
root.moveCursor(dx, dy)
}
onActivateRequested: if (root.cursorActive) root.activateCursor()
onCloseRequested: root.close()
onTabRequested: function(direction) { root.switchPanel(direction) }
onTextKey: function(text) {
var key = String(text || "").toLowerCase()
if (!omaportless) return
if (key === "t") omaportless.toggleProxy()
else if (key === "o") omaportless.openIndex()
else if (key === "r") omaportless.refresh()
}
Flickable {
id: panelFlick
anchors.fill: parent
contentWidth: width
contentHeight: column.implicitHeight
clip: true
boundsBehavior: Flickable.StopAtBounds
flickableDirection: Flickable.VerticalFlick
interactive: contentHeight > height
ScrollBar.vertical: ScrollBar { policy: ScrollBar.AsNeeded }
Column {
id: column
width: panelFlick.width
spacing: Style.space(12)
Item {
width: parent.width
implicitHeight: hero.implicitHeight
PanelHero {
id: hero
width: parent.width
title: "omaportless"
meta: Model.metaLabel(omaportless ? omaportless.status : Model.emptyStatus())
detail: ""
foreground: root.foreground
fontFamily: root.fontFamily
iconOpacity: root.proxyRunning ? 1.0 : 0.5
iconComponent: Component {
PortlessIcon {
iconSize: Style.font.display
color: root.foreground
pulse: root.proxyRunning
}
}
trailingControl: Component {
ToggleSwitch {
id: powerSwitch
checked: root.proxyRunning
busy: omaportless ? omaportless.busy : false
hasCursor: root.cursorTarget === "power"
foreground: hero.foreground
onHovered: function(on) {
if (!on) return
root.cursorActive = true
root.cursorIndex = root.targets.indexOf("power")
}
onToggled: if (omaportless) omaportless.toggleProxy()
PanelToolTip {
visible: powerSwitch.containsMouse
text: root.proxyRunning ? "Stop the proxy" : "Start the proxy"
fontFamily: hero.fontFamily
}
}
}
}
}
Text {
visible: notice !== ""
width: parent.width
property string notice: {
if (!omaportless) return ""
if (omaportless.actionStatus !== "") return omaportless.actionStatus
if (omaportless.lastError !== "") return omaportless.lastError
return ""
}
text: notice
color: omaportless && omaportless.lastError !== "" && omaportless.actionStatus === "" ? root.urgent : root.dim
font.family: root.fontFamily
font.pixelSize: Style.font.bodySmall
wrapMode: Text.WordWrap
}
Text {
visible: services.length === 0 && (!omaportless || !omaportless.lastError)
width: parent.width
text: "No local servers"
color: root.dim
font.family: root.fontFamily
font.pixelSize: Style.font.bodySmall
}
Repeater {
id: nameRepeater
model: services
delegate: ServiceRow {
required property var modelData
required property int index
width: column.width
service: modelData
rowIndex: index
}
}
Button {
width: parent.width
text: "Open localhost"
iconText: ""
bordered: true
foreground: root.foreground
fontFamily: root.fontFamily
hasCursor: root.cursorTarget === "index"
onHovered: function(on) {
if (!on) return
root.cursorActive = true
root.cursorIndex = root.targets.indexOf("index")
}
onClicked: if (omaportless) omaportless.openIndex()
}
}
}
}
}
component ServiceRow: Column {
id: serviceRow
property var service: ({})
property int rowIndex: 0
readonly property string nameTarget: "name" + rowIndex
readonly property string openTarget: "open" + rowIndex
readonly property string closeTarget: "close" + rowIndex
readonly property string pathText: Model.cwdLabel(service.cwd)
property bool nameFocused: nameField.activeFocus
spacing: Style.space(4)
width: parent ? parent.width : implicitWidth
onNameFocusedChanged: {
if (nameFocused) root.nameFocusCount += 1
else root.nameFocusCount = Math.max(0, root.nameFocusCount - 1)
}
function openService(mouse) {
if (!omaportless) return
if (mouse && mouse.button === Qt.RightButton) omaportless.copyUrl(serviceRow.service.url)
else omaportless.openUrl(serviceRow.service.url)
}
RowLayout {
width: parent.width
spacing: Style.space(16)
TextField {
id: nameField
Layout.fillWidth: true
placeholderText: "name"
foreground: root.foreground
verticalPadding: Style.space(6)
hasCursor: root.cursorTarget === serviceRow.nameTarget
Component.onCompleted: text = serviceRow.service.hostname || ""
onHoveredChanged: if (hovered) {
root.cursorActive = true
root.cursorIndex = Math.max(0, root.targets.indexOf(serviceRow.nameTarget))
}
onEditingFinished: root.commitName(serviceRow.service, text)
Keys.onReturnPressed: {
root.commitName(serviceRow.service, text)
keyCatcher.forceActiveFocus()
}
}
Text {
id: suffixLabel
text: ".localhost"
color: root.dim
font.family: root.fontFamily
font.pixelSize: Style.font.body
Layout.alignment: Qt.AlignVCenter
horizontalAlignment: Text.AlignRight
MouseArea {
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: function(mouse) { serviceRow.openService(mouse) }
}
}
Text {
id: portText
text: serviceRow.service.port ? String(serviceRow.service.port) : ""
color: root.dim
font.family: root.fontFamily
font.pixelSize: Style.font.body
Layout.alignment: Qt.AlignVCenter
Layout.preferredWidth: portMetrics.width
horizontalAlignment: Text.AlignRight
TextMetrics {
id: portMetrics
font: portText.font
text: "65535"
}
MouseArea {
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: function(mouse) { serviceRow.openService(mouse) }
}
}
Item {
id: closeBtn
Layout.preferredWidth: Style.space(32)
Layout.preferredHeight: nameField.implicitHeight
Layout.alignment: Qt.AlignVCenter
readonly property bool hot: closeHover.hovered || root.cursorTarget === serviceRow.closeTarget
HoverHandler {
id: closeHover
cursorShape: Qt.PointingHandCursor
}
TapHandler {
acceptedButtons: Qt.LeftButton
onTapped: if (omaportless) omaportless.closeService(serviceRow.service.id)
}
Rectangle {
anchors.fill: parent
radius: Style.cornerRadius
color: closeBtn.hot ? Qt.rgba(root.urgent.r, root.urgent.g, root.urgent.b, 0.16) : "transparent"
}
Text {
anchors.centerIn: parent
text: ""
color: closeBtn.hot ? root.urgent : root.dim
font.family: root.fontFamily
font.pixelSize: Style.font.icon
}
}
}
Connections {
target: serviceRow
function onServiceChanged() {
if (!nameField.activeFocus)
nameField.text = serviceRow.service.hostname || ""
}
}
Item {
id: openRow
width: parent.width
implicitHeight: pathTextItem.implicitHeight + Style.space(6)
visible: serviceRow.pathText !== ""
HoverHandler {
id: pathHover
cursorShape: Qt.PointingHandCursor
}
TapHandler {
acceptedButtons: Qt.LeftButton
onTapped: if (omaportless) omaportless.openUrl(serviceRow.service.url)
}
TapHandler {
acceptedButtons: Qt.RightButton
onTapped: if (omaportless) omaportless.copyUrl(serviceRow.service.url)
}
Text {
id: pathTextItem
width: parent.width
anchors.verticalCenter: parent.verticalCenter
text: serviceRow.pathText
color: pathHover.hovered || root.cursorTarget === serviceRow.openTarget ? root.foreground : root.dim
font.family: root.fontFamily
font.pixelSize: Style.font.caption
elide: Text.ElideMiddle
}
}
}
}