status/keyboard: Port to the new input sources settings

https://bugzilla.gnome.org/show_bug.cgi?id=641531
This commit is contained in:
Rui Matos 2012-04-19 04:01:29 +02:00
parent 76005f5adf
commit ec0730f3e5
3 changed files with 125 additions and 116 deletions

View File

@ -75,6 +75,7 @@ TELEPATHY_LOGGER_MIN_VERSION=0.2.4
POLKIT_MIN_VERSION=0.100 POLKIT_MIN_VERSION=0.100
STARTUP_NOTIFICATION_MIN_VERSION=0.11 STARTUP_NOTIFICATION_MIN_VERSION=0.11
GCR_MIN_VERSION=3.3.90 GCR_MIN_VERSION=3.3.90
GNOME_DESKTOP_REQUIRED_VERSION=3.5.1
# Collect more than 20 libraries for a prize! # Collect more than 20 libraries for a prize!
PKG_CHECK_MODULES(GNOME_SHELL, gio-unix-2.0 >= $GIO_MIN_VERSION PKG_CHECK_MODULES(GNOME_SHELL, gio-unix-2.0 >= $GIO_MIN_VERSION
@ -95,7 +96,8 @@ PKG_CHECK_MODULES(GNOME_SHELL, gio-unix-2.0 >= $GIO_MIN_VERSION
telepathy-logger-0.2 >= $TELEPATHY_LOGGER_MIN_VERSION telepathy-logger-0.2 >= $TELEPATHY_LOGGER_MIN_VERSION
polkit-agent-1 >= $POLKIT_MIN_VERSION xfixes polkit-agent-1 >= $POLKIT_MIN_VERSION xfixes
libnm-glib libnm-util gnome-keyring-1 libnm-glib libnm-util gnome-keyring-1
gcr-3 >= $GCR_MIN_VERSION) gcr-3 >= $GCR_MIN_VERSION
gnome-desktop-3.0 >= $GNOME_DESKTOP_REQUIRED_VERSION)
PKG_CHECK_MODULES(SHELL_PERF_HELPER, gtk+-3.0 gio-2.0) PKG_CHECK_MODULES(SHELL_PERF_HELPER, gtk+-3.0 gio-2.0)
@ -121,7 +123,7 @@ PKG_CHECK_MODULES(GNOME_SHELL_JS, gio-2.0 gjs-internals-1.0 >= $GJS_MIN_VERSION)
PKG_CHECK_MODULES(ST, clutter-1.0 gtk+-3.0 libcroco-0.6 >= 0.6.2 x11) PKG_CHECK_MODULES(ST, clutter-1.0 gtk+-3.0 libcroco-0.6 >= 0.6.2 x11)
PKG_CHECK_MODULES(TRAY, gtk+-3.0) PKG_CHECK_MODULES(TRAY, gtk+-3.0)
PKG_CHECK_MODULES(GVC, libpulse libpulse-mainloop-glib gobject-2.0) PKG_CHECK_MODULES(GVC, libpulse libpulse-mainloop-glib gobject-2.0)
PKG_CHECK_MODULES(DESKTOP_SCHEMAS, gsettings-desktop-schemas >= 0.1.7) PKG_CHECK_MODULES(DESKTOP_SCHEMAS, gsettings-desktop-schemas >= 3.5.1)
AC_MSG_CHECKING([for bluetooth support]) AC_MSG_CHECKING([for bluetooth support])
PKG_CHECK_EXISTS([gnome-bluetooth-1.0 >= 3.1.0], PKG_CHECK_EXISTS([gnome-bluetooth-1.0 >= 3.1.0],

View File

@ -11,7 +11,7 @@ const STANDARD_STATUS_AREA_SHELL_IMPLEMENTATION = {
'a11y': imports.ui.status.accessibility.ATIndicator, 'a11y': imports.ui.status.accessibility.ATIndicator,
'volume': imports.ui.status.volume.Indicator, 'volume': imports.ui.status.volume.Indicator,
'battery': imports.ui.status.power.Indicator, 'battery': imports.ui.status.power.Indicator,
'keyboard': imports.ui.status.keyboard.XKBIndicator, 'keyboard': imports.ui.status.keyboard.InputSourceIndicator,
'userMenu': imports.ui.userMenu.UserMenuButton 'userMenu': imports.ui.userMenu.UserMenuButton
}; };
@ -49,7 +49,7 @@ const _modes = {
'a11y': imports.ui.status.accessibility.ATIndicator, 'a11y': imports.ui.status.accessibility.ATIndicator,
'volume': imports.ui.status.volume.Indicator, 'volume': imports.ui.status.volume.Indicator,
'battery': imports.ui.status.power.Indicator, 'battery': imports.ui.status.power.Indicator,
'keyboard': imports.ui.status.keyboard.XKBIndicator, 'keyboard': imports.ui.status.keyboard.InputSourceIndicator,
'powerMenu': imports.gdm.powerMenu.PowerMenuButton 'powerMenu': imports.gdm.powerMenu.PowerMenuButton
} }
} }

View File

@ -2,9 +2,9 @@
const Clutter = imports.gi.Clutter; const Clutter = imports.gi.Clutter;
const GdkPixbuf = imports.gi.GdkPixbuf; const GdkPixbuf = imports.gi.GdkPixbuf;
const Gkbd = imports.gi.Gkbd;
const Gio = imports.gi.Gio; const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib; const GLib = imports.gi.GLib;
const GnomeDesktop = imports.gi.GnomeDesktop;
const Lang = imports.lang; const Lang = imports.lang;
const Shell = imports.gi.Shell; const Shell = imports.gi.Shell;
const St = imports.gi.St; const St = imports.gi.St;
@ -14,30 +14,28 @@ const PopupMenu = imports.ui.popupMenu;
const PanelMenu = imports.ui.panelMenu; const PanelMenu = imports.ui.panelMenu;
const Util = imports.misc.util; const Util = imports.misc.util;
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';
const LayoutMenuItem = new Lang.Class({ const LayoutMenuItem = new Lang.Class({
Name: 'LayoutMenuItem', Name: 'LayoutMenuItem',
Extends: PopupMenu.PopupBaseMenuItem, Extends: PopupMenu.PopupBaseMenuItem,
_init: function(config, id, indicator, long_name) { _init: function(displayName, shortName) {
this.parent(); this.parent();
this._config = config; this.label = new St.Label({ text: displayName });
this._id = id; this.indicator = new St.Label({ text: shortName });
this.label = new St.Label({ text: long_name });
this.indicator = indicator;
this.addActor(this.label); this.addActor(this.label);
this.addActor(this.indicator); this.addActor(this.indicator);
},
activate: function(event) {
this.parent(event);
this._config.lock_group(this._id);
} }
}); });
const XKBIndicator = new Lang.Class({ const InputSourceIndicator = new Lang.Class({
Name: 'XKBIndicator', Name: 'InputSourceIndicator',
Extends: PanelMenu.Button, Extends: PanelMenu.Button,
_init: function() { _init: function() {
@ -50,18 +48,17 @@ const XKBIndicator = new Lang.Class({
this.actor.add_actor(this._container); this.actor.add_actor(this._container);
this.actor.add_style_class_name('panel-status-button'); this.actor.add_style_class_name('panel-status-button');
this._iconActor = new St.Icon({ icon_name: 'keyboard', icon_type: St.IconType.SYMBOLIC, style_class: 'system-status-icon' }); this._labelActors = {};
this._container.add_actor(this._iconActor); this._layoutItems = {};
this._labelActors = [ ];
this._layoutItems = [ ];
this._showFlags = false; this._settings = new Gio.Settings({ schema: DESKTOP_INPUT_SOURCES_SCHEMA });
this._config = Gkbd.Configuration.get(); this._settings.connect('changed::' + KEY_CURRENT_INPUT_SOURCE, Lang.bind(this, this._currentInputSourceChanged));
this._config.connect('changed', Lang.bind(this, this._syncConfig)); this._settings.connect('changed::' + KEY_INPUT_SOURCES, Lang.bind(this, this._inputSourcesChanged));
this._config.connect('group-changed', Lang.bind(this, this._syncGroup));
this._config.start_listen();
this._syncConfig(); this._currentSourceIndex = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
this._xkbInfo = new GnomeDesktop.XkbInfo();
this._inputSourcesChanged();
// re-using "allowSettings" for the keyboard layout is a bit shady, // re-using "allowSettings" for the keyboard layout is a bit shady,
// but at least for now it is used as "allow popping up windows // but at least for now it is used as "allow popping up windows
@ -69,107 +66,126 @@ const XKBIndicator = new Lang.Class({
// option if need arises. // option if need arises.
if (Main.sessionMode.allowSettings) { if (Main.sessionMode.allowSettings) {
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem()); this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
this.menu.addAction(_("Show Keyboard Layout"), Lang.bind(this, function() { this.menu.addAction(_("Show Keyboard Layout"), Lang.bind(this, this._showLayout));
Main.overview.hide();
Util.spawn(['gkbd-keyboard-display', '-g', String(this._config.get_current_group() + 1)]);
}));
} }
this.menu.addSettingsAction(_("Region and Language Settings"), 'gnome-region-panel.desktop'); this.menu.addSettingsAction(_("Region and Language Settings"), 'gnome-region-panel.desktop');
}, },
_adjustGroupNames: function(names) { _currentInputSourceChanged: function() {
// Disambiguate duplicate names with a subscript let nVisibleSources = Object.keys(this._layoutItems).length;
// This is O(N^2) to avoid sorting names if (nVisibleSources < 2)
// but N <= 4 so who cares? return;
for (let i = 0; i < names.length; i++) { let nSources = this._settings.get_value(KEY_INPUT_SOURCES).n_children();
let name = names[i]; let newCurrentSourceIndex = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
let cnt = 0; if (newCurrentSourceIndex >= nSources)
for (let j = i + 1; j < names.length; j++) { return;
if (names[j] == name) {
cnt++; if (!this._layoutItems[newCurrentSourceIndex]) {
// U+2081 SUBSCRIPT ONE // This source index is invalid as we weren't able to
names[j] = name + String.fromCharCode(0x2081 + cnt); // build a menu item for it, so we hide ourselves since we
} // can't fix it here. *shrug*
} this.menu.close();
if (cnt != 0) this.actor.hide();
names[i] = name + '\u2081'; return;
} else {
this.actor.show();
} }
return names; if (this._layoutItems[this._currentSourceIndex]) {
this._layoutItems[this._currentSourceIndex].setShowDot(false);
this._container.set_skip_paint(this._labelActors[this._currentSourceIndex], true);
}
this._layoutItems[newCurrentSourceIndex].setShowDot(true);
this._container.set_skip_paint(this._labelActors[newCurrentSourceIndex], false);
this._currentSourceIndex = newCurrentSourceIndex;
}, },
_syncConfig: function() { _inputSourcesChanged: function() {
this._showFlags = this._config.if_flags_shown(); let sources = this._settings.get_value(KEY_INPUT_SOURCES);
if (this._showFlags) { let nSources = sources.n_children();
this._container.set_skip_paint(this._iconActor, false);
} else { for (let i in this._layoutItems)
this._container.set_skip_paint(this._iconActor, true); 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++) {
let [type, id] = sources.get_child_value(i).deep_unpack();
if (type != INPUT_SOURCE_TYPE_XKB)
continue;
let info = {};
[info.exists, info.displayName, info.shortName, , ] =
this._xkbInfo.get_layout_info(id);
if (!info.exists)
continue;
info.sourceIndex = i;
if (!(info.shortName in infosByShortName))
infosByShortName[info.shortName] = [];
infosByShortName[info.shortName].push(info);
infos.push(info);
} }
let groups = this._config.get_group_names(); if (infos.length > 1) {
if (groups.length > 1) {
this.actor.show(); this.actor.show();
} else { } else {
this.menu.close(); this.menu.close();
this.actor.hide(); this.actor.hide();
} }
for (let i = 0; i < this._layoutItems.length; i++) for (let i = 0; i < infos.length; i++) {
this._layoutItems[i].destroy(); let info = infos[i];
if (infosByShortName[info.shortName].length > 1) {
let sub = infosByShortName[info.shortName].indexOf(info) + 1;
info.shortName += String.fromCharCode(0x2080 + sub);
}
for (let i = 0; i < this._labelActors.length; i++) let item = new LayoutMenuItem(info.displayName, info.shortName);
this._labelActors[i].destroy(); this._layoutItems[info.sourceIndex] = item;
let short_names = this._adjustGroupNames(this._config.get_short_group_names());
this._selectedLayout = null;
this._layoutItems = [ ];
this._selectedLabel = null;
this._labelActors = [ ];
for (let i = 0; i < groups.length; i++) {
let icon_name = this._config.get_group_name(i);
let actor;
if (this._showFlags)
actor = new St.Icon({ icon_name: icon_name, icon_type: St.IconType.SYMBOLIC, style_class: 'popup-menu-icon' });
else
actor = new St.Label({ text: short_names[i] });
let item = new LayoutMenuItem(this._config, i, actor, groups[i]);
item._short_group_name = short_names[i];
item._icon_name = icon_name;
this._layoutItems.push(item);
this.menu.addMenuItem(item, i); this.menu.addMenuItem(item, i);
item.connect('activate', Lang.bind(this, function() {
this._settings.set_value(KEY_CURRENT_INPUT_SOURCE,
GLib.Variant.new_uint32(info.sourceIndex));
}));
let shortLabel = new St.Label({ text: short_names[i] }); let shortLabel = new St.Label({ text: info.shortName });
this._labelActors.push(shortLabel); this._labelActors[info.sourceIndex] = shortLabel;
this._container.add_actor(shortLabel); this._container.add_actor(shortLabel);
this._container.set_skip_paint(shortLabel, true); this._container.set_skip_paint(shortLabel, true);
} }
this._syncGroup(); this._currentInputSourceChanged();
}, },
_syncGroup: function() { _showLayout: function() {
let selected = this._config.get_current_group(); Main.overview.hide();
if (this._selectedLayout) { let sources = this._settings.get_value(KEY_INPUT_SOURCES);
this._selectedLayout.setShowDot(false); let current = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
this._selectedLayout = null; let id = sources.get_child_value(current).deep_unpack()[1];
} let [, , , xkbLayout, xkbVariant] = this._xkbInfo.get_layout_info(id);
if (this._selectedLabel) { if (!xkbLayout || xkbLayout.length == 0)
this._container.set_skip_paint(this._selectedLabel, true); return;
this._selectedLabel = null;
}
let item = this._layoutItems[selected]; let description = xkbLayout;
item.setShowDot(true); if (xkbVariant.length > 0)
description = description + '\t' + xkbVariant;
this._iconActor.icon_name = item._icon_name; Util.spawn(['gkbd-keyboard-display', '-l', description]);
this._selectedLabel = this._labelActors[selected];
this._container.set_skip_paint(this._selectedLabel, this._showFlags);
this._selectedLayout = item;
}, },
_containerGetPreferredWidth: function(container, for_height, alloc) { _containerGetPreferredWidth: function(container, for_height, alloc) {
@ -177,15 +193,11 @@ const XKBIndicator = new Lang.Class({
// for the height of all children, but we ignore the results // for the height of all children, but we ignore the results
// for those we don't actually display. // for those we don't actually display.
let max_min_width = 0, max_natural_width = 0; let max_min_width = 0, max_natural_width = 0;
if (this._showFlags)
[max_min_width, max_natural_width] = this._iconActor.get_preferred_width(for_height);
for (let i = 0; i < this._labelActors.length; i++) { for (let i in this._labelActors) {
let [min_width, natural_width] = this._labelActors[i].get_preferred_width(for_height); let [min_width, natural_width] = this._labelActors[i].get_preferred_width(for_height);
if (!this._showFlags) { max_min_width = Math.max(max_min_width, min_width);
max_min_width = Math.max(max_min_width, min_width); max_natural_width = Math.max(max_natural_width, natural_width);
max_natural_width = Math.max(max_natural_width, natural_width);
}
} }
alloc.min_size = max_min_width; alloc.min_size = max_min_width;
@ -194,15 +206,11 @@ const XKBIndicator = new Lang.Class({
_containerGetPreferredHeight: function(container, for_width, alloc) { _containerGetPreferredHeight: function(container, for_width, alloc) {
let max_min_height = 0, max_natural_height = 0; let max_min_height = 0, max_natural_height = 0;
if (this._showFlags)
[max_min_height, max_natural_height] = this._iconActor.get_preferred_height(for_width); for (let i in this._labelActors) {
for (let i = 0; i < this._labelActors.length; i++) {
let [min_height, natural_height] = this._labelActors[i].get_preferred_height(for_width); let [min_height, natural_height] = this._labelActors[i].get_preferred_height(for_width);
if (!this._showFlags) { max_min_height = Math.max(max_min_height, min_height);
max_min_height = Math.max(max_min_height, min_height); max_natural_height = Math.max(max_natural_height, natural_height);
max_natural_height = Math.max(max_natural_height, natural_height);
}
} }
alloc.min_size = max_min_height; alloc.min_size = max_min_height;
@ -216,8 +224,7 @@ const XKBIndicator = new Lang.Class({
box.y2 -= box.y1; box.y2 -= box.y1;
box.y1 = 0; box.y1 = 0;
this._iconActor.allocate_align_fill(box, 0.5, 0, false, false, flags); for (let i in this._labelActors)
for (let i = 0; i < this._labelActors.length; i++)
this._labelActors[i].allocate_align_fill(box, 0.5, 0, false, false, flags); this._labelActors[i].allocate_align_fill(box, 0.5, 0, false, false, flags);
} }
}); });