diff browser_side/panels.py @ 314:70ac3067d641

browser_side: status can be edited with a click on it
author souliane <souliane@mailoo.org>
date Mon, 30 Dec 2013 13:02:03 +0100
parents 5ad70625867a
children 09205b410a53
line wrap: on
line diff
--- a/browser_side/panels.py	Mon Dec 30 13:00:41 2013 +0100
+++ b/browser_side/panels.py	Mon Dec 30 13:02:03 2013 +0100
@@ -891,20 +891,53 @@
         return False
 
 
-class StatusPanel(HTMLPanel):
+class StatusPanel(HTMLPanel, ClickHandler, FocusHandler, KeyboardHandler):
+
+    EMPTY_STATUS = '<click to set a status>'
+
     def __init__(self, host, status=''):
         self.host = host
-        self.status = status or '&nbsp;'
+        self.status = status
         HTMLPanel.__init__(self, self.__getContent())
         self.setStyleName('statusPanel')
+        ClickHandler.__init__(self)
+        FocusHandler.__init__(self)
+        KeyboardHandler.__init__(self)
+        self.addClickListener(self)
 
     def __getContent(self):
-        return "<span class='status'>%(status)s</span>" % {'status': html_sanitize(self.status)}
+        return "<span class='status'>%(status)s</span>" % {'status': html_sanitize(self.status or self.EMPTY_STATUS)}
 
     def changeStatus(self, new_status):
-        self.status = new_status or '&nbsp;'
+        self.status = new_status
         self.setHTML(self.__getContent())
 
+    def onClick(self, sender):
+        self.textarea = TextArea()
+        self.textarea.setStyleName('status-edit')
+        self.textarea.setText(self.status)
+        self.textarea.addKeyboardListener(self)
+        self.setVisible(False)
+        self.parent.insert(self.textarea, self.parent.getWidgetIndex(self))
+        self.textarea.setFocus(True)
+        self.textarea.setSelectionRange(0, len(self.status))
+        self.textarea.addFocusListener(self)
+        self.textarea.addKeyboardListener(self)
+
+    def onKeyPress(self, sender, keycode, modifiers):
+        text = self.textarea.getText()
+
+        if keycode == KEY_ENTER:
+            if text != self.status:
+                self.host.bridge.call('setStatus', None, self.host.status_panel.presence, text)
+            self.parent.remove(self.textarea)
+            self.setVisible(True)
+
+    def onLostFocus(self, sender):
+        FocusHandler.onLostFocus(self, sender)
+        if sender == self.textarea:
+            self.onKeyPress(self, KEY_ENTER, None)
+
 
 class PresenceStatusPanel(HorizontalPanel, ClickHandler):
 
@@ -929,6 +962,8 @@
         panel = HorizontalPanel()
         panel.add(self.presence_button)
         panel.add(self.status_panel)
+        panel.setCellVerticalAlignment(self.presence_button, 'baseline')
+        panel.setCellVerticalAlignment(self.status_panel, 'baseline')
         panel.setStyleName("marginAuto")
         self.add(panel)