Browse Source

Allow styling special CliMultiMenu states

Now a selected and a selected and highlighted option can be styled.
Sebastian Lohff 1 year ago
parent
commit
7a09d45ab8
1 changed files with 25 additions and 3 deletions
  1. 25
    3
      clintermission/climenu.py

+ 25
- 3
clintermission/climenu.py View File

@@ -48,10 +48,12 @@ class CliMenuStyle:
48 48
 
49 49
     Allows to select header, option and selected option color
50 50
     """
51
-    def __init__(self, option='', highlighted='', text=''):
51
+    def __init__(self, option='', highlighted='', text='', selected=None, selected_highlighted=None):
52 52
         self.option = option
53 53
         self.highlighted = highlighted
54 54
         self.text = text
55
+        self.selected = selected
56
+        self.selected_highlighted = selected_highlighted
55 57
 
56 58
 
57 59
 class CliSelectionStyle:
@@ -351,8 +353,11 @@ class CliMultiMenu(CliMenu):
351 353
         self._selection_icons = selection_icons if selection_icons is not None else self.default_selection_icons
352 354
         super().__init__(*args, **kwargs)
353 355
 
354
-    def add_option(self, text, item=_EmptyParameter, selected=False):
355
-        super().add_option(text, item)
356
+    def add_option(self, text, item=_EmptyParameter, selected=False,
357
+                   style=None, highlighted_style=None, selected_style=None, selected_highlighted_style=None):
358
+        super().add_option(text, item, style, highlighted_style=highlighted_style)
359
+        self._items[-1].selected_style = selected_style
360
+        self._items[-1].selected_highlighted_style = selected_highlighted_style
356 361
         if selected:
357 362
             self._multi_selected.append(len(self._items) - 1)
358 363
 
@@ -393,6 +398,23 @@ class CliMultiMenu(CliMenu):
393 398
         else:
394 399
             return prefix
395 400
 
401
+    def _get_style(self, item, lineno, highlighted):
402
+        s = self._style
403
+        if item.focusable and lineno in self._multi_selected:
404
+            if highlighted:
405
+                if item.selected_highlighted_style is not None:
406
+                    return item.selected_highlighted_style
407
+                if s.selected_highlighted is not None:
408
+                    return s.selected_highlighted
409
+            else:
410
+                if item.selected_style is not None:
411
+                    return item.selected_style
412
+                if s.selected is not None:
413
+                    return s.selected
414
+
415
+        # no style specified or no selected state, call parent
416
+        return super()._get_style(item, lineno, highlighted)
417
+
396 418
     def _preflight(self):
397 419
         super()._preflight()
398 420
         if self._min_selection_count > self._item_num:

Loading…
Cancel
Save