Repository navigation
Expand file tree
/
Copy pathNotchButton.qml
More file actions
198 lines (174 loc) · 7 KB
/
Copy pathNotchButton.qml
File metadata and controls
198 lines (174 loc) · 7 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
import QtQuick
import qs.Commons
import qs.Ui
// The panel's one button: at least a 44 px target (theme.target), a real
// accessible button with a name, the panel's focus ring (FocusRing.qml),
// and hover and pressed fills. Glyph only (a square icon button), text only, or both, with an
// optional key hint on the right ("o").
//
// NotchButton { theme: shell.theme; iconText: ""; text: "Open notch"; hint: "o"
// accessibleName: "Open notch"; onClicked: shell.openTui() }
//
// Children declared inside an instance draw over the surface, for a button
// with its own content (the hero's labels, a tab's underline).
//
// It doesn't take focus when clicked: the panel's keys go to the shell's key
// catcher, and a button holding focus would swallow them. Tab reaches it
// inside a form (where the key catcher is blocked), and Enter or Space then
// press it.
FocusScope {
id: root
required property NotchTheme theme
property string text: ""
property string iconText: ""
property string hint: ""
// A dim caption before the text (a tab's number key).
property string prefix: ""
// What a screen reader says. Defaults to the text; an icon-only button
// must set it.
property string accessibleName: text
property string accessibleDescription: ""
property string tooltipText: ""
// "plain" (no border), "bordered" (1 px line), "primary" (accent fill
// and border, as the hero's running stop button), "danger" (urgent text
// and border, for delete).
property string variant: "bordered"
property bool leftAlign: false
property bool busy: false
// For toggles (a chip, a tab): announced as checked, and drawn selected.
property bool checkable: false
property bool checked: false
property color foreground: variant === "danger" ? theme.errorText
: variant === "primary" ? theme.accentText : theme.text
// A checked toggle's fill and text (NotchChip draws a solid chip).
property color checkedFill: theme.selectedFill
property color checkedForeground: foreground
readonly property color _ink: checked ? checkedForeground : foreground
property real fontSize: theme.fontBody
property real iconSize: theme.fontIcon
property bool bold: false
// Square by default when there's no text.
property real minWidth: theme.target
property real minHeight: theme.target
signal clicked()
signal middleClicked()
readonly property bool hovered: mouse.containsMouse && enabled
readonly property bool pressed: mouse.pressed && enabled
// The keyboard cursor is on it without Qt focus (the confirm's selected
// button, a list's cursor row): drawn with the same ring as focus.
property bool hasCursor: false
readonly property bool showFocus: (activeFocus || hasCursor) && enabled
// Off for a button whose disabled state is still content worth reading
// at full strength (the hero's labels while idle).
property bool dimWhenDisabled: true
activeFocusOnTab: enabled
opacity: enabled || !dimWhenDisabled ? 1 : theme.disabledOpacity
implicitWidth: Math.max(minWidth, content.implicitWidth + (text !== "" ? Style.spacing.xxl * 2 : 0))
implicitHeight: Math.max(minHeight, content.implicitHeight + Style.spacing.controlPaddingY * 2)
// A tab sets Accessible.PageTab.
property int accessibleRole: checkable ? Accessible.CheckBox : Accessible.Button
Accessible.role: root.accessibleRole
Accessible.name: root.accessibleName
Accessible.description: root.accessibleDescription !== "" ? root.accessibleDescription : root.tooltipText
Accessible.checkable: root.checkable
Accessible.checked: root.checked
Accessible.focusable: true
Accessible.focused: root.activeFocus
Accessible.onPressAction: if (root.enabled) root.clicked()
Accessible.onToggleAction: if (root.enabled) root.clicked()
Keys.onReturnPressed: function(event) { if (root.enabled) { root.clicked(); event.accepted = true } }
Keys.onEnterPressed: function(event) { if (root.enabled) { root.clicked(); event.accepted = true } }
Keys.onSpacePressed: function(event) { if (root.enabled) { root.clicked(); event.accepted = true } }
Rectangle {
id: surface
anchors.fill: parent
radius: root.theme.radius
// A checked toggle keeps its fill under the pointer: its text is drawn
// for that fill (a chip's is the background colour).
color: root.checked ? root.checkedFill
: root.pressed ? root.theme.pressedFill
: root.hovered ? root.theme.hoverFill
: root.variant === "primary" ? root.theme.accentFill
: "transparent"
// Focused, FocusRing below draws the ring over this border.
border.width: root.variant === "plain" ? 0 : 1
border.color: root.variant === "primary" ? root.theme.accent
: root.variant === "danger" ? root.theme.errorText
: root.theme.line
Behavior on color { ColorAnimation { duration: 120 } }
}
FocusRing {
theme: root.theme
anchors.fill: parent
visible: root.showFocus
}
Row {
id: content
spacing: Style.spacing.controlGap
anchors.verticalCenter: parent.verticalCenter
anchors.left: root.leftAlign ? parent.left : undefined
anchors.leftMargin: root.leftAlign ? Style.spacing.xxl : 0
anchors.horizontalCenter: root.leftAlign ? undefined : parent.horizontalCenter
Text {
visible: root.prefix !== ""
textFormat: Text.PlainText
text: root.prefix
color: root.theme.dim
font.family: root.theme.fontFamily
font.pixelSize: root.theme.fontCaption
anchors.verticalCenter: parent.verticalCenter
Accessible.ignored: true
}
Text {
visible: root.iconText !== ""
textFormat: Text.PlainText
text: root.iconText
color: root._ink
font.family: root.theme.fontFamily
font.pixelSize: root.iconSize
anchors.verticalCenter: parent.verticalCenter
Accessible.ignored: true
}
Text {
visible: root.text !== ""
textFormat: Text.PlainText
text: root.busy ? root.text + "…" : root.text
color: root._ink
font.family: root.theme.fontFamily
font.pixelSize: root.fontSize
font.bold: root.bold || root.checked
anchors.verticalCenter: parent.verticalCenter
Accessible.ignored: true
}
}
// The key that does the same thing, at the right edge (the boards' "o").
Text {
visible: root.hint !== ""
textFormat: Text.PlainText
text: root.hint
color: root.theme.dim
font.family: root.theme.fontFamily
font.pixelSize: root.theme.fontCaption
anchors.right: parent.right
anchors.rightMargin: Style.spacing.xxl
anchors.verticalCenter: parent.verticalCenter
Accessible.ignored: true
}
MouseArea {
id: mouse
anchors.fill: parent
enabled: root.enabled
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
acceptedButtons: Qt.LeftButton | Qt.MiddleButton
onClicked: function(event) {
if (event.button === Qt.MiddleButton) root.middleClicked()
else root.clicked()
}
}
PanelToolTip {
visible: root.tooltipText !== "" && mouse.containsMouse
text: root.tooltipText
fontFamily: root.theme.fontFamily
}
}