Skip to content

Instantly share code, notes, and snippets.

@yoshinari-nomura
Created May 17, 2022 04:14
Show Gist options
  • Save yoshinari-nomura/c7f0b68606c2c3dbd745a15f643b9810 to your computer and use it in GitHub Desktop.
Save yoshinari-nomura/c7f0b68606c2c3dbd745a15f643b9810 to your computer and use it in GitHub Desktop.
// Copyright (C) 2022 Takashi Kokubun
// Licence: GPLv2+
const { Gio } = imports.gi;
class Xremap {
constructor() {
}
enable() {
const dbus_object = `
<node>
<interface name="com.k0kubun.Xremap">
<method name="ActiveWindow">
<arg type="s" direction="out" name="win"/>
</method>
<method name="WMClass">
<arg type="s" direction="out" name="win"/>
</method>
<method name="SetFocusByWMClass">
<arg type="s" direction="in" name="wm_class"/>
</method>
</interface>
</node>
`;
this.dbus = Gio.DBusExportedObject.wrapJSObject(dbus_object, this);
this.dbus.export(Gio.DBus.session, '/com/k0kubun/Xremap');
}
disable() {
this.dbus.flush();
this.dbus.unexport();
delete this.dbus;
}
ActiveWindow() {
const actor = global.get_window_actors().find(a=>a.meta_window.has_focus()===true);
if (actor) {
const w = actor.get_meta_window();
return JSON.stringify({ wm_class: w.get_wm_class(), title: w.get_title() });
} else {
return '{}';
}
}
WMClass() {
const actor = global.get_window_actors().find(a=>a.meta_window.has_focus()===true);
return actor && actor.get_meta_window().get_wm_class();
}
SetFocusByWMClass(wm_class) {
const window = global.get_window_actors().map(a=>a.meta_window).find(w=>w.get_wm_class() === wm_class && !w.has_focus());
return window && window.activate(global.get_current_time());
}
}
function init() {
return new Xremap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment