Skip to content

Instantly share code, notes, and snippets.

@kiwanami
Last active December 26, 2015 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiwanami/7121125 to your computer and use it in GitHub Desktop.
Save kiwanami/7121125 to your computer and use it in GitHub Desktop.
InputSourceを素早く切り替えたり、特定のウインドウでは何もしないような gnome-shell extension。以前のよくあるIMの動作を再現する。gnome-shell-extension-tool -c で作って、xmlはschemasディレクトリを作ってglib-compile-schemas schemasとかでコンパイルする感じで。
// -*- coding: utf-8; -*-
// for gnome-shell 3.16.2
const Gio = imports.gi.Gio;
const Shell = imports.gi.Shell;
const Meta = imports.gi.Meta;
const St = imports.gi.St;
const Main = imports.ui.main;
const ExtensionUtils = imports.misc.extensionUtils;
function getSettings(schema) {
let extension = ExtensionUtils.getCurrentExtension();
schema = schema || extension.metadata['settings-schema'];
const GioSSS = Gio.SettingsSchemaSource;
let schemaDir = extension.dir.get_child('schemas');
let schemaSource;
if (schemaDir.query_exists(null))
schemaSource = GioSSS.new_from_directory(schemaDir.get_path(),
GioSSS.get_default(),
false);
else
schemaSource = GioSSS.get_default();
let schemaObj = schemaSource.lookup(schema, true);
if (!schemaObj)
throw new Error('Schema ' + schema + ' could not be found for extension '
+ extension.metadata.uuid + '. Please check your installation.');
return new Gio.Settings({ settings_schema: schemaObj });
}
// Event handlers for InputSources
function _switchInputSource(display, screen, window, binding) {
//log("SwitchInputSource!");
// hard coding!!
var ism = Main.panel.statusArea.keyboard._inputSourceManager;
var current = ism.currentSource;
var sources = ism.inputSources;
for (var i in sources) {
if (sources[i] != current) {
sources[i].activate();
break;
}
}
}
// Binding and Unbinding
let enabledFlag = false;
function _enable() {
//log(">> Enable");
if (!enabledFlag) {
try {
Main.wm.addKeybinding(
'custom-switch-is-key',
getSettings("org.gnome.shell.extensions.custom-switch-is"),
Meta.KeyBindingFlags.IS_REVERSED,
Shell.ActionMode.ALL,_switchInputSource);
//log(">> Enable OK");
} catch (e) {
log(e.message);
} finally {
enabledFlag = true;
}
}
}
function _disable() {
//log(">> Disable");
try {
Main.wm.removeKeybinding('custom-switch-is-key');
} catch (e) {
log(e.message);
} finally {
enabledFlag = false;
}
}
function _windowsRestacked() {
if (!Main.overview.visible || Main.overview.animationInProgress) {
if (global.display.focus_window && global.display.focus_window.get_wm_class() == "Emacs") {
_disable();
} else {
_enable();
}
} else {
_enable();
}
}
// Initialize and Cleanup
let initState = null;
function _init() {
if (initState) return;
initState = {};
initState.idRestacked = global.screen.connect('restacked', _windowsRestacked);
}
function _cleanup() {
if (!initState) return;
global.screen.disconnect(initState.idRestacked);
initState = null;
}
function init() {
// do nothing
}
function enable() {
_init();
_enable();
}
function disable() {
_disable();
_cleanup();
}
<?xml version="1.0" encoding="UTF-8"?>
<schemalist gettext-domain="gnome-shell-extensions">
<schema path="/org/gnome/shell/extensions/custom-switch-is/" id="org.gnome.shell.extensions.custom-switch-is">
<key type="as" name="custom-switch-is-key">
<default><![CDATA[['<Control>o']]]></default>
<summary>Switch InputSource Key</summary>
</key>
</schema>
</schemalist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment