Skip to content

Commit c486045

Browse files
committed
Adapt keyboard applet, fix display modes, dupe ids.
1 parent 688c06d commit c486045

File tree

6 files changed

+161
-184
lines changed

6 files changed

+161
-184
lines changed

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

Lines changed: 67 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Util = imports.misc.util;
77
const Gio = imports.gi.Gio;
88
const Cairo = imports.cairo;
99
const Signals = imports.signals;
10+
const KeyboardManager = imports.ui.keyboardManager;
1011

1112
const PANEL_EDIT_MODE_KEY = "panel-edit-mode";
1213

@@ -112,120 +113,18 @@ class LayoutMenuItem extends PopupMenu.PopupBaseMenuItem {
112113
}
113114
}
114115

115-
class KbdLayoutController {
116-
constructor() {
117-
this._bus_watch_id = 0;
118-
this._layouts = [];
119-
this._current_layout_idx = 0;
120-
}
121-
122-
applet_added() {
123-
this._xappController = new XApp.KbdLayoutController();
124-
this._xappController.connect('layout-changed', () => this._on_layout_changed());
125-
this._xappController.connect('config-changed', () => this._on_config_changed());
126-
127-
if (this._bus_watch_id === 0) {
128-
const on_ibus = (is_active) => {
129-
this._ibus_active = is_active;
130-
this.emit("layout-changed");
131-
this.emit("config-changed");
132-
};
133-
this._bus_watch_id = Gio.DBus.session.watch_name(
134-
"org.fcitx.Fcitx", Gio.BusNameWatcherFlags.NONE,
135-
() => on_ibus(true), () => on_ibus(false)
136-
);
137-
}
138-
139-
this._on_config_changed();
140-
}
141-
142-
applet_removed() {
143-
if (this._bus_watch_id > 0) {
144-
Gio.DBus.session.unwatch_name(this._bus_watch_id);
145-
this._bus_watch_id = 0;
146-
}
147-
}
148-
149-
_retrieve_current_layout_idx() {
150-
let idx = this._xappController.get_enabled() ?
151-
this._xappController.get_current_group() : 0;
152-
if (idx >= this.get_layouts_count()) {
153-
this.set_current_layout_idx(0);
154-
} else {
155-
this._current_layout_idx = idx;
156-
}
157-
}
158-
159-
_on_layout_changed() {
160-
this._retrieve_current_layout_idx();
161-
this.emit("layout-changed");
162-
}
163-
164-
_on_config_changed() {
165-
if (this._xappController.get_enabled()) {
166-
const layouts = [];
167-
168-
const groups = this._xappController.get_all_names();
169-
for (let i = 0; i < groups.length; i++) {
170-
layouts.push({
171-
display_name: groups[i],
172-
flag_name: this._xappController.get_icon_name_for_group(i),
173-
layout_dupe_id: this._xappController.get_flag_id_for_group(i),
174-
short_name: this._xappController.get_short_group_label_for_group(i),
175-
variant_name: this._xappController.get_variant_label_for_group(i),
176-
});
177-
}
178-
this._layouts = layouts;
179-
this._retrieve_current_layout_idx();
180-
} else {
181-
this._layouts = [];
182-
this._current_layout_idx = 0;
183-
}
184-
185-
this.emit("config-changed");
186-
}
187-
188-
have_multiple_layouts() {
189-
return (this.get_layouts_count() > 1) || false;
190-
}
191-
192-
get_layouts_count() {
193-
return this._layouts?.length || 0;
194-
}
195-
196-
get_current_layout_idx() {
197-
return this._current_layout_idx || 0;
198-
}
199-
200-
set_current_layout_idx(idx) {
201-
if (this._xappController.get_enabled()) {
202-
this._xappController.set_current_group(idx);
203-
this._current_layout_idx = idx;
204-
}
205-
}
206-
207-
get_layout_data(idx) {
208-
const layouts = this._layouts || [];
209-
return layouts[idx] || {};
210-
}
211-
212-
is_ibus_active() {
213-
return !!this._ibus_active;
214-
}
215-
}
216-
Signals.addSignalMethods(KbdLayoutController.prototype);
217-
218116
class CinnamonKeyboardApplet extends Applet.TextIconApplet {
219117
constructor(metadata, orientation, panel_height, instance_id) {
220118
super(orientation, panel_height, instance_id);
221119

222120
this.setAllowedLayout(Applet.AllowedLayout.BOTH);
223121

224-
this._layoutItems = [ ];
225-
this._layoutIcons = [ ];
122+
this._layoutItems = new Map();
123+
this._layoutIcons = new Map();
226124

227125
this._maxSeenWidth = this._maxSeenHeight = 0;
228126
this.show_flags = this.use_upper = this.use_variants = false;
127+
this._bus_watch_id = 0
229128

230129
try {
231130
this.metadata = metadata;
@@ -242,28 +141,38 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
242141
const _syncConfig = () => this._syncConfig();
243142

244143
this.desktop_settings.connect("changed::keyboard-layout-show-flags", _syncConfig);
245-
this.desktop_settings.connect("changed::keyboard-layout-use-upper", _syncConfig);
246-
this.desktop_settings.connect("changed::keyboard-layout-prefer-variant-names", _syncConfig);
247144
global.settings.connect('changed::' + PANEL_EDIT_MODE_KEY, () => this._onPanelEditModeChanged());
248145

249146
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
250147
this.menu.addAction(_("Show Keyboard Layout"), () => {
251148
Main.overview.hide();
252-
Util.spawn(['gkbd-keyboard-display', '-g', String(this._controller.get_current_layout_idx() + 1)]);
149+
global.log
150+
Util.spawn(['gkbd-keyboard-display', '-g', String(this._manager.currentSource.index + 1)]);
253151
});
254152
this.menu.addAction(_("Show Character Table"), () => {
255153
Main.overview.hide();
256154
Util.spawn(['gucharmap']);
257155
});
258156
this.menu.addSettingsAction(_("Keyboard Settings"), 'keyboard');
259157

260-
this._controller = new KbdLayoutController();
158+
this._manager = KeyboardManager.getInputSourceManager();
159+
this._manager.connect("sources-changed", this._onSourcesChanged.bind(this));
160+
this._manager.connect("current-source-changed", this._onCurrentSourceChanged.bind(this));
161+
this._syncConfig();
261162
}
262163
catch (e) {
263164
global.logError(e);
264165
}
265166
}
266167

168+
_onCurrentSourceChanged() {
169+
this._syncGroup();
170+
}
171+
172+
_onSourcesChanged() {
173+
this._syncConfig();
174+
}
175+
267176
_onPanelEditModeChanged() {
268177
if (global.settings.get_boolean(PANEL_EDIT_MODE_KEY)) {
269178
if (!this.actor.visible) {
@@ -279,10 +188,17 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
279188
if (global.settings.get_boolean(PANEL_EDIT_MODE_KEY)) {
280189
this._onPanelEditModeChanged();
281190
}
282-
this._controller.connect('layout-changed', () => this._syncGroup());
283-
this._controller.connect('config-changed', () => this._syncConfig());
284191
this.connect('orientation-changed', () => this.on_orientation_changed());
285-
this._controller.applet_added();
192+
193+
if (this._bus_watch_id === 0) {
194+
const on_ibus = (is_active) => {
195+
this.actor.visible = !is_active;
196+
};
197+
this._bus_watch_id = Gio.DBus.session.watch_name(
198+
"org.fcitx.Fcitx", Gio.BusNameWatcherFlags.NONE,
199+
() => on_ibus(true), () => on_ibus(false)
200+
);
201+
}
286202
}
287203

288204
on_orientation_changed() {
@@ -293,10 +209,14 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
293209
_onButtonPressEvent(actor, event) {
294210
// Cycle to the next layout
295211
if (event.get_button() === 2) {
296-
const selected_group = this._controller.get_current_layout_idx();
297-
const new_group = (selected_group + 1) % this._layoutItems.length;
298-
this._controller.set_current_layout_idx(new_group);
212+
let new_index = this._manager.currentSource.index + 1;
213+
if (new_index == this._manager.numInputSources) {
214+
new_index = 0;
215+
}
216+
217+
this._manager.activateInputSourceIndex(new_index);
299218
}
219+
300220
return Applet.Applet.prototype._onButtonPressEvent.call(this, actor, event);
301221
}
302222

@@ -306,34 +226,33 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
306226

307227
_setLayoutItems(items) {
308228
this._selectedLayout = null;
309-
this._layoutItems.forEach(item => item.destroy());
310-
this._layoutItems = items || [];
229+
230+
this._layoutItems.forEach((v, k, m) => v.destroy());
231+
this._layoutItems = items || new Map();
311232
}
312233

313234
_setLayoutIcons(icons) {
314-
this._layoutIcons = icons || [];
235+
this._layoutIcons = icons || new Map();
315236
}
316237

317-
_createIcon(layoutIndex, actorClass) {
318-
const layoutInfo = this._controller.get_layout_data(layoutIndex);
319-
238+
_createIcon(source, actorClass) {
320239
let isFlagIcon = false;
321240
let iconActor = null;
322241
let iconInstance = null;
323-
let name = layoutInfo.flag_name;
242+
let name = source.flagName;
324243

325244
if (this.show_flags) {
326245
const file = Gio.file_new_for_path(getFlagFileName(name));
327246
if (file.query_exists(null)) {
328-
iconInstance = new EmblemedIcon(file.get_path(), layoutInfo.layout_dupe_id, actorClass);
247+
iconInstance = new EmblemedIcon(file.get_path(), source.dupeId, actorClass);
329248
iconActor = iconInstance.actor;
330249
isFlagIcon = true;
331250
}
332251
}
333252

334253
if (!isFlagIcon) {
335-
name = this.use_variants ? layoutInfo.variant_name : layoutInfo.short_name;
336-
name = this.use_upper ? name.toUpperCase() : name;
254+
255+
name = source.shortName;
337256
iconActor = new St.Label({ text: name });
338257
}
339258

@@ -347,43 +266,36 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
347266
_syncConfig() {
348267
this._maxSeenWidth = this._maxSeenHeight = 0;
349268

350-
if (!this._controller.have_multiple_layouts()) {
269+
if (!this._manager.multipleSources) {
351270
this._setLayoutItems([]);
352271
this.menu.close();
353272
this.actor.hide();
354273
return;
355274
}
356275

357-
const layoutItems = [];
358-
const layoutIcons = [];
276+
const layoutItems = new Map();
277+
const layoutIcons = new Map();
359278

360279
this.show_flags = this.desktop_settings.get_boolean("keyboard-layout-show-flags");
361-
this.use_upper = this.desktop_settings.get_boolean("keyboard-layout-use-upper");
362-
this.use_variants = this.desktop_settings.get_boolean("keyboard-layout-prefer-variant-names");
363280

364281
if (this.show_flags) {
365282
this._maxSeenWidth = this._maxSeenHeight = 0;
366283
}
367284

368285
this.actor.show();
369286

370-
const layoutsCount = this._controller.get_layouts_count();
371-
372-
for (let i = 0; i < layoutsCount; i++) {
373-
const idx = i;
374-
375-
const layoutInfo = this._controller.get_layout_data(idx);
287+
for (const sourceId of Object.keys(this._manager.inputSources)) {
288+
const source = this._manager.inputSources[sourceId];
376289

377-
const popupIconInfo = this._createIcon(idx, POPUP_MENU_ICON_STYLE_CLASS);
290+
const popupIconInfo = this._createIcon(source, POPUP_MENU_ICON_STYLE_CLASS);
378291
const menuItem = new LayoutMenuItem(
379-
() => this._controller.set_current_layout_idx(idx),
380-
popupIconInfo.iconActor, layoutInfo.display_name
292+
() => source.activate(),
293+
popupIconInfo.iconActor, source.displayName
381294
);
382-
layoutItems.push(menuItem);
383-
384-
this.menu.addMenuItem(menuItem, idx);
385-
const appletIconInfo = this._createIcon(idx, APPLET_ICON_STYLE_CLASS);
386-
layoutIcons.push(appletIconInfo);
295+
layoutItems.set(source, menuItem);
296+
this.menu.addMenuItem(menuItem);
297+
const appletIconInfo = this._createIcon(source, APPLET_ICON_STYLE_CLASS);
298+
layoutIcons.set(source, appletIconInfo);
387299
}
388300

389301
this._setLayoutItems(layoutItems);
@@ -393,9 +305,9 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
393305
}
394306

395307
_syncGroup() {
396-
const selected = this._controller.get_current_layout_idx();
308+
const selected = this._manager.currentSource;
397309

398-
if (!this._layoutItems.length) {
310+
if (!this._manager.multipleSources) {
399311
this.actor.hide();
400312
return;
401313
}
@@ -405,20 +317,17 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
405317
this._selectedLayout = null;
406318
}
407319

408-
const item = this._layoutItems[selected];
320+
const item = this._layoutItems.get(selected);
409321
item.setShowDot(true);
410322

411323
this._selectedLayout = item;
412-
413-
const layoutInfo = this._controller.get_layout_data(selected);
414-
415-
this.set_applet_tooltip(layoutInfo.display_name);
324+
this.set_applet_tooltip(selected.displayName);
416325

417326
const _applet_label_box = this._applet_label.get_parent();
418327
this._setMargin(_applet_label_box, 0, 0);
419328
this._setMargin(this._applet_icon_box, 0, 0);
420329

421-
const {name, iconActor, iconInstance, isFlagIcon} = this._layoutIcons[selected];
330+
const {name, iconActor, iconInstance, isFlagIcon} = this._layoutIcons.get(selected);
422331
if (isFlagIcon) {
423332
this._applet_icon = iconInstance;
424333
this._applet_icon_box.set_child(iconActor);
@@ -448,12 +357,15 @@ class CinnamonKeyboardApplet extends Applet.TextIconApplet {
448357
if (isFlagIcon) {
449358
this._setStyle();
450359
}
451-
452-
this.im_running ? this.actor.hide() : this.actor.show();
453360
}
454361

455362
on_applet_removed_from_panel() {
456-
this._controller.applet_removed();
363+
if (this._bus_watch_id > 0) {
364+
Gio.DBus.session.unwatch_name(this._bus_watch_id);
365+
this._bus_watch_id = 0;
366+
}
367+
368+
// TODO disconnect ISM signals.
457369

458370
Main.systrayManager.unregisterTrayIconReplacement(this.metadata.uuid);
459371
}

0 commit comments

Comments
 (0)