summaryrefslogtreecommitdiff
path: root/tidal_dl_ng/ui/info_tab_widget.py
blob: 05e0d6f7a09f0d9f23288dd5d3e91bc905b48193 (plain) (blame)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
"""Info Tab Widget - Dynamic tabbed information panel for track hover preview.

This module provides a sophisticated tabbed widget that displays rich metadata
for TIDAL media items on hover, implementing a debounced event system to prevent
UI flickering and unnecessary API calls.

Architecture:
    - InfoTabWidget: Main container with two tabs (Details + Cover Art)
    - DebounceTimer: Configurable timer to prevent event flooding
    - TrackInfoFormatter: Utility class to format track metadata consistently

Performance Considerations:
    - Pre-loaded metadata to avoid API chattiness
    - Debounced hover events (300-400ms delay)
    - Thread-safe signal emissions for cross-thread updates
"""

import datetime
from collections.abc import Callable, Iterable
from contextlib import suppress
from typing import TYPE_CHECKING

from PySide6 import QtCore, QtGui, QtWidgets
from tidalapi import Album, Mix, Playlist, Track, Video
from tidalapi.artist import Artist
from tidalapi.media import AudioMode

from tidal_dl_ng.helper.metadata_utils import extract_names_from_mixed, find_attr, safe_str
from tidal_dl_ng.helper.path import resource_path
from tidal_dl_ng.helper.tidal import name_builder_artist, name_builder_title, quality_audio_highest

if TYPE_CHECKING:
    pass


class TrackInfoFormatter:
    """Formats track metadata into human-readable strings.

    This utility class handles the conversion of raw TIDAL API objects into
    formatted display strings for the UI.
    """

    @staticmethod
    def _get_artist_name(artist: object) -> str:
        """Safely get artist name from artist object."""
        name = getattr(artist, "name", None)
        if name is None:
            with suppress(Exception):
                return str(artist)
            return "Unknown"
        return str(name)

    @staticmethod
    def _format_artist_roles(artist: object) -> list[str]:
        """Extract and format artist roles."""
        if not hasattr(artist, "roles") or not artist.roles:
            return []
        roles = []
        for role in artist.roles:
            rname = getattr(role, "name", None)
            if rname is None:
                with suppress(Exception):
                    rname = str(role)
            if rname:
                roles.append(str(rname))
        return roles

    @staticmethod
    def format_artists(media: Track | Video | Album) -> str:
        """Format artist names with roles if available.

        Args:
            media (Track | Video | Album): The media object with artist information.

        Returns:
            str: Formatted artist string (e.g., "Artist1, Artist2 (Feature)").
        """
        with suppress(Exception):
            artist_parts: list[str] = []
            artists = getattr(media, "artists", [])

            for artist in artists:
                name = TrackInfoFormatter._get_artist_name(artist)
                roles = TrackInfoFormatter._format_artist_roles(artist)

                if roles and roles[0] != "main":
                    name = f"{name} ({', '.join(roles)})"
                artist_parts.append(name)

            if artist_parts:
                return ", ".join(artist_parts)

        return name_builder_artist(media) if hasattr(media, "artists") else "Unknown Artist"

    @staticmethod
    def format_codec(track: Track) -> str:
        """Format codec information including audio mode.

        Args:
            track (Track): The track object.

        Returns:
            str: Formatted codec string (e.g., "FLAC / Dolby Atmos").
        """
        codec_parts: list[str] = []

        # If this is a Video object, prefer video_quality (e.g., '1080p')
        if hasattr(track, "video_quality"):
            with suppress(Exception):
                return str(track.video_quality)

        # Get base quality for audio tracks
        quality = quality_audio_highest(track)
        if quality:
            codec_parts.append(str(quality).upper())

        # Check for Dolby Atmos
        if hasattr(track, "audio_modes") and track.audio_modes and AudioMode.dolby_atmos.value in track.audio_modes:
            codec_parts.append("Dolby Atmos")

        # Check for MQA
        if (
            hasattr(track, "media_metadata_tags")
            and track.media_metadata_tags
            and "MQA" in str(track.media_metadata_tags)
        ):
            codec_parts.append("MQA")

        return " / ".join(codec_parts) if codec_parts else "N/A"

    @staticmethod
    def format_duration(duration_sec: int | None) -> str:
        """Format duration in seconds to MM:SS format.

        Args:
            duration_sec (int | None): Duration in seconds.

        Returns:
            str: Formatted duration string.
        """
        # Treat 0 as a valid duration (00:00); only None or missing -> N/A
        if duration_sec is None:
            return "N/A"

        minutes = duration_sec // 60
        seconds = duration_sec % 60
        return f"{minutes:02d}:{seconds:02d}"

    @staticmethod
    def format_date(date_obj) -> str:
        """Format date object to readable string.

        Args:
            date_obj: Date object (datetime or similar).

        Returns:
            str: Formatted date string (YYYY-MM-DD) or N/A.
        """
        if date_obj is None:
            return "N/A"

        try:
            # Only call strftime on actual date/datetime objects to avoid
            # mocks or unrelated objects having a 'strftime' attribute.
            if isinstance(date_obj, datetime.date | datetime.datetime):
                return date_obj.strftime("%Y-%m-%d")

            # If it's already a string, return as-is
            if isinstance(date_obj, str):
                return date_obj

            # Fallback to string conversion
            return str(date_obj)
        except Exception:
            return "N/A"

    @staticmethod
    def format_bitrate(track: Track) -> str:
        """Format bitrate information.

        Args:
            track (Track): The track object.

        Returns:
            str: Formatted bitrate string.
        """
        # Check for bit depth and sample rate
        bit_depth = getattr(track, "bit_depth", None)
        sample_rate = getattr(track, "sample_rate", None)

        if bit_depth and sample_rate:
            return f"{bit_depth}-bit / {sample_rate / 1000:.1f} kHz"
        elif sample_rate:
            return f"{sample_rate / 1000:.1f} kHz"
        return "N/A"


class InfoTabWidget(QtCore.QObject):
    """Controller for the tabbed widget displaying rich media information.

    This class manages the logic for the info tabs but is not a widget itself.
    It binds to existing widgets created from a .ui file.
    """

    # Signals for cross-thread communication
    s_update_details: QtCore.Signal = QtCore.Signal(object)
    s_update_cover: QtCore.Signal = QtCore.Signal(str)
    s_spinner_start: QtCore.Signal = QtCore.Signal()
    s_spinner_stop: QtCore.Signal = QtCore.Signal()
    s_search_in_app: QtCore.Signal = QtCore.Signal(str, str)
    s_search_in_browser: QtCore.Signal = QtCore.Signal(str, str)

    def _setup_search_roots(self, parent_widget: QtWidgets.QTabWidget | None) -> list:
        """Setup list of widget roots to search for UI elements."""
        search_roots = [parent_widget]
        if hasattr(parent_widget, "parent") and parent_widget.parent() is not None:
            search_roots.append(parent_widget.parent())
        with suppress(Exception):
            root_window = parent_widget.window()
            if root_window is not None:
                search_roots.append(root_window)
        return search_roots

    def _find_widget_in_roots(self, objname: str, search_roots: list) -> QtWidgets.QLabel | None:
        """Find a QLabel widget by objectName in search roots."""
        for root in search_roots:
            with suppress(Exception):
                found = root.findChild(QtWidgets.QLabel, objname)
                if found is not None:
                    return found
        return None

    def _create_fallback_widget(self, desc: str, parent_widget: QtWidgets.QWidget) -> tuple[str, QtWidgets.QLabel]:
        """Create a fallback invisible widget for a missing UI element."""
        name = desc.split("(")[-1].rstrip(")") if "(" in desc and ")" in desc else f"fallback_{desc.replace(' ', '_')}"

        fallback = QtWidgets.QLabel("—", parent_widget)
        fallback.setObjectName(name)
        fallback.setVisible(False)
        return name, fallback

    def __init__(
        self,
        parent: QtWidgets.QWidget | None = None,
        existing_tab_widget: QtWidgets.QTabWidget | None = None,
    ) -> None:
        """Initialize the InfoTabWidget controller.

        Args:
            parent (QWidget | None): Parent widget. Defaults to None.
            existing_tab_widget (QTabWidget | None): The tab widget from the UI to control.
        """
        super().__init__(parent)

        # State management
        self.current_media_hovered: Track | Video | Album | Mix | Playlist | Artist | None = None
        self.current_media_selected: Track | Video | Album | Mix | Playlist | Artist | None = None
        self.cover_url_current: str = ""
        self._track_extras_provider: Callable[[str], dict] | None = None

        # Bind to widgets created in the .ui by exact objectName.
        self.tab_widget = existing_tab_widget
        parent_widget = self.tab_widget

        # REQUIRED object names provided by main.ui
        required = {
            "lbl_title": "Title label (lbl_title)",
            "lbl_version": "Version label (lbl_version)",
            "lbl_artists": "Artists label (lbl_artists)",
            "lbl_album": "Album label (lbl_album)",
            "lbl_codec": "Codec label (lbl_codec)",
            "lbl_bitrate": "Bitrate label (lbl_bitrate)",
            "lbl_duration": "Duration label (lbl_duration)",
            "lbl_release_date": "Release date label (lbl_release_date)",
            "lbl_popularity": "Popularity label (lbl_popularity)",
            "lbl_bpm": "BPM label (lbl_bpm)",
            "lbl_isrc": "ISRC label (lbl_isrc)",
            "lbl_track_number": "Track number label (lbl_track_number)",
            "l_pm_cover": "Cover QLabel (l_pm_cover)",
        }

        # Lookup and assign
        search_roots = self._setup_search_roots(parent_widget)
        missing = []

        for objname, desc in required.items():
            found_widget = self._find_widget_in_roots(objname, search_roots)
            if not found_widget:
                missing.append(desc)
            else:
                attr_name = "cover_label" if objname == "l_pm_cover" else objname
                setattr(self, attr_name, found_widget)

        # Create fallback widgets for missing elements
        if missing:
            for desc in missing:
                name, fallback = self._create_fallback_widget(desc, parent_widget)
                if name == "l_pm_cover":
                    self.cover_label = fallback
                else:
                    setattr(self, name, fallback)

        # Ensure cover_label exists
        if not hasattr(self, "cover_label"):
            self.cover_label = QtWidgets.QLabel("", parent_widget)
            self.cover_label.setVisible(False)
            self.cover_label.setObjectName("cover_label_fallback")

        # connect signals
        self._connect_signals()

        # Make labels clickable using linkActivated signal
        clickable_labels = [self.lbl_title, self.lbl_artists, self.lbl_album]
        for label in clickable_labels:
            label.setTextFormat(QtCore.Qt.RichText)
            label.setOpenExternalLinks(False)  # We handle links manually
            label.linkActivated.connect(self._on_link_activated)
            label.installEventFilter(self)  # For right-click handling
            label.setToolTip("Left-click to search in app, Ctrl+click or right-click to search in browser.")

    def eventFilter(self, watched: QtCore.QObject, event: QtCore.QEvent) -> bool:
        """Filter events for clickable labels to handle right-clicks."""
        if event.type() == QtCore.QEvent.Type.MouseButtonPress and isinstance(watched, QtWidgets.QLabel):
            event: QtGui.QMouseEvent
            if event.button() == QtCore.Qt.MouseButton.RightButton:
                # On right-click, find the link under the cursor
                anchor = watched.anchorAt(event.pos())
                if anchor:
                    self._on_link_activated(anchor, right_click=True)
                    return True  # Event handled

        return super().eventFilter(watched, event)

    def _create_link(self, search_type: str, text: str) -> str:
        """Create an HTML link for search, using a specific color."""
        if not text or text == "—":
            return "—"
        # The link format is "search:type:text"
        link_target = f"search:{search_type}:{text}"
        # Use a standard link color; Qt will handle styling.
        return f"<a href='{link_target}' style='color:#367af6;'>{text}</a>"

    @QtCore.Slot(str)
    def _on_link_activated(self, link: str, right_click: bool = False) -> None:
        """Handle clicks on rich text links in labels."""
        if not link:
            return

        parts = link.split(":", 2)
        if len(parts) < 3 or parts[0] != "search":
            return

        search_type = parts[1]
        search_term = parts[2]
        modifiers = QtWidgets.QApplication.keyboardModifiers()

        is_browser_action = right_click or (modifiers & QtCore.Qt.KeyboardModifier.ControlModifier)

        if is_browser_action:
            # Ctrl+Click or Right-Click: Open in browser
            self.s_search_in_browser.emit(search_term, search_type)
        else:
            # Left-Click: Search in app
            self.s_search_in_app.emit(search_term, search_type)

    def _connect_signals(self) -> None:
        """Connect internal signals to slots."""
        self.s_update_details.connect(self._on_update_details)
        self.s_update_cover.connect(self._on_update_cover)
        self.s_spinner_start.connect(self._spin_start)
        self.s_spinner_stop.connect(self._spin_stop)

    def _on_update_cover(self, cover_url: str) -> None:
        """Update cover image from URL.

        Args:
            cover_url (str): URL or path to the cover image.
        """
        if cover_url and cover_url != self.cover_url_current:
            self.cover_url_current = cover_url
            with suppress(Exception):
                self.set_cover_pixmap(cover_url)

    def set_track_extras_provider(
        self, provider: Callable[[str, Callable[[str, dict | None], None]], dict | None] | None
    ) -> None:
        """Register a callable that fetches track extras asynchronously.

        The provider must accept ``track_id`` and a callback; it may return
        immediate extras synchronously. Passing ``None`` removes the provider.
        """
        self._track_extras_provider = provider

    def set_cover_pixmap(self, content: QtGui.QPixmap | str) -> None:
        """Set the cover image pixmap or path."""
        with suppress(Exception):
            if isinstance(content, QtGui.QPixmap):
                self.cover_label.setPixmap(content)
            elif isinstance(content, str) and content:
                pixmap = QtGui.QPixmap()
                if pixmap.load(content):
                    self.cover_label.setPixmap(pixmap)

    def _spin_start(self) -> None:
        """Track nested spinner requests so UI stays responsive."""
        count = getattr(self, "_spinner_depth", 0) + 1
        self._spinner_depth = count
        parent = self.parent()
        if count == 1 and parent and isinstance(parent, QtWidgets.QWidget):
            parent.setCursor(QtCore.Qt.CursorShape.BusyCursor)

    def _spin_stop(self) -> None:
        """Release spinner state when last request finishes."""
        count = max(getattr(self, "_spinner_depth", 1) - 1, 0)
        self._spinner_depth = count
        parent = self.parent()
        if count == 0 and parent and isinstance(parent, QtWidgets.QWidget):
            parent.setCursor(QtCore.Qt.CursorShape.ArrowCursor)

    def _request_track_extras_if_needed(self, media: object) -> None:
        track_id = getattr(media, "id", None)
        if not isinstance(track_id, int | str):
            return
        if not isinstance(media, Track):
            return
        if self._track_extras_provider is None:
            return
        self.s_spinner_start.emit()
        try:
            immediate = self._track_extras_provider(str(track_id), self._handle_track_extras_ready)
            if immediate:
                self._apply_track_extras(immediate)
                self.s_spinner_stop.emit()
        except Exception:
            self.s_spinner_stop.emit()

    def _handle_track_extras_ready(self, track_id: str, extras: dict | None) -> None:
        """Callback invoked by the provider when async extras are ready."""
        try:
            if extras:
                current = self.current_media_hovered or self.current_media_selected
                current_id = getattr(current, "id", None)
                if current_id is not None and str(current_id) == str(track_id):
                    self._apply_track_extras(extras)
        finally:
            self.s_spinner_stop.emit()

    def _apply_track_extras(self, extras: dict | None) -> None:
        """Apply the track extras to the UI.

        This method updates the details tab with additional information such as
        BPM if available.

        Note: Only BPM is available from TIDAL API extras. Genres, label, producers
        and composers are not reliably available.

        Args:
            extras (dict | None): A dictionary containing extra information
                about the track, or None if no extras are available.
        """
        if not isinstance(extras, dict):
            return
        with suppress(RuntimeError):
            self._update_extras_ui(extras)

    @QtCore.Slot(dict)
    def _update_extras_ui(self, extras: dict):
        """Safely update UI with extras from the main thread."""
        if not isinstance(extras, dict):
            return
        try:
            bpm_value = extras.get("bpm")
            display_value = "—" if bpm_value is None else str(bpm_value)
            self.lbl_bpm.setText(display_value)
        except RuntimeError:
            pass  # Widgets deleted, silently ignore

    def update_on_hover(self, media: Track | Video | Album | Mix | Playlist | Artist | None) -> None:
        """Update display based on hovered media item.

        This method is called when the user hovers over a track in the results list.
        It displays information from pre-loaded metadata without triggering API calls.

        Args:
            media (Track | Video | Album | Mix | Playlist | Artist | None): The hovered media item.
        """
        if not media:
            return

        self.current_media_hovered = media

        # Update details tab (from pre-loaded data)
        self.s_update_details.emit(media)

        # Cover is handled by MainWindow
        self._request_track_extras_if_needed(media)

    def update_on_selection(self, media: Track | Video | Album | Mix | Playlist | Artist | None) -> None:
        """Update display based on selected media item.

        This method is called when the user clicks on a track in the results list.
        It persists the selection so the UI can revert to it after hover leaves.

        Args:
            media (Track | Video | Album | Mix | Playlist | Artist | None): The selected media item.
        """
        if not media:
            return

        self.current_media_selected = media

        # Update both tabs
        self.s_update_details.emit(media)
        # Cover is handled by MainWindow
        self._request_track_extras_if_needed(media)

    def revert_to_selection(self) -> None:
        """Revert display to the currently selected media.

        Called when hover leaves the results list to prevent leaving the UI
        in an inconsistent state.
        """
        if self.current_media_selected:
            self.update_on_selection(self.current_media_selected)
        else:
            self._clear_display()

    def _clear_display(self) -> None:
        """Clear all displayed information and reset to default state."""
        self.current_media_hovered = None

        try:
            self.lbl_title.setText("—")
            self.lbl_version.setText("—")
            self.lbl_artists.setText("—")
            self.lbl_album.setText("—")
            self.lbl_codec.setText("—")
            self.lbl_bitrate.setText("—")
            self.lbl_duration.setText("—")
            self.lbl_release_date.setText("—")
            self.lbl_popularity.setText("—")
            self.lbl_bpm.setText("—")
            self.lbl_isrc.setText("—")
            self.lbl_track_number.setText("—")

            # Reset cover to default
            path_image: str = resource_path("tidal_dl_ng/ui/default_album_image.png")
            self.cover_label.setPixmap(QtGui.QPixmap(path_image))
            self.cover_url_current = ""
        except RuntimeError:
            pass  # Widgets deleted, silently ignore

    def _on_update_details(self, media: Track | Video | Album | Mix | Playlist | Artist) -> None:
        """Update details tab with media metadata.

        Args:
            media (Track | Video | Album | Mix | Playlist | Artist): The media item to display.
        """
        # Handle different media types
        if isinstance(media, Track):
            self._populate_track_details(media)
        elif isinstance(media, Video):
            self._populate_video_details(media)
        elif isinstance(media, Album):
            self._populate_album_details(media)
        elif isinstance(media, Playlist | Mix):
            self._populate_playlist_details(media)
        elif isinstance(media, Artist):
            self._populate_artist_details(media)

    def _populate_track_details(self, track: Track) -> None:
        """Populate details for a Track.

        Args:
            track (Track): The track object.
        """
        # Title and version
        title = name_builder_title(track)
        self.lbl_title.setText(self._create_link("track", title))
        version = getattr(track, "version", None)
        self.lbl_version.setText(safe_str(version))

        # Artists
        artist_parts = []
        if hasattr(track, "artists") and track.artists:
            for artist in track.artists:
                name = TrackInfoFormatter._get_artist_name(artist)
                roles = TrackInfoFormatter._format_artist_roles(artist)
                link = self._create_link("artist", name)
                if roles and roles[0] != "main":
                    artist_parts.append(f"{link} ({', '.join(roles)})")
                else:
                    artist_parts.append(link)
        self.lbl_artists.setText(", ".join(artist_parts) if artist_parts else "—")

        # Album name (prefer album object's name)
        album_name = None
        if hasattr(track, "album") and track.album:
            album_name = getattr(track.album, "name", None) or None
        self.lbl_album.setText(self._create_link("album", album_name))

        # Codec & Bitrate
        codec = TrackInfoFormatter.format_codec(track)
        self.lbl_codec.setText(safe_str(codec))
        bitrate = TrackInfoFormatter.format_bitrate(track)
        self.lbl_bitrate.setText(safe_str(bitrate))

        # Duration
        duration = TrackInfoFormatter.format_duration(getattr(track, "duration", None))
        self.lbl_duration.setText(safe_str(duration))

        # Release date (from album if exists)
        release_date = None
        if hasattr(track, "album") and track.album and hasattr(track.album, "release_date"):
            release_date = TrackInfoFormatter.format_date(track.album.release_date)
        self.lbl_release_date.setText(safe_str(release_date))

        # Popularity
        popularity = getattr(track, "popularity", None)
        self.lbl_popularity.setText(safe_str(popularity))

        # BPM: Show loading indicator, will be updated when extras are loaded
        self.lbl_bpm.setText("⏳ Loading...")

        # ISRC
        isrc = getattr(track, "isrc", None)
        self.lbl_isrc.setText(safe_str(isrc))

        # Track Number
        track_number = find_attr(track, "track_number", "tracknumber", "number", "position", "track") or None
        if track_number is None and hasattr(track, "album") and track.album:
            track_number = find_attr(track.album, "track_number", "tracknumber", "number") or None
        self.lbl_track_number.setText(safe_str(track_number))

    def _populate_video_details(self, video: Video) -> None:
        """Populate details for a Video.

        Args:
            video (Video): The video object.
        """
        # Title and version
        title = name_builder_title(video)
        self.lbl_title.setText(self._create_link("video", title))
        version = getattr(video, "version", None)
        self.lbl_version.setText(safe_str(version))

        # Artists
        artists_str = extract_names_from_mixed(
            getattr(video, "credits", None), match_types=("performer", "artist", "actor")
        )
        if not artists_str:
            artist_parts = []
            if hasattr(video, "artists") and video.artists:
                for artist in video.artists:
                    name = TrackInfoFormatter._get_artist_name(artist)
                    artist_parts.append(self._create_link("artist", name))
            self.lbl_artists.setText(", ".join(artist_parts) if artist_parts else "—")
        else:
            self.lbl_artists.setText(self._create_link("artist", artists_str))

        # Album name
        album_name = None
        if hasattr(video, "album") and video.album:
            album_name = getattr(video.album, "name", None) or None
        self.lbl_album.setText(self._create_link("album", album_name))

        # Codec & Bitrate
        codec = TrackInfoFormatter.format_codec(video)
        self.lbl_codec.setText(safe_str(codec))
        bitrate = TrackInfoFormatter.format_bitrate(video)
        self.lbl_bitrate.setText(safe_str(bitrate))

        # Duration
        duration = TrackInfoFormatter.format_duration(getattr(video, "duration", None))
        self.lbl_duration.setText(safe_str(duration))

        # Release date
        release_date = None
        if hasattr(video, "album") and video.album and hasattr(video.album, "release_date"):
            release_date = TrackInfoFormatter.format_date(video.album.release_date)
        self.lbl_release_date.setText(safe_str(release_date))

        # Popularity
        popularity = getattr(video, "popularity", None)
        self.lbl_popularity.setText(safe_str(popularity))

        # ISRC
        isrc = getattr(video, "isrc", None)
        if not isrc and hasattr(video, "track"):
            isrc = getattr(video.track, "isrc", None)
        self.lbl_isrc.setText(safe_str(isrc))

    def _populate_album_details(self, album: Album) -> None:
        """Populate details for an Album.

        Args:
            album (Album): The album object.
        """
        # Title and version
        title = name_builder_title(album)
        self.lbl_title.setText(self._create_link("album", title))
        version = getattr(album, "version", None)
        self.lbl_version.setText(safe_str(version))

        # Artists
        artist_parts = []
        if hasattr(album, "artists") and album.artists:
            for artist in album.artists:
                name = TrackInfoFormatter._get_artist_name(artist)
                artist_parts.append(self._create_link("artist", name))
        self.lbl_artists.setText(", ".join(artist_parts) if artist_parts else "—")

        # Album name (same as title for an album)
        self.lbl_album.setText(self._create_link("album", title))

        # Codec & Bitrate
        codec = TrackInfoFormatter.format_codec(album)
        self.lbl_codec.setText(safe_str(codec))
        bitrate = TrackInfoFormatter.format_bitrate(album)
        self.lbl_bitrate.setText(safe_str(bitrate))

        # Duration
        duration = TrackInfoFormatter.format_duration(getattr(album, "duration", None))
        self.lbl_duration.setText(safe_str(duration))

        # Release date
        release_date = TrackInfoFormatter.format_date(getattr(album, "release_date", None))
        self.lbl_release_date.setText(safe_str(release_date))

        # Popularity
        popularity = getattr(album, "popularity", None)
        self.lbl_popularity.setText(safe_str(popularity))

        self.lbl_isrc.setText("—")

    def _extract_playlist_artists(self, playlist: Playlist | Mix) -> str:
        """Extract and format artists from playlist tracks."""
        if not hasattr(playlist, "tracks") or not isinstance(playlist.tracks, list | Iterable):
            return "—"

        all_artists = set()
        for track in playlist.tracks:
            if isinstance(track, Track | Video | Album) and hasattr(track, "artists"):
                for artist in track.artists:
                    name = TrackInfoFormatter._get_artist_name(artist)
                    if name and name != "Unknown Artist":
                        all_artists.add(name)

        if all_artists:
            artist_links = [self._create_link("artist", name) for name in list(all_artists)[:5]]
            return ", ".join(artist_links)
        return "—"

    def _extract_playlist_release_date(self, playlist: Playlist | Mix) -> str:
        """Extract release date from playlist or its first track."""
        release_date = TrackInfoFormatter.format_date(getattr(playlist, "release_date", None))
        if release_date != "N/A":
            return release_date

        if hasattr(playlist, "tracks") and isinstance(playlist.tracks, list | Iterable):
            for track in playlist.tracks:
                if isinstance(track, Track | Video | Album) and hasattr(track, "album"):
                    release_date = TrackInfoFormatter.format_date(getattr(track.album, "release_date", None))
                    if release_date and release_date != "N/A":
                        return release_date
        return "N/A"

    def _populate_playlist_details(self, playlist: Playlist | Mix) -> None:
        """Populate details for a Playlist or Mix.

        Args:
            playlist (Playlist | Mix): The playlist or mix object.
        """
        # Title and version
        title = name_builder_title(playlist)
        self.lbl_title.setText(self._create_link("playlist", title))
        version = getattr(playlist, "version", None)
        self.lbl_version.setText(safe_str(version))

        # Artists
        artists_html = self._extract_playlist_artists(playlist)
        self.lbl_artists.setText(artists_html)

        # Album name (playlist title)
        album_name = getattr(playlist, "name", None)
        self.lbl_album.setText(self._create_link("playlist", album_name))

        # Codec & Bitrate
        self.lbl_codec.setText("—")
        self.lbl_bitrate.setText("—")

        # Duration
        duration = TrackInfoFormatter.format_duration(getattr(playlist, "duration", None))
        self.lbl_duration.setText(safe_str(duration))

        # Release date
        release_date = self._extract_playlist_release_date(playlist)
        self.lbl_release_date.setText(safe_str(release_date))

        # Popularity
        popularity = getattr(playlist, "popularity", None)
        self.lbl_popularity.setText(safe_str(popularity))

        self.lbl_isrc.setText("—")

    def _extract_artist_album_name(self, artist: Artist) -> str | None:
        """Extract first album name from artist's albums."""
        if not hasattr(artist, "albums") or not isinstance(artist.albums, list | Iterable):
            return None

        for album in artist.albums:
            if isinstance(album, Album) and hasattr(album, "name"):
                return getattr(album, "name", None)
        return None

    def _calculate_artist_total_duration(self, artist: Artist) -> int:
        """Calculate total duration of all artist's albums."""
        if not hasattr(artist, "albums") or not isinstance(artist.albums, list | Iterable):
            return 0

        total_duration = 0
        for album in artist.albums:
            if isinstance(album, Album) and hasattr(album, "duration"):
                album_duration = album.duration
                if isinstance(album_duration, int | float):
                    total_duration += album_duration
        return total_duration

    def _extract_artist_release_date(self, artist: Artist) -> str | None:
        """Extract release date from artist's first album."""
        if not hasattr(artist, "albums") or not isinstance(artist.albums, list | Iterable):
            return None

        for album in artist.albums:
            if isinstance(album, Album) and hasattr(album, "release_date"):
                release_date = TrackInfoFormatter.format_date(getattr(album, "release_date", None))
                if release_date and release_date != "N/A":
                    return release_date
        return None

    def _populate_artist_details(self, artist: Artist) -> None:
        """Populate details for an Artist.

        Args:
            artist (Artist): The artist object.
        """
        # Name
        name = getattr(artist, "name", None)
        self.lbl_title.setText(self._create_link("artist", name))

        # Version
        version = None
        if hasattr(artist, "type"):
            version = getattr(artist.type, "name", None)
        elif hasattr(artist, "role"):
            version = getattr(artist.role, "name", None)
        self.lbl_version.setText(safe_str(version))

        # Artists
        self.lbl_artists.setText("—")

        # Album name
        album_name = self._extract_artist_album_name(artist)
        self.lbl_album.setText(self._create_link("album", album_name))

        # Codec & Bitrate
        self.lbl_codec.setText("—")
        self.lbl_bitrate.setText("—")

        # Duration
        total_duration = self._calculate_artist_total_duration(artist)
        duration = TrackInfoFormatter.format_duration(total_duration)
        self.lbl_duration.setText(safe_str(duration))

        # Release date
        release_date = self._extract_artist_release_date(artist)
        self.lbl_release_date.setText(safe_str(release_date))

        # Popularity
        popularity = getattr(artist, "popularity", None)
        self.lbl_popularity.setText(safe_str(popularity))

        self.lbl_isrc.setText("—")