Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Placeholder color cycle #2607

Merged
merged 3 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ 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

### Removed

- `Placeholder.reset_color_cycle`

## [0.25.0] - 2023-05-17

Expand Down
8 changes: 5 additions & 3 deletions docs/examples/styles/width_comparison.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from textual.app import App
from textual.containers import Horizontal
from textual.widgets import Placeholder, Label, Static
from textual.widgets import Label, Placeholder, Static


class Ruler(Static):
Expand All @@ -9,7 +9,7 @@ def compose(self):
yield Label(ruler_text)


class HeightComparisonApp(App):
class WidthComparisonApp(App):
def compose(self):
yield Horizontal(
Placeholder(id="cells"), # (1)!
Expand All @@ -25,4 +25,6 @@ def compose(self):
yield Ruler()


app = HeightComparisonApp(css_path="width_comparison.css")
app = WidthComparisonApp(css_path="width_comparison.css")
if __name__ == "__main__":
app.run()
20 changes: 12 additions & 8 deletions src/textual/widgets/_placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -113,8 +112,6 @@ def __init__(

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."""

Expand All @@ -123,6 +120,13 @@ def __init__(
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.

Expand Down
128 changes: 64 additions & 64 deletions tests/snapshot_tests/__snapshots__/test_snapshots.ambr

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions tests/snapshot_tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def test_buttons_render(snap_compare):

def test_placeholder_render(snap_compare):
# Testing the rendering of the multiple placeholder variants and labels.
Placeholder.reset_color_cycle()
assert snap_compare(WIDGET_EXAMPLES_DIR / "placeholder.py")


Expand Down Expand Up @@ -261,7 +260,6 @@ def test_select_expanded_changed(snap_compare):
@pytest.mark.parametrize("file_name", PATHS)
def test_css_property(file_name, snap_compare):
path_to_app = STYLES_EXAMPLES_DIR / file_name
Placeholder.reset_color_cycle()
assert snap_compare(path_to_app)


Expand Down