From cc1ee21e0704d7f02552f1617737f6cf7471fa52 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Wed, 20 Jan 2021 14:05:29 +0100 Subject: widgets/search: cleanup ThreadlineWidget Remove unnecessary instance variables that are only really needed within one function. Prepare the class for async rebuild. --- alot/widgets/search.py | 58 ++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/alot/widgets/search.py b/alot/widgets/search.py index d511f2f7..dda171ac 100644 --- a/alot/widgets/search.py +++ b/alot/widgets/search.py @@ -12,31 +12,36 @@ from .utils import AttrFlipWidget from .globals import TagWidget -class ThreadlineWidget(urwid.AttrMap): +class ThreadlineWidget(urwid.WidgetPlaceholder): """ selectable line widget that represents a :class:`~alot.db.Thread` in the :class:`~alot.buffers.SearchBuffer`. """ + + tid = None + + _dbman = None + _thread = None + + _widgets = None + def __init__(self, tid, dbman): - self.dbman = dbman - self.tid = tid - self.thread = None # will be set by refresh() - self.structure = None + self.tid = tid + self._dbman = dbman + + super().__init__(urwid.Text('')) + self.rebuild() - normal = self.structure['normal'] - focussed = self.structure['focus'] - urwid.AttrMap.__init__(self, self.columns, normal, focussed) def rebuild(self): - self.thread = self.dbman.get_thread(self.tid) - self.widgets = [] - self.structure = settings.get_threadline_theming(self.thread) + thread = self._dbman.get_thread(self.tid) + structure = settings.get_threadline_theming(thread) columns = [] # combine width info and widget into an urwid.Column entry def add_column(width, part): - width_tuple = self.structure[partname]['width'] + width_tuple = structure[partname]['width'] if width_tuple[0] == 'weight': columnentry = width_tuple + (part,) else: @@ -44,30 +49,33 @@ class ThreadlineWidget(urwid.AttrMap): columns.append(columnentry) # create a column for every part of the threadline - for partname in self.structure['parts']: + widgets = [] + for partname in structure['parts']: # build widget(s) around this part's content and remember them so # that self.render() may change local attributes. if partname == 'tags': - width, part = build_tags_part(self.thread.get_tags(), - self.structure['tags']['normal'], - self.structure['tags']['focus']) + width, part = build_tags_part(thread.get_tags(), + structure['tags']['normal'], + structure['tags']['focus']) if part: add_column(width, part) for w in part.widget_list: - self.widgets.append(w) + widgets.append(w) else: - width, part = build_text_part(partname, self.thread, - self.structure[partname]) + width, part = build_text_part(partname, thread, + structure[partname]) add_column(width, part) - self.widgets.append(part) + widgets.append(part) - self.columns = urwid.Columns(columns, dividechars=1) - self.original_widget = self.columns + columns = urwid.Columns(columns, dividechars = 1) + self.original_widget = urwid.AttrMap(columns, structure['normal'], structure['focus']) + self._thread = thread + self._widgets = widgets def render(self, size, focus=False): - for w in self.widgets: + for w in self._widgets: w.set_map('focus' if focus else 'normal') - return urwid.AttrMap.render(self, size, focus) + return super().render(size, focus) def selectable(self): return True @@ -76,7 +84,7 @@ class ThreadlineWidget(urwid.AttrMap): return key def get_thread(self): - return self.thread + return self._thread def build_tags_part(tags, attr_normal, attr_focus): -- cgit v1.2.3