[kupfer] data: Check if an Action is valid when displayed



commit 8eb76212a20cd7a5887ee2472623af17160cac21
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Thu Sep 17 15:00:24 2009 +0200

    data: Check if an Action is valid when displayed
    
    We postpone valid checking for actions (just like for leaves) until it
    is displayed; Only the first item is normally checked. This avoids any
    action checking that is not immediately needed (to display the
    first/default action). When browsed or searched, the actions are
    immediately checked passing through the iterator.
    
    We cache the result for a particular item.

 kupfer/data.py |   24 ++++++++++++++++++++++--
 1 files changed, 22 insertions(+), 2 deletions(-)
---
diff --git a/kupfer/data.py b/kupfer/data.py
index 178bb23..9e6d62b 100644
--- a/kupfer/data.py
+++ b/kupfer/data.py
@@ -583,9 +583,14 @@ gobject.signal_new("new-source", LeafPane, gobject.SIGNAL_RUN_LAST,
 		gobject.TYPE_BOOLEAN, (gobject.TYPE_PYOBJECT,))
 
 class PrimaryActionPane (Pane):
+	def __init__(self):
+		super(PrimaryActionPane, self).__init__()
+		self.set_item(None)
+
 	def set_item(self, item):
 		"""Set which @item we are currently listing actions for"""
 		self.current_item = item
+		self._action_valid_cache = {}
 
 	def search(self, key=u"", context=None, text_mode=False):
 		"""Search: Register the search method in the event loop
@@ -605,9 +610,24 @@ class PrimaryActionPane (Pane):
 			for act in sc.get_actions_for_leaf(leaf):
 				actions.append(act)
 
-		actions = [a for a in actions if a.valid_for_item(self.current_item)]
+		def is_valid_cached(action):
+			"""Check if @action is valid for current item"""
+			cache = self._action_valid_cache
+			valid = cache.get(action)
+			if valid is None:
+				valid = action.valid_for_item(self.current_item)
+			cache[action] = valid
+			return valid
+
+		def valid_decorator(seq):
+			"""Check if actions are valid before access"""
+			for obj in seq:
+				if is_valid_cached(obj.object):
+					yield obj
+
 		sources = (actions, )
-		match, match_iter = self.searcher.search(sources, key)
+		match, match_iter = self.searcher.search(sources, key,
+				decorator=valid_decorator)
 		self.emit_search_result(match, match_iter, context)
 
 class SecondaryObjectPane (LeafPane):



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