comparison browser_side/panels.py @ 310:5d11ec1f2c22

browser_side: make UniBox inherit from a more basic MessageBox class
author souliane <souliane@mailoo.org>
date Sun, 29 Dec 2013 23:15:35 +0100
parents 05e264e96a1c
children 3135ff840b8e
comparison
equal deleted inserted replaced
309:05e264e96a1c 310:5d11ec1f2c22
116 left = self.host.panel._contacts.getAbsoluteLeft() + self.host.panel._contacts.getOffsetWidth() 116 left = self.host.panel._contacts.getAbsoluteLeft() + self.host.panel._contacts.getOffsetWidth()
117 ideal_width = right - left - 40 117 ideal_width = right - left - 40
118 self.host.richtext.setWidth("%spx" % ideal_width) 118 self.host.richtext.setWidth("%spx" % ideal_width)
119 119
120 120
121 class UniBox(TextArea, MouseHandler): #AutoCompleteTextBox): 121 class MessageBox(TextArea):
122 """This text box is used as a main typing point, for message, microblog, etc""" 122 """A basic text area for entering messages"""
123 123
124 def __init__(self, host): 124 def __init__(self, host):
125 TextArea.__init__(self) 125 TextArea.__init__(self)
126 #AutoCompleteTextBox.__init__(self) 126 self.host = host
127 self.__size = (0, 0) 127 self.__size = (0, 0)
128 self._popup = None 128 self.setStyleName('messageBox')
129 self._timer = Timer(notify=self._timeCb)
130 self.host = host
131 self.setStyleName('uniBox')
132 self.addKeyboardListener(self) 129 self.addKeyboardListener(self)
133 MouseHandler.__init__(self) 130 MouseHandler.__init__(self)
134 self.addMouseListener(self) 131 self.addMouseListener(self)
135 self._selected_cache = None 132 self._selected_cache = None
133
134 def onBrowserEvent(self, event):
135 # XXX: woraroung a pyjamas bug: self.currentEvent is not set
136 # so the TextBox's cancelKey doens't work. This is a workaround
137 # FIXME: fix the bug upstream
138 self.currentEvent = event
139 TextArea.onBrowserEvent(self, event)
140
141 def onKeyPress(self, sender, keycode, modifiers):
142 _txt = self.getText()
143
144 def history_cb(text):
145 self.setText(text)
146 Timer(5, lambda: self.setCursorPos(len(text)))
147
148 if keycode == KEY_ENTER:
149 if _txt:
150 self._selected_cache.onTextEntered(_txt)
151 self.host._updateInputHistory(_txt)
152 self.setText('')
153 sender.cancelKey()
154 elif keycode == KEY_UP:
155 self.host._updateInputHistory(_txt, -1, history_cb)
156 elif keycode == KEY_DOWN:
157 self.host._updateInputHistory(_txt, +1, history_cb)
158 else:
159 self.__onComposing()
160
161 def __onComposing(self):
162 """Callback when the user is composing a text."""
163 if hasattr(self._selected_cache, "target"):
164 self._selected_cache.state_machine._onEvent("composing")
165
166 def onMouseUp(self, sender, x, y):
167 size = (self.getOffsetWidth(), self.getOffsetHeight())
168 if size != self.__size:
169 self.__size = size
170 self.host.resize()
171
172 def onSelectedChange(self, selected):
173 self._selected_cache = selected
174
175
176 class UniBox(MessageBox, MouseHandler): #AutoCompleteTextBox):
177 """This text box is used as a main typing point, for message, microblog, etc"""
178
179 def __init__(self, host):
180 MessageBox.__init__(self, host)
181 #AutoCompleteTextBox.__init__(self)
182 self.setStyleName('uniBox')
183 self._popup = None
184 self._timer = Timer(notify=self._timeCb)
136 host.addSelectedListener(self.onSelectedChange) 185 host.addSelectedListener(self.onSelectedChange)
137 186
138 def addKey(self, key): 187 def addKey(self, key):
139 return 188 return
140 #self.getCompletionItems().completions.append(key) 189 #self.getCompletionItems().completions.append(key)
229 print "ERROR: Unknown target" 278 print "ERROR: Unknown target"
230 target_hook, _type, msg = getSelectedOrStatus() 279 target_hook, _type, msg = getSelectedOrStatus()
231 280
232 return (target_hook, _type, msg) 281 return (target_hook, _type, msg)
233 282
234 def onBrowserEvent(self, event):
235 # XXX: woraroung a pyjamas bug: self.currentEvent is not set
236 # so the TextBox's cancelKey doens't work. This is a workaround
237 # FIXME: fix the bug upstream
238 self.currentEvent = event
239 TextArea.onBrowserEvent(self, event)
240
241 def onKeyPress(self, sender, keycode, modifiers): 283 def onKeyPress(self, sender, keycode, modifiers):
242 _txt = self.getText() 284 _txt = self.getText()
243 target = self._getTarget(_txt) 285 target = self._getTarget(_txt)
244 if not self._popup: 286 if not self._popup:
245 self.showWarning(target) 287 self.showWarning(target)
247 self._timeCb(None) # we remove the popup 289 self._timeCb(None) # we remove the popup
248 self.showWarning(target) 290 self.showWarning(target)
249 291
250 self._timer.schedule(2000) 292 self._timer.schedule(2000)
251 293
252 def history_cb(text):
253 self.setText(text)
254 Timer(5, lambda: self.setCursorPos(len(text)))
255
256 # if keycode == KEY_ENTER and not self.visible:
257 if keycode == KEY_ENTER: 294 if keycode == KEY_ENTER:
258 if _txt: 295 if _txt:
259 target_hook, type_, msg = target 296 target_hook, type_, msg = target
260 if target_hook: 297 if target_hook:
261 parsed_txt, data = target_hook 298 parsed_txt, data = target_hook
262 self.host.send([(type_, data)], parsed_txt) 299 self.host.send([(type_, data)], parsed_txt)
263 else: # we send the message to the selected target 300 self.host._updateInputHistory(_txt)
264 self._selected_cache.onTextEntered(_txt) 301 self.setText('')
265 self.host._updateInputHistory(_txt)
266 self.setText('')
267 self._timeCb(None) # we remove the popup 302 self._timeCb(None) # we remove the popup
268 sender.cancelKey() 303 MessageBox.onKeyPress(self, sender, keycode, modifiers)
269 elif keycode == KEY_UP:
270 self.host._updateInputHistory(_txt, -1, history_cb)
271 elif keycode == KEY_DOWN:
272 self.host._updateInputHistory(_txt, +1, history_cb)
273 else:
274 self.__onComposing()
275 304
276 def getTargetAndData(self): 305 def getTargetAndData(self):
277 """For external use, to get information about the (hypothetical) message 306 """For external use, to get information about the (hypothetical) message
278 that would be sent if we press Enter right now in the unibox. 307 that would be sent if we press Enter right now in the unibox.
279 @return a tuple (target, data) with: 308 @return a tuple (target, data) with:
295 elif isinstance(self._selected_cache, ChatPanel): 324 elif isinstance(self._selected_cache, ChatPanel):
296 target = self._selected_cache.target 325 target = self._selected_cache.target
297 else: 326 else:
298 target = None 327 target = None
299 return (_txt, target) 328 return (_txt, target)
300
301 def __onComposing(self):
302 """Callback when the user is composing a text."""
303 if hasattr(self._selected_cache, "target"):
304 self._selected_cache.state_machine._onEvent("composing")
305
306 def onMouseUp(self, sender, x, y):
307 size = (self.getOffsetWidth(), self.getOffsetHeight())
308 if size != self.__size:
309 self.__size = size
310 self.host.resize()
311
312 def onSelectedChange(self, selected):
313 self._selected_cache = selected
314 329
315 def onWidgetClosed(self, lib_wid): 330 def onWidgetClosed(self, lib_wid):
316 """Called when a libervia widget is closed""" 331 """Called when a libervia widget is closed"""
317 if self._selected_cache == lib_wid: 332 if self._selected_cache == lib_wid:
318 self.onSelectedChange(None) 333 self.onSelectedChange(None)