ocarina: Added shortcut famework

Now to add more shortcuts...
This commit is contained in:
Bryan Schumaker 2011-04-24 08:55:33 -04:00
parent 2feb354631
commit 72e59c9263
3 changed files with 37 additions and 0 deletions

View File

@ -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)

30
ocarina/shortcuts.py Normal file
View File

@ -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

View File

@ -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()