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__(self, callback: Callable[[Any], Any],
20                 existing_playlist: str | None, existing_entry: Any):
21        from ba.macmusicapp import MacMusicAppMusicPlayer
22        self._r = 'editSoundtrackWindow'
23        self._callback = callback
24        self._existing_playlist = existing_playlist
25        self._existing_entry = copy.deepcopy(existing_entry)
26        self._width = 520.0
27        self._height = 520.0
28        self._spacing = 45.0
29        v = self._height - 90.0
30        v -= self._spacing * 1.0
31        super().__init__(root_widget=ba.containerwidget(
32            size=(self._width, self._height), transition='in_right'))
33        btn = ba.buttonwidget(parent=self._root_widget,
34                              position=(35, self._height - 65),
35                              size=(130, 50),
36                              label=ba.Lstr(resource='cancelText'),
37                              on_activate_call=self._back,
38                              autoselect=True)
39        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
40        ba.textwidget(parent=self._root_widget,
41                      position=(20, self._height - 54),
42                      size=(self._width, 25),
43                      text=ba.Lstr(resource=self._r + '.selectAPlaylistText'),
44                      color=ba.app.ui.title_color,
45                      h_align='center',
46                      v_align='center',
47                      maxwidth=200)
48        self._scrollwidget = ba.scrollwidget(parent=self._root_widget,
49                                             position=(40, v - 340),
50                                             size=(self._width - 80, 400),
51                                             claims_tab=True,
52                                             selection_loops_to_parent=True)
53        ba.widget(edit=self._scrollwidget, right_widget=self._scrollwidget)
54        self._column = ba.columnwidget(parent=self._scrollwidget,
55                                       claims_tab=True,
56                                       selection_loops_to_parent=True)
57
58        ba.textwidget(parent=self._column,
59                      size=(self._width - 80, 22),
60                      text=ba.Lstr(resource=self._r + '.fetchingITunesText'),
61                      color=(0.6, 0.9, 0.6, 1.0),
62                      scale=0.8)
63        musicplayer = ba.app.music.get_music_player()
64        assert isinstance(musicplayer, MacMusicAppMusicPlayer)
65        musicplayer.get_playlists(self._playlists_cb)
66        ba.containerwidget(edit=self._root_widget,
67                           selected_child=self._scrollwidget)
68
69    def _playlists_cb(self, playlists: list[str]) -> None:
70        if self._column:
71            for widget in self._column.get_children():
72                widget.delete()
73            for i, playlist in enumerate(playlists):
74                txt = ba.textwidget(parent=self._column,
75                                    size=(self._width - 80, 30),
76                                    text=playlist,
77                                    v_align='center',
78                                    maxwidth=self._width - 110,
79                                    selectable=True,
80                                    on_activate_call=ba.Call(
81                                        self._sel, playlist),
82                                    click_activate=True)
83                ba.widget(edit=txt, show_buffer_top=40, show_buffer_bottom=40)
84                if playlist == self._existing_playlist:
85                    ba.columnwidget(edit=self._column,
86                                    selected_child=txt,
87                                    visible_child=txt)
88                if i == len(playlists) - 1:
89                    ba.widget(edit=txt, down_widget=txt)
90
91    def _sel(self, selection: str) -> None:
92        if self._root_widget:
93            ba.containerwidget(edit=self._root_widget, transition='out_right')
94            self._callback({'type': 'iTunesPlaylist', 'name': selection})
95
96    def _back(self) -> None:
97        ba.containerwidget(edit=self._root_widget, transition='out_right')
98        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__(self, callback: Callable[[Any], Any],
21                 existing_playlist: str | None, existing_entry: Any):
22        from ba.macmusicapp import MacMusicAppMusicPlayer
23        self._r = 'editSoundtrackWindow'
24        self._callback = callback
25        self._existing_playlist = existing_playlist
26        self._existing_entry = copy.deepcopy(existing_entry)
27        self._width = 520.0
28        self._height = 520.0
29        self._spacing = 45.0
30        v = self._height - 90.0
31        v -= self._spacing * 1.0
32        super().__init__(root_widget=ba.containerwidget(
33            size=(self._width, self._height), transition='in_right'))
34        btn = ba.buttonwidget(parent=self._root_widget,
35                              position=(35, self._height - 65),
36                              size=(130, 50),
37                              label=ba.Lstr(resource='cancelText'),
38                              on_activate_call=self._back,
39                              autoselect=True)
40        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
41        ba.textwidget(parent=self._root_widget,
42                      position=(20, self._height - 54),
43                      size=(self._width, 25),
44                      text=ba.Lstr(resource=self._r + '.selectAPlaylistText'),
45                      color=ba.app.ui.title_color,
46                      h_align='center',
47                      v_align='center',
48                      maxwidth=200)
49        self._scrollwidget = ba.scrollwidget(parent=self._root_widget,
50                                             position=(40, v - 340),
51                                             size=(self._width - 80, 400),
52                                             claims_tab=True,
53                                             selection_loops_to_parent=True)
54        ba.widget(edit=self._scrollwidget, right_widget=self._scrollwidget)
55        self._column = ba.columnwidget(parent=self._scrollwidget,
56                                       claims_tab=True,
57                                       selection_loops_to_parent=True)
58
59        ba.textwidget(parent=self._column,
60                      size=(self._width - 80, 22),
61                      text=ba.Lstr(resource=self._r + '.fetchingITunesText'),
62                      color=(0.6, 0.9, 0.6, 1.0),
63                      scale=0.8)
64        musicplayer = ba.app.music.get_music_player()
65        assert isinstance(musicplayer, MacMusicAppMusicPlayer)
66        musicplayer.get_playlists(self._playlists_cb)
67        ba.containerwidget(edit=self._root_widget,
68                           selected_child=self._scrollwidget)
69
70    def _playlists_cb(self, playlists: list[str]) -> None:
71        if self._column:
72            for widget in self._column.get_children():
73                widget.delete()
74            for i, playlist in enumerate(playlists):
75                txt = ba.textwidget(parent=self._column,
76                                    size=(self._width - 80, 30),
77                                    text=playlist,
78                                    v_align='center',
79                                    maxwidth=self._width - 110,
80                                    selectable=True,
81                                    on_activate_call=ba.Call(
82                                        self._sel, playlist),
83                                    click_activate=True)
84                ba.widget(edit=txt, show_buffer_top=40, show_buffer_bottom=40)
85                if playlist == self._existing_playlist:
86                    ba.columnwidget(edit=self._column,
87                                    selected_child=txt,
88                                    visible_child=txt)
89                if i == len(playlists) - 1:
90                    ba.widget(edit=txt, down_widget=txt)
91
92    def _sel(self, selection: str) -> None:
93        if self._root_widget:
94            ba.containerwidget(edit=self._root_widget, transition='out_right')
95            self._callback({'type': 'iTunesPlaylist', 'name': selection})
96
97    def _back(self) -> None:
98        ba.containerwidget(edit=self._root_widget, transition='out_right')
99        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__(self, callback: Callable[[Any], Any],
21                 existing_playlist: str | None, existing_entry: Any):
22        from ba.macmusicapp import MacMusicAppMusicPlayer
23        self._r = 'editSoundtrackWindow'
24        self._callback = callback
25        self._existing_playlist = existing_playlist
26        self._existing_entry = copy.deepcopy(existing_entry)
27        self._width = 520.0
28        self._height = 520.0
29        self._spacing = 45.0
30        v = self._height - 90.0
31        v -= self._spacing * 1.0
32        super().__init__(root_widget=ba.containerwidget(
33            size=(self._width, self._height), transition='in_right'))
34        btn = ba.buttonwidget(parent=self._root_widget,
35                              position=(35, self._height - 65),
36                              size=(130, 50),
37                              label=ba.Lstr(resource='cancelText'),
38                              on_activate_call=self._back,
39                              autoselect=True)
40        ba.containerwidget(edit=self._root_widget, cancel_button=btn)
41        ba.textwidget(parent=self._root_widget,
42                      position=(20, self._height - 54),
43                      size=(self._width, 25),
44                      text=ba.Lstr(resource=self._r + '.selectAPlaylistText'),
45                      color=ba.app.ui.title_color,
46                      h_align='center',
47                      v_align='center',
48                      maxwidth=200)
49        self._scrollwidget = ba.scrollwidget(parent=self._root_widget,
50                                             position=(40, v - 340),
51                                             size=(self._width - 80, 400),
52                                             claims_tab=True,
53                                             selection_loops_to_parent=True)
54        ba.widget(edit=self._scrollwidget, right_widget=self._scrollwidget)
55        self._column = ba.columnwidget(parent=self._scrollwidget,
56                                       claims_tab=True,
57                                       selection_loops_to_parent=True)
58
59        ba.textwidget(parent=self._column,
60                      size=(self._width - 80, 22),
61                      text=ba.Lstr(resource=self._r + '.fetchingITunesText'),
62                      color=(0.6, 0.9, 0.6, 1.0),
63                      scale=0.8)
64        musicplayer = ba.app.music.get_music_player()
65        assert isinstance(musicplayer, MacMusicAppMusicPlayer)
66        musicplayer.get_playlists(self._playlists_cb)
67        ba.containerwidget(edit=self._root_widget,
68                           selected_child=self._scrollwidget)
Inherited Members
ba.ui.Window
get_root_widget