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