Lomiri
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 
17 import QtQuick 2.12
18 import AccountsService 0.1
19 import GlobalShortcut 1.0
20 import QMenuModel 1.0
21 import QtMir.Application 0.1
22 
23 QtObject {
24  id: root
25 
26  // to be set from outside
27  property var focusedSurface: null
28 
29  property GlobalShortcut shortcutNext: GlobalShortcut {
30  shortcut: Qt.MetaModifier|Qt.Key_Space
31  onTriggered: root.nextKeymap()
32  active: root.keymapCount > 1
33  }
34 
35  property GlobalShortcut shortcutPrevious: GlobalShortcut {
36  shortcut: Qt.MetaModifier|Qt.ShiftModifier|Qt.Key_Space
37  onTriggered: root.previousKeymap()
38  active: root.keymapCount > 1
39  }
40 
41  readonly property var keymaps: AccountsService.keymaps
42  readonly property int keymapCount: keymaps.length
43  // default keymap, either the one remembered by the indicator, or the 1st one selected by user
44  property int currentKeymapIndex: actionGroup.currentAction.valid ? actionGroup.currentAction.state : 0
45  readonly property string currentKeymap: keymaps[currentKeymapIndex]
46 
47  function nextKeymap() {
48  var nextIndex = 0;
49 
50  if (currentKeymapIndex !== -1 && currentKeymapIndex < keymapCount - 1) {
51  nextIndex = currentKeymapIndex + 1;
52  }
53  currentKeymapIndex = nextIndex;
54  if (actionGroup.currentAction.valid) {
55  actionGroup.currentAction.updateState(currentKeymapIndex);
56  }
57  }
58 
59  function previousKeymap() {
60  var prevIndex = keymapCount - 1;
61 
62  if (currentKeymapIndex > 0) {
63  prevIndex = currentKeymapIndex - 1;
64  }
65  currentKeymapIndex = prevIndex;
66  if (actionGroup.currentAction.valid) {
67  actionGroup.currentAction.updateState(currentKeymapIndex);
68  }
69  }
70 
71  property Binding surfaceKeymapBinding: Binding { // NB: needed mainly for xmir & libertine apps
72  target: root.focusedSurface
73  property: "keymap"
74  value: root.currentKeymap
75  }
76 
77  property Binding lomiriKeymapBinding: Binding {
78  target: Mir
79  property: "currentKeymap"
80  value: root.currentKeymap
81  }
82 
83  // indicator
84  property QDBusActionGroup actionGroup: QDBusActionGroup {
85  busType: DBus.SessionBus
86  busName: "org.ayatana.indicator.keyboard"
87  objectPath: "/org/ayatana/indicator/keyboard"
88 
89  property variant currentAction: action("current") // the one that's checked by the indicator
90  property variant activeAction: action("active") // the one that we clicked
91 
92  Component.onCompleted: actionGroup.start();
93  }
94 
95  readonly property int activeActionState: actionGroup.activeAction.valid ? actionGroup.activeAction.state : -1
96 
97  onActiveActionStateChanged: {
98  if (activeActionState != -1) {
99  currentKeymapIndex = activeActionState;
100  }
101  }
102 }