goosemusic 1.3.2

A music player with YouTube search, local playback, and OS media controls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use iced::{
    alignment,
    widget::{scrollable, text, Button, Column, Container, Row},
    Element, Length,
};

use super::{
    shared_components::{
        empty_state, loading_state, scope_tab_row, thumbnail, toggle_bookmark_button,
    },
    styles::{fg_accent, fg_secondary},
    view_track_list, Message, MusicPlayer,
};
use crate::{
    app::{
        pane::PaneId,
        view_data::{AlbumRef, PlaylistRef},
        TrackListKind, ViewKind,
    },
    load_state::LoadState,
    providers::{ArtistSection, ArtistSectionKind, ProviderId, SectionContent},
    theme::{self, AppTheme},
};

const CARD_WIDTH: f32 = 140.0;
const CARD_IMAGE_SIZE: f32 = 120.0;

/// The artist page: header (picture, name, stats), then one section per row,
/// each with its own "Provided by" provider picker.
fn section_kind_label(kind: ArtistSectionKind, tr: &crate::i18n::Strings) -> &str {
    match kind {
        ArtistSectionKind::Popular => tr.most_popular_songs,
        ArtistSectionKind::Albums => tr.albums,
        ArtistSectionKind::Playlists => tr.playlists,
        ArtistSectionKind::Related => tr.fans_also_like,
    }
}

pub(super) fn view_artist<'a>(
    player: &'a MusicPlayer,
    pane: PaneId,
) -> Element<'a, Message, AppTheme> {
    let ViewKind::Artist(entry) = &player.view_data_in(pane).kind else {
        return empty_state(player.strings.not_an_artist_page);
    };
    let mut children: Vec<Element<'a, Message, AppTheme>> = Vec::new();
    children.push(header(
        player,
        pane,
        &entry.id,
        &entry.name,
        entry.page.header.as_ref(),
        entry.page.header_provider,
    ));

    for kind in [
        ArtistSectionKind::Popular,
        ArtistSectionKind::Albums,
        ArtistSectionKind::Playlists,
        ArtistSectionKind::Related,
    ] {
        let section = entry.page.section(kind);
        children.push(section_header(pane, kind, section.provider, player.strings));
        children.push(section_body(player, pane, section, kind));
    }

    scrollable(
        Column::with_children(children)
            .spacing(theme::SPACING_2XS)
            .padding(iced::Padding {
                bottom: theme::SPACING_LG,
                ..Default::default()
            }),
    )
    .width(Length::Fill)
    .height(Length::Fill)
    .into()
}

/// Thumbnail cache key for the artist header image, namespaced by the
/// provider supplying it so switching providers swaps the picture.
pub(crate) fn header_thumb_key(id: &str) -> String {
    format!("artist-header:{id}")
}

/// Provider picker for the header block (thumbnail / description source).
fn header_provider_picker(
    pane: PaneId,
    selected: Option<ProviderId>,
    tr: &'static crate::i18n::Strings,
) -> Element<'static, Message, AppTheme> {
    Row::with_children([
        text(tr.provided_by)
            .size(theme::TEXT_SIZE_XS)
            .style(fg_secondary())
            .into(),
        scope_tab_row(ProviderId::header_providers().iter().map(|&p| {
            (
                p.label().to_string(),
                selected == Some(p),
                Message::ArtistHeaderProviderChanged(pane, p),
            )
        })),
    ])
    .spacing(theme::SPACING_XS)
    .align_y(alignment::Vertical::Center)
    .into()
}

/// Header block: picture, artist name and the "label: value" stat pairs of
/// whichever provider's header arrived first.
fn header<'a>(
    player: &'a MusicPlayer,
    pane: PaneId,
    id: &str,
    name: &'a str,
    header: Option<&'a crate::providers::ArtistHeader>,
    header_provider: Option<ProviderId>,
) -> Element<'a, Message, AppTheme> {
    let thumb = player
        .thumbnail_index
        .get(header_provider.unwrap_or_default(), &header_thumb_key(id));
    let image = thumbnail(theme::PAGE_THUMBNAIL_SIZE, thumb);

    let stats_line = header
        .as_ref()
        .map(|h| {
            h.stats
                .iter()
                .map(|(label, value)| format!("{label}: {value}"))
                .collect::<Vec<_>>()
                .join("  \u{00b7}  ")
        })
        .unwrap_or_default();

    let mut info = vec![
        text(name).size(theme::TEXT_SIZE_XL).into(),
        text(stats_line)
            .size(theme::TEXT_SIZE_SM)
            .style(fg_secondary())
            .into(),
    ];
    if let Some(description) = header
        .as_ref()
        .map(|h| h.description.as_str())
        .filter(|d| !d.is_empty())
    {
        info.push(
            text(description)
                .size(theme::TEXT_SIZE_XS)
                .style(fg_secondary())
                .into(),
        );
    }

    let mut actions = vec![header_provider_picker(
        pane,
        header_provider,
        player.strings,
    )];
    if let Some(item) = player.current_library_item(pane) {
        let saved = player.library.contains(item.kind, &item.id);
        actions.push(
            Container::new(
                toggle_bookmark_button(saved).on_press(Message::ToggleLibrarySave(item)),
            )
            .align_y(alignment::Vertical::Bottom)
            .height(Length::Fill)
            .into(),
        );
    }

    Row::with_children([
        image,
        Column::with_children(info)
            .spacing(theme::SPACING_2XS)
            .width(Length::Fill)
            .height(Length::Fill)
            .into(),
        Column::with_children(actions)
            .align_x(alignment::Horizontal::Right)
            .into(),
    ])
    .spacing(theme::SPACING_LG)
    .padding([theme::SPACING_MD, theme::SPACING_XL])
    .into()
}

fn section_header(
    pane: PaneId,
    kind: ArtistSectionKind,
    selected: Option<ProviderId>,
    tr: &'static crate::i18n::Strings,
) -> Element<'static, Message, AppTheme> {
    let picker = scope_tab_row(kind.providers().iter().map(|&provider| {
        (
            provider.label(),
            selected == Some(provider),
            Message::ArtistSectionProviderChanged(pane, kind, provider),
        )
    }));
    Container::new(
        Row::with_children([
            text(section_kind_label(kind, tr))
                .style(fg_accent())
                .size(theme::TEXT_SIZE_LG)
                .width(Length::Fill)
                .into(),
            text(tr.provided_by)
                .size(theme::TEXT_SIZE_SM)
                .style(fg_secondary())
                .into(),
            picker,
        ])
        .align_y(alignment::Vertical::Center)
        .spacing(theme::SPACING_MD)
        .padding([theme::SPACING_SM, theme::SPACING_XL]),
    )
    .into()
}

/// Render one card section's contents into card widgets.
fn cards<'a>(
    player: &'a MusicPlayer,
    pane: PaneId,
    provider: ProviderId,
    content: &'a SectionContent,
) -> Vec<Element<'a, Message, AppTheme>> {
    match content {
        SectionContent::Albums(v) => v
            .iter()
            .map(|c| {
                let subtitle: String =
                    super::search::browse_meta(&c.badge, &c.date).unwrap_or_default();
                h_card(
                    player,
                    provider,
                    &c.id,
                    &c.title,
                    &subtitle,
                    Message::Browse(
                        pane,
                        ViewKind::Album(AlbumRef {
                            id: c.id.clone(),
                            name: c.title.clone(),
                            badge: c.badge.clone(),
                            date: c.date.clone(),
                            thumbnail: c.thumbnail.clone(),
                            provider,
                        }),
                        provider,
                    ),
                )
            })
            .collect(),
        SectionContent::Playlists(v) => v
            .iter()
            .map(|c| {
                h_card(
                    player,
                    provider,
                    &c.id,
                    &c.title,
                    "",
                    Message::Browse(
                        pane,
                        ViewKind::PlaylistView(PlaylistRef {
                            id: c.id.clone(),
                            name: c.title.clone(),
                            thumbnail: c.thumbnail.clone(),
                            provider,
                        }),
                        provider,
                    ),
                )
            })
            .collect(),
        SectionContent::Related(v) => v
            .iter()
            .map(|r| {
                h_card(
                    player,
                    provider,
                    &r.id,
                    &r.name,
                    &r.stat,
                    Message::OpenArtist {
                        pane,
                        id: r.id.clone(),
                        name: r.name.clone(),
                        source: provider,
                    },
                )
            })
            .collect(),
        SectionContent::Tracks(_) => Vec::new(),
    }
}

fn failed_state<'a>(
    pane: PaneId,
    provider: Option<ProviderId>,
    kind: ArtistSectionKind,
    e: &str,
    tr: &'a crate::i18n::Strings,
) -> Element<'a, Message, AppTheme> {
    Container::new(
        Column::with_children([
            text((tr.couldnt_load)(e)).into(),
            Button::new(tr.retry)
                .padding([theme::SPACING_2XS, theme::SPACING_SM])
                .on_press_maybe(
                    provider.map(|p| Message::ArtistSectionProviderChanged(pane, kind, p)),
                )
                .into(),
        ])
        .align_x(alignment::Horizontal::Center)
        .spacing(theme::SPACING_SM),
    )
    .center(Length::Fill)
    .into()
}

fn section_body<'a>(
    player: &'a MusicPlayer,
    pane: PaneId,
    section: &'a ArtistSection,
    kind: ArtistSectionKind,
) -> Element<'a, Message, AppTheme> {
    let view_data = player.view_data_in(pane);

    if kind == ArtistSectionKind::Popular {
        // Popular tracks live in the view's track list so all the usual
        // interactions (play, context menu, drag) work on them.
        return match &view_data.content {
            LoadState::Ready(tracks) => {
                if tracks.is_empty() {
                    empty_state(player.strings.nothing_here)
                } else {
                    view_track_list(tracks.as_slice(), player, pane, TrackListKind::Active, 0)
                }
            }
            LoadState::Failed(e) => {
                return failed_state(pane, section.provider, kind, e, player.strings);
            }
            LoadState::Loading => {
                return loading_state(player.strings.loading);
            }
        };
    }

    // Failed sections offer an in-place retry (re-requesting the provider).
    let content = match &section.state {
        LoadState::Ready(content) => content,
        LoadState::Failed(e) => {
            return failed_state(pane, section.provider, kind, e, player.strings);
        }
        LoadState::Loading => {
            return loading_state(player.strings.loading);
        }
    };
    let provider = section.provider.unwrap_or_default();
    h_scroll_cards(cards(player, pane, provider, content), player.strings)
}

/// A horizontal row of square art cards (pic on top, text below).
fn h_scroll_cards<'a, I>(cards: I, tr: &'a crate::i18n::Strings) -> Element<'a, Message, AppTheme>
where
    I: IntoIterator<Item = Element<'a, Message, AppTheme>>,
{
    let row: Vec<Element<'a, Message, AppTheme>> = cards.into_iter().collect();
    if row.is_empty() {
        return empty_state(tr.nothing_here);
    }
    scrollable(
        Row::with_children(row)
            .spacing(theme::SPACING_MD)
            .padding([theme::SPACING_SM, theme::SPACING_XL]),
    )
    .direction(scrollable::Direction::Horizontal(
        scrollable::Scrollbar::new(),
    ))
    .into()
}

/// One square art card: picture with title/date underneath. Clicking fires
/// `on_press` (drill-down).
fn h_card<'a>(
    player: &'a MusicPlayer,
    provider: ProviderId,
    thumb_id: &str,
    title: &'a str,
    subtitle: &str,
    on_press: Message,
) -> Element<'a, Message, AppTheme> {
    let thumb_path = player.thumbnail_index.get(provider, thumb_id);
    let image = Container::new(thumbnail(CARD_IMAGE_SIZE, thumb_path)).height(CARD_IMAGE_SIZE);
    let mut body = vec![image.into()];
    body.push(
        text(title)
            .size(theme::TEXT_SIZE_SM)
            .width(CARD_WIDTH)
            .wrapping(text::Wrapping::Word)
            .into(),
    );
    if !subtitle.trim().is_empty() {
        body.push(
            text(subtitle.trim().to_string())
                .size(theme::TEXT_SIZE_XS)
                .style(fg_secondary())
                .into(),
        );
    }
    Button::new(Column::with_children(body).spacing(theme::SPACING_2XS))
        .padding(theme::SPACING_2XS)
        .width(CARD_WIDTH)
        .style(super::styles::button_style_album())
        .on_press(on_press)
        .into()
}