From b07f97c35e1028eac596720166933d73662a857c Mon Sep 17 00:00:00 2001 From: Michael Webster Date: Sat, 30 Mar 2013 23:46:01 -0400 Subject: [PATCH] Use current keybinding to close menu when it's open. Update keybinding when gsettings key changes. Improve keybinding setter in preferences to allow teaching the keybinding. Needs further improvement - a way to use/assign single modifiers. Added a combobox temporarily, not happy with it yet... maybe buttons instead... --- usr/lib/linuxmint/mintMenu/keybinding.py | 115 +++++++++++++++++- usr/lib/linuxmint/mintMenu/mintMenu.py | 19 +-- .../linuxmint/mintMenu/mintMenuConfig.glade | 33 +---- usr/lib/linuxmint/mintMenu/mintMenuConfig.py | 9 +- 4 files changed, 129 insertions(+), 47 deletions(-) diff --git a/usr/lib/linuxmint/mintMenu/keybinding.py b/usr/lib/linuxmint/mintMenu/keybinding.py index 52cd31c..1de1bac 100644 --- a/usr/lib/linuxmint/mintMenu/keybinding.py +++ b/usr/lib/linuxmint/mintMenu/keybinding.py @@ -34,6 +34,7 @@ from ctypes import * import capi gdk = CDLL("libgdk-x11-2.0.so.0") +gtk = CDLL("libgtk-x11-2.0.so.0") class GlobalKeyBinding(GObject.GObject, threading.Thread): __gsignals__ = { @@ -51,6 +52,19 @@ class GlobalKeyBinding(GObject.GObject, threading.Thread): self.root = self.screen.root self.ignored_masks = self.get_mask_combinations(X.LockMask | X.Mod2Mask | X.Mod5Mask) self.map_modifiers() + self.raw_keyval = None + + def is_hotkey(self, key, modifier): + keymatch = False + modmatch = False + modint = int(modifier) + if self.get_keycode(key) == self.keycode: + keymatch = True + for ignored_mask in self.ignored_masks: + if self.modifiers | ignored_mask == modint | ignored_mask: + modmatch = True + break + return keymatch and modmatch def map_modifiers(self): gdk_modifiers =(Gdk.ModifierType.CONTROL_MASK, Gdk.ModifierType.SHIFT_MASK, Gdk.ModifierType.MOD1_MASK, @@ -61,6 +75,13 @@ class GlobalKeyBinding(GObject.GObject, threading.Thread): if "Mod" not in Gtk.accelerator_name(0, modifier): self.known_modifiers_mask |= modifier + def get_keycode(self, keyval): + count = c_int() + array = (KeymapKey * 10)() + keys = cast(array, POINTER(KeymapKey)) + gdk.gdk_keymap_get_entries_for_keyval(hash(self.keymap), keyval, byref(keys), byref(count)) + return keys[0].keycode + def grab(self, key): accelerator = key keyval, modifiers = Gtk.accelerator_parse(accelerator) @@ -68,11 +89,8 @@ class GlobalKeyBinding(GObject.GObject, threading.Thread): self.keycode = None self.modifiers = None return False - count = c_int() - array = (KeymapKey * 10)() - keys = cast(array, POINTER(KeymapKey)) - gdk.gdk_keymap_get_entries_for_keyval(hash(self.keymap), keyval, byref(keys), byref(count)) - self.keycode = keys[0].keycode + + self.keycode = self.get_keycode(keyval) self.modifiers = int(modifiers) catch = error.CatchError(error.BadAccess) @@ -84,7 +102,7 @@ class GlobalKeyBinding(GObject.GObject, threading.Thread): return False return True - def ungrab(self, key): + def ungrab(self): if self.keycode: self.root.ungrab_key(self.keycode, X.AnyModifier, self.root) @@ -128,3 +146,88 @@ class KeymapKey(Structure): _fields_ = [("keycode", c_uint), ("group", c_int), ("level", c_int)] + + +class KeybindingWidget(Gtk.HBox): + __gsignals__ = { + 'accel-edited': (GObject.SignalFlags.RUN_LAST, None, ()), + } + def __init__(self, desc): + super(KeybindingWidget, self).__init__() + self.desc = desc + self.label = Gtk.Label(desc) + self.model = Gtk.ListStore(str, object) + self.tree = Gtk.TreeView.new() + self.tree.set_headers_visible(False) + self.cell = Gtk.CellRendererAccel() + self.cell.set_alignment(.5, .5) + self.change_id = self.cell.connect('accel-edited', self.on_my_value_changed) + self.cell.set_property('editable', True) + self.cell.set_property('accel-mode', Gtk.CellRendererAccelMode.OTHER) + col = Gtk.TreeViewColumn("col", self.cell, text=0) + col.set_min_width(200) + col.set_alignment(.5) + self.tree.append_column(col) + self.value = "" + self.tree.set_model(self.model) + self.model.append((self.value, self)) + if self.desc != "": + self.pack_start(self.label, False, False, 0) + shadow = Gtk.Frame() + shadow.set_shadow_type(Gtk.ShadowType.ETCHED_IN) + shadow.add(self.tree) + self.pack_start(shadow, False, False, 2) + + self.combomodel = Gtk.ListStore(str, str) + + customs = (["Select a single modifier...", "x"], + ["Left Super", "Super_L" ], + ["Right Super", "Super_R" ], + ["Left Control", "Control_L" ], + ["Right Control", "Control_R" ], + ["Left Alt", "Alt_L" ], + ["Right Alt", "Alt_R" ]) + + first = None + for option in customs: + iter = self.combomodel.insert_before(None, None) + self.combomodel.set_value(iter, 0, option[0]) + self.combomodel.set_value(iter, 1, option[1]) + if first is None: + first = iter + + self.combo = Gtk.ComboBox.new_with_model(self.combomodel) + renderer_text = Gtk.CellRendererText() + self.combo.pack_start(renderer_text, True) + self.combo.add_attribute(renderer_text, "text", 0) + + self.combo.set_active_iter(first) + self.combo.connect('changed', self.on_combo_changed) + + self.pack_start(self.combo, False, False, 0) + + self.show_all() + + def on_combo_changed(self, widget): + tree_iter = widget.get_active_iter() + if tree_iter != None: + value = self.combomodel[tree_iter][1] + self.set_val(value) + self.emit("accel-edited") + + def on_my_value_changed(self, cell, path, keyval, mask, keycode): + gtk.gtk_accelerator_name.restype = c_char_p + accel_string = gtk.gtk_accelerator_name(keyval, mask) + accel_string = accel_string.replace("", "") + self.value = accel_string + self.emit("accel-edited") + + def get_val(self): + return self.value + + def set_val(self, value): + self.cell.handler_block(self.change_id) + self.value = value + self.model.clear() + self.model.append((self.value, self)) + self.cell.handler_unblock(self.change_id) diff --git a/usr/lib/linuxmint/mintMenu/mintMenu.py b/usr/lib/linuxmint/mintMenu/mintMenu.py index 1060542..4c05f1e 100755 --- a/usr/lib/linuxmint/mintMenu/mintMenu.py +++ b/usr/lib/linuxmint/mintMenu/mintMenu.py @@ -64,10 +64,10 @@ from execute import * class MainWindow( object ): """This is the main class for the application""" - def __init__( self, toggleButton, settings ): + def __init__( self, toggleButton, settings, keybinder ): self.settings = settings - + self.keybinder = keybinder self.path = PATH sys.path.append( os.path.join( self.path, "plugins") ) @@ -126,7 +126,6 @@ class MainWindow( object ): self.settings.connect( "changed::border-width", self.toggleBorderWidth ) self.settings.connect( "changed::opacity", self.toggleOpacity ) - def on_window1_destroy (self, widget, data=None): Gtk.main_quit() sys.exit(0) @@ -471,8 +470,9 @@ class MainWindow( object ): plugin.onHideMenu() def onKeyPress( self, widget, event ): - if event.keyval == Gdk.KEY_Escape or event.keyval == Gdk.KEY_Super_L: + if event.keyval == Gdk.KEY_Escape or self.keybinder.is_hotkey(event.keyval, event.get_state()): self.hide() + return True return False def onButtonPress( self, widget, event ): @@ -514,13 +514,14 @@ class MenuWin( object ): def __init__( self, applet, iid ): self.applet = applet self.settings = Gio.Settings.new("com.linuxmint.mintmenu") - + self.keybinder = keybinding.GlobalKeyBinding() self.settings.connect( "changed::applet-text", self.reloadSettings ) self.settings.connect( "changed::theme-name", self.changeTheme ) self.settings.connect( "changed::hot-key", self.reloadSettings ) self.settings.connect( "changed::applet-icon", self.reloadSettings ) self.settings.connect( "changed::hide-applet-icon", self.reloadSettings ) self.settings.connect( "changed::applet-icon-size", self.reloadSettings ) + self.settings.connect( "changed::hot-key", self.hotkeyChanged ) self.loadSettings() mate_settings = Gio.Settings.new("org.mate.interface") @@ -534,7 +535,7 @@ class MenuWin( object ): self.applet.connect( "change-background", self.changeBackground ) self.applet.connect("enter-notify-event", self.enter_notify) self.applet.connect("leave-notify-event", self.leave_notify) - self.mainwin = MainWindow( self.button_box, self.settings ) + self.mainwin = MainWindow( self.button_box, self.settings, self.keybinder ) self.mainwin.window.connect( "map-event", lambda *args: self.applet.set_state( Gtk.StateType.SELECTED ) ) self.mainwin.window.connect( "unmap-event", lambda *args: self.applet.set_state( Gtk.StateType.NORMAL ) ) self.mainwin.window.connect( "size-allocate", lambda *args: self.positionMenu() ) @@ -692,7 +693,6 @@ class MenuWin( object ): def bind_hot_key (self): try: - self.keybinder = keybinding.GlobalKeyBinding() self.keybinder.grab( self.hotkeyText ) self.keybinder.connect("activate", self.onBindingPress) self.keybinder.start() @@ -704,6 +704,11 @@ class MenuWin( object ): print "Error Report :\n", str(cause) pass + def hotkeyChanged (self, schema, key): + self.keybinder.ungrab() + self.hotkeyText = self.settings.get_string( "hot-key" ) + self.keybinder.grab(self.hotkeyText) + def sizeButton( self ): if self.hideIcon: self.button_icon.hide() diff --git a/usr/lib/linuxmint/mintMenu/mintMenuConfig.glade b/usr/lib/linuxmint/mintMenu/mintMenuConfig.glade index 3f45639..bad374e 100644 --- a/usr/lib/linuxmint/mintMenu/mintMenuConfig.glade +++ b/usr/lib/linuxmint/mintMenu/mintMenuConfig.glade @@ -358,7 +358,7 @@ 10 10 - + True False 4 @@ -399,18 +399,7 @@ - - True - False - 0 - Keyboard shortcut: - - - 2 - 3 - GTK_FILL - - + @@ -447,23 +436,7 @@ - - True - True - - False - False - True - True - - - 1 - 2 - 2 - 3 - GTK_FILL - - + diff --git a/usr/lib/linuxmint/mintMenu/mintMenuConfig.py b/usr/lib/linuxmint/mintMenu/mintMenuConfig.py index c663ba3..1e90b13 100755 --- a/usr/lib/linuxmint/mintMenu/mintMenuConfig.py +++ b/usr/lib/linuxmint/mintMenu/mintMenuConfig.py @@ -6,6 +6,7 @@ import gi gi.require_version("Gtk", "2.0") from gi.repository import Gtk, Gdk +import keybinding try: @@ -114,8 +115,6 @@ class mintMenuConfig( object ): self.newPlaceDialogTitle = (_("New Place")) self.folderChooserDialogTitle = (_("Select a folder")) - self.builder.get_object("hotkey_label").set_text(_("Keyboard shortcut:")) - self.startWithFavorites = self.builder.get_object( "startWithFavorites" ) self.showAppComments = self.builder.get_object( "showAppComments" ) self.useAPT = self.builder.get_object( "use_apt" ) @@ -143,7 +142,9 @@ class mintMenuConfig( object ): self.headingColorLabel = self.builder.get_object( "headingColorLabel" ) self.showButtonIcon = self.builder.get_object( "showButtonIcon" ) self.buttonText = self.builder.get_object( "buttonText" ) - self.hotkeyText = self.builder.get_object( "hotkeyText" ) + self.hotkeyWidget = keybinding.KeybindingWidget(_("Keyboard shortcut:")) + table = self.builder.get_object( "main_table" ) + table.attach(self.hotkeyWidget, 0, 2, 2, 3, Gtk.AttachOptions.FILL, Gtk.AttachOptions.FILL, 0, 0) self.buttonIcon = self.builder.get_object( "buttonIcon" ) self.buttonIconChooser = self.builder.get_object( "button_icon_chooser" ) self.image_filter = Gtk.FileFilter() @@ -221,7 +222,7 @@ class mintMenuConfig( object ): self.bindGSettingsValueToWidget( self.settings, "color", "custom-border-color", self.borderColor, "color-set", self.borderColor.set_color, self.getBorderColor ) self.bindGSettingsValueToWidget( self.settings, "bool", "hide-applet-icon", self.showButtonIcon, "toggled", self.setShowButtonIcon, self.getShowButtonIcon ) self.bindGSettingsValueToWidget( self.settings, "string", "applet-text", self.buttonText, "changed", self.buttonText.set_text, self.buttonText.get_text ) - self.bindGSettingsValueToWidget( self.settings, "string", "hot-key", self.hotkeyText, "changed", self.hotkeyText.set_text, self.hotkeyText.get_text ) + self.bindGSettingsValueToWidget( self.settings, "string", "hot-key", self.hotkeyWidget, "accel-edited", self.hotkeyWidget.set_val, self.hotkeyWidget.get_val ) self.bindGSettingsValueToWidget( self.settings, "string", "applet-icon", self.buttonIconChooser, "file-set", self.setButtonIcon, self.buttonIconChooser.get_filename ) self.bindGSettingsValueToWidget( self.settingsApplications, "string", "search-command", self.searchCommand, "changed", self.searchCommand.set_text, self.searchCommand.get_text )