From ab605b896fc3a2f513fbba32f5e03cd5aa6505f2 Mon Sep 17 00:00:00 2001 From: ivoarch Date: Wed, 8 Feb 2017 15:34:09 +0100 Subject: [PATCH] Fix #1 --- README.md | 6 ++- extension.js | 83 +++++++++++++++++++++-------------- metadata.json | 5 ++- prefs.js | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++ utils.js | 40 +++++++++++++++++ 5 files changed, 216 insertions(+), 36 deletions(-) create mode 100644 prefs.js create mode 100755 utils.js diff --git a/README.md b/README.md index 4d96829..89a33d3 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,9 @@ restart GNOME Shell (`Alt+F2 r Enter`) and enable the extension through gnome-tw ### Custom hotkey -Default shortcut is (F10) , to setup custom shortcut key edit **schemas/org.gnome.shell.extensions.terminix-dropdown.gschema.xml** schema file and compile with `glib-compile-schemas .` inside the directory . +Change keyboard shortcut key via extension settings from gnome-tweak-tool . -**note:** Make sure you have terminix installed! +## Note + +Make sure you have terminix installed! diff --git a/extension.js b/extension.js index 4fec480..a61eda3 100644 --- a/extension.js +++ b/extension.js @@ -1,4 +1,4 @@ -const ExtensionUtils = imports.misc.extensionUtils; +// Library imports const Gio = imports.gi.Gio; const Lang = imports.lang; const Main = imports.ui.main; @@ -6,14 +6,19 @@ const Meta = imports.gi.Meta; const Shell = imports.gi.Shell; const St = imports.gi.St; const Tweener = imports.ui.tweener; -const Util = imports.misc.util; -let text, button; +// Extension imports +const Utils = imports.misc.extensionUtils.getCurrentExtension().imports.utils; +const mySettings = Utils.getSettings(); -const KEY_SETTING_NAME = "key"; +// Globals +const key_bindings = { + 'key': function(){ + _startTerminix(); + } +}; -// 3.14 compatibility -const ShellActionMode = (Shell.ActionMode)?Shell.ActionMode:Shell.KeyBindingMode; +let text, button; function init(extensionMeta) { button = new St.Bin({ @@ -41,36 +46,50 @@ function _startTerminix() { } } -function loadSettings() { - let extension = ExtensionUtils.getCurrentExtension(); - - let schemaSource = Gio.SettingsSchemaSource.new_from_directory( - extension.dir.get_child('schemas').get_path(), - Gio.SettingsSchemaSource.get_default(), - false); - - let schema = schemaSource.lookup(extension.metadata['settings-schema'], true); - if (!schema) - throw new Error('Schema ' + schema + ' could not be found'); - - return new Gio.Settings({ settings_schema: schema }); -} - function enable() { - let settings = loadSettings(); - Main.wm.addKeybinding( - KEY_SETTING_NAME, - settings, - Meta.KeyBindingFlags.NONE, - //Shell.ActionMode.NORMAL, - //Shell.KeyBindingMode.NORMAL, - ShellActionMode.NORMAL, - _startTerminix); - let children = Main.panel._rightBox.get_children(); + let key; + for (key in key_bindings) { + if (Main.wm.addKeybinding && Shell.ActionMode) { // introduced in 3.16 + Main.wm.addKeybinding( + key, + mySettings, + Meta.KeyBindingFlags.NONE, + Shell.ActionMode.NORMAL, + key_bindings[key] + ); + } + else if (Main.wm.addKeybinding && Shell.KeyBindingMode) { // introduced in 3.7.5 + Main.wm.addKeybinding( + key, + mySettings, + Meta.KeyBindingFlags.NONE, + Shell.KeyBindingMode.NORMAL | Shell.KeyBindingMode.MESSAGE_TRAY, + key_bindings[key] + ); + } + else { + global.display.add_keybinding( + key, + mySettings, + Meta.KeyBindingFlags.NONE, + key_bindings[key] + ); + } + } + Main.panel._rightBox.insert_child_at_index(button, 0); } function disable() { - Main.wm.removeKeybinding(KEY_SETTING_NAME); + let key; + for (key in key_bindings) { + if (Main.wm.removeKeybinding) { // introduced in 3.7.2 + Main.wm.removeKeybinding(key); + } + else { + global.display.remove_keybinding(key); + } + } + Main.panel._rightBox.remove_child(button); } diff --git a/metadata.json b/metadata.json index a569754..3e3bf0d 100644 --- a/metadata.json +++ b/metadata.json @@ -1,5 +1,5 @@ { - "description": "Open Terminix in Quake mode .\n Credit to extensions.gnome.org/extension/877/empathy-button/ .\n Add keyboard shortcut (F10)", + "description": "Open Terminix in Quake mode .\n Add default keyboard shortcut (F10) .\n Easy way to change keyboard shortcut key via extension settings", "name": "Terminix DropDown", "shell-version": [ "3.14", @@ -10,7 +10,8 @@ ], "url": "https://github.com/ivoarch/gnome-shell-TerminixDropdown", "uuid": "TerminixDropdown@ivkuzev@gmail.com", - "version": 2, + "version": 3, "settings-schema": "org.gnome.shell.extensions.terminix-dropdown" } + diff --git a/prefs.js b/prefs.js new file mode 100644 index 0000000..5fbe96c --- /dev/null +++ b/prefs.js @@ -0,0 +1,118 @@ +// Library imports +const GObject = imports.gi.GObject; +const Gdk = imports.gi.Gdk; +const Gtk = imports.gi.Gtk; + +// Extension imports +const Utils = imports.misc.extensionUtils.getCurrentExtension().imports.utils; +const mySettings = Utils.getSettings(); + +// Globals +const pretty_names = { + 'key': 'start terminix in quake mode' +} + +function init() { +} + +function buildPrefsWidget() { + let model = new Gtk.ListStore(); + + model.set_column_types([ + GObject.TYPE_STRING, + GObject.TYPE_STRING, + GObject.TYPE_INT, + GObject.TYPE_INT + ]); + + global.log("Modal created."); + + let settings = Utils.getSettings(); + + for (key in pretty_names) { + append_hotkey(model, settings, key, pretty_names[key]); + } + + global.log("Added hotkeys to model"); + + let treeview = new Gtk.TreeView({ + 'expand': true, + 'model': model + }); + + global.log("TreeView created."); + + let col; + let cellrend; + + cellrend = new Gtk.CellRendererText(); + + col = new Gtk.TreeViewColumn({ + 'title': 'Keybinding', + 'expand': true + }); + + col.pack_start(cellrend, true); + col.add_attribute(cellrend, 'text', 1); + + + treeview.append_column(col); + + global.log("Column one created."); + + cellrend = new Gtk.CellRendererAccel({ + 'editable': true, + 'accel-mode': Gtk.CellRendererAccelMode.GTK + }); + + cellrend.connect('accel-edited', function(rend, iter, key, mods) { + let value = Gtk.accelerator_name(key, mods); + + let [success, iter] = model.get_iter_from_string(iter); + + if (!success) { + throw new Error("Something be broken, yo."); + } + + let name = model.get_value(iter, 0); + + model.set(iter, [ 2, 3 ], [ mods, key ]); + + global.log("Changing value for " + name + ": " + value); + + settings.set_strv(name, [value]); + }); + + col = new Gtk.TreeViewColumn({ + 'title': 'Accel' + }); + + col.pack_end(cellrend, false); + col.add_attribute(cellrend, 'accel-mods', 2); + col.add_attribute(cellrend, 'accel-key', 3); + + treeview.append_column(col); + + global.log("Column two created."); + + let win = new Gtk.ScrolledWindow({ + 'vexpand': true + }); + win.add(treeview); + + global.log("ScrolledWindow created."); + + win.show_all(); + + global.log("Returning."); + + return win; +} + +function append_hotkey(model, settings, name, pretty_name) { + let [key, mods] = Gtk.accelerator_parse(settings.get_strv(name)[0]); + + let row = model.insert(10); + + model.set(row, [0, 1, 2, 3], [name, pretty_name, mods, key ]); +} diff --git a/utils.js b/utils.js new file mode 100755 index 0000000..38a177f --- /dev/null +++ b/utils.js @@ -0,0 +1,40 @@ +const Gio = imports.gi.Gio; +const Config = imports.misc.config; +const ExtensionUtils = imports.misc.extensionUtils; + +/** + * getSettings: + * @schema: (optional): the GSettings schema id + * + * Builds and return a GSettings schema for @schema, using schema files + * in extensionsdir/schemas. If @schema is not provided, it is taken from + * metadata['settings-schema']. + */ +function getSettings(schema) { + let extension = ExtensionUtils.getCurrentExtension(); + + schema = schema || extension.metadata['settings-schema']; + + const GioSSS = Gio.SettingsSchemaSource; + + // check if this extension was built with "make zip-file", and thus + // has the schema files in a subfolder + // otherwise assume that extension has been installed in the + // same prefix as gnome-shell (and therefore schemas are available + // in the standard folders) + 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 }); +}