[kupfer] triggers: Make suggestions for Accelerators



commit 1c9c30725a71be1b1b1ba2b1251bb99b83dd57fb
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sat Jan 2 20:18:41 2010 +0100

    triggers: Make suggestions for Accelerators
    
    Use a custom (TextLeaf) TextSource to make suggestions about triggers
    keys. This is a temporary improvement, until we make a real "key
    capture" solution.

 kupfer/plugin/triggers.py |   36 +++++++++++++++++++++++++++++++++---
 1 files changed, 33 insertions(+), 3 deletions(-)
---
diff --git a/kupfer/plugin/triggers.py b/kupfer/plugin/triggers.py
index 5a1ba55..32991ee 100644
--- a/kupfer/plugin/triggers.py
+++ b/kupfer/plugin/triggers.py
@@ -3,7 +3,7 @@ import gtk
 
 from kupfer import keybindings
 
-from kupfer.objects import Leaf, Action, Source
+from kupfer.objects import Leaf, Action, Source, TextSource
 from kupfer.objects import ComposedLeaf, TextLeaf, RunnableLeaf
 from kupfer import puid
 
@@ -93,6 +93,33 @@ class Triggers (Source):
 	def get_icon_name(self):
 		return "key_bindings"
 
+def _accelerator_label(keystr):
+	return gtk.accelerator_get_label(*gtk.accelerator_parse(keystr))
+
+def _valid_accelerator(keystr):
+	val, mod = gtk.accelerator_parse(keystr)
+	return val or mod
+
+class AcceleratorSuggestionsSource (TextSource):
+	def __init__(self):
+		TextSource.__init__(self, _("Triggers"))
+
+	def get_text_items(self, text):
+		if not text or not _valid_accelerator(text):
+			return
+		pfix = ["<Ctrl><Alt>", "<Ctrl><Shift>", "<Ctrl><Alt><Shift>", "<Super>"]
+		texts = [text] + [p + text for p in pfix]
+		for accel in texts:
+			if not _valid_accelerator(accel):
+				continue
+			yield TextLeaf(accel, _accelerator_label(accel))
+
+	def get_items(self, text):
+		return self.get_text_items(text)
+
+	def provides(self):
+		yield TextLeaf
+
 class AddTrigger (Action):
 	def __init__(self):
 		Action.__init__(self, _("Add Trigger..."))
@@ -107,9 +134,11 @@ class AddTrigger (Action):
 		return True
 	def object_types(self):
 		yield TextLeaf
+	def object_source(self, for_item=None):
+		return AcceleratorSuggestionsSource()
+
 	def valid_object(self, iobj, for_item=None):
-		val, mod = gtk.accelerator_parse(iobj.object)
-		return val or mod
+		return len(iobj.object) > 1 and _valid_accelerator(iobj.object)
 
 	def get_icon_name(self):
 		return "list-add"
@@ -123,3 +152,4 @@ class RemoveTrigger (Action):
 
 	def get_icon_name(self):
 		return "list-remove"
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]