From cb53370600706c1c3127ac5089e4a46b89512bfc Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Wed, 14 Aug 2024 22:55:14 +0100 Subject: [PATCH] Auto-focus in confirmation dialog --- src/posting/widgets/collection/browser.py | 2 +- src/posting/widgets/confirmation.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/posting/widgets/collection/browser.py b/src/posting/widgets/collection/browser.py index ac551d90..833cf9cb 100644 --- a/src/posting/widgets/collection/browser.py +++ b/src/posting/widgets/collection/browser.py @@ -496,7 +496,7 @@ def deletion_callback(confirmed: bool | None) -> None: ) await self.app.push_screen( - ConfirmationModal(confirmation_message), + ConfirmationModal(confirmation_message, auto_focus="confirm"), callback=deletion_callback, ) diff --git a/src/posting/widgets/confirmation.py b/src/posting/widgets/confirmation.py index 53e37115..417881e4 100644 --- a/src/posting/widgets/confirmation.py +++ b/src/posting/widgets/confirmation.py @@ -1,5 +1,6 @@ """A modal screen for confirming a destructive action.""" +from typing import Literal from textual.app import ComposeResult from textual.containers import Horizontal, Vertical from textual.screen import ModalScreen @@ -31,6 +32,7 @@ def __init__( confirm_binding: str = "y", cancel_text: str = "No \[n]", cancel_binding: str = "n", + auto_focus: Literal["confirm", "cancel"] | None = "confirm", name: str | None = None, id: str | None = None, classes: str | None = None, @@ -41,11 +43,14 @@ def __init__( self.confirm_binding = confirm_binding self.cancel_text = cancel_text self.cancel_binding = cancel_binding + self.auto_focus = auto_focus def on_mount(self) -> None: self._bindings.bind(self.confirm_binding, "screen.dismiss(True)") self._bindings.bind(self.cancel_binding, "screen.dismiss(False)") self._bindings.bind("escape", "screen.dismiss(False)") + if self.auto_focus is not None: + self.query_one(f"#{self.auto_focus}-button").focus() def compose(self) -> ComposeResult: with Vertical(id="confirmation-screen", classes="modal-body") as container: