Skip to content

Instantly share code, notes, and snippets.

@Envek
Created November 22, 2021 17:50
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Envek/85f40478d1c8b9658621190569046447 to your computer and use it in GitHub Desktop.
Save Envek/85f40478d1c8b9658621190569046447 to your computer and use it in GitHub Desktop.
GNOME Shell extension to switch between two most recently used keyboard layouts
// Installation:
// 1. mkdir -p ~/.local/share/gnome-shell/extensions/switch-to-last-keyboard-layout@envek
// 2. cp ./extension.js ./metadata.json ~/.local/share/gnome-shell/extensions/switch-to-last-keyboard-layout@envek
// 3. Restart GNOME Shell (e.g. log out and log in)
// 4. Enable it in the GNOME Extensions App
// Usage:
// gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/SwitchToLastKeyboardLayout --method org.gnome.Shell.Extensions.SwitchToLastKeyboardLayout.Call
const { Gio } = imports.gi;
const { getInputSourceManager } = imports.ui.status.keyboard;
const MR_DBUS_IFACE = `
<node>
<interface name="org.gnome.Shell.Extensions.SwitchToLastKeyboardLayout">
<method name="Call">
</method>
</interface>
</node>`;
class Extension {
enable() {
this._dbus = Gio.DBusExportedObject.wrapJSObject(MR_DBUS_IFACE, this);
this._dbus.export(Gio.DBus.session, '/org/gnome/Shell/Extensions/SwitchToLastKeyboardLayout');
}
disable() {
this._dbus.flush();
this._dbus.unexport();
delete this._dbus;
}
Call() {
getInputSourceManager()._mruSources[1].activate()
}
}
function init() {
return new Extension();
}
{
"name": "Switch to last keyboard layout",
"description": "Switch to latest previously used keyboard layout",
"uuid": "switch-to-last-keyboard-layout@envek",
"shell-version": [
"41"
]
}
@PerchunPak
Copy link

GNOME 45 version:

// Installation:
//  1. mkdir -p ~/.local/share/gnome-shell/extensions/switch-to-last-keyboard-layout@envek
//  2. cp ./extension.js ./metadata.json ~/.local/share/gnome-shell/extensions/switch-to-last-keyboard-layout@envek
//  3. Restart GNOME Shell (e.g. log out and log in)
//  4. Enable it in the GNOME Extensions App
// Usage:
// gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell/Extensions/SwitchToLastKeyboardLayout --method org.gnome.Shell.Extensions.SwitchToLastKeyboardLayout.Call

import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
import Gio from "gi://Gio";
import { getInputSourceManager } from "resource:///org/gnome/shell/ui/status/keyboard.js";

const MR_DBUS_IFACE = `
<node>
    <interface name="org.gnome.Shell.Extensions.SwitchToLastKeyboardLayout">
        <method name="Call">
        </method>
    </interface>
</node>`;

export default class MyExtension extends Extension {
    enable() {
        this._dbus = Gio.DBusExportedObject.wrapJSObject(MR_DBUS_IFACE, this);
        this._dbus.export(Gio.DBus.session, '/org/gnome/Shell/Extensions/SwitchToLastKeyboardLayout');
    }

    disable() {
        this._dbus.flush();
        this._dbus.unexport();
        delete this._dbus;
    }

    Call() {
        getInputSourceManager()._mruSources[1].activate()
    }
}

@Envek
Copy link
Author

Envek commented Jan 29, 2024

Thanks for sharing the updated version, @PerchunPak!

I realised that I'm not using this anymore, instead I switched to this one published in Gnome Extensions: https://extensions.gnome.org/extension/4042/switch-two-layouts/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment