Skip to content

Commit

Permalink
Reasonable theme defaults for URL highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Jul 29, 2024
1 parent 664b2dd commit ec93150
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/posting/posting.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
19 changes: 18 additions & 1 deletion src/posting/themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/posting/widgets/request/url_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions src/posting/widgets/variable_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit ec93150

Please sign in to comment.