Sfoglia il codice sorgente

make tab advance focus to dynamic fields too

Daniel Sheffield 1 anno fa
parent
commit
6ed44f66b1

+ 6 - 8
app/activities/PriceCheck.py

@@ -330,14 +330,12 @@ class PriceCheck(FocusWidget):
         ])
         widget = Filler(widget, 'top')
         widget = AttrMap(widget, 'bg')
-        super().__init__(widget, map(
-            lambda x: next(w[1] for w in chain(
+        super().__init__(widget, [
+                'product', 'organic', 'unit', 'quantity', 'price',
+                'clear', 'exit', 'sort_price', 'sort_date',
+            ], lambda: chain(
                 self.buttons.items(),
                 self.edit_fields.items(),
                 self.checkboxes.items(),
-            ) if x == w[0]),
-            [
-                'product', 'organic', 'unit', 'quantity', 'price',
-                'clear', 'exit', 'sort_price', 'sort_date',
-            ]
-        ))
+            )
+        )

+ 10 - 13
app/activities/RecipeEditor.py

@@ -509,23 +509,20 @@ class RecipeEditor(FocusWidget):
         ])
         widget = Filler(widget, 'top')
         widget = AttrMap(widget, 'bg')
-        super().__init__(widget, map(
-            lambda x: next(w for n,w in chain(
+        super().__init__(widget, [
+                'instructions',
+                'ingredients', #'quantity', 'units',
+                'add',
+                'organic',
+                'clear', 'fname', 'save', 'exit',
+            ], lambda: chain(
                 self.buttons.items(),
+                chain(*[product(['ingredients',], [*x[1:],x[0]]) for x in self.ingredients]),
                 [
                     ('fname', self.fname),
-                    ('ingredients', self.ingredients[-1][0]),
                     ('instructions', self.instructions),
-                    ('quantity', self.ingredients[-1][1]),
-                    ('units', self.ingredients[-1][2]),
                     ('organic', self.organic)
                 ],
-            ) if x == n),
-            [
-                'instructions',
-                'ingredients', 'quantity', 'units', 'add',
-                'organic',
-                'clear', 'fname', 'save', 'exit',
-            ]
-        ))
+            )
+        )
         self.update()

+ 9 - 10
app/activities/TransactionEditor.py

@@ -531,19 +531,18 @@ class TransactionEditor(FocusWidget):
         ])
         widget = Filler(widget, 'top')
         widget = AttrMap(widget, 'bg')
-        super().__init__(widget, map(
-            lambda x: next(w[1] for w in chain(
-                [('tags', self._tags[-1][0]),],
-                self.buttons.items(),
-                self.edit_fields.items(),
-                self.checkboxes.items()
-            ) if x == w[0]),
-            [
+        super().__init__(widget, [
                 'product', 'organic',
                 'unit', 'quantity', 'price',
-                'description', 'tags',# 'add',
+                'description', 'tags', 'add',
                 'done', 'clear',
                 'category', 'group',
                 'ts', 'store'
-            ]))
+            ], lambda: chain(
+                (('tags', w[0]) for w in self._tags),
+                self.buttons.items(),
+                self.edit_fields.items(),
+                self.checkboxes.items()
+            )
+        )
         self.update()

+ 16 - 14
app/widgets.py

@@ -128,21 +128,21 @@ class NoTabCheckBox(urwid.CheckBox):
 
 class FocusWidget(urwid.WidgetWrap):
     ignore_focus = True
-    def __init__(self, widget, focus_widgets):
+    def __init__(self, widget, focus_order, focus_widgets):
         super().__init__(widget)
-        self._focus_widgets = [ w for w in focus_widgets ]
-        assert len(self._focus_widgets) > 0
-        for w, p in self.iter_focus_paths():
-            if w is self._focus_widgets[0]:
-                self.container.set_focus_path(p)
+        self._focus_order = focus_order
+        self._focus_widgets = focus_widgets
+        p = next(p for (k,w), p in self.iter_focus_paths() if k == focus_order[0])
+        self.container.set_focus_path(p)
 
     @property
     def container(self):
         return self._w.original_widget.original_widget
 
     def find_widget(self, widget=None):
-        if widget in self._focus_widgets:
-            return widget
+        found = next((x for x in self._focus_widgets() if x[1] is widget), None)
+        if found is not None:
+            return found
         _w1 = getattr(widget, 'original_widget', None)
         _w2 = getattr(widget, 'contents', None)
         if _w2 is not None:
@@ -166,7 +166,7 @@ class FocusWidget(urwid.WidgetWrap):
         yield from chain(*_chain)
 
     def focus_on(self, w):
-        _, p = next(filter(lambda x: x[0] is w, self.iter_focus_paths()))
+        _, p = next(filter(lambda x: x[0][1] is w, self.iter_focus_paths()))
         path = self.container.get_focus_path()
         while path != p:
             self.advance_focus()
@@ -175,14 +175,16 @@ class FocusWidget(urwid.WidgetWrap):
     def iter_focus_paths(self):
         for c, path in self.iter_containers():
             for idx, w in enumerate(c.contents):
-                yield self.find_widget(widget=w[0]), [*path, idx]
+                r = self.find_widget(widget=w[0])
+                if r is not None:
+                    yield r, [*path, idx]
 
     def advance_focus(self, reverse=False):
         _c = self.container.get_focus_path()
-        _p = [ x[1] for w,x in product(self._focus_widgets, [x for x in filter(
-            lambda x: x[0] is not None,
-            self.iter_focus_paths()
-        )]) if x[0] is w ]
+        _p = [ next((x[1] for x in self.iter_focus_paths() if x[0][1] is w)) for k,(n,w) in product(
+            self._focus_order,
+            self._focus_widgets()
+        ) if k == n]
 
         for _prev, _cur, _next in zip([_p[-1], *_p[:-1]], _p, [*_p[1:], _p[0]]):
             if list(_c) == list(_cur):