bastd.ui.soundtrack.macmusicapp

UI functionality related to using the macOS Music app for soundtracks.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""UI functionality related to using the macOS Music app for soundtracks."""
  4
  5from __future__ import annotations
  6
  7import copy
  8from typing import TYPE_CHECKING
  9
 10import ba
 11
 12if TYPE_CHECKING:
 13    from typing import Any, Callable
 14
 15
 16class MacMusicAppPlaylistSelectWindow(ba.Window):
 17    """Window for selecting an iTunes playlist."""
 18
 19    def __init__(
 20        self,
 21        callback: Callable[[Any], Any],
 22        existing_playlist: str | None,
 23        existing_entry: Any,
 24    ):
 25        from ba.macmusicapp import MacMusicAppMusicPlayer
 26
 27        self._r = 'editSoundtrackWindow'
 28        self._callback = callback
 29        self._existing_playlist = existing_playlist
 30        self._existing_entry = copy.deepcopy(existing_entry)
 31        self._width = 520.0
 32        self._height = 520.0
 33        self._spacing = 45.0
 34        v = self._height - 90.0
 35        v -= self._spacing * 1.0
 36        super().__init__(
 37            root_widget=ba.containerwidget(
 38                size=(self._width, self._height), transition='in_right'
 39            )
 40        )
 41        btn = ba.buttonwidget(
 42            parent=self._root_widget,
 43            position=(35, self._height - 65),
 44            size=(130, 50),
 45            label=ba.Lstr(resource='cancelText'),
 46            on_activate_call=self._back,
 47            autoselect=True,
 48        )
 49        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
 50        ba.textwidget(
 51            parent=self._root_widget,
 52            position=(20, self._height - 54),
 53            size=(self._width, 25),
 54            text=ba.Lstr(resource=self._r + '.selectAPlaylistText'),
 55            color=ba.app.ui.title_color,
 56            h_align='center',
 57            v_align='center',
 58            maxwidth=200,
 59        )
 60        self._scrollwidget = ba.scrollwidget(
 61            parent=self._root_widget,
 62            position=(40, v - 340),
 63            size=(self._width - 80, 400),
 64            claims_tab=True,
 65            selection_loops_to_parent=True,
 66        )
 67        ba.widget(edit=self._scrollwidget, right_widget=self._scrollwidget)
 68        self._column = ba.columnwidget(
 69            parent=self._scrollwidget,
 70            claims_tab=True,
 71            selection_loops_to_parent=True,
 72        )
 73
 74        ba.textwidget(
 75            parent=self._column,
 76            size=(self._width - 80, 22),
 77            text=ba.Lstr(resource=self._r + '.fetchingITunesText'),
 78            color=(0.6, 0.9, 0.6, 1.0),
 79            scale=0.8,
 80        )
 81        musicplayer = ba.app.music.get_music_player()
 82        assert isinstance(musicplayer, MacMusicAppMusicPlayer)
 83        musicplayer.get_playlists(self._playlists_cb)
 84        ba.containerwidget(
 85            edit=self._root_widget, selected_child=self._scrollwidget
 86        )
 87
 88    def _playlists_cb(self, playlists: list[str]) -> None:
 89        if self._column:
 90            for widget in self._column.get_children():
 91                widget.delete()
 92            for i, playlist in enumerate(playlists):
 93                txt = ba.textwidget(
 94                    parent=self._column,
 95                    size=(self._width - 80, 30),
 96                    text=playlist,
 97                    v_align='center',
 98                    maxwidth=self._width - 110,
 99                    selectable=True,
100                    on_activate_call=ba.Call(self._sel, playlist),
101                    click_activate=True,
102                )
103                ba.widget(edit=txt, show_buffer_top=40, show_buffer_bottom=40)
104                if playlist == self._existing_playlist:
105                    ba.columnwidget(
106                        edit=self._column, selected_child=txt, visible_child=txt
107                    )
108                if i == len(playlists) - 1:
109                    ba.widget(edit=txt, down_widget=txt)
110
111    def _sel(self, selection: str) -> None:
112        if self._root_widget:
113            ba.containerwidget(edit=self._root_widget, transition='out_right')
114            self._callback({'type': 'iTunesPlaylist', 'name': selection})
115
116    def _back(self) -> None:
117        ba.containerwidget(edit=self._root_widget, transition='out_right')
118        self._callback(self._existing_entry)
class MacMusicAppPlaylistSelectWindow(ba.ui.Window):
 17class MacMusicAppPlaylistSelectWindow(ba.Window):
 18    """Window for selecting an iTunes playlist."""
 19
 20    def __init__(
 21        self,
 22        callback: Callable[[Any], Any],
 23        existing_playlist: str | None,
 24        existing_entry: Any,
 25    ):
 26        from ba.macmusicapp import MacMusicAppMusicPlayer
 27
 28        self._r = 'editSoundtrackWindow'
 29        self._callback = callback
 30        self._existing_playlist = existing_playlist
 31        self._existing_entry = copy.deepcopy(existing_entry)
 32        self._width = 520.0
 33        self._height = 520.0
 34        self._spacing = 45.0
 35        v = self._height - 90.0
 36        v -= self._spacing * 1.0
 37        super().__init__(
 38            root_widget=ba.containerwidget(
 39                size=(self._width, self._height), transition='in_right'
 40            )
 41        )
 42        btn = ba.buttonwidget(
 43            parent=self._root_widget,
 44            position=(35, self._height - 65),
 45            size=(130, 50),
 46            label=ba.Lstr(resource='cancelText'),
 47            on_activate_call=self._back,
 48            autoselect=True,
 49        )
 50        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
 51        ba.textwidget(
 52            parent=self._root_widget,
 53            position=(20, self._height - 54),
 54            size=(self._width, 25),
 55            text=ba.Lstr(resource=self._r + '.selectAPlaylistText'),
 56            color=ba.app.ui.title_color,
 57            h_align='center',
 58            v_align='center',
 59            maxwidth=200,
 60        )
 61        self._scrollwidget = ba.scrollwidget(
 62            parent=self._root_widget,
 63            position=(40, v - 340),
 64            size=(self._width - 80, 400),
 65            claims_tab=True,
 66            selection_loops_to_parent=True,
 67        )
 68        ba.widget(edit=self._scrollwidget, right_widget=self._scrollwidget)
 69        self._column = ba.columnwidget(
 70            parent=self._scrollwidget,
 71            claims_tab=True,
 72            selection_loops_to_parent=True,
 73        )
 74
 75        ba.textwidget(
 76            parent=self._column,
 77            size=(self._width - 80, 22),
 78            text=ba.Lstr(resource=self._r + '.fetchingITunesText'),
 79            color=(0.6, 0.9, 0.6, 1.0),
 80            scale=0.8,
 81        )
 82        musicplayer = ba.app.music.get_music_player()
 83        assert isinstance(musicplayer, MacMusicAppMusicPlayer)
 84        musicplayer.get_playlists(self._playlists_cb)
 85        ba.containerwidget(
 86            edit=self._root_widget, selected_child=self._scrollwidget
 87        )
 88
 89    def _playlists_cb(self, playlists: list[str]) -> None:
 90        if self._column:
 91            for widget in self._column.get_children():
 92                widget.delete()
 93            for i, playlist in enumerate(playlists):
 94                txt = ba.textwidget(
 95                    parent=self._column,
 96                    size=(self._width - 80, 30),
 97                    text=playlist,
 98                    v_align='center',
 99                    maxwidth=self._width - 110,
100                    selectable=True,
101                    on_activate_call=ba.Call(self._sel, playlist),
102                    click_activate=True,
103                )
104                ba.widget(edit=txt, show_buffer_top=40, show_buffer_bottom=40)
105                if playlist == self._existing_playlist:
106                    ba.columnwidget(
107                        edit=self._column, selected_child=txt, visible_child=txt
108                    )
109                if i == len(playlists) - 1:
110                    ba.widget(edit=txt, down_widget=txt)
111
112    def _sel(self, selection: str) -> None:
113        if self._root_widget:
114            ba.containerwidget(edit=self._root_widget, transition='out_right')
115            self._callback({'type': 'iTunesPlaylist', 'name': selection})
116
117    def _back(self) -> None:
118        ba.containerwidget(edit=self._root_widget, transition='out_right')
119        self._callback(self._existing_entry)

Window for selecting an iTunes playlist.

MacMusicAppPlaylistSelectWindow( callback: Callable[[Any], Any], existing_playlist: str | None, existing_entry: Any)
20    def __init__(
21        self,
22        callback: Callable[[Any], Any],
23        existing_playlist: str | None,
24        existing_entry: Any,
25    ):
26        from ba.macmusicapp import MacMusicAppMusicPlayer
27
28        self._r = 'editSoundtrackWindow'
29        self._callback = callback
30        self._existing_playlist = existing_playlist
31        self._existing_entry = copy.deepcopy(existing_entry)
32        self._width = 520.0
33        self._height = 520.0
34        self._spacing = 45.0
35        v = self._height - 90.0
36        v -= self._spacing * 1.0
37        super().__init__(
38            root_widget=ba.containerwidget(
39                size=(self._width, self._height), transition='in_right'
40            )
41        )
42        btn = ba.buttonwidget(
43            parent=self._root_widget,
44            position=(35, self._height - 65),
45            size=(130, 50),
46            label=ba.Lstr(resource='cancelText'),
47            on_activate_call=self._back,
48            autoselect=True,
49        )
50        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
51        ba.textwidget(
52            parent=self._root_widget,
53            position=(20, self._height - 54),
54            size=(self._width, 25),
55            text=ba.Lstr(resource=self._r + '.selectAPlaylistText'),
56            color=ba.app.ui.title_color,
57            h_align='center',
58            v_align='center',
59            maxwidth=200,
60        )
61        self._scrollwidget = ba.scrollwidget(
62            parent=self._root_widget,
63            position=(40, v - 340),
64            size=(self._width - 80, 400),
65            claims_tab=True,
66            selection_loops_to_parent=True,
67        )
68        ba.widget(edit=self._scrollwidget, right_widget=self._scrollwidget)
69        self._column = ba.columnwidget(
70            parent=self._scrollwidget,
71            claims_tab=True,
72            selection_loops_to_parent=True,
73        )
74
75        ba.textwidget(
76            parent=self._column,
77            size=(self._width - 80, 22),
78            text=ba.Lstr(resource=self._r + '.fetchingITunesText'),
79            color=(0.6, 0.9, 0.6, 1.0),
80            scale=0.8,
81        )
82        musicplayer = ba.app.music.get_music_player()
83        assert isinstance(musicplayer, MacMusicAppMusicPlayer)
84        musicplayer.get_playlists(self._playlists_cb)
85        ba.containerwidget(
86            edit=self._root_widget, selected_child=self._scrollwidget
87        )
Inherited Members
ba.ui.Window
get_root_widget