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 Unity.Application 0.1
21 
22 QtObject {
23  id: root
24 
25  property GlobalShortcut shortcutNext: GlobalShortcut {
26  shortcut: Qt.MetaModifier|Qt.Key_Space
27  onTriggered: root.nextKeymap()
28  active: root.keymapCount > 1
29  }
30 
31  property GlobalShortcut shortcutPrevious: GlobalShortcut {
32  shortcut: Qt.MetaModifier|Qt.ShiftModifier|Qt.Key_Space
33  onTriggered: root.previousKeymap()
34  active: root.keymapCount > 1
35  }
36 
37  readonly property var keymaps: AccountsService.keymaps
38  readonly property int keymapCount: keymaps.length
39  property int currentKeymapIndex: 0
40  readonly property string currentKeymap: keymaps[currentKeymapIndex]
41 
42  function nextKeymap() {
43  var nextIndex = 0;
44 
45  if (currentKeymapIndex !== -1 && currentKeymapIndex < keymapCount - 1) {
46  nextIndex = currentKeymapIndex + 1;
47  }
48  currentKeymapIndex = nextIndex;
49  }
50 
51  function previousKeymap() {
52  var prevIndex = keymapCount - 1;
53 
54  if (currentKeymapIndex > 0) {
55  prevIndex = currentKeymapIndex - 1;
56  }
57  currentKeymapIndex = prevIndex;
58  }
59 
60  property Binding surfaceKeymapBinding: Binding {
61  target: MirFocusController.focusedSurface
62  property: "keymap"
63  value: root.currentKeymap
64  }
65 }