ThemedTextField.qml
2.05 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
import QtQuick
import QtQuick.Controls.Basic
import "../" as App
TextField {
id: control
implicitHeight: App.Theme.controlHLg
leftPadding: 14
rightPadding: 14
font.family: App.Theme.fontFamily
font.pointSize: App.Theme.fontBase
color: App.Theme.textPrimary
placeholderTextColor: App.Theme.textTertiary
selectionColor: App.Theme.accent
selectedTextColor: App.Theme.textOnAccent
background: Rectangle {
radius: App.Theme.radiusMd
color: App.Theme.bgSurface
border.width: control.activeFocus ? 2 : 1
border.color: control.activeFocus
? App.Theme.accent
: App.Theme.borderDefault
Behavior on border.color {
ColorAnimation { duration: 100 }
}
}
// 右键弹编辑菜单(剪切 / 复制 / 粘贴 / 全选)
// 密码框 (echoMode === TextInput.Password) 时禁用 复制 / 剪切 防泄漏
Menu {
id: editMenu
MenuItem {
text: "剪切"
enabled: control.selectedText.length > 0
&& control.echoMode !== TextInput.Password
onTriggered: control.cut()
}
MenuItem {
text: "复制"
enabled: control.selectedText.length > 0
&& control.echoMode !== TextInput.Password
onTriggered: control.copy()
}
MenuItem {
text: "粘贴"
enabled: control.canPaste
onTriggered: control.paste()
}
MenuSeparator {}
MenuItem {
text: "全选"
enabled: control.length > 0
onTriggered: control.selectAll()
}
}
// TapHandler 在 TextField 内部不可靠(内嵌 TextInput 会先 grab 鼠标),
// 改用 MouseArea + acceptedButtons:Qt.RightButton —
// 未列出的 LeftButton 会自动透传给下方 TextInput,不影响光标 / 选区。
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton
cursorShape: Qt.IBeamCursor
onClicked: editMenu.popup()
}
}