diff --git a/textual1/app.py b/textual1/app.py index eb39930..7dfb5b0 100644 --- a/textual1/app.py +++ b/textual1/app.py @@ -2,6 +2,7 @@ import configparser import logging import asyncio import random +import math import os import re import io @@ -152,22 +153,6 @@ class ArtistAlbums(Screen): app.pop_screen() app.uninstall_screen("artistAlbums") -class ArtistInfoPanel(Widget): - - artist: SubSonic.Artist - - def compose(self) -> ComposeResult: - with Horizontal(): - yield ArtistArt(classes="artistInfoArt") - yield Placeholder(classes="artistInfoTextbox") - yield Rule() - - def set_artist(self, artist: SubSonic.Artist) -> None: - self.artist = artist - - artist_art = self.query_one(ArtistArt) - artist_art.get_artist_image(self.artist.id) - class AlbumPanel(Widget, can_focus=True, can_focus_children=True): album: SubSonic.Album @@ -204,7 +189,8 @@ class ArtistAlbums2(Screen): yield Label(Text("F3 ", style="bold orange").append_text(Text("Albums", style="white")), classes="headerb") yield Label(Text("F4 ", style="bold orange").append_text(Text("Favourites", style="white")), classes="headerb") yield Label(Text("F5 ", style="bold orange").append_text(Text("Playlists", style="white")), classes="headerb") - yield ArtistInfoPanel(classes="artistInfo") + yield Label(classes='artistNameHeader') + yield Rule() with Horizontal(): yield AlbumPanel(classes="albumPanel") yield AlbumPanel(classes="albumPanel") @@ -220,8 +206,8 @@ class ArtistAlbums2(Screen): def on_mount(self) -> None: self.albums = conn.getArtistAlbums(self.artist.id) - artist_info_panel = self.query_one(ArtistInfoPanel) - artist_info_panel.set_artist(self.artist) + artist_header = self.query_one(Label) + artist_header.update(self.artist.name) albums_displayed = 0 album_panels = self.query(AlbumPanel) @@ -292,7 +278,6 @@ class ArtistArt(Static): def render(self) -> RenderResult: if self.art: width, height = self.size - logging.debug(f'{width=}, {height=}') height *= 2 @@ -301,7 +286,10 @@ class ArtistArt(Static): elif height > width: height = width - return Pixels.from_image(self.art, resize=(width, height)) + try: + return Pixels.from_image(self.art, resize=(width, height)) + except Exception as e: + logging.error(f'Couldn\'t load image {e}') return '' @@ -357,7 +345,14 @@ class ArtistPanel(Widget, can_focus=True, can_focus_children=True): class Artists2(Screen): + BINDINGS = [ + Binding('d', "page_forward()", "Next Page"), + Binding('a', "page_backward()", "Prev. Page") + ] + artists: list[SubSonic.Artist] + artists_displayed: int = 0 + page: int = 1 def compose(self) -> ComposeResult: yield Header(icon='') @@ -385,17 +380,52 @@ class Artists2(Screen): yield ArtistPanel(classes="artistPanel") yield ArtistPanel(classes="artistPanel") yield ArtistPanel(classes="artistPanel") + yield Label(id="page-counter") yield Footer() def on_mount(self) -> None: app.sub_title = 'Artists2' self.artists = conn.getArtists() - artists_displayed = 0 + self.artists_displayed = 0 + + self.update_pager() + artist_panels = self.query(ArtistPanel) for artist_panel in artist_panels: - artist_panel.set_artist(self.artists[artists_displayed]) - artists_displayed += 1 + if self.artists_displayed >= len(self.artists) - 1: + return + + artist_panel.set_artist(self.artists[self.artists_displayed]) + self.artists_displayed += 1 + + def update_pager(self) -> None: + pager = self.query_one("#page-counter", Label) + pager.update(f"{self.page} ... {math.ceil(len(self.artists)/15)}") + + def action_page_forward(self) -> None: + artist_panels = self.query(ArtistPanel) + + self.page += 1 + self.update_pager() + + for artist_panel in artist_panels: + if self.artists_displayed >= len(self.artists) - 1: + return + + artist_panel.set_artist(self.artists[self.artists_displayed]) + self.artists_displayed += 1 + + def action_page_backward(self) -> None: + artist_panels = self.query(ArtistPanel) + + self.page -= 1 + self.update_pager() + + self.artists_displayed -= 30 + for artist_panel in artist_panels: + artist_panel.set_artist(self.artists[self.artists_displayed]) + self.artists_displayed += 1 class Albums(Screen): diff --git a/textual1/main.tcss b/textual1/main.tcss index 682f96b..dc79ffd 100644 --- a/textual1/main.tcss +++ b/textual1/main.tcss @@ -82,3 +82,8 @@ text-align: center; width: 100%; } + +.artistNameHeader { + width: 100%; + text-align: center; +}