Skip to content

SettingsScreen

Base classes

Name Children Inherits
ModalScreen
textual.screen
A screen with bindings that take precedence over the App's key bindings.

⋔ Inheritance diagram

graph TD
  1881787346["settings.SettingsScreen"]
  1923423225["screen.ModalScreen"]
  642899982["screen.Screen"]
  3481597255["typing.Generic"]
  2751680898["builtins.object"]
  4188532303["widget.Widget"]
  3946681048["dom.DOMNode"]
  53685856["message_pump.MessagePump"]
  1923423225 --> 1881787346
  642899982 --> 1923423225
  3481597255 --> 642899982
  2751680898 --> 3481597255
  4188532303 --> 642899982
  3946681048 --> 4188532303
  53685856 --> 3946681048
  2751680898 --> 53685856

🛈 DocStrings

Bases: ModalScreen[None]

Change and save settings; enter cycles a value (themes: a list).

Source code in src/acptui/ui/screens/settings.py
class SettingsScreen(ModalScreen[None]):
    """Change and save settings; ``enter`` cycles a value (themes: a list)."""

    DEFAULT_CSS = "SettingsScreen {" + DIALOG_CSS + " OptionList { max-height: 30; } }"
    BINDINGS: ClassVar = [Binding("escape", "back", "Back")]

    def __init__(self, settings: Settings, path: Path, themes: list[str]) -> None:
        super().__init__()
        self.settings = settings
        self.path = path
        self.themes = themes
        self.picking_theme = False

    def compose(self) -> ComposeResult:
        with Vertical(id="dialog"):
            yield Static("Settings", id="dialog-title")
            yield OptionList(id="settings")
            yield Static(Content(f"enter: change · saved to {self.path}"), id="dialog-hint")

    def on_mount(self) -> None:
        self._show_fields()

    def _show_fields(self, highlight: int = 0) -> None:
        self.picking_theme = False
        options = self.query_one(OptionList)
        options.clear_options()
        for field in FIELDS:
            value = show(get_value(self.settings, field.key))
            note = "  (new tabs / restart)" if field.restart else ""
            options.add_option(
                Option(
                    Content.assemble(
                        (field.title, "bold"),
                        ": ",
                        (value, "$text-accent"),
                        (note, "$text-muted italic"),
                        "\n  ",
                        (field.help, "$text-muted"),
                    ),
                    id=field.key,
                )
            )
        options.highlighted = highlight
        options.focus()

    def _show_themes(self) -> None:
        self.picking_theme = True
        options = self.query_one(OptionList)
        options.clear_options()
        current = self.settings.theme
        for theme in self.themes:
            marker = "● " if theme == current else "  "
            options.add_option(Option(marker + theme, id=f"theme:{theme}"))
        options.highlighted = self.themes.index(current) if current in self.themes else 0

    def on_option_list_option_selected(self, event: OptionList.OptionSelected) -> None:
        key = event.option_id or ""
        if key.startswith("theme:"):
            self.change("theme", key.removeprefix("theme:"))
            self._show_fields(0)
            return
        if key == "theme":
            self._show_themes()
            return
        values = choices(key)
        current = get_value(self.settings, key)
        new = values[(values.index(current) + 1) % len(values)] if current in values else values[0]
        self.change(key, new)
        self._show_fields(next(i for i, field in enumerate(FIELDS) if field.key == key))

    def change(self, key: str, value: SettingValue) -> None:
        """Change a setting: in the app now, and in the config file."""
        self.settings = with_value(self.settings, key, value)
        try:
            save_setting(self.path, key, value)
        except (OSError, ValueError) as exc:
            self.notify(f"Couldn't save {key}: {exc}", severity="error")
        self.post_message(SettingsChanged(self.settings, key))

    def action_back(self) -> None:
        """Back to the settings, or close."""
        if self.picking_theme:
            self._show_fields(0)
        else:
            self.dismiss(None)

action_back

action_back() -> None

Back to the settings, or close.

Source code in src/acptui/ui/screens/settings.py
def action_back(self) -> None:
    """Back to the settings, or close."""
    if self.picking_theme:
        self._show_fields(0)
    else:
        self.dismiss(None)

change

change(key: str, value: SettingValue) -> None

Change a setting: in the app now, and in the config file.

Source code in src/acptui/ui/screens/settings.py
def change(self, key: str, value: SettingValue) -> None:
    """Change a setting: in the app now, and in the config file."""
    self.settings = with_value(self.settings, key, value)
    try:
        save_setting(self.path, key, value)
    except (OSError, ValueError) as exc:
        self.notify(f"Couldn't save {key}: {exc}", severity="error")
    self.post_message(SettingsChanged(self.settings, key))

:fa-brands-github: Show source on GitHub