improve choice selector

This commit is contained in:
jpt 2025-04-15 00:19:13 -05:00
parent d0e80bb59e
commit 99cbbfe5f0

View File

@ -14,6 +14,15 @@ class ConfirmModal(ModalScreen):
align: center middle;
background: $primary 30%;
}
ConfirmModal Vertical {
border: double teal;
height: 10;
width: 30;
}
ConfirmModal Label.hints {
border: solid grey;
height: 4;
}
"""
BINDINGS = [
@ -43,6 +52,15 @@ class ChoiceModal(ModalScreen):
align: center middle;
background: $primary 30%;
}
ChoiceModal Vertical {
border: double teal;
height: 10;
width: 30;
}
ChoiceModal Label.hints {
border: solid grey;
height: 4;
}
ChoiceModal Label {
height: 1;
}
@ -72,21 +90,28 @@ class ChoiceModal(ModalScreen):
super().__init__()
def compose(self):
for idx, e in enumerate(self._enum):
yield Label(
("> " if idx == self.sel_idx else " ") +
get_color_enum(e.value, self._enum),
classes="selected" if idx == self.sel_idx else "",
)
with Vertical():
for idx, e in enumerate(self._enum):
yield Label(
(f"> {idx} " if idx == self.sel_idx else f" {idx} ")
+ get_color_enum(e.value, self._enum),
classes="selected" if idx == self.sel_idx else "",
)
yield Label("""(h/j/k/l) move (0-9) quick select
(enter) confirm (esc) quit""", classes="hints")
def _move_cursor(self, dir):
labels = self.query(Label)
# reset old
labels[self.sel_idx].update(" " + get_color_enum(self.enum_by_idx[self.sel_idx], self._enum))
labels[self.sel_idx].update(
f" {self.sel_idx} " + get_color_enum(self.enum_by_idx[self.sel_idx], self._enum)
)
# move cursor
self.sel_idx = (self.sel_idx + dir) % len(self._enum)
# reset new
labels[self.sel_idx].update("> " + get_color_enum(self.enum_by_idx[self.sel_idx], self._enum))
labels[self.sel_idx].update(
f"> {self.sel_idx} " + get_color_enum(self.enum_by_idx[self.sel_idx], self._enum)
)
def action_cursor_down(self):
self._move_cursor(1)
@ -100,6 +125,12 @@ class ChoiceModal(ModalScreen):
def action_cancel(self):
self.app.pop_screen()
def on_key(self, event) -> None:
key = event.key
if key in "0123456789":
idx = int(key)
self.dismiss(self.enum_by_idx[idx])
class DateModal(ModalScreen):
CSS = """
@ -112,8 +143,6 @@ class DateModal(ModalScreen):
height: 10;
width: 50;
}
DateModal Horizonal {
}
DateModal Label {
border: solid white;
align: center middle;
@ -155,9 +184,15 @@ class DateModal(ModalScreen):
with Vertical():
with Horizontal():
for i in range(3):
yield Label(f"{self.pieces[i]}", classes="selected-date" if self.selected == i else "")
yield Label("""(h/j/k/l) move (enter) confirm (esc) quit
(p)ast (t)oday (f)uture""", classes="hints")
yield Label(
f"{self.pieces[i]}",
classes="selected-date" if self.selected == i else "",
)
yield Label(
"""(h/j/k/l) move (enter) confirm (esc) quit
(p)ast (t)oday (f)uture""",
classes="hints",
)
def action_future(self):
self.pieces = list(SPECIAL_DATES_PIECES["future"])