Add un-set keybinding action, improve key grabbing during keybinding edit

This commit is contained in:
Michael Webster 2013-04-01 19:50:50 -04:00
parent 4b963d4069
commit 0c2626d1ff
3 changed files with 36 additions and 15 deletions

View File

@ -163,16 +163,16 @@ class KeybindingWidget(Gtk.HBox):
__gsignals__ = { __gsignals__ = {
'accel-edited': (GObject.SignalFlags.RUN_LAST, None, ()), 'accel-edited': (GObject.SignalFlags.RUN_LAST, None, ()),
} }
def __init__(self, desc, mainwindow): def __init__(self, desc):
super(KeybindingWidget, self).__init__() super(KeybindingWidget, self).__init__()
self.desc = desc self.desc = desc
self.win = mainwindow
self.label = Gtk.Label(desc) self.label = Gtk.Label(desc)
if self.desc != "": if self.desc != "":
self.pack_start(self.label, False, False, 0) self.pack_start(self.label, False, False, 0)
self.button = Gtk.Button() self.button = Gtk.Button()
self.button.set_tooltip_text(_("Click to set a new accelerator key for opening and closing the menu. ") + self.button.set_tooltip_text(_("Click to set a new accelerator key for opening and closing the menu. ") +
_("Press Escape or click again to cancel the operation")) _("Press Escape or click again to cancel the operation. ") +
_("Press Backspace to clear the existing keybinding."))
self.button.connect("clicked", self.clicked) self.button.connect("clicked", self.clicked)
self.button.set_size_request(200, -1) self.button.set_size_request(200, -1)
self.pack_start(self.button, False, False, 4) self.pack_start(self.button, False, False, 4)
@ -183,29 +183,39 @@ class KeybindingWidget(Gtk.HBox):
def clicked(self, widget): def clicked(self, widget):
if not self.teaching: if not self.teaching:
Gdk.keyboard_grab(self.get_window(), False, Gdk.CURRENT_TIME)
self.button.set_label(_("Pick an accelerator")) self.button.set_label(_("Pick an accelerator"))
self.event_id = self.win.connect( "key-release-event", self.on_key_release ) self.event_id = self.connect( "key-release-event", self.on_key_release )
self.teaching = True self.teaching = True
else: else:
if self.event_id: if self.event_id:
self.win.disconnect(self.event_id) self.disconnect(self.event_id)
self.button.set_label(self.value) self.ungrab()
self.set_button_text()
self.teaching = False self.teaching = False
def on_key_release(self, widget, event): def on_key_release(self, widget, event):
self.win.disconnect(self.event_id) self.disconnect(self.event_id)
self.ungrab()
self.event_id = None self.event_id = None
if event.keyval == Gdk.KEY_Escape: if event.keyval == Gdk.KEY_Escape:
self.button.set_label(self.value) self.set_button_text()
self.teaching = False
return True
if event.keyval == Gdk.KEY_BackSpace:
self.teaching = False
self.value = ""
self.set_button_text()
self.emit("accel-edited")
return True return True
gtk.gtk_accelerator_name.restype = c_char_p gtk.gtk_accelerator_name.restype = c_char_p
accel_string = gtk.gtk_accelerator_name(event.keyval, event.state) accel_string = gtk.gtk_accelerator_name(event.keyval, event.state)
accel_string = self.sanitize(accel_string) accel_string = self.sanitize(accel_string)
self.value = accel_string self.value = accel_string
self.emit("accel-edited") self.set_button_text()
self.button.set_label(self.value)
self.teaching = False self.teaching = False
self.emit("accel-edited")
return True return True
def sanitize(self, string): def sanitize(self, string):
@ -221,4 +231,13 @@ class KeybindingWidget(Gtk.HBox):
def set_val(self, value): def set_val(self, value):
self.value = value self.value = value
self.button.set_label(value) self.set_button_text()
def ungrab(self):
Gdk.keyboard_ungrab(Gdk.CURRENT_TIME)
def set_button_text(self):
if self.value == "":
self.button.set_label(_("<not set>"))
else:
self.button.set_label(self.value)

View File

@ -693,7 +693,8 @@ class MenuWin( object ):
def bind_hot_key (self): def bind_hot_key (self):
try: try:
self.keybinder.grab( self.hotkeyText ) if self.hotkeyText != "":
self.keybinder.grab( self.hotkeyText )
self.keybinder.connect("activate", self.onBindingPress) self.keybinder.connect("activate", self.onBindingPress)
self.keybinder.start() self.keybinder.start()
# Binding menu to hotkey # Binding menu to hotkey
@ -707,7 +708,8 @@ class MenuWin( object ):
def hotkeyChanged (self, schema, key): def hotkeyChanged (self, schema, key):
self.keybinder.ungrab() self.keybinder.ungrab()
self.hotkeyText = self.settings.get_string( "hot-key" ) self.hotkeyText = self.settings.get_string( "hot-key" )
self.keybinder.grab(self.hotkeyText) if self.hotkeyText != "":
self.keybinder.grab(self.hotkeyText)
def sizeButton( self ): def sizeButton( self ):
if self.hideIcon: if self.hideIcon:

View File

@ -142,7 +142,7 @@ class mintMenuConfig( object ):
self.headingColorLabel = self.builder.get_object( "headingColorLabel" ) self.headingColorLabel = self.builder.get_object( "headingColorLabel" )
self.showButtonIcon = self.builder.get_object( "showButtonIcon" ) self.showButtonIcon = self.builder.get_object( "showButtonIcon" )
self.buttonText = self.builder.get_object( "buttonText" ) self.buttonText = self.builder.get_object( "buttonText" )
self.hotkeyWidget = keybinding.KeybindingWidget(_("Keyboard shortcut:"), self.mainWindow) self.hotkeyWidget = keybinding.KeybindingWidget(_("Keyboard shortcut:") )
table = self.builder.get_object( "main_table" ) table = self.builder.get_object( "main_table" )
table.attach(self.hotkeyWidget, 0, 2, 2, 3, Gtk.AttachOptions.FILL, Gtk.AttachOptions.FILL, 0, 0) table.attach(self.hotkeyWidget, 0, 2, 2, 3, Gtk.AttachOptions.FILL, Gtk.AttachOptions.FILL, 0, 0)
self.buttonIcon = self.builder.get_object( "buttonIcon" ) self.buttonIcon = self.builder.get_object( "buttonIcon" )