Tidying up, applying opacity if it exists in styles

This commit is contained in:
Darren Burns
2022-02-11 11:46:04 +00:00
parent 43696294d2
commit 80251e3234
4 changed files with 14 additions and 10 deletions

View File

@@ -18,6 +18,7 @@ class BasicApp(App):
self.bind("tab", "toggle_class('#sidebar', '-active')") self.bind("tab", "toggle_class('#sidebar', '-active')")
self.bind("a", "toggle_class('#header', '-visible')") self.bind("a", "toggle_class('#header', '-visible')")
self.bind("c", "toggle_class('#content', '-content-visible')") self.bind("c", "toggle_class('#content', '-content-visible')")
self.bind("d", "toggle_class('#footer', 'dim')")
def on_mount(self): def on_mount(self):
"""Build layout here.""" """Build layout here."""

View File

@@ -34,7 +34,6 @@ Widget:hover {
#header { #header {
text: $text on $primary; text: $text on $primary;
opacity: 0.2;
height: 3; height: 3;
border-bottom: hkey $secondary; border-bottom: hkey $secondary;
} }
@@ -53,7 +52,12 @@ Widget:hover {
} }
#footer { #footer {
opacity: 1;
text: $text on $primary; text: $text on $primary;
height: 3; height: 3;
border-top: hkey $secondary; border-top: hkey $secondary;
} }
#footer.dim {
opacity: 0.5;
}

View File

@@ -130,7 +130,7 @@ class StylesBuilder:
self.error( self.error(
name, name,
token, token,
f"invalid value for visibility (received {value!r}, expected {friendly_list(VALID_VISIBILITY)})", f"property 'visibility' has invalid value {value!r}; expected {friendly_list(VALID_VISIBILITY)}",
) )
else: else:
self.error(name, token, f"invalid token {value!r} in this context") self.error(name, token, f"invalid token {value!r} in this context")
@@ -152,18 +152,12 @@ class StylesBuilder:
self.styles.set_rule(name, opacity) self.styles.set_rule(name, opacity)
except ValueError: except ValueError:
error = True error = True
# self.error(
# name, token, f"unable to process value {value!r} as percentage"
# )
elif token_name == "number": elif token_name == "number":
try: try:
opacity = clamp(float(value), 0, 1) opacity = clamp(float(value), 0, 1)
self.styles.set_rule(name, opacity) self.styles.set_rule(name, opacity)
except ValueError: except ValueError:
error = True error = True
# self.error(
# name, token, f"unable to process value {value!r} as float"
# )
else: else:
error = True error = True
@@ -171,7 +165,9 @@ class StylesBuilder:
self.error( self.error(
name, name,
token, token,
f"property 'opacity' has invalid value {_join_tokens(tokens)!r}; expected a scalar percentage or float between 0 and 1; example valid values: '0.4', '40%'", f"property 'opacity' has invalid value {_join_tokens(tokens)!r}; "
f"expected a percentage or float between 0 and 1; "
f"example valid values: '0.4', '40%'",
) )
def _process_space(self, name: str, tokens: list[Token]) -> None: def _process_space(self, name: str, tokens: list[Token]) -> None:

View File

@@ -32,7 +32,7 @@ from .geometry import Size, Spacing
from .message import Message from .message import Message
from .messages import Layout, Update from .messages import Layout, Update
from .reactive import watch from .reactive import watch
from .renderables.opacity import Opacity
if TYPE_CHECKING: if TYPE_CHECKING:
from .view import View from .view import View
@@ -174,6 +174,9 @@ class Widget(DOMNode):
style=renderable_text_style, style=renderable_text_style,
) )
if styles.opacity:
renderable = Opacity(renderable, opacity=styles.opacity)
return renderable return renderable
@property @property