diff browser_side/panels.py @ 361:f4efffb9627c

browser_side: the popup notifying the message's recipient is no more dependent to the unibox.
author souliane <souliane@mailoo.org>
date Wed, 19 Feb 2014 14:17:03 +0100
parents c943fd54c90e
children 019e1e706e74
line wrap: on
line diff
--- a/browser_side/panels.py	Tue Feb 18 00:15:43 2014 +0100
+++ b/browser_side/panels.py	Wed Feb 19 14:17:03 2014 +0100
@@ -170,8 +170,6 @@
         MessageBox.__init__(self, host)
         #AutoCompleteTextBox.__init__(self)
         self.setStyleName('uniBox')
-        self._popup = None
-        self._timer = Timer(notify=self._timeCb)
         host.addSelectedListener(self.onSelectedChange)
 
     def addKey(self, key):
@@ -188,45 +186,6 @@
         except KeyError:
             print "WARNING: trying to remove an unknown key"
 
-    def showWarning(self, target_data):
-        target_hook, _type, msg = target_data
-        if _type == "NONE":
-            return
-        if not msg:
-            print "WARNING: no msg set uniBox warning"
-            return
-        if _type == "PUBLIC":
-            style = "targetPublic"
-        elif _type == "GROUP":
-            style = "targetGroup"
-        elif _type == "STATUS":
-            msg = "This will be your new status message"
-            style = "targetStatus"
-        elif _type == "ONE2ONE":
-            style = "targetOne2One"
-        else:
-            print "ERROR: unknown message type"
-            return
-        contents = HTML(msg)
-
-        self._popup = dialog.PopupPanelWrapper(autoHide=False, modal=False)
-        self._popup.target_data = target_data
-        self._popup.add(contents)
-        self._popup.setStyleName("warningPopup")
-        if style:
-            self._popup.addStyleName(style)
-
-        left = 0
-        top = 0  # max(0, self.getAbsoluteTop() - contents.getOffsetHeight() - 2)
-        self._popup.setPopupPosition(left, top)
-        self._popup.show()
-
-    def _timeCb(self, timer):
-        if self._popup:
-            self._popup.hide()
-            del self._popup
-            self._popup = None
-
     def _getTarget(self, txt):
         """ Say who will receive the messsage
         @return: a tuple (selected, target_type, target info) with:
@@ -272,24 +231,17 @@
 
     def onKeyPress(self, sender, keycode, modifiers):
         _txt = self.getText()
-        target = self._getTarget(_txt)
-        if not self._popup:
-            self.showWarning(target)
-        elif target != self._popup.target_data:
-            self._timeCb(None)  # we remove the popup
-            self.showWarning(target)
-
-        self._timer.schedule(2000)
+        target_hook, type_, msg = self._getTarget(_txt)
+        self.host.showWarning(type_, msg)
 
         if keycode == KEY_ENTER:
             if _txt:
-                target_hook, type_, msg = target
                 if target_hook:
                     parsed_txt, data = target_hook
                     self.host.send([(type_, data)], parsed_txt)
                     self.host._updateInputHistory(_txt)
                     self.setText('')
-            self._timeCb(None)  # we remove the popup
+            self.host.showWarning(None, None)
         MessageBox.onKeyPress(self, sender, keycode, modifiers)
 
     def getTargetAndData(self):
@@ -329,6 +281,77 @@
         return AutoCompleteTextBox.complete(self)"""
 
 
+class WarningPopup():
+
+    def __init__(self):
+        self._popup = None
+        self._timer = Timer(notify=self._timeCb)
+
+    def showWarning(self, type_=None, msg=None):
+        """Display a popup information message, e.g. to notify the recipient of a message being composed.
+        If type_ is None, a popup being currently displayed will be hidden.
+        @type_: a type determining the CSS style to be applied (see __showWarning)
+        @msg: message to be displayed
+        """
+        if type_ is None:
+            self.__removeWarning()
+            return
+        if not self._popup:
+            self.__showWarning(type_, msg)
+        elif (type_, msg) != self._popup.target_data:
+            self._timeCb(None)  # we remove the popup
+            self.__showWarning(type_, msg)
+
+        self._timer.schedule(2000)
+
+    def __showWarning(self, type_, msg):
+        """Display a popup information message, e.g. to notify the recipient of a message being composed.
+        @type_: a type determining the CSS style to be applied. For now the defined styles are
+        "NONE" (will do nothing), "PUBLIC", "GROUP", "STATUS" and "ONE2ONE".
+        @msg: message to be displayed
+        """
+        if type_ == "NONE":
+            return
+        if not msg:
+            print "WARNING: no msg set uniBox warning"
+            return
+        if type_ == "PUBLIC":
+            style = "targetPublic"
+        elif type_ == "GROUP":
+            style = "targetGroup"
+        elif type_ == "STATUS":
+            msg = "This will be your new status message"
+            style = "targetStatus"
+        elif type_ == "ONE2ONE":
+            style = "targetOne2One"
+        else:
+            print "ERROR: unknown message type"
+            return
+        contents = HTML(msg)
+
+        self._popup = dialog.PopupPanelWrapper(autoHide=False, modal=False)
+        self._popup.target_data = (type_, msg)
+        self._popup.add(contents)
+        self._popup.setStyleName("warningPopup")
+        if style:
+            self._popup.addStyleName(style)
+
+        left = 0
+        top = 0  # max(0, self.getAbsoluteTop() - contents.getOffsetHeight() - 2)
+        self._popup.setPopupPosition(left, top)
+        self._popup.show()
+
+    def _timeCb(self, timer):
+        if self._popup:
+            self._popup.hide()
+            del self._popup
+            self._popup = None
+
+    def __removeWarning(self):
+        """Remove the popup"""
+        self._timeCb(None)
+
+
 class MicroblogItem():
     # XXX: should be moved in a separated module