changeset 193:f2ae8e170c49

browser side: selected widget caching in UniBox, to avoid to ask the getter at each key pressed, which make the box very slow.
author Goffi <goffi@goffi.org>
date Mon, 04 Mar 2013 00:01:23 +0100
parents cf5c83e7d515
children 6198be95a39c
files browser_side/panels.py libervia.py
diffstat 2 files changed, 32 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/panels.py	Sun Mar 03 23:30:25 2013 +0100
+++ b/browser_side/panels.py	Mon Mar 04 00:01:23 2013 +0100
@@ -383,7 +383,8 @@
         self.addKeyboardListener(self)
         MouseHandler.__init__(self)
         self.addMouseListener(self)
-        
+        self._selected_cache = None
+        host.addSelectedListener(self.onSelectedChange)
 
     def addKey(self, key):
         return
@@ -439,7 +440,6 @@
         Return a tuple (target_type, target info)"""
         type = None
         target = None
-        selected = self.host.getSelected()
         if txt.startswith('@@: '):
             type = "PUBLIC"
         elif txt.startswith('@'):
@@ -451,13 +451,13 @@
                 target = txt[1:_end] #only one target group is managed for the moment
                 if not target in self.host.contact_panel.getGroups():
                     target = None
-        elif selected == None:
+        elif self._selected_cache == None:
             type = "STATUS"
-        elif isinstance(selected, ChatPanel):
+        elif isinstance(self._selected_cache, ChatPanel):
             type = "ONE2ONE"
-            target = str(selected.target)
+            target = str(self._selected_cache.target)
         else:
-            print "Unknown selected host:",selected
+            print "Unknown selected host:",self._selected_cache
             type = "UNKNOWN"
         return (type, target)
 
@@ -480,16 +480,15 @@
 
         self._timer.schedule(2000)
 
-        selected = self.host.getSelected()
         #if keycode == KEY_ENTER and not self.visible:
         if keycode == KEY_ENTER:
             if _txt:
                 if _txt.startswith('@'):
                     self.host.bridge.call('sendMblog', None, self.getText())
-                elif selected == None:
+                elif self._selected_cache == None:
                     self.host.bridge.call('setStatus', None, _txt)
-                elif isinstance(selected, ChatPanel):
-                    _chat = selected
+                elif isinstance(self._selected_cache, ChatPanel):
+                    _chat = self._selected_cache
                     mess_type = "groupchat" if _chat.type=='group' else "chat"
                     self.host.bridge.call('sendMessage', None, str(_chat.target), _txt, '', mess_type)
             self.setText('')
@@ -500,7 +499,10 @@
         size = (self.getOffsetWidth(), self.getOffsetHeight())
         if size != self.__size:
             self.__size = size
-            self.host.resize() 
+            self.host.resize()
+
+    def onSelectedChange(self, selected):
+        self._selected_cache = selected
 
     """def complete(self):
         
--- a/libervia.py	Sun Mar 03 23:30:25 2013 +0100
+++ b/libervia.py	Mon Mar 04 00:01:23 2013 +0100
@@ -121,6 +121,7 @@
     def onModuleLoad(self):
         print "============ onModuleLoad =============="
         self.whoami = None
+        self._selected_listeners = set()
         self.bridge = BridgeCall()
         self.bridge_signals = BridgeSignals(self)
         self.uni_box = None
@@ -129,6 +130,7 @@
         self.panel = panels.MainPanel(self)
         self.discuss_panel = self.panel.discuss_panel
         self.tab_panel = self.panel.tab_panel
+        self.tab_panel.addTabListener(self)
         self.libervia_widgets = set() #keep track of all actives LiberviaWidgets
         self.room_list = set() #set of rooms 
         self.mblog_cache = [] #used to keep our own blog entries in memory, to show them in new mblog panel
@@ -143,7 +145,9 @@
         self.resize()
         self._register = RegisterCall()
         self._register.call('isRegistered',self._isRegisteredCB)
-        self.selected = property(self.getSelected, self.setSelected)
+
+    def addSelectedListener(self, callback):
+        self._selected_listeners.add(callback)
 
     def getSelected(self):
         wid = self.tab_panel.getCurrentPanel()
@@ -160,21 +164,32 @@
 
         selected = widgets_panel.selected
 
+        if selected == widget:
+            return
+        
         if selected:
-            if selected == widget:
-                return
             selected.removeStyleName('selected_widget')
         
         widgets_panel.selected = widget
+        
         if widget:
             widgets_panel.selected.addStyleName('selected_widget')
 
-
+        for callback in self._selected_listeners:
+            callback(widget)
 
     def resize(self):
         """Resize elements"""
         Window.onResize()
 
+    def onBeforeTabSelected(self, sender, tab_index):
+        return True
+
+    def onTabSelected(self, sender, tab_index):
+        selected = self.getSelected()
+        for callback in self._selected_listeners:
+            callback(selected)
+
     def onEventPreview(self, event):
         if event.type in ["keydown", "keypress", "keyup"] and event.keyCode == KEY_ESCAPE:
             #needed to prevent request cancellation in Firefox