status/keyboard: Make input sources be proper objects
Introduce an InputSource class which makes it easier to keep track of all the data we have about them. https://bugzilla.gnome.org/show_bug.cgi?id=682318
This commit is contained in:
parent
3bd5563a7e
commit
c41424b57b
@ -181,6 +181,50 @@ const LayoutMenuItem = new Lang.Class({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const InputSource = new Lang.Class({
|
||||||
|
Name: 'InputSource',
|
||||||
|
|
||||||
|
_init: function(type, id, displayName, shortName, index) {
|
||||||
|
this.type = type;
|
||||||
|
this.id = id;
|
||||||
|
this.displayName = displayName;
|
||||||
|
this._shortName = shortName;
|
||||||
|
this.index = index;
|
||||||
|
|
||||||
|
this._menuItem = new LayoutMenuItem(this.displayName, this._shortName);
|
||||||
|
this._menuItem.connect('activate', Lang.bind(this, this.activate));
|
||||||
|
this._indicatorLabel = new St.Label({ text: this._shortName });
|
||||||
|
},
|
||||||
|
|
||||||
|
destroy: function() {
|
||||||
|
this._menuItem.destroy();
|
||||||
|
this._indicatorLabel.destroy();
|
||||||
|
},
|
||||||
|
|
||||||
|
get shortName() {
|
||||||
|
return this._shortName;
|
||||||
|
},
|
||||||
|
|
||||||
|
set shortName(v) {
|
||||||
|
this._shortName = v;
|
||||||
|
this._menuItem.indicator.set_text(v);
|
||||||
|
this._indicatorLabel.set_text(v);
|
||||||
|
},
|
||||||
|
|
||||||
|
get menuItem() {
|
||||||
|
return this._menuItem;
|
||||||
|
},
|
||||||
|
|
||||||
|
get indicatorLabel() {
|
||||||
|
return this._indicatorLabel;
|
||||||
|
},
|
||||||
|
|
||||||
|
activate: function() {
|
||||||
|
this.emit('activate');
|
||||||
|
},
|
||||||
|
});
|
||||||
|
Signals.addSignalMethods(InputSource.prototype);
|
||||||
|
|
||||||
const InputSourceIndicator = new Lang.Class({
|
const InputSourceIndicator = new Lang.Class({
|
||||||
Name: 'InputSourceIndicator',
|
Name: 'InputSourceIndicator',
|
||||||
Extends: PanelMenu.Button,
|
Extends: PanelMenu.Button,
|
||||||
@ -195,14 +239,16 @@ const InputSourceIndicator = 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._labelActors = {};
|
// All valid input sources currently in the gsettings
|
||||||
this._layoutItems = {};
|
// KEY_INPUT_SOURCES list indexed by their index there
|
||||||
|
this._inputSources = {};
|
||||||
|
|
||||||
|
this._currentSource = null;
|
||||||
|
|
||||||
this._settings = new Gio.Settings({ schema: DESKTOP_INPUT_SOURCES_SCHEMA });
|
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_CURRENT_INPUT_SOURCE, Lang.bind(this, this._currentInputSourceChanged));
|
||||||
this._settings.connect('changed::' + KEY_INPUT_SOURCES, Lang.bind(this, this._inputSourcesChanged));
|
this._settings.connect('changed::' + KEY_INPUT_SOURCES, Lang.bind(this, this._inputSourcesChanged));
|
||||||
|
|
||||||
this._currentSourceIndex = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
|
|
||||||
this._xkbInfo = new GnomeDesktop.XkbInfo();
|
this._xkbInfo = new GnomeDesktop.XkbInfo();
|
||||||
|
|
||||||
this._propSeparator = new PopupMenu.PopupSeparatorMenuItem();
|
this._propSeparator = new PopupMenu.PopupSeparatorMenuItem();
|
||||||
@ -236,17 +282,17 @@ const InputSourceIndicator = new Lang.Class({
|
|||||||
},
|
},
|
||||||
|
|
||||||
_currentInputSourceChanged: function() {
|
_currentInputSourceChanged: function() {
|
||||||
let nVisibleSources = Object.keys(this._layoutItems).length;
|
let nVisibleSources = Object.keys(this._inputSources).length;
|
||||||
let newCurrentSourceIndex = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
|
let newSourceIndex = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
|
||||||
let newLayoutItem = this._layoutItems[newCurrentSourceIndex];
|
let newSource = this._inputSources[newSourceIndex];
|
||||||
let hasProperties;
|
let hasProperties;
|
||||||
|
|
||||||
if (newLayoutItem)
|
if (newSource)
|
||||||
hasProperties = this._ibusManager.hasProperties(newLayoutItem.ibusEngineId);
|
hasProperties = this._ibusManager.hasProperties(newSource.id);
|
||||||
else
|
else
|
||||||
hasProperties = false;
|
hasProperties = false;
|
||||||
|
|
||||||
if (!newLayoutItem || (nVisibleSources < 2 && !hasProperties)) {
|
if (!newSource || (nVisibleSources < 2 && !hasProperties)) {
|
||||||
// This source index might be invalid if we weren't able
|
// This source index might be invalid if we weren't able
|
||||||
// to build a menu item for it, so we hide ourselves since
|
// to build a menu item for it, so we hide ourselves since
|
||||||
// we can't fix it here. *shrug*
|
// we can't fix it here. *shrug*
|
||||||
@ -260,88 +306,79 @@ const InputSourceIndicator = new Lang.Class({
|
|||||||
|
|
||||||
this.actor.show();
|
this.actor.show();
|
||||||
|
|
||||||
if (this._layoutItems[this._currentSourceIndex]) {
|
let oldSource;
|
||||||
this._layoutItems[this._currentSourceIndex].setShowDot(false);
|
[oldSource, this._currentSource] = [this._currentSource, newSource];
|
||||||
this._container.set_skip_paint(this._labelActors[this._currentSourceIndex], true);
|
|
||||||
|
if (oldSource) {
|
||||||
|
oldSource.menuItem.setShowDot(false);
|
||||||
|
this._container.set_skip_paint(oldSource.indicatorLabel, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
newLayoutItem.setShowDot(true);
|
|
||||||
|
|
||||||
let newLabelActor = this._labelActors[newCurrentSourceIndex];
|
|
||||||
this._container.set_skip_paint(newLabelActor, false);
|
|
||||||
|
|
||||||
if (hasProperties)
|
if (hasProperties)
|
||||||
newLabelActor.set_text(newLayoutItem.indicator.get_text());
|
newSource.indicatorLabel.set_text(newSource.shortName);
|
||||||
|
newSource.menuItem.setShowDot(true);
|
||||||
this._currentSourceIndex = newCurrentSourceIndex;
|
this._container.set_skip_paint(newSource.indicatorLabel, false);
|
||||||
},
|
},
|
||||||
|
|
||||||
_inputSourcesChanged: function() {
|
_inputSourcesChanged: function() {
|
||||||
let sources = this._settings.get_value(KEY_INPUT_SOURCES);
|
let sources = this._settings.get_value(KEY_INPUT_SOURCES);
|
||||||
let nSources = sources.n_children();
|
let nSources = sources.n_children();
|
||||||
|
|
||||||
for (let i in this._layoutItems)
|
for (let i in this._inputSources)
|
||||||
this._layoutItems[i].destroy();
|
this._inputSources[i].destroy();
|
||||||
|
|
||||||
for (let i in this._labelActors)
|
this._inputSources = {};
|
||||||
this._labelActors[i].destroy();
|
|
||||||
|
|
||||||
this._layoutItems = {};
|
let inputSourcesByShortName = {};
|
||||||
this._labelActors = {};
|
|
||||||
|
|
||||||
let infos = [];
|
|
||||||
let infosByShortName = {};
|
|
||||||
|
|
||||||
for (let i = 0; i < nSources; i++) {
|
for (let i = 0; i < nSources; i++) {
|
||||||
let info = { exists: false };
|
let displayName;
|
||||||
|
let shortName;
|
||||||
let [type, id] = sources.get_child_value(i).deep_unpack();
|
let [type, id] = sources.get_child_value(i).deep_unpack();
|
||||||
|
let exists = false;
|
||||||
|
|
||||||
if (type == INPUT_SOURCE_TYPE_XKB) {
|
if (type == INPUT_SOURCE_TYPE_XKB) {
|
||||||
[info.exists, info.displayName, info.shortName, , ] =
|
[exists, displayName, shortName, , ] =
|
||||||
this._xkbInfo.get_layout_info(id);
|
this._xkbInfo.get_layout_info(id);
|
||||||
} else if (type == INPUT_SOURCE_TYPE_IBUS) {
|
} else if (type == INPUT_SOURCE_TYPE_IBUS) {
|
||||||
let engineDesc = this._ibusManager.getEngineDesc(id);
|
let engineDesc = this._ibusManager.getEngineDesc(id);
|
||||||
if (engineDesc) {
|
if (engineDesc) {
|
||||||
let language = IBus.get_language_name(engineDesc.get_language());
|
let language = IBus.get_language_name(engineDesc.get_language());
|
||||||
|
exists = true;
|
||||||
info.exists = true;
|
displayName = language + ' (' + engineDesc.get_longname() + ')';
|
||||||
info.displayName = language + ' (' + engineDesc.get_longname() + ')';
|
shortName = this._makeEngineShortName(engineDesc);
|
||||||
info.shortName = this._makeEngineShortName(engineDesc);
|
|
||||||
info.ibusEngineId = id;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!info.exists)
|
if (!exists)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
info.sourceIndex = i;
|
let is = new InputSource(type, id, displayName, shortName, i);
|
||||||
|
|
||||||
if (!(info.shortName in infosByShortName))
|
is.connect('activate', Lang.bind(this, function() {
|
||||||
infosByShortName[info.shortName] = [];
|
|
||||||
infosByShortName[info.shortName].push(info);
|
|
||||||
infos.push(info);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
let item = new LayoutMenuItem(info.displayName, info.shortName);
|
|
||||||
item.ibusEngineId = info.ibusEngineId;
|
|
||||||
this._layoutItems[info.sourceIndex] = item;
|
|
||||||
this.menu.addMenuItem(item, i);
|
|
||||||
item.connect('activate', Lang.bind(this, function() {
|
|
||||||
this._settings.set_value(KEY_CURRENT_INPUT_SOURCE,
|
this._settings.set_value(KEY_CURRENT_INPUT_SOURCE,
|
||||||
GLib.Variant.new_uint32(info.sourceIndex));
|
GLib.Variant.new_uint32(is.index));
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let shortLabel = new St.Label({ text: info.shortName });
|
if (!(is.shortName in inputSourcesByShortName))
|
||||||
this._labelActors[info.sourceIndex] = shortLabel;
|
inputSourcesByShortName[is.shortName] = [];
|
||||||
this._container.add_actor(shortLabel);
|
inputSourcesByShortName[is.shortName].push(is);
|
||||||
this._container.set_skip_paint(shortLabel, true);
|
|
||||||
|
this._inputSources[is.index] = is;
|
||||||
|
}
|
||||||
|
|
||||||
|
let menuIndex = 0;
|
||||||
|
for (let i in this._inputSources) {
|
||||||
|
let is = this._inputSources[i];
|
||||||
|
if (inputSourcesByShortName[is.shortName].length > 1) {
|
||||||
|
let sub = inputSourcesByShortName[is.shortName].indexOf(is) + 1;
|
||||||
|
is.shortName += String.fromCharCode(0x2080 + sub);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.menu.addMenuItem(is.menuItem, menuIndex++);
|
||||||
|
|
||||||
|
this._container.add_actor(is.indicatorLabel);
|
||||||
|
this._container.set_skip_paint(is.indicatorLabel, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
this._currentInputSourceChanged();
|
this._currentInputSourceChanged();
|
||||||
@ -350,16 +387,14 @@ const InputSourceIndicator = new Lang.Class({
|
|||||||
_showLayout: function() {
|
_showLayout: function() {
|
||||||
Main.overview.hide();
|
Main.overview.hide();
|
||||||
|
|
||||||
let sources = this._settings.get_value(KEY_INPUT_SOURCES);
|
let source = this._currentSource;
|
||||||
let current = this._settings.get_uint(KEY_CURRENT_INPUT_SOURCE);
|
|
||||||
let [type, id] = sources.get_child_value(current).deep_unpack();
|
|
||||||
let xkbLayout = '';
|
let xkbLayout = '';
|
||||||
let xkbVariant = '';
|
let xkbVariant = '';
|
||||||
|
|
||||||
if (type == INPUT_SOURCE_TYPE_XKB) {
|
if (source.type == INPUT_SOURCE_TYPE_XKB) {
|
||||||
[, , , xkbLayout, xkbVariant] = this._xkbInfo.get_layout_info(id);
|
[, , , xkbLayout, xkbVariant] = this._xkbInfo.get_layout_info(source.id);
|
||||||
} else if (type == INPUT_SOURCE_TYPE_IBUS) {
|
} else if (source.type == INPUT_SOURCE_TYPE_IBUS) {
|
||||||
let engineDesc = this._ibusManager.getEngineDesc(id);
|
let engineDesc = this._ibusManager.getEngineDesc(source.id);
|
||||||
if (engineDesc) {
|
if (engineDesc) {
|
||||||
xkbLayout = engineDesc.get_layout();
|
xkbLayout = engineDesc.get_layout();
|
||||||
xkbVariant = '';
|
xkbVariant = '';
|
||||||
@ -416,16 +451,9 @@ const InputSourceIndicator = new Lang.Class({
|
|||||||
},
|
},
|
||||||
|
|
||||||
_updateIndicatorLabel: function(text) {
|
_updateIndicatorLabel: function(text) {
|
||||||
let layoutItem = this._layoutItems[this._currentSourceIndex];
|
let hasProperties = this._ibusManager.hasProperties(this._currentSource.id);
|
||||||
let hasProperties;
|
|
||||||
|
|
||||||
if (layoutItem)
|
|
||||||
hasProperties = this._ibusManager.hasProperties(layoutItem.ibusEngineId);
|
|
||||||
else
|
|
||||||
hasProperties = false;
|
|
||||||
|
|
||||||
if (hasProperties)
|
if (hasProperties)
|
||||||
this._labelActors[this._currentSourceIndex].set_text(text);
|
this._currentSource.indicatorLabel.set_text(text);
|
||||||
},
|
},
|
||||||
|
|
||||||
_buildPropSection: function() {
|
_buildPropSection: function() {
|
||||||
@ -543,8 +571,9 @@ const InputSourceIndicator = new Lang.Class({
|
|||||||
// 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;
|
||||||
|
|
||||||
for (let i in this._labelActors) {
|
for (let i in this._inputSources) {
|
||||||
let [min_width, natural_width] = this._labelActors[i].get_preferred_width(for_height);
|
let is = this._inputSources[i];
|
||||||
|
let [min_width, natural_width] = is.indicatorLabel.get_preferred_width(for_height);
|
||||||
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);
|
||||||
}
|
}
|
||||||
@ -556,8 +585,9 @@ const InputSourceIndicator = 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;
|
||||||
|
|
||||||
for (let i in this._labelActors) {
|
for (let i in this._inputSources) {
|
||||||
let [min_height, natural_height] = this._labelActors[i].get_preferred_height(for_width);
|
let is = this._inputSources[i];
|
||||||
|
let [min_height, natural_height] = is.indicatorLabel.get_preferred_height(for_width);
|
||||||
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);
|
||||||
}
|
}
|
||||||
@ -573,7 +603,9 @@ const InputSourceIndicator = new Lang.Class({
|
|||||||
box.y2 -= box.y1;
|
box.y2 -= box.y1;
|
||||||
box.y1 = 0;
|
box.y1 = 0;
|
||||||
|
|
||||||
for (let i in this._labelActors)
|
for (let i in this._inputSources) {
|
||||||
this._labelActors[i].allocate_align_fill(box, 0.5, 0, false, false, flags);
|
let is = this._inputSources[i];
|
||||||
|
is.indicatorLabel.allocate_align_fill(box, 0.5, 0, false, false, flags);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user