2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2011-01-04 22:04:56 +00:00
|
|
|
|
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const GdkPixbuf = imports.gi.GdkPixbuf;
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const GLib = imports.gi.GLib;
|
2012-04-19 02:01:29 +00:00
|
|
|
const GnomeDesktop = imports.gi.GnomeDesktop;
|
2011-01-04 22:04:56 +00:00
|
|
|
const Lang = imports.lang;
|
2012-05-29 17:30:14 +00:00
|
|
|
const Mainloop = imports.mainloop;
|
2011-01-04 22:04:56 +00:00
|
|
|
const Shell = imports.gi.Shell;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
2012-05-29 17:30:14 +00:00
|
|
|
try {
|
|
|
|
var IBus = imports.gi.IBus;
|
|
|
|
if (!('new_async' in IBus.Bus))
|
|
|
|
throw "IBus version is too old";
|
2012-05-29 17:30:46 +00:00
|
|
|
const IBusCandidatePopup = imports.ui.ibusCandidatePopup;
|
2012-05-29 17:30:14 +00:00
|
|
|
} catch (e) {
|
|
|
|
var IBus = null;
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
|
2011-06-21 22:26:57 +00:00
|
|
|
const Main = imports.ui.main;
|
2011-01-04 22:04:56 +00:00
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
2011-02-05 18:30:16 +00:00
|
|
|
const Util = imports.misc.util;
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
const DESKTOP_INPUT_SOURCES_SCHEMA = 'org.gnome.desktop.input-sources';
|
|
|
|
const KEY_CURRENT_INPUT_SOURCE = 'current';
|
|
|
|
const KEY_INPUT_SOURCES = 'sources';
|
|
|
|
|
|
|
|
const INPUT_SOURCE_TYPE_XKB = 'xkb';
|
2012-05-29 17:30:14 +00:00
|
|
|
const INPUT_SOURCE_TYPE_IBUS = 'ibus';
|
|
|
|
|
|
|
|
const IBusManager = new Lang.Class({
|
|
|
|
Name: 'IBusManager',
|
|
|
|
|
|
|
|
_init: function(readyCallback) {
|
|
|
|
if (!IBus)
|
|
|
|
return;
|
|
|
|
|
|
|
|
IBus.init();
|
|
|
|
|
|
|
|
this._readyCallback = readyCallback;
|
2012-05-29 17:30:46 +00:00
|
|
|
this._candidatePopup = new IBusCandidatePopup.CandidatePopup();
|
2012-05-29 17:30:14 +00:00
|
|
|
|
|
|
|
this._ibus = null;
|
2012-05-29 17:30:46 +00:00
|
|
|
this._panelService = null;
|
2012-05-29 17:30:14 +00:00
|
|
|
this._engines = {};
|
|
|
|
this._ready = false;
|
|
|
|
|
|
|
|
this._nameWatcherId = Gio.DBus.session.watch_name(IBus.SERVICE_IBUS,
|
|
|
|
Gio.BusNameWatcherFlags.NONE,
|
|
|
|
Lang.bind(this, this._onNameAppeared),
|
|
|
|
Lang.bind(this, this._clear));
|
|
|
|
},
|
|
|
|
|
|
|
|
_clear: function() {
|
2012-05-29 17:30:46 +00:00
|
|
|
if (this._panelService)
|
|
|
|
this._panelService.destroy();
|
2012-05-29 17:30:14 +00:00
|
|
|
if (this._ibus)
|
|
|
|
this._ibus.destroy();
|
|
|
|
|
|
|
|
this._ibus = null;
|
2012-05-29 17:30:46 +00:00
|
|
|
this._panelService = null;
|
|
|
|
this._candidatePopup.setPanelService(null);
|
2012-05-29 17:30:14 +00:00
|
|
|
this._engines = {};
|
|
|
|
this._ready = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
_onNameAppeared: function() {
|
|
|
|
this._ibus = IBus.Bus.new_async();
|
|
|
|
this._ibus.connect('connected', Lang.bind(this, this._onConnected));
|
|
|
|
},
|
|
|
|
|
|
|
|
_onConnected: function() {
|
|
|
|
this._ibus.list_engines_async(-1, null, Lang.bind(this, this._initEngines));
|
2012-05-29 17:30:46 +00:00
|
|
|
this._ibus.request_name_async(IBus.SERVICE_PANEL,
|
|
|
|
IBus.BusNameFlag.REPLACE_EXISTING,
|
|
|
|
-1, null,
|
|
|
|
Lang.bind(this, this._initPanelService));
|
2012-05-29 17:30:14 +00:00
|
|
|
this._ibus.connect('disconnected', Lang.bind(this, this._clear));
|
|
|
|
},
|
|
|
|
|
|
|
|
_initEngines: function(ibus, result) {
|
|
|
|
let enginesList = this._ibus.list_engines_async_finish(result);
|
|
|
|
if (enginesList) {
|
|
|
|
for (let i = 0; i < enginesList.length; ++i) {
|
|
|
|
let name = enginesList[i].get_name();
|
|
|
|
this._engines[name] = enginesList[i];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._clear();
|
2012-05-29 17:30:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._updateReadiness();
|
|
|
|
},
|
|
|
|
|
|
|
|
_initPanelService: function(ibus, result) {
|
|
|
|
let success = this._ibus.request_name_async_finish(result);
|
|
|
|
if (success) {
|
|
|
|
this._panelService = new IBus.PanelService({ connection: this._ibus.get_connection(),
|
|
|
|
object_path: IBus.PATH_PANEL });
|
|
|
|
this._candidatePopup.setPanelService(this._panelService);
|
|
|
|
} else {
|
|
|
|
this._clear();
|
|
|
|
return;
|
2012-05-29 17:30:14 +00:00
|
|
|
}
|
2012-05-29 17:30:46 +00:00
|
|
|
|
|
|
|
this._updateReadiness();
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateReadiness: function() {
|
|
|
|
this._ready = (Object.keys(this._engines).length > 0 &&
|
|
|
|
this._panelService != null);
|
|
|
|
|
|
|
|
if (this._ready && this._readyCallback)
|
|
|
|
this._readyCallback();
|
2012-05-29 17:30:14 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getEngineDesc: function(id) {
|
|
|
|
if (!IBus || !this._ready)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return this._engines[id];
|
|
|
|
}
|
|
|
|
});
|
2012-04-19 02:01:29 +00:00
|
|
|
|
2011-11-20 13:10:48 +00:00
|
|
|
const LayoutMenuItem = new Lang.Class({
|
|
|
|
Name: 'LayoutMenuItem',
|
|
|
|
Extends: PopupMenu.PopupBaseMenuItem,
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
_init: function(displayName, shortName) {
|
2011-11-20 13:10:48 +00:00
|
|
|
this.parent();
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
this.label = new St.Label({ text: displayName });
|
|
|
|
this.indicator = new St.Label({ text: shortName });
|
2011-01-04 22:04:56 +00:00
|
|
|
this.addActor(this.label);
|
|
|
|
this.addActor(this.indicator);
|
|
|
|
}
|
2011-11-20 13:10:48 +00:00
|
|
|
});
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
const InputSourceIndicator = new Lang.Class({
|
|
|
|
Name: 'InputSourceIndicator',
|
2011-11-20 14:38:48 +00:00
|
|
|
Extends: PanelMenu.Button,
|
2011-01-04 22:04:56 +00:00
|
|
|
|
|
|
|
_init: function() {
|
2012-03-23 20:12:51 +00:00
|
|
|
this.parent(0.0, _("Keyboard"));
|
2011-01-04 22:04:56 +00:00
|
|
|
|
|
|
|
this._container = new Shell.GenericContainer();
|
2011-09-19 12:07:40 +00:00
|
|
|
this._container.connect('get-preferred-width', Lang.bind(this, this._containerGetPreferredWidth));
|
|
|
|
this._container.connect('get-preferred-height', Lang.bind(this, this._containerGetPreferredHeight));
|
|
|
|
this._container.connect('allocate', Lang.bind(this, this._containerAllocate));
|
2011-06-09 15:50:24 +00:00
|
|
|
this.actor.add_actor(this._container);
|
2011-09-19 12:21:43 +00:00
|
|
|
this.actor.add_style_class_name('panel-status-button');
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
this._labelActors = {};
|
|
|
|
this._layoutItems = {};
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
this._settings = new Gio.Settings({ schema: DESKTOP_INPUT_SOURCES_SCHEMA });
|
|
|
|
this._settings.connect('changed::' + KEY_CURRENT_INPUT_SOURCE, Lang.bind(this, this._currentInputSourceChanged));
|
|
|
|
this._settings.connect('changed::' + KEY_INPUT_SOURCES, Lang.bind(this, this._inputSourcesChanged));
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
this._currentSourceIndex = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
|
|
|
|
this._xkbInfo = new GnomeDesktop.XkbInfo();
|
|
|
|
|
2012-05-29 17:30:14 +00:00
|
|
|
this._ibusManager = new IBusManager(Lang.bind(this, this._inputSourcesChanged));
|
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
this._inputSourcesChanged();
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-05-16 23:43:59 +00:00
|
|
|
// re-using "allowSettings" for the keyboard layout is a bit shady,
|
|
|
|
// but at least for now it is used as "allow popping up windows
|
|
|
|
// from shell menus"; we can always add a separate sessionMode
|
|
|
|
// option if need arises.
|
|
|
|
if (Main.sessionMode.allowSettings) {
|
2011-09-16 13:42:43 +00:00
|
|
|
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
|
2012-04-19 02:01:29 +00:00
|
|
|
this.menu.addAction(_("Show Keyboard Layout"), Lang.bind(this, this._showLayout));
|
2011-09-16 13:42:43 +00:00
|
|
|
}
|
2011-08-23 14:14:55 +00:00
|
|
|
this.menu.addSettingsAction(_("Region and Language Settings"), 'gnome-region-panel.desktop');
|
2011-01-04 22:04:56 +00:00
|
|
|
},
|
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
_currentInputSourceChanged: function() {
|
|
|
|
let nVisibleSources = Object.keys(this._layoutItems).length;
|
|
|
|
if (nVisibleSources < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let nSources = this._settings.get_value(KEY_INPUT_SOURCES).n_children();
|
|
|
|
let newCurrentSourceIndex = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
|
|
|
|
if (newCurrentSourceIndex >= nSources)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!this._layoutItems[newCurrentSourceIndex]) {
|
|
|
|
// This source index is invalid as we weren't able to
|
|
|
|
// build a menu item for it, so we hide ourselves since we
|
|
|
|
// can't fix it here. *shrug*
|
|
|
|
this.menu.close();
|
|
|
|
this.actor.hide();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
this.actor.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._layoutItems[this._currentSourceIndex]) {
|
|
|
|
this._layoutItems[this._currentSourceIndex].setShowDot(false);
|
|
|
|
this._container.set_skip_paint(this._labelActors[this._currentSourceIndex], true);
|
2011-05-15 17:31:02 +00:00
|
|
|
}
|
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
this._layoutItems[newCurrentSourceIndex].setShowDot(true);
|
|
|
|
this._container.set_skip_paint(this._labelActors[newCurrentSourceIndex], false);
|
|
|
|
|
|
|
|
this._currentSourceIndex = newCurrentSourceIndex;
|
2011-05-15 17:31:02 +00:00
|
|
|
},
|
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
_inputSourcesChanged: function() {
|
|
|
|
let sources = this._settings.get_value(KEY_INPUT_SOURCES);
|
|
|
|
let nSources = sources.n_children();
|
|
|
|
|
|
|
|
for (let i in this._layoutItems)
|
|
|
|
this._layoutItems[i].destroy();
|
|
|
|
|
|
|
|
for (let i in this._labelActors)
|
|
|
|
this._labelActors[i].destroy();
|
|
|
|
|
|
|
|
this._layoutItems = {};
|
|
|
|
this._labelActors = {};
|
|
|
|
|
|
|
|
let infos = [];
|
|
|
|
let infosByShortName = {};
|
|
|
|
|
|
|
|
for (let i = 0; i < nSources; i++) {
|
2012-05-29 17:30:14 +00:00
|
|
|
let info = { exists: false };
|
2012-04-19 02:01:29 +00:00
|
|
|
let [type, id] = sources.get_child_value(i).deep_unpack();
|
|
|
|
|
2012-05-29 17:30:14 +00:00
|
|
|
if (type == INPUT_SOURCE_TYPE_XKB) {
|
|
|
|
[info.exists, info.displayName, info.shortName, , ] =
|
|
|
|
this._xkbInfo.get_layout_info(id);
|
|
|
|
} else if (type == INPUT_SOURCE_TYPE_IBUS) {
|
|
|
|
let engineDesc = this._ibusManager.getEngineDesc(id);
|
|
|
|
if (engineDesc) {
|
|
|
|
info.exists = true;
|
|
|
|
info.displayName = engineDesc.get_longname();
|
|
|
|
info.shortName = engineDesc.get_symbol();
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 02:01:29 +00:00
|
|
|
|
|
|
|
if (!info.exists)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
info.sourceIndex = i;
|
|
|
|
|
|
|
|
if (!(info.shortName in infosByShortName))
|
|
|
|
infosByShortName[info.shortName] = [];
|
|
|
|
infosByShortName[info.shortName].push(info);
|
|
|
|
infos.push(info);
|
2011-01-04 22:04:56 +00:00
|
|
|
}
|
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
if (infos.length > 1) {
|
2011-01-04 22:04:56 +00:00
|
|
|
this.actor.show();
|
|
|
|
} else {
|
|
|
|
this.menu.close();
|
|
|
|
this.actor.hide();
|
|
|
|
}
|
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
for (let i = 0; i < infos.length; i++) {
|
|
|
|
let info = infos[i];
|
|
|
|
if (infosByShortName[info.shortName].length > 1) {
|
|
|
|
let sub = infosByShortName[info.shortName].indexOf(info) + 1;
|
|
|
|
info.shortName += String.fromCharCode(0x2080 + sub);
|
|
|
|
}
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
let item = new LayoutMenuItem(info.displayName, info.shortName);
|
|
|
|
this._layoutItems[info.sourceIndex] = item;
|
2011-01-04 22:04:56 +00:00
|
|
|
this.menu.addMenuItem(item, i);
|
2012-04-19 02:01:29 +00:00
|
|
|
item.connect('activate', Lang.bind(this, function() {
|
|
|
|
this._settings.set_value(KEY_CURRENT_INPUT_SOURCE,
|
|
|
|
GLib.Variant.new_uint32(info.sourceIndex));
|
|
|
|
}));
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
let shortLabel = new St.Label({ text: info.shortName });
|
|
|
|
this._labelActors[info.sourceIndex] = shortLabel;
|
2011-01-04 22:04:56 +00:00
|
|
|
this._container.add_actor(shortLabel);
|
|
|
|
this._container.set_skip_paint(shortLabel, true);
|
|
|
|
}
|
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
this._currentInputSourceChanged();
|
2011-01-04 22:04:56 +00:00
|
|
|
},
|
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
_showLayout: function() {
|
|
|
|
Main.overview.hide();
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
let sources = this._settings.get_value(KEY_INPUT_SOURCES);
|
|
|
|
let current = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
|
2012-05-29 17:30:14 +00:00
|
|
|
let [type, id] = sources.get_child_value(current).deep_unpack();
|
|
|
|
let xkbLayout = '';
|
|
|
|
let xkbVariant = '';
|
|
|
|
|
|
|
|
if (type == INPUT_SOURCE_TYPE_XKB) {
|
|
|
|
[, , , xkbLayout, xkbVariant] = this._xkbInfo.get_layout_info(id);
|
|
|
|
} else if (type == INPUT_SOURCE_TYPE_IBUS) {
|
|
|
|
let engineDesc = this._ibusManager.getEngineDesc(id);
|
|
|
|
if (engineDesc) {
|
|
|
|
xkbLayout = engineDesc.get_layout();
|
|
|
|
xkbVariant = '';
|
|
|
|
}
|
|
|
|
}
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
if (!xkbLayout || xkbLayout.length == 0)
|
|
|
|
return;
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
let description = xkbLayout;
|
|
|
|
if (xkbVariant.length > 0)
|
|
|
|
description = description + '\t' + xkbVariant;
|
2011-01-04 22:04:56 +00:00
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
Util.spawn(['gkbd-keyboard-display', '-l', description]);
|
2011-01-04 22:04:56 +00:00
|
|
|
},
|
|
|
|
|
2011-09-19 12:07:40 +00:00
|
|
|
_containerGetPreferredWidth: function(container, for_height, alloc) {
|
|
|
|
// Here, and in _containerGetPreferredHeight, we need to query
|
|
|
|
// for the height of all children, but we ignore the results
|
|
|
|
// for those we don't actually display.
|
2011-01-04 22:04:56 +00:00
|
|
|
let max_min_width = 0, max_natural_width = 0;
|
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
for (let i in this._labelActors) {
|
2011-01-04 22:04:56 +00:00
|
|
|
let [min_width, natural_width] = this._labelActors[i].get_preferred_width(for_height);
|
2012-04-19 02:01:29 +00:00
|
|
|
max_min_width = Math.max(max_min_width, min_width);
|
|
|
|
max_natural_width = Math.max(max_natural_width, natural_width);
|
2011-01-04 22:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
alloc.min_size = max_min_width;
|
|
|
|
alloc.natural_size = max_natural_width;
|
|
|
|
},
|
|
|
|
|
2011-09-19 12:07:40 +00:00
|
|
|
_containerGetPreferredHeight: function(container, for_width, alloc) {
|
2011-01-04 22:04:56 +00:00
|
|
|
let max_min_height = 0, max_natural_height = 0;
|
2012-04-19 02:01:29 +00:00
|
|
|
|
|
|
|
for (let i in this._labelActors) {
|
2011-01-04 22:04:56 +00:00
|
|
|
let [min_height, natural_height] = this._labelActors[i].get_preferred_height(for_width);
|
2012-04-19 02:01:29 +00:00
|
|
|
max_min_height = Math.max(max_min_height, min_height);
|
|
|
|
max_natural_height = Math.max(max_natural_height, natural_height);
|
2011-01-04 22:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
alloc.min_size = max_min_height;
|
|
|
|
alloc.natural_size = max_natural_height;
|
|
|
|
},
|
|
|
|
|
2011-09-19 12:07:40 +00:00
|
|
|
_containerAllocate: function(container, box, flags) {
|
2011-01-04 22:04:56 +00:00
|
|
|
// translate box to (0, 0)
|
|
|
|
box.x2 -= box.x1;
|
|
|
|
box.x1 = 0;
|
|
|
|
box.y2 -= box.y1;
|
|
|
|
box.y1 = 0;
|
|
|
|
|
2012-04-19 02:01:29 +00:00
|
|
|
for (let i in this._labelActors)
|
2011-01-04 22:04:56 +00:00
|
|
|
this._labelActors[i].allocate_align_fill(box, 0.5, 0, false, false, flags);
|
|
|
|
}
|
2011-11-20 14:38:48 +00:00
|
|
|
});
|