Skip to content

Commit ad5c5fd

Browse files
committed
virtual keyboard: Add layout switch button.
1 parent 8240994 commit ad5c5fd

File tree

3 files changed

+88
-25
lines changed

3 files changed

+88
-25
lines changed

files/usr/share/cinnamon/applets/[email protected]/applet.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,6 @@ class CinnamonKeyboardApplet extends Applet.Applet {
148148
this.menu.toggle();
149149
}
150150

151-
_createFlagIcon(source, actorClass, size) {
152-
let actor = null;
153-
let name = source.flagName;
154-
155-
const file = Gio.file_new_for_path(KeyboardManager.getFlagFileName(name));
156-
if (file.query_exists(null)) {
157-
actor = new KeyboardManager.SubscriptableFlagIcon({
158-
style_class: actorClass,
159-
file: file,
160-
subscript: source.dupeId > 0 ? String(source.dupeId) : null,
161-
height: size,
162-
});
163-
}
164-
165-
return actor;
166-
}
167-
168151
_syncConfig() {
169152
this._layoutItems.forEach((v, k, m) => v.destroy());
170153
this._layoutItems = new Map()
@@ -177,7 +160,7 @@ class CinnamonKeyboardApplet extends Applet.Applet {
177160
let actor = null;
178161

179162
if (this._inputSourcesManager.showFlags) {
180-
actor = this._createFlagIcon(source, POPUP_MENU_ICON_STYLE_CLASS, 22 * global.ui_scale);
163+
actor = this._inputSourcesManager.createFlagIcon(source, POPUP_MENU_ICON_STYLE_CLASS, 22 * global.ui_scale);
181164
}
182165

183166
if (actor == null) {
@@ -219,7 +202,7 @@ class CinnamonKeyboardApplet extends Applet.Applet {
219202
const iconSize = this.getPanelIconSize(St.IconType.SYMBOLIC);
220203

221204
if (this._inputSourcesManager.showFlags) {
222-
actor = this._createFlagIcon(selected, APPLET_ICON_STYLE_CLASS, iconSize);
205+
actor = this._inputSourcesManager.createFlagIcon(selected, APPLET_ICON_STYLE_CLASS, iconSize);
223206
}
224207

225208
if (actor == null) {

js/ui/keyboardManager.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,23 @@ var InputSourceManager = class {
931931
get showFlags() {
932932
return this._interface_settings.get_boolean("keyboard-layout-show-flags");
933933
}
934+
935+
createFlagIcon(source, actorClass, size) {
936+
let actor = null;
937+
let name = source.flagName;
938+
939+
const file = Gio.file_new_for_path(getFlagFileName(name));
940+
if (file.query_exists(null)) {
941+
actor = new SubscriptableFlagIcon({
942+
style_class: actorClass,
943+
file: file,
944+
subscript: source.dupeId > 0 ? String(source.dupeId) : null,
945+
height: size,
946+
});
947+
}
948+
949+
return actor;
950+
}
934951
};
935952
Signals.addSignalMethods(InputSourceManager.prototype);
936953

js/ui/virtualKeyboard.js

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ const dir_keys = [{ keyval: Clutter.KEY_Left, label: '←',
3939
{ keyval: Clutter.KEY_Up, label: '↑', extraClassName: 'non-alpha-key' },
4040
{ keyval: Clutter.KEY_Down, label: '↓', extraClassName: 'non-alpha-key'},
4141
{ keyval: Clutter.KEY_Right, label: '→', extraClassName: 'non-alpha-key' }];
42+
const layout_key = { action: 'next-layout', icon: 'input-keyboard-symbolic', extraClassName: 'non-alpha-key' };
4243

4344
const defaultKeysPre = [
44-
[[escape_key], [tab_key], [{ width: 1.5, level: 1, extraClassName: 'shift-key-lowercase', icon: 'keyboard-shift-filled-symbolic' }], [_123_key]],
45-
[[escape_key], [tab_key], [{ width: 1.5, level: 0, extraClassName: 'shift-key-uppercase', icon: 'keyboard-shift-filled-symbolic' }], [_123_key]],
45+
[[escape_key], [tab_key], [{ width: 1.5, level: 1, extraClassName: 'shift-key-lowercase', icon: 'keyboard-shift-filled-symbolic' }], [layout_key, _123_key]],
46+
[[escape_key], [tab_key], [{ width: 1.5, level: 0, extraClassName: 'shift-key-uppercase', icon: 'keyboard-shift-filled-symbolic' }], [layout_key, _123_key]],
4647
[[escape_key], [tab_key], [{ label: '=/<', width: 1.5, level: 3, extraClassName: 'non-alpha-key' }], [abc_key]],
4748
[[escape_key], [tab_key], [{ label: '?123', width: 1.5, level: 2, extraClassName: 'non-alpha-key' }], [abc_key]],
4849
];
@@ -251,6 +252,13 @@ var Key = GObject.registerClass({
251252
this._longPress = false;
252253
}
253254

255+
updateKey(label, icon = null) {
256+
this.key = label || "";
257+
this.keyButton.destroy();
258+
this.keyButton = this._makeKey(this.key, icon);
259+
this.add_child(this.keyButton);
260+
}
261+
254262
_onDestroy() {
255263
if (this._boxPointer) {
256264
this._boxPointer.destroy();
@@ -384,9 +392,14 @@ var Key = GObject.registerClass({
384392
});
385393

386394
if (icon) {
387-
let child = new St.Icon({ icon_name: icon });
388-
button.set_child(child);
389-
this._icon = child;
395+
if (icon instanceof Clutter.Actor) {
396+
button.set_child(icon);
397+
this._icon = icon;
398+
} else {
399+
let child = new St.Icon({ icon_name: icon });
400+
button.set_child(child);
401+
this._icon = child;
402+
}
390403
} else {
391404
let label = GLib.markup_escape_text(key, -1);
392405
button.set_label(label);
@@ -468,6 +481,29 @@ var Key = GObject.registerClass({
468481
}
469482
});
470483

484+
var ActiveGroupKey = GObject.registerClass({}, class ActiveGroupKey extends Key {
485+
_init(controller) {
486+
this._controller = controller;
487+
let [shortName, icon] = this._controller.getCurrentGroupLabelIcon();
488+
489+
super._init(shortName, [], icon);
490+
this._groupChangedId = this._controller.connect('active-group', this._onGroupChanged.bind(this));
491+
this.connect('destroy', this._onDestroy.bind(this));
492+
}
493+
494+
_onGroupChanged(id) {
495+
let [shortName, icon] = this._controller.getCurrentGroupLabelIcon();
496+
this.updateKey(shortName, icon);
497+
}
498+
499+
_onDestroy() {
500+
if (this._groupChangedId > 0) {
501+
this._controller.disconnect(this._groupChangedId);
502+
this._groupChangedId = 0;
503+
}
504+
}
505+
});
506+
471507
var KeyboardModel = class {
472508
constructor(groupName) {
473509
let names = [groupName];
@@ -1006,7 +1042,11 @@ class Keyboard extends St.BoxLayout {
10061042
let action = key.action;
10071043
let icon = key.icon;
10081044

1009-
extraButton = new Key(key.label || '', [], icon);
1045+
if (action === 'next-layout') {
1046+
extraButton = new ActiveGroupKey(this._keyboardController);
1047+
} else {
1048+
extraButton = new Key(key.label || '', [], icon);
1049+
}
10101050

10111051
// extraButton.keyButton.add_style_class_name('default-key');
10121052
if (key.extraClassName != null)
@@ -1030,6 +1070,8 @@ class Keyboard extends St.BoxLayout {
10301070
this._keyboardController.keyvalRelease(keyval);
10311071
else if (action == 'hide')
10321072
this.close();
1073+
else if (action == 'next-layout')
1074+
this._keyboardController.activateNextGroup();
10331075
});
10341076

10351077
if (switchToLevel == 0) {
@@ -1458,6 +1500,27 @@ var KeyboardController = class {
14581500

14591501
getIbusInputActive() {
14601502
return this._currentSource.type === "ibus";
1503+
1504+
activateNextGroup() {
1505+
let new_index = this._inputSourceManager.currentSource.index + 1;
1506+
if (new_index == this._inputSourceManager.numInputSources) {
1507+
new_index = 0;
1508+
}
1509+
1510+
this._inputSourceManager.activateInputSourceIndex(new_index);
1511+
}
1512+
1513+
getCurrentGroupLabelIcon() {
1514+
let actor = null;
1515+
1516+
if (this._inputSourceManager.showFlags) {
1517+
actor = this._inputSourceManager.createFlagIcon(this._currentSource, null, 16);
1518+
}
1519+
1520+
if (actor == null) {
1521+
return [this._currentSource.shortName, null];
1522+
}
1523+
return [null, actor];
14611524
}
14621525

14631526
commitString(string, fromKey) {

0 commit comments

Comments
 (0)