From 72e59c92635a7850f0f563fed9717f64842a1f76 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Sun, 24 Apr 2011 08:55:33 -0400 Subject: [PATCH] ocarina: Added shortcut famework Now to add more shortcuts... --- ocarina/body/header/entry.py | 5 +++++ ocarina/shortcuts.py | 30 ++++++++++++++++++++++++++++++ ocarina/window.py | 2 ++ 3 files changed, 37 insertions(+) create mode 100644 ocarina/shortcuts.py diff --git a/ocarina/body/header/entry.py b/ocarina/body/header/entry.py index 1666ac2b..76bf978b 100644 --- a/ocarina/body/header/entry.py +++ b/ocarina/body/header/entry.py @@ -2,10 +2,15 @@ import gtk import ocarina +from ocarina import shortcuts entry = gtk.Entry() entry.show() +has_focus = entry.is_focus + def do_filter(entry): ocarina.body.cur_page_filter(entry.get_text()) entry.connect("changed", do_filter) + +shortcuts.register_shortcut("slash", entry.grab_focus) diff --git a/ocarina/shortcuts.py b/ocarina/shortcuts.py new file mode 100644 index 00000000..6957b64c --- /dev/null +++ b/ocarina/shortcuts.py @@ -0,0 +1,30 @@ +# Bryan Schumaker (3 / 11 / 2011) + +import gtk +from libsaria import controls + +keyval_name = gtk.gdk.keyval_name +shortcut_map = {} + +def null_shortcut(): + return False + +# Setting allow_filter_focus to True means that the +# registered shortcut will always run, even if the +# filter entry currently has focus +def register_shortcut(key, func, allow_filter_focus = False): + shortcut_map[key] = (func, allow_filter_focus) + +def key_pressed(widget, event): + from ocarina import body + from ocarina.body.header import entry + + name = keyval_name(event.keyval) + func, allow_filter_focus = shortcut_map.get(name, (null_shortcut, False)) + if (entry.has_focus() and not allow_filter_focus): + return + if func() == False: + return False + widget.emit_stop_by_name("key-press-event") + return True + diff --git a/ocarina/window.py b/ocarina/window.py index b09f85f9..ea95e1d4 100644 --- a/ocarina/window.py +++ b/ocarina/window.py @@ -4,6 +4,7 @@ import gtk import ocarina import libsaria import body +import shortcuts #files = None #TARGET_TYPE_URI_LIST = 80 @@ -16,6 +17,7 @@ height = libsaria.init_pref("ocarina.window.height", 600) window = gtk.Window(gtk.WINDOW_TOPLEVEL) window.resize(width, height) window.connect("delete-event", ocarina.quit) +window.connect("key-press-event", shortcuts.key_pressed) window.add(body.body) window.show()