From b5e0ec8438c4459de9337f6c1541b61967d7a3bb Mon Sep 17 00:00:00 2001 From: Isaiah Odhner Date: Sat, 15 Apr 2023 01:45:23 -0400 Subject: [PATCH] Confirm overwriting files A Textual layout bug is unfortunately making the Yes button HUGE, and the No button INVISIBLE, until you mouse over the dialog, which is pretty funny... " already exists. Do you want to replace it? [Yes]" --- paint.css | 11 +++++++++++ paint.py | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/paint.css b/paint.css index cbad07f..1bf18ff 100644 --- a/paint.css +++ b/paint.css @@ -197,3 +197,14 @@ Button { width: auto; height: auto; } + +#overwrite_dialog .window_content { + padding: 2 4; + width: 40; + height: 15; +} +#overwrite_dialog .window_content Button { + width: auto; + height: auto; +} + diff --git a/paint.py b/paint.py index 4b1b53a..96009b0 100644 --- a/paint.py +++ b/paint.py @@ -719,6 +719,30 @@ class PaintApp(App): ) self.mount(window) + def confirm_overwrite(self, filename: str, callback) -> None: + for old_window in self.query("#overwrite_dialog").nodes: + old_window.close() + window = Window( + classes="dialog", + id="overwrite_dialog", + title="Save As", + ) + window.content.mount( + Static(filename + " already exists.", markup=False), + Static("Do you want to replace it?"), + Horizontal( + Button("Yes", id="overwrite_yes_button"), + Button("No", id="overwrite_no_button"), + ) + ) + self.mount(window) + def on_button_pressed(event): + if event.button.id == "overwrite_yes_button": + callback() + window.close() + window.on_button_pressed = on_button_pressed + + # def action_open(self) -> None: # """Open an image from a file.""" # filename = self.query_one("#file_open").value @@ -968,9 +992,15 @@ class PaintApp(App): if name: if self.directory_tree_selected_path: name = os.path.join(self.directory_tree_selected_path, name) - self.filename = name - self.action_save() - self.query_one("#save_as_dialog", Window).close() + def on_save_confirmed(): + self.query_one("#save_as_dialog", Window).close() + self.filename = name + self.action_save() + if os.path.exists(name): + self.confirm_overwrite(name, on_save_confirmed) + else: + on_save_confirmed() + elif button_id == "save_as_cancel_button": self.query_one("#save_as_dialog", Window).close()