forked from linuxdeepin/deepin-fcitx5configtool-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortcutsModule.qml
More file actions
353 lines (317 loc) · 14.3 KB
/
ShortcutsModule.qml
File metadata and controls
353 lines (317 loc) · 14.3 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
// SPDX-FileCopyrightText: 2024 - 2026 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
import QtQuick 2.15
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.15
import org.deepin.dtk 1.0 as D
import org.deepin.dcc 1.0
DccObject {
id: root
// Config path constants
readonly property string cfgGroup: "Hotkey"
readonly property string cfgEnumKey: "EnumerateForwardKeys"
readonly property string cfgTriggerKey: "TriggerKeys"
readonly property string cfgEnum0: cfgGroup + "/" + cfgEnumKey + "/0"
readonly property string cfgEnum1: cfgGroup + "/" + cfgEnumKey + "/1"
readonly property string cfgTrigger0: cfgGroup + "/" + cfgTriggerKey + "/0"
readonly property string cfgTrigger1: cfgGroup + "/" + cfgTriggerKey + "/1"
readonly property var enumKeys: ["Ctrl+Shift", "Alt+Shift", "None"]
readonly property var enumKeysI18n: ["Ctrl+Shift", "Alt+Shift", qsTr("None")]
readonly property var triggerEnumKeys: ["Shift", "Ctrl+Space", "None"]
readonly property var triggerEnumKeysI18n: ["Shift", "Ctrl+Space", qsTr("None")]
property var triggerKeys: dccData.fcitx5ConfigProxy.globalConfigOption(
cfgGroup, cfgTriggerKey)
// 改为受控属性,避免等值赋值不触发刷新
property int enumerateForwardKeys: -1
property int currentTriggerKeys: -1
property bool isUserChanging: false
function calculateEnumerateForwardKeys(value) {
for (var i = 0; i < value.length; i++) {
value[i] = String(value[i]).toUpperCase().replace("META", "SUPER")
if (value[i].endsWith("_L") || value[i].endsWith("_R")) {
value[i] = value[i].slice(0, -2)
}
}
let formattedValue = value.length > 0 ? value.join("_") : ""
if (formattedValue === "CTRL_SHIFT") formattedValue = "Ctrl+Shift"
if (formattedValue === "ALT_SHIFT") formattedValue = "Alt+Shift"
if (formattedValue === "") formattedValue = "None" // 空配置映射到 "None"
return enumKeys.indexOf(formattedValue)
}
function calculateTriggerKeys(value) {
// 获取完整的TriggerKeys配置
let triggerKeys0 = dccData.fcitx5ConfigProxy.globalConfigOption(cfgGroup, cfgTriggerKey).value
let triggerKeys1 = []
try {
// 尝试获取第二个TriggerKey
let triggerKeysObj = dccData.fcitx5ConfigProxy.globalConfigOption(cfgGroup, cfgTriggerKey)
if (triggerKeysObj && triggerKeysObj.hasOwnProperty("1")) {
triggerKeys1 = triggerKeysObj["1"] || []
}
} catch(e) {
console.warn("Could not get TriggerKeys/1:", e)
}
// 检查是否是分别设置的左右Shift
let hasShiftL = (triggerKeys0 && triggerKeys0.includes && triggerKeys0.includes("Shift_L")) ||
(triggerKeys1 && triggerKeys1.includes && triggerKeys1.includes("Shift_L"))
let hasShiftR = (triggerKeys0 && triggerKeys0.includes && triggerKeys0.includes("Shift_R")) ||
(triggerKeys1 && triggerKeys1.includes && triggerKeys1.includes("Shift_R"))
if (hasShiftL || hasShiftR) {
console.log("CASE: Detected separate Shift keys, returning Shift (0)")
return 0 // "Shift"
}
// 优先使用triggerKeys0的数据进行判断,而不是value参数
let actualKeys = triggerKeys0 || []
// 如果TriggerKeys/0配置真正为空或未定义,这是初始状态,默认使用Shift
if (!actualKeys || actualKeys.length === 0) {
console.log("CASE: Empty TriggerKeys/0, returning Shift (0)")
return 0 // "Shift" - 初始默认
}
// 如果是用户主动设置的None(fcitx5格式为[""])
if (actualKeys.length === 1 && actualKeys[0] === "") {
console.warn("CASE: User selected None, returning None (2)")
return 2 // "None" - 用户主动选择的None
}
let keyStrings = actualKeys.map(key => String(key))
// 检查是否是Ctrl+Space组合
let isCtrlSpace = keyStrings.length === 2 &&
(keyStrings.includes("Ctrl") || keyStrings.includes("Control") || keyStrings.includes("Control_L")) &&
(keyStrings.includes("Space") || keyStrings.includes("space"))
if (isCtrlSpace) {
console.warn("CASE: Detected Ctrl+Space, returning Ctrl+Space (1)")
return 1 // "Ctrl+Space"
}
return 2 // "None"
}
function reverseTriggerKeys(index) {
console.warn("=== reverseTriggerKeys ===")
console.warn("Input index:", index)
let result
switch(index) {
case 0:
result = "Shift_L Shift_R"
break
case 1:
result = ["Ctrl", "Space"]
break
case 2:
result = [""] // "None"
break
default:
result = ["Shift_L", "Shift_R"]
break
}
return result
}
// title
DccObject {
name: "ShortcutsTitle"
parentName: "Shortcuts"
displayName: qsTr("Shortcuts")
weight: 210
pageType: DccObject.Item
page: RowLayout {
DccTitle {
Layout.leftMargin: 10
text: dccObj.displayName
}
D.Button {
id: resetBtn
text: qsTr("Restore Defaults")
Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
Layout.rightMargin: 10
background: null
textColor: D.Palette {
normal: D.DTK.makeColor(D.Color.Highlight)
}
font.pixelSize: D.DTK.fontManager.t8.pixelSize
onClicked: {
dccData.fcitx5ConfigProxy.restoreDefault(cfgGroup)
console.log("Restore Defaults button clicked")
}
}
}
}
Component.onCompleted: {
// 设置翻译后的显示数组
enumKeysDisplay = ["Ctrl+Shift", "Alt+Shift", qsTr("None")]
triggerEnumKeysDisplay = ["Shift", "Ctrl+Space", qsTr("None")]
let enumInitOpt = dccData.fcitx5ConfigProxy.globalConfigOption(cfgGroup, cfgEnumKey)
let enumInitVal = enumInitOpt && enumInitOpt.value ? enumInitOpt.value : []
let enumInitIdx = calculateEnumerateForwardKeys(enumInitVal)
let triggerInitOpt = dccData.fcitx5ConfigProxy.globalConfigOption(cfgGroup, cfgTriggerKey)
let triggerInitVal = triggerInitOpt && triggerInitOpt.value ? triggerInitOpt.value : []
let triggerInitIdx = calculateTriggerKeys(triggerInitVal)
currentTriggerKeys = triggerInitIdx
}
// 使用 Connections 绑定信号,确保对象替换/重载后仍能收到
Connections {
target: dccData.fcitx5ConfigProxy
function onRequestConfigFinished() {
let fullTriggerKeys = dccData.fcitx5ConfigProxy.globalConfigOption(cfgGroup, cfgTriggerKey)
let enumValue = dccData.fcitx5ConfigProxy.globalConfigOption(cfgGroup, cfgEnumKey).value
let newTriggerIdx = calculateTriggerKeys(fullTriggerKeys.value)
if (!isUserChanging) {
triggerKeys = fullTriggerKeys
if (currentTriggerKeys === newTriggerIdx) {
currentTriggerKeys = -1
Qt.callLater(() => { currentTriggerKeys = newTriggerIdx })
} else {
currentTriggerKeys = newTriggerIdx
}
} else {
console.warn("[QML] Skipping set triggerKeys (user changing)")
}
const idxEnum = calculateEnumerateForwardKeys(enumValue)
if (enumerateForwardKeys === idxEnum) {
enumerateForwardKeys = -1
Qt.callLater(() => { enumerateForwardKeys = idxEnum })
} else {
enumerateForwardKeys = idxEnum
}
}
}
// Always returns [binding0, binding1] where each binding is an array.
// binding1 is [""] when no second binding is needed.
function reverseEnumerateForwardKeys(index) {
if (index < 0 || index >= enumKeys.length) {
return [[""], [""]]
}
let value = enumKeys[index]
// Ctrl+Shift needs two key bindings to cover both press orders:
// Binding 0: press Ctrl then Shift_L triggers forward switch
// Binding 1: press Ctrl+Shift then Ctrl_L triggers forward switch
// Without both bindings, only one press order works.
if (value === "Ctrl+Shift") {
return [["Control", "Shift_L"], ["Control", "Shift", "Control_L"]]
}
if (value === "Alt+Shift") {
return [["Alt", "Shift_L"], [""]]
}
if (value === "None") {
return [[""], [""]]
}
// Fallback for unknown enum values
return [[value], [""]]
}
// Shortcut of scroll IM
DccObject {
name: "scrollIM"
parentName: "Shortcuts"
weight: 220
displayName: qsTr("Scroll between input methods")
backgroundType: DccObject.Normal
pageType: DccObject.Editor
page: D.ComboBox {
id: comboBox
flat: true
model: enumKeysI18n
currentIndex: enumerateForwardKeys
FontMetrics { id: enumFm; font: comboBox.font }
topPadding: 4
bottomPadding: 6
implicitHeight: enumFm.height + topPadding + bottomPadding
clip: false
// 恢复默认 delegate,由主题负责显示选中项勾号
Component.onCompleted: {
if (root.enumerateForwardKeys === -1) {
try {
let ev = dccData.fcitx5ConfigProxy.globalConfigOption(cfgGroup, cfgEnumKey).value || []
let eidx = calculateEnumerateForwardKeys(ev)
console.warn("[QML][page] init enum from backend idx=", eidx)
root.enumerateForwardKeys = eidx
} catch (e) {
console.warn("[QML][page] init enum failed:", e)
}
}
}
// 只在用户激活选择时写回,避免程序化更新触发写回
onActivated: {
let reversed = reverseEnumerateForwardKeys(currentIndex)
dccData.fcitx5ConfigProxy.setValue(
cfgEnum0,
reversed[0], true)
dccData.fcitx5ConfigProxy.setValue(
cfgEnum1,
reversed[1], true)
}
}
}
// Shortcut of turn on or off
DccObject {
name: "turnGrouup"
parentName: "Shortcuts"
weight: 230
pageType: DccObject.Item
page: DccGroupView {}
DccObject {
name: "shortcutSetting"
parentName: "Shortcuts/turnGrouup"
displayName: qsTr("Turn on or off input methods")
weight: 231
pageType: DccObject.Editor
page: D.ComboBox {
id: triggerComboBox
flat: true
model: triggerEnumKeysI18n
currentIndex: currentTriggerKeys
FontMetrics { id: triggerFm; font: triggerComboBox.font }
topPadding: 4
bottomPadding: 6
implicitHeight: triggerFm.height + topPadding + bottomPadding
clip: false
// 恢复默认 delegate,由主题负责显示选中项勾号
Component.onCompleted: {
console.warn("[QML][page] trigger ComboBox created: idx=", triggerComboBox.currentIndex,
" root.trigger=", root.currentTriggerKeys)
if (root.currentTriggerKeys === -1) {
try {
let tv = dccData.fcitx5ConfigProxy.globalConfigOption(cfgGroup, cfgTriggerKey).value || []
let tidx = calculateTriggerKeys(tv)
console.warn("[QML][page] init trigger from backend idx=", tidx)
root.currentTriggerKeys = tidx
} catch (e) {
console.warn("[QML][page] init trigger failed:", e)
}
}
}
onCurrentIndexChanged: {
console.warn("[QML] trigger onCurrentIndexChanged idx=", currentIndex, " text=", model[currentIndex])
isUserChanging = true
if (currentIndex === 0) {
console.warn("[QML] write TriggerKeys -> Shift_L + Shift_R")
dccData.fcitx5ConfigProxy.setValue(cfgTrigger0, ["Shift_L"], true)
dccData.fcitx5ConfigProxy.setValue(cfgTrigger1, ["Shift_R"], true)
} else {
let newKeys = reverseTriggerKeys(currentIndex)
console.warn("[QML] write TriggerKeys ->", JSON.stringify(newKeys))
dccData.fcitx5ConfigProxy.setValue(cfgTrigger0, newKeys, true)
dccData.fcitx5ConfigProxy.setValue(cfgTrigger1, [""], true)
}
Qt.callLater(() => { isUserChanging = false })
}
}
}
DccObject {
name: "shortcutSettingDesc"
parentName: "Shortcuts/turnGrouup"
weight: 232
pageType: DccObject.Item
page: Label {
topPadding: 5
bottomPadding: 5
leftPadding: 10
rightPadding: 10
Layout.fillWidth: true
font: D.DTK.fontManager.t8
opacity: 0.5
wrapMode: Text.Wrap
text: qsTr(
"It turns on or off the currently used input method."
+ "If no input method is being used or the first input "
+ "method is not the keyboard, it switches between the "
+ "first input method and the currently used keyboard/input method.")
}
}
}
}