From c85e428228be8f82a93814bc48457a4f456d90f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Gir=C3=A3o=20Serr=C3=A3o?= <5621605+rodrigogiraoserrao@users.noreply.github.com> Date: Thu, 18 May 2023 16:24:07 +0100 Subject: [PATCH] Fix placeholder color cycling. --- CHANGELOG.md | 5 +++++ src/textual/widgets/_placeholder.py | 20 ++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42bf44a47..98a555d10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ 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/). +## Unreleased + +### Changed + +- `Placeholder` now sets its color cycle per app https://github.com/Textualize/textual/issues/2590 ## [0.25.0] - 2023-05-17 diff --git a/src/textual/widgets/_placeholder.py b/src/textual/widgets/_placeholder.py index fb0858aaf..a6ac37302 100644 --- a/src/textual/widgets/_placeholder.py +++ b/src/textual/widgets/_placeholder.py @@ -3,10 +3,14 @@ from __future__ import annotations from itertools import cycle +from typing import Iterator +from weakref import WeakKeyDictionary from rich.console import RenderableType from typing_extensions import Literal, Self +from textual.app import App + from .. import events from ..css._error_tools import friendly_list from ..reactive import Reactive, reactive @@ -72,18 +76,13 @@ class Placeholder(Widget): """ # Consecutive placeholders get assigned consecutive colors. - _COLORS = cycle(_PLACEHOLDER_BACKGROUND_COLORS) + _COLORS: WeakKeyDictionary[App, Iterator[str]] = WeakKeyDictionary() _SIZE_RENDER_TEMPLATE = "[b]{} x {}[/b]" variant: Reactive[PlaceholderVariant] = reactive[PlaceholderVariant]("default") _renderables: dict[PlaceholderVariant, str] - @classmethod - def reset_color_cycle(cls) -> None: - """Reset the placeholder background color cycle.""" - cls._COLORS = cycle(_PLACEHOLDER_BACKGROUND_COLORS) - def __init__( self, label: str | None = None, @@ -113,8 +112,6 @@ class Placeholder(Widget): super().__init__(name=name, id=id, classes=classes) - self.styles.background = f"{next(Placeholder._COLORS)} 50%" - self.variant = self.validate_variant(variant) """The current variant of the placeholder.""" @@ -123,6 +120,13 @@ class Placeholder(Widget): while next(self._variants_cycle) != self.variant: pass + def on_mount(self) -> None: + """Set the color for this placeholder.""" + colors = Placeholder._COLORS.setdefault( + self.app, cycle(_PLACEHOLDER_BACKGROUND_COLORS) + ) + self.styles.background = f"{next(colors)} 50%" + def render(self) -> RenderableType: """Render the placeholder.