]> git.ipfire.org Git - people/ms/bricklayer.git/commitdiff
tui: Merge multi-select into select window
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 16:54:11 +0000 (16:54 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 16:54:11 +0000 (16:54 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/disk.py
src/python/tui.py

index f8ba461f10ab1456e114c61e6d374de7ed07b987..b0ce3fb3a2612bfee2b4f4fbd5da1a44bb48fa60 100644 (file)
@@ -432,10 +432,10 @@ class SelectDisk(step.InteractiveStep):
 
                while True:
                        # Select disks
-                       selection = tui.multi_select(
+                       selection = tui.select(
                                _("Disk Selection"),
                                _("Please select all disks for installation"),
-                               disks, selection=selection, width=60,
+                               disks, default=selection, multi=True, width=60,
                        )
 
                        # Is at least one disk selected?
index 9a43f051829fb770163e88df99480ed234a43614..6fbbeff5cffd788e964eeb7df06ddd3e287d4aa7 100644 (file)
@@ -148,44 +148,13 @@ class Tui(object):
                return ProgressWindow(self, title, text, max_value=max_value,
                        height=height, width=width, help=help)
 
-       def select(self, title, text, items, default=None, buttons=None, height=None,
-                       width=40, help=None):
-               window = ListboxChoiceWindow(self, title, text, items, default=default,
+       def select(self, title, text, items, default=None, multi=False, buttons=None,
+                       height=None, width=40, help=None):
+               window = SelectWindow(self, title, text, items, default=default, multi=multi,
                        buttons=buttons, height=height, width=width, help=help)
 
                return window.run()
 
-       def multi_select(self, title, text, items, selection=[], buttons=None,
-                       width=40, height=None, help=None):
-               assert self.screen
-
-               if height is None:
-                       height = len(items)
-
-               # Set some default buttons
-               if buttons is None:
-                       buttons = (_("Select"), _("Cancel"))
-
-               button_bar = snack.ButtonBar(self.screen, buttons)
-               text_box = snack.TextboxReflowed(width, text)
-
-               # Create checkboxes
-               checkboxes = snack.CheckboxTree(height, scroll=len(items) > height)
-               for key in items:
-                       checkboxes.append(items[key], key, key in selection)
-
-               # Create grid
-               grid = snack.GridFormHelp(self.screen, title, help, 1, 3)
-               grid.add(text_box, 0, 0)
-               grid.add(checkboxes, 0, 1, padding=(0, 1, 0, 1))
-               grid.add(button_bar, 0, 2, growx=True)
-
-               # Run the window
-               rc = grid.runOnce()
-
-               # Return the selection
-               return checkboxes.getSelection()
-
 
 class Window(object):
        def __init__(self, tui, title, text, height=None, width=None, help=None):
@@ -256,9 +225,9 @@ class ButtonsWindow(Window):
                pass
 
 
-class ListboxChoiceWindow(ButtonsWindow):
-       def __init__(self, tui, title, text, items, default=None, buttons=None, height=None,
-                       width=None, help=None):
+class SelectWindow(ButtonsWindow):
+       def __init__(self, tui, title, text, items, default=None, multi=False, buttons=None,
+                       height=None, width=None, help=None):
                # Set height to number of items by default
                if height is None:
                        height = len(items)
@@ -275,18 +244,25 @@ class ListboxChoiceWindow(ButtonsWindow):
 
                self.items = items
                self.default = default
+               self.multi = multi
 
                # Should we enable scrolling?
                scroll = height < len(items)
 
                # Create the list box
-               self.listbox = snack.Listbox(self.height, scroll=scroll, returnExit=True)
-               for key in self.items:
-                       self.listbox.append(self.items[key], key)
-
-               # Set the default value
-               if self.default:
-                       self.listbox.setCurrent(self.default)
+               if self.multi:
+                       self.listbox = snack.CheckboxTree(self.height, scroll=scroll)
+                       for key in self.items:
+                               self.listbox.append(self.items[key], key,
+                                       key in default if default else False)
+               else:
+                       self.listbox = snack.Listbox(self.height, scroll=scroll, returnExit=True)
+                       for key in self.items:
+                               self.listbox.append(self.items[key], key)
+
+                       # Set the default value
+                       if self.default:
+                               self.listbox.setCurrent(self.default)
 
        def _make_window(self):
                # Create a grid
@@ -305,7 +281,10 @@ class ListboxChoiceWindow(ButtonsWindow):
                return grid
 
        def default_action(self):
-               # Return the selected item
+               # Return the selected item(s)
+               if self.multi:
+                       return self.listbox.getSelection()
+
                return self.listbox.current()