"""Confirmation dialog screen for destructive actions.""" from textual import on from textual.app import ComposeResult from textual.binding import Binding from textual.containers import Horizontal, Vertical, Container from textual.screen import ModalScreen from textual.widgets import Button, Label, Static class ConfirmDialog(ModalScreen[bool]): """A modal confirmation dialog that returns True if confirmed, False otherwise.""" BINDINGS = [ Binding("escape", "cancel", "Cancel"), Binding("enter", "confirm", "Confirm"), Binding("y", "confirm", "Yes"), Binding("n", "cancel", "No"), ] DEFAULT_CSS = """ ConfirmDialog { align: center middle; } ConfirmDialog #confirm-container { width: 50; height: auto; min-height: 7; background: $surface; border: thick $primary; padding: 1 2; } ConfirmDialog #confirm-title { text-style: bold; width: 1fr; height: 1; text-align: center; margin-bottom: 1; } ConfirmDialog #confirm-message { width: 1fr; height: 1; text-align: center; margin-bottom: 1; } ConfirmDialog #confirm-buttons { width: 1fr; height: 3; align: center middle; margin-top: 1; } ConfirmDialog Button { margin: 0 1; } """ def __init__( self, title: str = "Confirm", message: str = "Are you sure?", confirm_label: str = "Yes", cancel_label: str = "No", **kwargs, ): """Initialize the confirmation dialog. Args: title: The dialog title message: The confirmation message confirm_label: Label for the confirm button cancel_label: Label for the cancel button """ super().__init__(**kwargs) self._title = title self._message = message self._confirm_label = confirm_label self._cancel_label = cancel_label def compose(self) -> ComposeResult: with Container(id="confirm-container"): yield Label(self._title, id="confirm-title") yield Label(self._message, id="confirm-message") with Horizontal(id="confirm-buttons"): yield Button(self._cancel_label, id="cancel", variant="default") yield Button(self._confirm_label, id="confirm", variant="error") def on_mount(self) -> None: """Focus the cancel button by default (safer option).""" self.query_one("#cancel", Button).focus() @on(Button.Pressed, "#confirm") def handle_confirm(self) -> None: self.dismiss(True) @on(Button.Pressed, "#cancel") def handle_cancel(self) -> None: self.dismiss(False) def action_confirm(self) -> None: self.dismiss(True) def action_cancel(self) -> None: self.dismiss(False)