Lomiri
Loading...
Searching...
No Matches
KeymapSwitcher.qml
1/*
2 * Copyright (C) 2016 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17import QtQuick 2.15
18import QtQml 2.15
19import AccountsService 0.1
20import GlobalShortcut 1.0
21import QMenuModel 1.0
22import QtMir.Application 0.1
23
24QtObject {
25 id: root
26
27 // to be set from outside
28 property var focusedSurface: null
29
30 property GlobalShortcut shortcutNext: GlobalShortcut {
31 shortcut: Qt.MetaModifier|Qt.Key_Space
32 onTriggered: root.nextKeymap()
33 active: root.keymapCount > 1
34 }
35
36 property GlobalShortcut shortcutPrevious: GlobalShortcut {
37 shortcut: Qt.MetaModifier|Qt.ShiftModifier|Qt.Key_Space
38 onTriggered: root.previousKeymap()
39 active: root.keymapCount > 1
40 }
41
42 readonly property var keymaps: AccountsService.keymaps
43 readonly property int keymapCount: keymaps.length
44 // default keymap, either the one remembered by the indicator, or the 1st one selected by user
45 property int currentKeymapIndex: actionGroup.currentAction.valid ? actionGroup.currentAction.state : 0
46 readonly property string currentKeymap: keymaps[currentKeymapIndex]
47
48 function nextKeymap() {
49 var nextIndex = 0;
50
51 if (currentKeymapIndex !== -1 && currentKeymapIndex < keymapCount - 1) {
52 nextIndex = currentKeymapIndex + 1;
53 }
54 currentKeymapIndex = nextIndex;
55 if (actionGroup.currentAction.valid) {
56 actionGroup.currentAction.updateState(currentKeymapIndex);
57 }
58 }
59
60 function previousKeymap() {
61 var prevIndex = keymapCount - 1;
62
63 if (currentKeymapIndex > 0) {
64 prevIndex = currentKeymapIndex - 1;
65 }
66 currentKeymapIndex = prevIndex;
67 if (actionGroup.currentAction.valid) {
68 actionGroup.currentAction.updateState(currentKeymapIndex);
69 }
70 }
71
72 property Binding surfaceKeymapBinding: Binding { // NB: needed mainly for xmir & libertine apps
73 target: root.focusedSurface
74 property: "keymap"
75 value: root.currentKeymap
76 restoreMode: Binding.RestoreBinding
77 }
78
79 property Binding lomiriKeymapBinding: Binding {
80 target: Mir
81 property: "currentKeymap"
82 value: root.currentKeymap
83 restoreMode: Binding.RestoreBinding
84 }
85
86 // indicator
87 property QDBusActionGroup actionGroup: QDBusActionGroup {
88 busType: DBus.SessionBus
89 busName: "org.ayatana.indicator.keyboard"
90 objectPath: "/org/ayatana/indicator/keyboard"
91
92 property variant currentAction: action("current") // the one that's checked by the indicator
93 property variant activeAction: action("active") // the one that we clicked
94
95 Component.onCompleted: actionGroup.start();
96 }
97
98 readonly property int activeActionState: actionGroup.activeAction.valid ? actionGroup.activeAction.state : -1
99
100 onActiveActionStateChanged: {
101 if (activeActionState != -1) {
102 currentKeymapIndex = activeActionState;
103 }
104 }
105}