mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Merge pull request #2607 from Textualize/placeholder-cycle
Placeholder color cycle
This commit is contained in:
22
CHANGELOG.md
22
CHANGELOG.md
@@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- `work` decorator accepts `description` parameter to add debug string https://github.com/Textualize/textual/issues/2597
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- `Placeholder` now sets its color cycle per app https://github.com/Textualize/textual/issues/2590
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
|
||||||
|
- `Placeholder.reset_color_cycle`
|
||||||
|
|
||||||
|
|
||||||
## [0.26.0] - 2023-05-20
|
## [0.26.0] - 2023-05-20
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
@@ -15,13 +30,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
- Textual will now scroll focused widgets to center if not in view
|
- Textual will now scroll focused widgets to center if not in view
|
||||||
|
|
||||||
## Unreleased
|
|
||||||
|
|
||||||
### Added
|
|
||||||
|
|
||||||
- `work` decorator accepts `description` parameter to add debug string https://github.com/Textualize/textual/issues/2597
|
|
||||||
|
|
||||||
|
|
||||||
## [0.25.0] - 2023-05-17
|
## [0.25.0] - 2023-05-17
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from textual.app import App
|
from textual.app import App
|
||||||
from textual.containers import Horizontal
|
from textual.containers import Horizontal
|
||||||
from textual.widgets import Placeholder, Label, Static
|
from textual.widgets import Label, Placeholder, Static
|
||||||
|
|
||||||
|
|
||||||
class Ruler(Static):
|
class Ruler(Static):
|
||||||
@@ -9,7 +9,7 @@ class Ruler(Static):
|
|||||||
yield Label(ruler_text)
|
yield Label(ruler_text)
|
||||||
|
|
||||||
|
|
||||||
class HeightComparisonApp(App):
|
class WidthComparisonApp(App):
|
||||||
def compose(self):
|
def compose(self):
|
||||||
yield Horizontal(
|
yield Horizontal(
|
||||||
Placeholder(id="cells"), # (1)!
|
Placeholder(id="cells"), # (1)!
|
||||||
@@ -25,4 +25,6 @@ class HeightComparisonApp(App):
|
|||||||
yield Ruler()
|
yield Ruler()
|
||||||
|
|
||||||
|
|
||||||
app = HeightComparisonApp(css_path="width_comparison.css")
|
app = WidthComparisonApp(css_path="width_comparison.css")
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run()
|
||||||
|
|||||||
@@ -3,10 +3,14 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from itertools import cycle
|
from itertools import cycle
|
||||||
|
from typing import Iterator
|
||||||
|
from weakref import WeakKeyDictionary
|
||||||
|
|
||||||
from rich.console import RenderableType
|
from rich.console import RenderableType
|
||||||
from typing_extensions import Literal, Self
|
from typing_extensions import Literal, Self
|
||||||
|
|
||||||
|
from textual.app import App
|
||||||
|
|
||||||
from .. import events
|
from .. import events
|
||||||
from ..css._error_tools import friendly_list
|
from ..css._error_tools import friendly_list
|
||||||
from ..reactive import Reactive, reactive
|
from ..reactive import Reactive, reactive
|
||||||
@@ -72,18 +76,13 @@ class Placeholder(Widget):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# Consecutive placeholders get assigned consecutive colors.
|
# Consecutive placeholders get assigned consecutive colors.
|
||||||
_COLORS = cycle(_PLACEHOLDER_BACKGROUND_COLORS)
|
_COLORS: WeakKeyDictionary[App, Iterator[str]] = WeakKeyDictionary()
|
||||||
_SIZE_RENDER_TEMPLATE = "[b]{} x {}[/b]"
|
_SIZE_RENDER_TEMPLATE = "[b]{} x {}[/b]"
|
||||||
|
|
||||||
variant: Reactive[PlaceholderVariant] = reactive[PlaceholderVariant]("default")
|
variant: Reactive[PlaceholderVariant] = reactive[PlaceholderVariant]("default")
|
||||||
|
|
||||||
_renderables: dict[PlaceholderVariant, str]
|
_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__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
label: str | None = None,
|
label: str | None = None,
|
||||||
@@ -113,8 +112,6 @@ class Placeholder(Widget):
|
|||||||
|
|
||||||
super().__init__(name=name, id=id, classes=classes)
|
super().__init__(name=name, id=id, classes=classes)
|
||||||
|
|
||||||
self.styles.background = f"{next(Placeholder._COLORS)} 50%"
|
|
||||||
|
|
||||||
self.variant = self.validate_variant(variant)
|
self.variant = self.validate_variant(variant)
|
||||||
"""The current variant of the placeholder."""
|
"""The current variant of the placeholder."""
|
||||||
|
|
||||||
@@ -123,6 +120,13 @@ class Placeholder(Widget):
|
|||||||
while next(self._variants_cycle) != self.variant:
|
while next(self._variants_cycle) != self.variant:
|
||||||
pass
|
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:
|
def render(self) -> RenderableType:
|
||||||
"""Render the placeholder.
|
"""Render the placeholder.
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -91,7 +91,6 @@ def test_buttons_render(snap_compare):
|
|||||||
|
|
||||||
def test_placeholder_render(snap_compare):
|
def test_placeholder_render(snap_compare):
|
||||||
# Testing the rendering of the multiple placeholder variants and labels.
|
# Testing the rendering of the multiple placeholder variants and labels.
|
||||||
Placeholder.reset_color_cycle()
|
|
||||||
assert snap_compare(WIDGET_EXAMPLES_DIR / "placeholder.py")
|
assert snap_compare(WIDGET_EXAMPLES_DIR / "placeholder.py")
|
||||||
|
|
||||||
|
|
||||||
@@ -261,7 +260,6 @@ PATHS = [
|
|||||||
@pytest.mark.parametrize("file_name", PATHS)
|
@pytest.mark.parametrize("file_name", PATHS)
|
||||||
def test_css_property(file_name, snap_compare):
|
def test_css_property(file_name, snap_compare):
|
||||||
path_to_app = STYLES_EXAMPLES_DIR / file_name
|
path_to_app = STYLES_EXAMPLES_DIR / file_name
|
||||||
Placeholder.reset_color_cycle()
|
|
||||||
assert snap_compare(path_to_app)
|
assert snap_compare(path_to_app)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user