From ec93150a81b27e4d89001a013e4031c5759c56f3 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Mon, 29 Jul 2024 23:08:50 +0100 Subject: [PATCH] Reasonable theme defaults for URL highlighting --- src/posting/posting.scss | 4 ++-- src/posting/themes.py | 19 ++++++++++++++++++- src/posting/widgets/request/url_bar.py | 4 ++-- src/posting/widgets/variable_input.py | 5 +++-- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/posting/posting.scss b/src/posting/posting.scss index 3b5b9df0..7bf6a4ae 100644 --- a/src/posting/posting.scss +++ b/src/posting/posting.scss @@ -301,11 +301,11 @@ Tree { Input { padding: 0 1; height: 1; - background: $surface; + background: $surface 80%; border: none; &:focus { - background: $surface-lighten-1; + background: $surface; padding-left: 0; border-left: outer $surface-lighten-2; diff --git a/src/posting/themes.py b/src/posting/themes.py index d74efb47..4cd2019d 100644 --- a/src/posting/themes.py +++ b/src/posting/themes.py @@ -73,6 +73,14 @@ class VariableStyles(BaseModel): unresolved: str | None = Field(default="dim") """The style to apply to unresolved variables.""" + def fill_with_defaults(self, theme: "Theme") -> "VariableStyles": + """Return a new VariableStyles object with `None` values filled + with reasonable defaults from the given theme.""" + return VariableStyles( + resolved=self.resolved or theme.success, + unresolved=self.unresolved or theme.secondary, + ) + class UrlStyles(BaseModel): """The style to apply to URL input fields.""" @@ -83,9 +91,18 @@ class UrlStyles(BaseModel): protocol: str | None = Field(default=None) """The style to apply to the URL protocol.""" - separator: str | None = Field(default="dim b") + separator: str | None = Field(default="dim") """The style to apply to URL separators e.g. `/`.""" + def fill_with_defaults(self, theme: "Theme") -> "UrlStyles": + """Return a new UrlStyles object with `None` values filled + with reasonable defaults from the given theme.""" + return UrlStyles( + base=self.base or theme.secondary, + protocol=self.protocol or theme.accent, + separator=self.separator or "dim", + ) + class Theme(BaseModel): name: str = Field(exclude=True) diff --git a/src/posting/widgets/request/url_bar.py b/src/posting/widgets/request/url_bar.py index 4b426119..2c4ee6b7 100644 --- a/src/posting/widgets/request/url_bar.py +++ b/src/posting/widgets/request/url_bar.py @@ -100,9 +100,9 @@ def watch_cursor_position(self, cursor_position: int) -> None: def on_theme_change(self, theme: Theme) -> None: super().on_theme_change(theme) if theme.variable: - self.highlighter.variable_styles = theme.variable + self.highlighter.variable_styles = theme.variable.fill_with_defaults(theme) if theme.url: - self.highlighter.url_styles = theme.url + self.highlighter.url_styles = theme.url.fill_with_defaults(theme) class SendRequestButton(Button, can_focus=False): diff --git a/src/posting/widgets/variable_input.py b/src/posting/widgets/variable_input.py index a12a1c7a..cf24af89 100644 --- a/src/posting/widgets/variable_input.py +++ b/src/posting/widgets/variable_input.py @@ -36,5 +36,6 @@ def on_theme_change(self, theme: Theme) -> None: theme: The new app theme. """ super().on_theme_change(theme) - self.highlighter.variable_styles = theme.variable - self.refresh() + if theme.variable: + self.highlighter.variable_styles = theme.variable.fill_with_defaults(theme) + self.refresh()