Unity 8
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.4
18 import AccountsService 0.1
19 import GlobalShortcut 1.0
20 import QMenuModel 0.1
21 import Unity.Application 0.1
22 
23 QtObject {
24  id: root
25 
26  property GlobalShortcut shortcutNext: GlobalShortcut {
27  shortcut: Qt.MetaModifier|Qt.Key_Space
28  onTriggered: root.nextKeymap()
29  active: root.keymapCount > 1
30  }
31 
32  property GlobalShortcut shortcutPrevious: GlobalShortcut {
33  shortcut: Qt.MetaModifier|Qt.ShiftModifier|Qt.Key_Space
34  onTriggered: root.previousKeymap()
35  active: root.keymapCount > 1
36  }
37 
38  readonly property var keymaps: AccountsService.keymaps
39  readonly property int keymapCount: keymaps.length
40  // default keymap, either the one remembered by the indicator, or the 1st one selected by user
41  property int currentKeymapIndex: actionGroup.currentAction.valid ? actionGroup.currentAction.state : 0
42  readonly property string currentKeymap: keymaps[currentKeymapIndex]
43 
44  function nextKeymap() {
45  var nextIndex = 0;
46 
47  if (currentKeymapIndex !== -1 && currentKeymapIndex < keymapCount - 1) {
48  nextIndex = currentKeymapIndex + 1;
49  }
50  currentKeymapIndex = nextIndex;
51  }
52 
53  function previousKeymap() {
54  var prevIndex = keymapCount - 1;
55 
56  if (currentKeymapIndex > 0) {
57  prevIndex = currentKeymapIndex - 1;
58  }
59  currentKeymapIndex = prevIndex;
60  }
61 
62  property Binding surfaceKeymapBinding: Binding {
63  target: MirFocusController.focusedSurface
64  property: "keymap"
65  value: root.currentKeymap
66  }
67 
68  // indicator
69  property QDBusActionGroup actionGroup: QDBusActionGroup {
70  busType: DBus.SessionBus
71  busName: "com.canonical.indicator.keyboard"
72  objectPath: "/com/canonical/indicator/keyboard"
73 
74  property variant currentAction: action("current")
75  property variant activeAction: action("active")
76 
77  Component.onCompleted: actionGroup.start();
78  }
79 
80  onCurrentKeymapIndexChanged: {
81  actionGroup.currentAction.updateState(currentKeymapIndex);
82  }
83 
84  readonly property int activeActionState: actionGroup.activeAction.valid ? actionGroup.activeAction.state : -1
85 
86  onActiveActionStateChanged: {
87  if (activeActionState != -1) {
88  currentKeymapIndex = activeActionState;
89  }
90  }
91 }