mirror of
https://github.com/1j01/textual-paint.git
synced 2024-09-11 15:56:03 +03:00
Move ThemedIcon to a separate file
This commit is contained in:
parent
0f617dd8c4
commit
80d9f38318
@ -18,73 +18,12 @@ Two nice things about embedding it are:
|
||||
TODO: unify formats/authoring workflow?
|
||||
"""
|
||||
|
||||
from rich.console import RenderableType
|
||||
from rich.protocol import is_renderable
|
||||
from rich.text import Text
|
||||
from textual.errors import RenderError
|
||||
from textual.widgets import Static
|
||||
|
||||
from textual_paint.args import args
|
||||
from textual_paint.localization.i18n import get as _
|
||||
|
||||
|
||||
def _check_renderable(renderable: object):
|
||||
"""Check if a renderable conforms to the Rich Console protocol
|
||||
(https://rich.readthedocs.io/en/latest/protocol.html)
|
||||
|
||||
Args:
|
||||
renderable: A potentially renderable object.
|
||||
|
||||
Raises:
|
||||
RenderError: If the object can not be rendered.
|
||||
"""
|
||||
if not is_renderable(renderable):
|
||||
raise RenderError(
|
||||
f"unable to render {renderable!r}; a string, Text, or other Rich renderable is required"
|
||||
)
|
||||
|
||||
class ThemedIcon(Static):
|
||||
"""A Static widget that changes its content based on the theme.
|
||||
|
||||
Args:
|
||||
light_renderable: A Rich renderable, or string containing console markup, for the light theme.
|
||||
dark_renderable: A Rich renderable, or string containing console markup, for the dark theme.
|
||||
name: Name of widget.
|
||||
id: ID of Widget.
|
||||
classes: Space separated list of class names.
|
||||
disabled: Whether the static is disabled or not.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
light_renderable: RenderableType,
|
||||
dark_renderable: RenderableType,
|
||||
*,
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
):
|
||||
"""Initialize the icon."""
|
||||
super().__init__("",
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
)
|
||||
self.light_renderable = light_renderable
|
||||
self.dark_renderable = dark_renderable
|
||||
_check_renderable(light_renderable)
|
||||
_check_renderable(dark_renderable)
|
||||
self.watch(self.app, "dark", self._on_dark_changed, init=False)
|
||||
self._on_dark_changed(False, self.app.dark)
|
||||
|
||||
def _on_dark_changed(self, old_value: bool, dark: bool) -> None:
|
||||
if dark:
|
||||
self.update(self.dark_renderable)
|
||||
else:
|
||||
self.update(self.light_renderable)
|
||||
|
||||
from textual_paint.themed_icon import ThemedIcon
|
||||
|
||||
# ASCII line art version:
|
||||
# get_warning_icon = lambda: Static("""[#ffff00]
|
||||
|
65
src/textual_paint/themed_icon.py
Normal file
65
src/textual_paint/themed_icon.py
Normal file
@ -0,0 +1,65 @@
|
||||
|
||||
from rich.console import RenderableType
|
||||
from rich.protocol import is_renderable
|
||||
from textual.errors import RenderError
|
||||
from textual.widgets import Static
|
||||
|
||||
from textual_paint.localization.i18n import get as _
|
||||
|
||||
|
||||
def _check_renderable(renderable: object):
|
||||
"""Check if a renderable conforms to the Rich Console protocol
|
||||
(https://rich.readthedocs.io/en/latest/protocol.html)
|
||||
|
||||
Args:
|
||||
renderable: A potentially renderable object.
|
||||
|
||||
Raises:
|
||||
RenderError: If the object can not be rendered.
|
||||
"""
|
||||
if not is_renderable(renderable):
|
||||
raise RenderError(
|
||||
f"unable to render {renderable!r}; a string, Text, or other Rich renderable is required"
|
||||
)
|
||||
|
||||
class ThemedIcon(Static):
|
||||
"""A Static widget that changes its content based on the theme.
|
||||
|
||||
Args:
|
||||
light_renderable: A Rich renderable, or string containing console markup, for the light theme.
|
||||
dark_renderable: A Rich renderable, or string containing console markup, for the dark theme.
|
||||
name: Name of widget.
|
||||
id: ID of Widget.
|
||||
classes: Space separated list of class names.
|
||||
disabled: Whether the static is disabled or not.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
light_renderable: RenderableType,
|
||||
dark_renderable: RenderableType,
|
||||
*,
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
):
|
||||
"""Initialize the icon."""
|
||||
super().__init__("",
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
)
|
||||
self.light_renderable = light_renderable
|
||||
self.dark_renderable = dark_renderable
|
||||
_check_renderable(light_renderable)
|
||||
_check_renderable(dark_renderable)
|
||||
self.watch(self.app, "dark", self._on_dark_changed, init=False)
|
||||
self._on_dark_changed(False, self.app.dark)
|
||||
|
||||
def _on_dark_changed(self, old_value: bool, dark: bool) -> None:
|
||||
if dark:
|
||||
self.update(self.dark_renderable)
|
||||
else:
|
||||
self.update(self.light_renderable)
|
Loading…
Reference in New Issue
Block a user