-
Notifications
You must be signed in to change notification settings - Fork 28
Display album artwork in the TUI via artwork@v1
#266
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
52cecf4
Add textual-image dependency for TUI artwork
maximmaxim345 abe7f7b
Add ArtworkHandler for artwork@v1 binary frames
maximmaxim345 d036c97
Add TUI artwork render helper with capability probe
maximmaxim345 1b044e8
Add artwork_image and artwork_generation to UIState
maximmaxim345 b9f8404
Wire ArtworkHandler into TUI app lifecycle
maximmaxim345 6c78d43
Render album artwork in the Now Playing panel
maximmaxim345 e5b4ef7
Fix artwork width and resize behavior
maximmaxim345 8339920
Request 128x128 artwork instead of 512x512
maximmaxim345 d9b2828
Tighten artwork modules
maximmaxim345 35b484b
Match artwork height to the metadata column
maximmaxim345 b154f21
Drop unused logger from `tui/artwork.py`
maximmaxim345 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """Artwork connector for bridging Sendspin client to the TUI.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import io | ||
| import logging | ||
| from collections.abc import Callable | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from PIL import Image, UnidentifiedImageError | ||
|
|
||
| if TYPE_CHECKING: | ||
| from aiosendspin.client import SendspinClient | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ArtworkHandler: | ||
| """Bridges between SendspinClient artwork frames and the TUI. | ||
|
|
||
| Subscribes to artwork binary frames (album channel only), decodes them via | ||
| Pillow, and routes the latest image to a callback. Empty payloads, stream | ||
| end, and stream clear all collapse to ``on_image(None)``. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| on_image: Callable[[Image.Image | None], None], | ||
| ) -> None: | ||
| self._on_image = on_image | ||
| self._unsubscribes: list[Callable[[], None]] = [] | ||
|
|
||
| def attach_client(self, client: SendspinClient) -> None: | ||
| """Register artwork, stream_end, and stream_clear listeners.""" | ||
| self._unsubscribes = [ | ||
| client.add_artwork_listener(self._on_artwork_frame), | ||
| client.add_stream_end_listener(self._on_stream_end), | ||
| client.add_stream_clear_listener(self._on_stream_clear), | ||
| ] | ||
|
|
||
| def detach(self) -> None: | ||
| """Unregister listeners. Silent: never fires the callback.""" | ||
| for unsub in self._unsubscribes: | ||
| unsub() | ||
| self._unsubscribes = [] | ||
|
|
||
| def _on_artwork_frame(self, channel: int, payload: bytes) -> None: | ||
| if channel != 0: | ||
| return | ||
| if not payload: | ||
| self._on_image(None) | ||
| return | ||
| try: | ||
| image = Image.open(io.BytesIO(payload)) | ||
| image.load() | ||
| except (UnidentifiedImageError, OSError) as exc: | ||
| logger.warning("Failed to decode artwork payload: %s", exc) | ||
| self._on_image(None) | ||
|
maximmaxim345 marked this conversation as resolved.
|
||
| return | ||
| self._on_image(image) | ||
|
|
||
| def _on_stream_end(self, roles: list[str] | None) -> None: | ||
| if roles is not None and "artwork" not in roles: | ||
|
maximmaxim345 marked this conversation as resolved.
|
||
| return | ||
| self._on_image(None) | ||
|
|
||
| def _on_stream_clear(self, roles: list[str] | None) -> None: | ||
| if roles is not None and "artwork" not in roles: | ||
|
maximmaxim345 marked this conversation as resolved.
|
||
| return | ||
| self._on_image(None) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """Artwork rendering helpers for the Sendspin TUI.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from textual_image.renderable import Image as TIImage | ||
| from textual_image.renderable import SixelImage, TGPImage | ||
|
|
||
| if TYPE_CHECKING: | ||
| from PIL.Image import Image as PILImage | ||
| from rich.console import RenderableType | ||
|
|
||
| _cache: tuple[tuple[int, int, int], "RenderableType"] | None = None | ||
|
|
||
|
|
||
| def clear_cache() -> None: | ||
| """Drop the cached renderable.""" | ||
| global _cache # noqa: PLW0603 | ||
| _cache = None | ||
|
|
||
|
|
||
| def render_artwork( | ||
| image: "PILImage | None", | ||
| generation: int, | ||
| height_rows: int, | ||
| width_cells: int, | ||
| ) -> "RenderableType | None": | ||
| """Return a Rich renderable for the given image, cached by (generation, height_rows, width_cells). | ||
|
|
||
| Returns None when image is None so the layout can collapse the image column. | ||
| """ | ||
| global _cache # noqa: PLW0603 | ||
| if image is None: | ||
| return None | ||
| key = (generation, height_rows, width_cells) | ||
| if _cache is not None and _cache[0] == key: | ||
| return _cache[1] | ||
| renderable = TIImage(image, width=width_cells, height=height_rows) | ||
| _cache = (key, renderable) | ||
| return renderable | ||
|
|
||
|
|
||
| def detect_support() -> bool: | ||
| """True when a real terminal graphics protocol (Kitty or Sixel) is available. | ||
|
|
||
| textual-image runs its terminal probe at module import. This function just | ||
| inspects the resolved Image class. Halfcell and Unicode fallbacks return False. | ||
| """ | ||
| return TIImage is SixelImage or TIImage is TGPImage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.