comparison frontends/src/primitivus/primitivus @ 524:0bb595eff25b

primitivus: Primitivus is now modal (vi-like behaviour): - there are currently 3 modes: NORMAL, INSERTION and COMMAND - when a chat window is selected, INSERTION mode is set if possible - completion is managed according to mode. Currently only INSERTION mode do something - first command implementation: :quit
author Goffi <goffi@goffi.org>
date Sun, 21 Oct 2012 16:40:19 +0200
parents 8ee9113d307b
children 6c07127ad2ed
comparison
equal deleted inserted replaced
523:24c0d51449e7 524:0bb595eff25b
64 self.progress_wid = Progress(self) 64 self.progress_wid = Progress(self)
65 urwid.connect_signal(self.notBar.progress,'click',lambda x:self.addWindow(self.progress_wid)) 65 urwid.connect_signal(self.notBar.progress,'click',lambda x:self.addWindow(self.progress_wid))
66 self.__saved_overlay = None 66 self.__saved_overlay = None
67 67
68 self.x_notify = Notify() 68 self.x_notify = Notify()
69
70 @property
71 def mode(self):
72 return self.editBar.mode
73
74 @mode.setter
75 def mode(self, value):
76 self.editBar.mode = value
77
78 def modeHint(self, value):
79 """Change mode if make sens (i.e.: if there is nothing in the editBar"""
80 if not self.editBar.get_edit_text():
81 self.mode = value
69 82
70 def debug(self): 83 def debug(self):
71 """convenient method to reset screen and launch p(u)db""" 84 """convenient method to reset screen and launch p(u)db"""
72 try: 85 try:
73 import pudb 86 import pudb
158 try: 171 try:
159 return self.menu_roller.checkShortcuts(input) 172 return self.menu_roller.checkShortcuts(input)
160 except AttributeError: 173 except AttributeError:
161 return input 174 return input
162 175
176 def commandHandler(self, editBar):
177 #TODO: separate class with auto documentation (with introspection)
178 # and completion method
179 command = editBar.get_edit_text()
180 if command == 'quit':
181 self.onExit()
182 raise urwid.ExitMainLoop()
183
163 def __buildMenuRoller(self): 184 def __buildMenuRoller(self):
164 menu = sat_widgets.Menu(self.loop) 185 menu = sat_widgets.Menu(self.loop)
165 general = _("General") 186 general = _("General")
166 menu.addMenu(general, _("Connect"), self.onConnectRequest) 187 menu.addMenu(general, _("Connect"), self.onConnectRequest)
167 menu.addMenu(general, _("Disconnect"), self.onDisconnectRequest) 188 menu.addMenu(general, _("Disconnect"), self.onDisconnectRequest)
191 212
192 def __buildMainWidget(self): 213 def __buildMainWidget(self):
193 self.contact_list = ContactList(self, on_click = self.contactSelected, on_change=lambda w: self.redraw()) 214 self.contact_list = ContactList(self, on_click = self.contactSelected, on_change=lambda w: self.redraw())
194 #self.center_part = urwid.Columns([('weight',2,self.contact_list),('weight',8,Chat('',self))]) 215 #self.center_part = urwid.Columns([('weight',2,self.contact_list),('weight',8,Chat('',self))])
195 self.center_part = urwid.Columns([('weight',2,self.contact_list), ('weight',8,urwid.Filler(urwid.Text('')))]) 216 self.center_part = urwid.Columns([('weight',2,self.contact_list), ('weight',8,urwid.Filler(urwid.Text('')))])
196 self.editBar = sat_widgets.AdvancedEdit(u'> ') 217
197 self.editBar.setCompletionMethod(self._nick_completion) 218 modes = {None: ('NORMAL', ''),
219 'i': ('INSERTION','> '),
220 ':': ('COMMAND',':')}
221 self.editBar = sat_widgets.ModalEdit(modes)
222 self.editBar.setCompletionMethod(self._text_completion)
198 urwid.connect_signal(self.editBar,'click',self.onTextEntered) 223 urwid.connect_signal(self.editBar,'click',self.onTextEntered)
199 self.menu_roller = self.__buildMenuRoller() 224 self.menu_roller = self.__buildMenuRoller()
200 self.main_widget = sat_widgets.FocusFrame(self.center_part, header=self.menu_roller, footer=self.editBar, focus_part='footer') 225 self.main_widget = sat_widgets.FocusFrame(self.center_part, header=self.menu_roller, footer=self.editBar, focus_part='footer')
201 return self.main_widget 226 return self.main_widget
227
228 def _text_completion(self, text, completion_data, mode):
229 if mode == 'INSERTION':
230 return self._nick_completion(text, completion_data)
231 else:
232 return text
202 233
203 def _nick_completion(self, text, completion_data): 234 def _nick_completion(self, text, completion_data):
204 """Completion method which complete pseudo in group chat 235 """Completion method which complete pseudo in group chat
205 for params, see AdvancedEdit""" 236 for params, see AdvancedEdit"""
206 contact = self.contact_list.getContact() ###Based on the fact that there is currently only one contact selectable at once 237 contact = self.contact_list.getContact() ###Based on the fact that there is currently only one contact selectable at once
289 self.center_part.widget_list[1] = self.chat_wins[contact] 320 self.center_part.widget_list[1] = self.chat_wins[contact]
290 self.menu_roller.addMenu(_('Chat menu'), self.chat_wins[contact].getMenu()) 321 self.menu_roller.addMenu(_('Chat menu'), self.chat_wins[contact].getMenu())
291 322
292 def onTextEntered(self, editBar): 323 def onTextEntered(self, editBar):
293 """Called when text is entered in the main edit bar""" 324 """Called when text is entered in the main edit bar"""
294 contact = self.contact_list.getContact() ###Based on the fact that there is currently only one contact selectableat once 325 if self.mode == 'INSERTION':
295 if contact: 326 contact = self.contact_list.getContact() ###Based on the fact that there is currently only one contact selectableat once
296 chat = self.chat_wins[contact] 327 if contact:
297 try: 328 chat = self.chat_wins[contact]
298 self.sendMessage(contact, 329 try:
299 editBar.get_edit_text(), 330 self.sendMessage(contact,
300 mess_type = "groupchat" if chat.type == 'group' else "chat", 331 editBar.get_edit_text(),
301 profile_key=self.profile) 332 mess_type = "groupchat" if chat.type == 'group' else "chat",
302 except: 333 profile_key=self.profile)
303 self.notify(_("Error while sending message")) 334 except:
304 editBar.set_edit_text('') 335 self.notify(_("Error while sending message"))
336 editBar.set_edit_text('')
337 elif self.mode == 'COMMAND':
338 self.commandHandler(editBar)
305 339
306 def newMessage(self, from_jid, to_jid, msg, _type, extra, profile): 340 def newMessage(self, from_jid, to_jid, msg, _type, extra, profile):
307 QuickApp.newMessage(self, from_jid, to_jid, msg, _type, extra, profile) 341 QuickApp.newMessage(self, from_jid, to_jid, msg, _type, extra, profile)
308 342
309 if not from_jid in self.contact_list and from_jid.short != self.profiles[profile]['whoami'].short: 343 if not from_jid in self.contact_list and from_jid.short != self.profiles[profile]['whoami'].short: