-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTextArea.qml
72 lines (62 loc) · 1.63 KB
/
TextArea.qml
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
// Version 7
import QtQuick
import QtQuick.Controls as QQC2
import QtQuick.Layouts
import org.kde.kirigami as Kirigami
QQC2.TextArea {
id: textArea
property string configKey: ''
readonly property var configValue: configKey ? plasmoid.configuration[configKey] : ""
onConfigValueChanged: deserialize()
onTextChanged: serializeTimer.restart()
wrapMode: TextArea.Wrap
Kirigami.FormData.labelAlignment: Qt.AlignTop
// An empty TextArea adjust to it's empty contents.
// So we need the TextArea to be wide enough.
Layout.fillWidth: true
// Since QQC2 defaults to implicitWidth=contentWidth, a really long
// line in TextArea will cause a binding loop on FormLayout.width
// when we only set fillWidth=true.
// Setting an implicitWidth fixes this and allows the text to wrap.
implicitWidth: Kirigami.Units.gridUnit * 20
// Load
function deserialize() {
if (configKey) {
var newText = valueToText(configValue)
setText(newText)
}
}
function valueToText(value) {
return value
}
function setText(newText) {
if (textArea.text != newText) {
if (textArea.focus) {
// TODO: Find cursor in newText and replace text before + after cursor.
} else {
textArea.text = newText
}
}
}
// Save
function serialize() {
var newValue = textToValue(textArea.text)
setConfigValue(newValue)
}
function textToValue(text) {
return text
}
function setConfigValue(newValue) {
if (configKey) {
var oldValue = plasmoid.configuration[configKey]
if (oldValue != newValue) {
plasmoid.configuration[configKey] = newValue
}
}
}
Timer { // throttle
id: serializeTimer
interval: 300
onTriggered: serialize()
}
}