primitivus (chat): moved occupants widget to a dedicated class + display occupants count in footer
This commit is contained in:
parent
71a2fe4599
commit
6b75a82f04
1 changed files with 47 additions and 13 deletions
|
|
@ -36,6 +36,8 @@ from functools import total_ordering
|
|||
import bisect
|
||||
|
||||
|
||||
OCCUPANTS_FOOTER = _(u"{} occupants")
|
||||
|
||||
class MessageWidget(urwid.WidgetWrap):
|
||||
|
||||
def __init__(self, mess_data):
|
||||
|
|
@ -198,6 +200,46 @@ class OccupantWidget(urwid.WidgetWrap):
|
|||
return markup
|
||||
|
||||
|
||||
class OccupantsWidget(urwid.WidgetWrap):
|
||||
|
||||
def __init__(self, parent):
|
||||
self.parent = parent
|
||||
self.occupants_walker = urwid.SimpleListWalker([])
|
||||
self.occupants_footer = urwid.Text('', align='center')
|
||||
self.updateFooter()
|
||||
occupants_widget = urwid.Frame(urwid.ListBox(self.occupants_walker), footer=self.occupants_footer)
|
||||
super(OccupantsWidget, self).__init__(occupants_widget)
|
||||
occupants_list = sorted(self.parent.occupants.keys(), key=lambda o:o.lower())
|
||||
for occupant in occupants_list:
|
||||
occupant_data = self.parent.occupants[occupant]
|
||||
self.occupants_walker.append(OccupantWidget(occupant_data))
|
||||
|
||||
def updateFooter(self):
|
||||
"""update footer widget"""
|
||||
txt = OCCUPANTS_FOOTER.format(len(self.parent.occupants))
|
||||
self.occupants_footer.set_text(txt)
|
||||
|
||||
def getNicks(self, start=u''):
|
||||
"""Return nicks of all occupants
|
||||
|
||||
@param start(unicode): only return nicknames which start with this text
|
||||
"""
|
||||
return [w.nick for w in self.occupants_walker if isinstance(w, OccupantWidget) and w.nick.startswith(start)]
|
||||
|
||||
def addUser(self, occupant_data):
|
||||
"""add a user to the list"""
|
||||
bisect.insort(self.occupants_walker, OccupantWidget(occupant_data))
|
||||
self.updateFooter()
|
||||
self.parent.host.redraw() # FIXME: should not be necessary
|
||||
|
||||
def removeUser(self, occupant_data):
|
||||
"""remove a user from the list"""
|
||||
for widget in occupant_data.widgets:
|
||||
self.occupants_walker.remove(widget)
|
||||
self.updateFooter()
|
||||
self.parent.host.redraw() # FIXME: should not be necessary
|
||||
|
||||
|
||||
class Chat(PrimitivusWidget, quick_chat.QuickChat):
|
||||
|
||||
def __init__(self, host, target, type_=C.CHAT_ONE2ONE, occupants=None, subject=None, profiles=None):
|
||||
|
|
@ -213,18 +255,11 @@ class Chat(PrimitivusWidget, quick_chat.QuickChat):
|
|||
# we must adapt the behaviour with the type
|
||||
if type_ == C.CHAT_GROUP:
|
||||
if len(self.chat_colums.contents) == 1:
|
||||
self.occupants_walker = urwid.SimpleListWalker([])
|
||||
# TODO: put a real ContactPanel class here, based on FocusWidget ?
|
||||
self.occupants_widgets = urwid.ListBox(self.occupants_walker)
|
||||
self.occupants_widget = OccupantsWidget(self)
|
||||
# FIXME
|
||||
# , option_type=sat_widgets.ClickableText, on_click=self._occupantsClicked)
|
||||
self.occupants_panel = sat_widgets.VerticalSeparator(self.occupants_widgets)
|
||||
self.occupants_panel = sat_widgets.VerticalSeparator(self.occupants_widget)
|
||||
self._appendOccupantsPanel()
|
||||
occupants_list = sorted(self.occupants.keys(), key=lambda o:o.lower())
|
||||
for occupant in occupants_list:
|
||||
occupant_data = self.occupants[occupant]
|
||||
self.occupants_walker.append(OccupantWidget(occupant_data))
|
||||
|
||||
self.host.addListener('presence', self.presenceListener, [profiles])
|
||||
|
||||
# focus marker is a separator indicated last visible message before focus was lost
|
||||
|
|
@ -273,7 +308,7 @@ class Chat(PrimitivusWidget, quick_chat.QuickChat):
|
|||
|
||||
space = text.rfind(" ")
|
||||
start = text[space + 1:]
|
||||
words = [w.nick for w in self.occupants_walker if isinstance(w, OccupantWidget) and w.nick.startswith(start)]
|
||||
words = self.occupants_widget.getNicks(start)
|
||||
if not words:
|
||||
return text
|
||||
try:
|
||||
|
|
@ -415,13 +450,12 @@ class Chat(PrimitivusWidget, quick_chat.QuickChat):
|
|||
|
||||
def addUser(self, nick):
|
||||
occupant = super(Chat, self).addUser(nick)
|
||||
bisect.insort(self.occupants_walker, OccupantWidget(occupant))
|
||||
self.occupants_widget.addUser(occupant)
|
||||
|
||||
def removeUser(self, occupant_data):
|
||||
occupant = super(Chat, self).removeUser(occupant_data)
|
||||
if occupant is not None:
|
||||
for widget in occupant.widgets:
|
||||
self.occupants_walker.remove(widget)
|
||||
self.occupants_widget.removeUser(occupant)
|
||||
|
||||
def _occupantsClicked(self, list_wid, clicked_wid):
|
||||
# FIXME: not called anymore after refactoring
|
||||
|
|
|
|||
Loading…
Reference in a new issue