From fea15472e3e21e50829a801f495af4fa3c144a0f Mon Sep 17 00:00:00 2001 From: 3wc <3wc@doesthisthing.work> Date: Mon, 25 Dec 2023 19:28:42 -0300 Subject: [PATCH] Add a confirmation dialogue when deleting activites.. ..which have >0 facts. Re #2 --- hamstertools/app.tcss | 24 ++++++++++++++++++- hamstertools/screens/hamster.py | 41 +++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/hamstertools/app.tcss b/hamstertools/app.tcss index 3f74269..43f9aca 100644 --- a/hamstertools/app.tcss +++ b/hamstertools/app.tcss @@ -18,7 +18,7 @@ DataTable:focus .datatable--cursor { width: 50%; } -ActivityEditScreen, ActivityMappingScreen { +ActivityEditScreen, ActivityMappingScreen, ActivityDeleteConfirmScreen { align: center middle; } @@ -62,3 +62,25 @@ ActivityMappingScreen AutoComplete { ActivityEditScreen Input { width: 60; } + +#dialog { + grid-size: 2; + grid-gutter: 1 2; + grid-rows: 1fr 3; + padding: 0 1; + width: 60; + height: 11; + border: thick $background 80%; + background: $surface; +} + +#question { + column-span: 2; + height: 1fr; + width: 1fr; + content-align: center middle; +} + +Button { + width: 100%; +} diff --git a/hamstertools/screens/hamster.py b/hamstertools/screens/hamster.py index 56a06c5..313bd8b 100644 --- a/hamstertools/screens/hamster.py +++ b/hamstertools/screens/hamster.py @@ -4,7 +4,7 @@ from textual import on from textual.app import ComposeResult from textual.binding import Binding from textual.coordinate import Coordinate -from textual.containers import Horizontal, Vertical +from textual.containers import Horizontal, Vertical, Grid from textual.events import DescendantBlur from textual.screen import Screen, ModalScreen from textual.widgets import ( @@ -16,6 +16,7 @@ from textual.widgets import ( Checkbox, TabbedContent, TabPane, + Button ) from peewee import fn, JOIN @@ -273,6 +274,29 @@ class ActivityMappingScreen(ModalScreen): self.dismiss(None) +class ActivityDeleteConfirmScreen(ModalScreen): + BINDINGS = [ + ("escape", "cancel", "Cancel"), + ] + + def compose(self) -> ComposeResult: + yield Grid( + Label("Are you sure you want to delete this activity?", id="question"), + Button("Confirm", variant="error", id="confirm"), + Button("Cancel", variant="primary", id="cancel"), + id="dialog", + ) + + def on_button_pressed(self, event: Button.Pressed) -> None: + if event.button.id == "quit": + self.dismiss(True) + else: + self.dismiss(False) + + def action_cancel(self): + self.dismiss(False) + + class ActivityList(ListPane): BINDINGS = [ ("s", "sort", "Sort"), @@ -384,10 +408,19 @@ class ActivityList(ListPane): ) activity = HamsterActivity.get(id=activity_id) - activity.delete_instance() - # supply the row key to `remove_row` to delete the row. - self.table.remove_row(row_key) + def check_delete(delete: bool) -> None: + """Called when QuitScreen is dismissed.""" + if delete: + activity.delete_instance() + + # supply the row key to `remove_row` to delete the row. + self.table.remove_row(row_key) + + if activity.facts.count() > 0: + self.app.push_screen(ActivityDeleteConfirmScreen(), check_delete) + else: + check_delete(True) def action_move_facts(self) -> None: row_idx: int = self.table.cursor_row