text-opacity, dont render text when 0

This commit is contained in:
Darren Burns
2022-09-14 17:40:26 +01:00
parent bc229b4ee6
commit 0332f637ab
3 changed files with 23 additions and 5 deletions

View File

@@ -1,8 +1,10 @@
Screen {
layout: center;
background: darkslategrey;
}
.box1 {
background: darkmagenta;
width: auto;
padding: 4 8;
}

View File

@@ -1,12 +1,22 @@
from __future__ import annotations
from textual.app import App, ComposeResult
from textual.widgets import Static
from textual.binding import Binding
from textual.widgets import Static, Footer
class JustABox(App):
BINDINGS = [
Binding(key="t", action="text_fade_out", description="text-opacity fade out")
]
def compose(self) -> ComposeResult:
yield Static("Hello, world!", classes="box1")
yield Footer()
def action_text_fade_out(self) -> None:
box = self.query_one(".box1")
self.animator.animate(box.styles, "text_opacity", value=0.0, duration=1)
app = JustABox(watch_css=True, css_path="../darren/just_a_box.css")

View File

@@ -1,6 +1,7 @@
import functools
from typing import Iterable
from rich.cells import cell_len
from rich.color import Color
from rich.console import ConsoleOptions, Console, RenderResult, RenderableType
from rich.segment import Segment
@@ -67,11 +68,16 @@ class TextOpacity:
color = style.color
bgcolor = style.bgcolor
if color and color.triplet and bgcolor and bgcolor.triplet:
color_style = _get_blended_style_cached(bgcolor, color, opacity)
yield _Segment(text, style + color_style)
if opacity > 0:
if color and color.triplet and bgcolor and bgcolor.triplet:
color_style = _get_blended_style_cached(bgcolor, color, opacity)
yield _Segment(text, style + color_style)
else:
yield segment
else:
yield segment
empty_text = cell_len(text) * " "
yield _Segment(empty_text, Style.from_color(bgcolor=bgcolor))
def __rich_console__(
self, console: Console, options: ConsoleOptions