Skip to content

Commit

Permalink
Merge pull request #4606 from Textualize/hatch-fix
Browse files Browse the repository at this point in the history
hatch hotfix
  • Loading branch information
willmcgugan authored Jun 5, 2024
2 parents 3291b02 + 8bc3810 commit 7925273
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 101 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.65.1] - 2024-06-05

### Fixed

- Fixed hot reloading with hatch rule https://github.com/Textualize/textual/pull/4606
- Fixed hatch style parsing https://github.com/Textualize/textual/pull/4606

## [0.65.0] - 2024-06-05

### Added
Expand Down Expand Up @@ -2062,6 +2069,7 @@ https://textual.textualize.io/blog/2022/11/08/version-040/#version-040
- New handler system for messages that doesn't require inheritance
- Improved traceback handling

[0.65.1]: https://github.com/Textualize/textual/compare/v0.65.0...v0.65.1
[0.65.0]: https://github.com/Textualize/textual/compare/v0.64.0...v0.65.0
[0.64.0]: https://github.com/Textualize/textual/compare/v0.63.6...v0.64.0
[0.63.6]: https://github.com/Textualize/textual/compare/v0.63.5...v0.63.6
Expand Down
1 change: 1 addition & 0 deletions docs/examples/styles/hatch.tcss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.hatch {
height: 1fr;
border: solid $secondary;

&.cross {
hatch: cross $success;
}
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "textual"
version = "0.65.0"
version = "0.65.1"
homepage = "https://github.com/Textualize/textual"
repository = "https://github.com/Textualize/textual"
documentation = "https://textual.textualize.io/"
Expand Down
6 changes: 5 additions & 1 deletion src/textual/css/_style_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,11 @@ class HatchProperty:
def __get__(self, obj: StylesBase, type: type[StylesBase]) -> tuple[str, Color]:
return cast("tuple[str, Color]", obj.get_rule("hatch", (" ", TRANSPARENT)))

def __set__(self, obj: StylesBase, value: tuple[str, Color | str]) -> None:
def __set__(self, obj: StylesBase, value: tuple[str, Color | str] | None) -> None:
_rich_traceback_omit = True
if value is None:
obj.clear_rule("hatch")
return
character, color = value
if len(character) != 1:
try:
Expand Down
96 changes: 59 additions & 37 deletions src/textual/css/_styles_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,51 +1058,73 @@ def process_constrain(self, name: str, tokens: list[Token]) -> None:
self.styles._rules[name] = value # type: ignore

def process_hatch(self, name: str, tokens: list[Token]) -> None:
character = " "
if not tokens:
return
character: str | None = None
color = TRANSPARENT
opacity = 1.0

for token in tokens:
if token.name == "token":
if token.value not in VALID_HATCH:
self.error(
name,
tokens[0],
string_enum_help_text(name, VALID_HATCH, context="css"),
)
character = HATCHES[token.value]
elif token.name == "string":
character = token.value[1:-1]
if len(character) != 1:
self.error(
name,
token,
f"Hatch requires a string of length 1; got {token.value}",
)
if cell_len(character) != 1:
self.error(
name,
token,
f"Hatch requires a string with a *cell length* of 1; got {token.value}",
)
elif token.name == "color":
try:
color = Color.parse(token.value)
except Exception as error:
self.error(
name,
token,
color_property_help_text(name, context="css", error=error),
)
elif token.name == "scalar":
opacity_scalar = opacity = Scalar.parse(token.value)
if len(tokens) not in (2, 3):
self.error(name, tokens[0], "2 or 3 values expected here")

character_token, color_token, *opacity_tokens = tokens

if character_token.name == "token":
if character_token.value not in VALID_HATCH:
self.error(
name,
tokens[0],
string_enum_help_text(name, VALID_HATCH, context="css"),
)
character = HATCHES[character_token.value]
elif character_token.name == "string":
character = character_token.value[1:-1]
if len(character) != 1:
self.error(
name,
character_token,
f"Hatch type requires a string of length 1; got {character_token.value}",
)
if cell_len(character) != 1:
self.error(
name,
character_token,
f"Hatch type requires a string with a *cell length* of 1; got {character_token.value}",
)

if color_token.name in ("color", "token"):
try:
color = Color.parse(color_token.value)
except Exception as error:
self.error(
name,
color_token,
color_property_help_text(name, context="css", error=error),
)
else:
self.error(
name, color_token, f"Expected a color; found {color_token.value!r}"
)

if opacity_tokens:
opacity_token = opacity_tokens[0]
if opacity_token.name == "scalar":
opacity_scalar = opacity = Scalar.parse(opacity_token.value)
if opacity_scalar.unit != Unit.PERCENT:
self.error(
name, token, "hatch alpha must be given as a percentage."
name,
opacity_token,
"hatch alpha must be given as a percentage.",
)
opacity = clamp(opacity_scalar.value / 100.0, 0, 1.0)
else:
self.error(
name,
opacity_token,
f"expected a percentage here; found {opacity_token.value!r}",
)

self.styles._rules[name] = (character, color.multiply_alpha(opacity))
self.styles._rules[name] = (character or " ", color.multiply_alpha(opacity))

def _get_suggested_property_name_for_rule(self, rule_name: str) -> str | None:
"""
Expand Down
123 changes: 62 additions & 61 deletions tests/snapshot_tests/__snapshots__/test_snapshots.ambr

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/snapshot_tests/snapshot_apps/hatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class HatchApp(App):
CSS = """
Screen {
hatch: right $primary;
hatch: right slateblue;
}
#one {
hatch: left $success;
Expand Down

0 comments on commit 7925273

Please sign in to comment.