42
45
this._readyCallback = readyCallback;
43
46
this._candidatePopup = new IBusCandidatePopup.CandidatePopup();
46
48
this._panelService = null;
47
49
this._engines = {};
48
50
this._ready = false;
49
51
this._registerPropertiesId = 0;
52
this._currentEngineName = null;
51
this._nameWatcherId = Gio.DBus.session.watch_name(IBus.SERVICE_IBUS,
52
Gio.BusNameWatcherFlags.NONE,
53
Lang.bind(this, this._onNameAppeared),
54
Lang.bind(this, this._clear));
54
this._ibus = IBus.Bus.new_async();
55
this._ibus.connect('connected', Lang.bind(this, this._onConnected));
56
this._ibus.connect('disconnected', Lang.bind(this, this._clear));
57
// Need to set this to get 'global-engine-changed' emitions
58
this._ibus.set_watch_ibus_signal(true);
59
this._ibus.connect('global-engine-changed', Lang.bind(this, this._engineChanged));
57
62
_clear: function() {
58
63
if (this._panelService)
59
64
this._panelService.destroy();
64
66
this._panelService = null;
65
67
this._candidatePopup.setPanelService(null);
66
68
this._engines = {};
67
69
this._ready = false;
68
70
this._registerPropertiesId = 0;
71
this._currentEngineName = null;
71
_onNameAppeared: function() {
72
this._ibus = IBus.Bus.new_async();
73
this._ibus.connect('connected', Lang.bind(this, this._onConnected));
73
if (this._readyCallback)
74
this._readyCallback(false);
76
77
_onConnected: function() {
103
101
this._panelService = new IBus.PanelService({ connection: this._ibus.get_connection(),
104
102
object_path: IBus.PATH_PANEL });
105
103
this._candidatePopup.setPanelService(this._panelService);
106
// Need to set this to get 'global-engine-changed' emitions
107
this._ibus.set_watch_ibus_signal(true);
108
this._ibus.connect('global-engine-changed', Lang.bind(this, this._resetProperties));
109
104
this._panelService.connect('update-property', Lang.bind(this, this._updateProperty));
110
this._resetProperties();
105
// If an engine is already active we need to get its properties
106
this._ibus.get_global_engine_async(-1, null, Lang.bind(this, function(i, result) {
109
engine = this._ibus.get_global_engine_async_finish(result);
115
this._engineChanged(this._ibus, engine.get_name());
117
this._updateReadiness();
116
this._updateReadiness();
119
123
_updateReadiness: function() {
120
124
this._ready = (Object.keys(this._engines).length > 0 &&
121
125
this._panelService != null);
123
if (this._ready && this._readyCallback)
124
this._readyCallback();
127
if (this._readyCallback)
128
this._readyCallback(this._ready);
127
_resetProperties: function() {
128
this.emit('properties-registered', null);
131
_engineChanged: function(bus, engineName) {
135
this._currentEngineName = engineName;
130
137
if (this._registerPropertiesId != 0)
184
const InputSource = new Lang.Class({
187
_init: function(type, id, displayName, shortName, index) {
190
this.displayName = displayName;
191
this._shortName = shortName;
194
this._menuItem = new LayoutMenuItem(this.displayName, this._shortName);
195
this._menuItem.connect('activate', Lang.bind(this, this.activate));
196
this._indicatorLabel = new St.Label({ text: this._shortName });
198
this.properties = null;
201
destroy: function() {
202
this._menuItem.destroy();
203
this._indicatorLabel.destroy();
207
return this._shortName;
212
this._menuItem.indicator.set_text(v);
213
this._indicatorLabel.set_text(v);
217
return this._menuItem;
220
get indicatorLabel() {
221
return this._indicatorLabel;
224
activate: function() {
225
this.emit('activate');
228
Signals.addSignalMethods(InputSource.prototype);
230
const InputSourcePopup = new Lang.Class({
231
Name: 'InputSourcePopup',
232
Extends: SwitcherPopup.SwitcherPopup,
234
_init: function(items, action, actionBackward) {
237
this._action = action;
238
this._actionBackward = actionBackward;
241
_createSwitcher: function() {
242
this._switcherList = new InputSourceSwitcher(this._items);
246
_initialSelection: function(backward, binding) {
247
if (binding == 'switch-input-source') {
249
this._selectedIndex = this._items.length - 1;
250
} else if (binding == 'switch-input-source-backward') {
252
this._selectedIndex = this._items.length - 1;
254
this._select(this._selectedIndex);
257
_keyPressHandler: function(keysym, backwards, action) {
258
if (action == this._action)
259
this._select(backwards ? this._previous() : this._next());
260
else if (action == this._actionBackward)
261
this._select(backwards ? this._next() : this._previous());
262
else if (keysym == Clutter.Left)
263
this._select(this._previous());
264
else if (keysym == Clutter.Right)
265
this._select(this._next());
268
_finish : function() {
271
this._items[this._selectedIndex].activate();
275
const InputSourceSwitcher = new Lang.Class({
276
Name: 'InputSourceSwitcher',
277
Extends: SwitcherPopup.SwitcherList,
279
_init: function(items) {
282
for (let i = 0; i < items.length; i++)
283
this._addIcon(items[i]);
286
_addIcon: function(item) {
287
let box = new St.BoxLayout({ vertical: true });
289
let bin = new St.Bin({ style_class: 'input-source-switcher-symbol' });
290
let symbol = new St.Label({ text: item.shortName });
291
bin.set_child(symbol);
292
box.add(bin, { x_fill: false, y_fill: false } );
294
let text = new St.Label({ text: item.displayName });
295
box.add(text, { x_fill: false });
297
this.addItem(box, text);
184
301
const InputSourceIndicator = new Lang.Class({
185
302
Name: 'InputSourceIndicator',
186
303
Extends: PanelMenu.Button,
188
_propertiesWhitelist: [
194
305
_init: function() {
195
306
this.parent(0.0, _("Keyboard"));
201
312
this.actor.add_actor(this._container);
202
313
this.actor.add_style_class_name('panel-status-button');
204
this._labelActors = {};
205
this._layoutItems = {};
315
// All valid input sources currently in the gsettings
316
// KEY_INPUT_SOURCES list indexed by their index there
317
this._inputSources = {};
318
// All valid input sources currently in the gsettings
319
// KEY_INPUT_SOURCES list of type INPUT_SOURCE_TYPE_IBUS
320
// indexed by the IBus ID
321
this._ibusSources = {};
323
this._currentSource = null;
325
// All valid input sources currently in the gsettings
326
// KEY_INPUT_SOURCES list ordered by most recently used
327
this._mruSources = [];
328
this._keybindingAction =
329
Main.wm.addKeybinding('switch-input-source',
330
new Gio.Settings({ schema: "org.gnome.desktop.wm.keybindings" }),
331
Meta.KeyBindingFlags.REVERSES,
332
Shell.KeyBindingMode.ALL & ~Shell.KeyBindingMode.MESSAGE_TRAY,
333
Lang.bind(this, this._switchInputSource));
334
this._keybindingActionBackward =
335
Main.wm.addKeybinding('switch-input-source-backward',
336
new Gio.Settings({ schema: "org.gnome.desktop.wm.keybindings" }),
337
Meta.KeyBindingFlags.REVERSES |
338
Meta.KeyBindingFlags.REVERSED,
339
Shell.KeyBindingMode.ALL & ~Shell.KeyBindingMode.MESSAGE_TRAY,
340
Lang.bind(this, this._switchInputSource));
207
341
this._settings = new Gio.Settings({ schema: DESKTOP_INPUT_SOURCES_SCHEMA });
208
342
this._settings.connect('changed::' + KEY_CURRENT_INPUT_SOURCE, Lang.bind(this, this._currentInputSourceChanged));
209
343
this._settings.connect('changed::' + KEY_INPUT_SOURCES, Lang.bind(this, this._inputSourcesChanged));
211
this._currentSourceIndex = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
212
345
this._xkbInfo = new GnomeDesktop.XkbInfo();
214
347
this._propSeparator = new PopupMenu.PopupSeparatorMenuItem();
241
380
this._showLayoutItem.actor.visible = Main.sessionMode.allowSettings;
383
_ibusReadyCallback: function(ready) {
384
if (this._ibusReady == ready)
387
this._ibusReady = ready;
388
this._mruSources = [];
389
this._inputSourcesChanged();
392
_switchInputSource: function(display, screen, window, binding) {
393
if (this._mruSources.length < 2)
396
let popup = new InputSourcePopup(this._mruSources, this._keybindingAction, this._keybindingActionBackward);
397
let modifiers = binding.get_modifiers();
398
let backwards = modifiers & Meta.VirtualModifier.SHIFT_MASK;
399
if (!popup.show(backwards, binding.get_name(), binding.get_mask()))
244
403
_currentInputSourceChanged: function() {
245
let nVisibleSources = Object.keys(this._layoutItems).length;
246
let newCurrentSourceIndex = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
247
let newLayoutItem = this._layoutItems[newCurrentSourceIndex];
251
hasProperties = this._ibusManager.hasProperties(newLayoutItem.ibusEngineId);
253
hasProperties = false;
255
if (!newLayoutItem || (nVisibleSources < 2 && !hasProperties)) {
404
let nVisibleSources = Object.keys(this._inputSources).length;
405
let newSourceIndex = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
406
let newSource = this._inputSources[newSourceIndex];
409
[oldSource, this._currentSource] = [this._currentSource, newSource];
412
oldSource.menuItem.setShowDot(false);
413
oldSource.indicatorLabel.hide();
416
if (!newSource || (nVisibleSources < 2 && !newSource.properties)) {
256
417
// This source index might be invalid if we weren't able
257
418
// to build a menu item for it, so we hide ourselves since
258
419
// we can't fix it here. *shrug*
267
428
this.actor.show();
269
if (this._layoutItems[this._currentSourceIndex]) {
270
this._layoutItems[this._currentSourceIndex].setShowDot(false);
271
this._container.set_skip_paint(this._labelActors[this._currentSourceIndex], true);
274
newLayoutItem.setShowDot(true);
276
let newLabelActor = this._labelActors[newCurrentSourceIndex];
277
this._container.set_skip_paint(newLabelActor, false);
280
newLabelActor.set_text(newLayoutItem.indicator.get_text());
282
this._currentSourceIndex = newCurrentSourceIndex;
430
newSource.menuItem.setShowDot(true);
431
newSource.indicatorLabel.show();
433
this._buildPropSection(newSource.properties);
435
for (let i = 1; i < this._mruSources.length; ++i)
436
if (this._mruSources[i] == newSource) {
437
let currentSource = this._mruSources.splice(i, 1);
438
this._mruSources = currentSource.concat(this._mruSources);
442
this._changePerWindowSource();
285
445
_inputSourcesChanged: function() {
286
446
let sources = this._settings.get_value(KEY_INPUT_SOURCES);
287
447
let nSources = sources.n_children();
289
for (let i in this._layoutItems)
290
this._layoutItems[i].destroy();
292
for (let i in this._labelActors)
293
this._labelActors[i].destroy();
295
this._layoutItems = {};
296
this._labelActors = {};
299
let infosByShortName = {};
449
for (let i in this._inputSources)
450
this._inputSources[i].destroy();
452
this._inputSources = {};
453
this._ibusSources = {};
455
let inputSourcesByShortName = {};
301
457
for (let i = 0; i < nSources; i++) {
302
let info = { exists: false };
303
460
let [type, id] = sources.get_child_value(i).deep_unpack();
305
463
if (type == INPUT_SOURCE_TYPE_XKB) {
306
[info.exists, info.displayName, info.shortName, , ] =
464
[exists, displayName, shortName, , ] =
307
465
this._xkbInfo.get_layout_info(id);
308
466
} else if (type == INPUT_SOURCE_TYPE_IBUS) {
309
467
let engineDesc = this._ibusManager.getEngineDesc(id);
310
468
if (engineDesc) {
311
469
let language = IBus.get_language_name(engineDesc.get_language());
314
info.displayName = language + ' (' + engineDesc.get_longname() + ')';
315
info.shortName = this._makeEngineShortName(engineDesc);
316
info.ibusEngineId = id;
471
displayName = language + ' (' + engineDesc.get_longname() + ')';
472
shortName = this._makeEngineShortName(engineDesc);
323
info.sourceIndex = i;
325
if (!(info.shortName in infosByShortName))
326
infosByShortName[info.shortName] = [];
327
infosByShortName[info.shortName].push(info);
331
for (let i = 0; i < infos.length; i++) {
333
if (infosByShortName[info.shortName].length > 1) {
334
let sub = infosByShortName[info.shortName].indexOf(info) + 1;
335
info.shortName += String.fromCharCode(0x2080 + sub);
338
let item = new LayoutMenuItem(info.displayName, info.shortName);
339
item.ibusEngineId = info.ibusEngineId;
340
this._layoutItems[info.sourceIndex] = item;
341
this.menu.addMenuItem(item, i);
342
item.connect('activate', Lang.bind(this, function() {
479
let is = new InputSource(type, id, displayName, shortName, i);
481
is.connect('activate', Lang.bind(this, function() {
482
if (this._currentSource && this._currentSource.index == is.index)
343
484
this._settings.set_value(KEY_CURRENT_INPUT_SOURCE,
344
GLib.Variant.new_uint32(info.sourceIndex));
485
GLib.Variant.new_uint32(is.index));
347
let shortLabel = new St.Label({ text: info.shortName });
348
this._labelActors[info.sourceIndex] = shortLabel;
349
this._container.add_actor(shortLabel);
350
this._container.set_skip_paint(shortLabel, true);
488
if (!(is.shortName in inputSourcesByShortName))
489
inputSourcesByShortName[is.shortName] = [];
490
inputSourcesByShortName[is.shortName].push(is);
492
this._inputSources[is.index] = is;
494
if (is.type == INPUT_SOURCE_TYPE_IBUS)
495
this._ibusSources[is.id] = is;
499
for (let i in this._inputSources) {
500
let is = this._inputSources[i];
501
if (inputSourcesByShortName[is.shortName].length > 1) {
502
let sub = inputSourcesByShortName[is.shortName].indexOf(is) + 1;
503
is.shortName += String.fromCharCode(0x2080 + sub);
506
this.menu.addMenuItem(is.menuItem, menuIndex++);
508
is.indicatorLabel.hide();
509
this._container.add_actor(is.indicatorLabel);
512
let sourcesList = [];
513
for (let i in this._inputSources)
514
sourcesList.push(this._inputSources[i]);
517
for (let i = 0; i < this._mruSources.length; i++) {
518
for (let j = 0; j < sourcesList.length; j++)
519
if (this._mruSources[i].type == sourcesList[j].type &&
520
this._mruSources[i].id == sourcesList[j].id) {
521
mruSources = mruSources.concat(sourcesList.splice(j, 1));
525
this._mruSources = mruSources.concat(sourcesList);
353
527
this._currentInputSourceChanged();
356
530
_showLayout: function() {
357
531
Main.overview.hide();
359
let sources = this._settings.get_value(KEY_INPUT_SOURCES);
360
let current = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
361
let [type, id] = sources.get_child_value(current).deep_unpack();
533
let source = this._currentSource;
362
534
let xkbLayout = '';
363
535
let xkbVariant = '';
365
if (type == INPUT_SOURCE_TYPE_XKB) {
366
[, , , xkbLayout, xkbVariant] = this._xkbInfo.get_layout_info(id);
367
} else if (type == INPUT_SOURCE_TYPE_IBUS) {
368
let engineDesc = this._ibusManager.getEngineDesc(id);
537
if (source.type == INPUT_SOURCE_TYPE_XKB) {
538
[, , , xkbLayout, xkbVariant] = this._xkbInfo.get_layout_info(source.id);
539
} else if (source.type == INPUT_SOURCE_TYPE_IBUS) {
540
let engineDesc = this._ibusManager.getEngineDesc(source.id);
369
541
if (engineDesc) {
370
542
xkbLayout = engineDesc.get_layout();
394
566
return String.fromCharCode(0x2328); // keyboard glyph
397
_propertyWhitelisted: function(prop) {
398
for (let i = 0; i < this._propertiesWhitelist.length; ++i) {
399
let key = prop.get_key();
400
if (key.substr(0, this._propertiesWhitelist[i].length) == this._propertiesWhitelist[i])
406
_ibusPropertiesRegistered: function(im, props) {
407
this._properties = props;
408
this._buildPropSection();
411
_ibusPropertyUpdated: function(im, prop) {
412
if (!this._propertyWhitelisted(prop))
415
if (this._updateSubProperty(this._properties, prop))
416
this._buildPropSection();
569
_ibusPropertiesRegistered: function(im, engineName, props) {
570
let source = this._ibusSources[engineName];
574
source.properties = props;
576
if (source == this._currentSource)
577
this._currentInputSourceChanged();
580
_ibusPropertyUpdated: function(im, engineName, prop) {
581
let source = this._ibusSources[engineName];
585
if (this._updateSubProperty(source.properties, prop) &&
586
source == this._currentSource)
587
this._currentInputSourceChanged();
419
590
_updateSubProperty: function(props, prop) {
677
case IBus.PropType.TOGGLE:
678
item = new PopupMenu.PopupSwitchMenuItem(prop.get_label().get_text(), prop.get_state() == IBus.PropState.CHECKED);
680
item.connect('toggled', Lang.bind(this, function() {
682
item.prop.set_state(IBus.PropState.CHECKED);
683
this._ibusManager.activateProperty(item.prop.get_key(),
684
IBus.PropState.CHECKED);
686
item.prop.set_state(IBus.PropState.UNCHECKED);
687
this._ibusManager.activateProperty(item.prop.get_key(),
688
IBus.PropState.UNCHECKED);
693
case IBus.PropType.NORMAL:
694
item = new PopupMenu.PopupMenuItem(prop.get_label().get_text());
696
item.connect('activate', Lang.bind(this, function() {
697
this._ibusManager.activateProperty(item.prop.get_key(),
698
item.prop.get_state());
702
case IBus.PropType.SEPARATOR:
703
item = new PopupMenu.PopupSeparatorMenuItem();
707
log ('IBus property %s has invalid type %d'.format(prop.get_key(), type));
716
_getNewInputSource: function(current) {
717
for (let i in this._inputSources) {
718
let is = this._inputSources[i];
719
if (is.type == current.type &&
723
return this._currentSource;
726
_getCurrentWindow: function() {
727
if (Main.overview.visible)
728
return Main.overview;
730
return global.display.focus_window;
733
_setPerWindowInputSource: function() {
734
let window = this._getCurrentWindow();
738
if (!window._inputSources) {
739
window._inputSources = this._inputSources;
740
window._currentSource = this._currentSource;
741
} else if (window._inputSources == this._inputSources) {
742
window._currentSource.activate();
744
window._inputSources = this._inputSources;
745
window._currentSource = this._getNewInputSource(window._currentSource);
746
window._currentSource.activate();
750
_sourcesPerWindowChanged: function() {
751
this._sourcesPerWindow = this._settings.get_boolean('per-window');
753
if (this._sourcesPerWindow && this._focusWindowNotifyId == 0) {
754
this._focusWindowNotifyId = global.display.connect('notify::focus-window',
755
Lang.bind(this, this._setPerWindowInputSource));
756
this._overviewShowingId = Main.overview.connect('showing',
757
Lang.bind(this, this._setPerWindowInputSource));
758
this._overviewHiddenId = Main.overview.connect('hidden',
759
Lang.bind(this, this._setPerWindowInputSource));
760
} else if (!this._sourcesPerWindow && this._focusWindowNotifyId != 0) {
761
global.display.disconnect(this._focusWindowNotifyId);
762
this._focusWindowNotifyId = 0;
763
Main.overview.disconnect(this._overviewShowingId);
764
this._overviewShowingId = 0;
765
Main.overview.disconnect(this._overviewHiddenId);
766
this._overviewHiddenId = 0;
768
let windows = global.get_window_actors().map(function(w) {
769
return w.meta_window;
771
for (let i = 0; i < windows.length; ++i) {
772
delete windows[i]._inputSources;
773
delete windows[i]._currentSource;
775
delete Main.overview._inputSources;
776
delete Main.overview._currentSource;
780
_changePerWindowSource: function() {
781
if (!this._sourcesPerWindow)
784
let window = this._getCurrentWindow();
788
window._inputSources = this._inputSources;
789
window._currentSource = this._currentSource;
525
792
_containerGetPreferredWidth: function(container, for_height, alloc) {
526
793
// Here, and in _containerGetPreferredHeight, we need to query
527
794
// for the height of all children, but we ignore the results
528
795
// for those we don't actually display.
529
796
let max_min_width = 0, max_natural_width = 0;
531
for (let i in this._labelActors) {
532
let [min_width, natural_width] = this._labelActors[i].get_preferred_width(for_height);
798
for (let i in this._inputSources) {
799
let is = this._inputSources[i];
800
let [min_width, natural_width] = is.indicatorLabel.get_preferred_width(for_height);
533
801
max_min_width = Math.max(max_min_width, min_width);
534
802
max_natural_width = Math.max(max_natural_width, natural_width);