bauiv1lib.playlist.share

UI functionality for importing shared playlists.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""UI functionality for importing shared playlists."""
  4
  5from __future__ import annotations
  6
  7import time
  8from typing import TYPE_CHECKING, override
  9
 10from bauiv1lib.sendinfo import SendInfoWindow
 11import bauiv1 as bui
 12
 13if TYPE_CHECKING:
 14    from typing import Any, Callable
 15
 16
 17class SharePlaylistImportWindow(SendInfoWindow):
 18    """Window for importing a shared playlist."""
 19
 20    def __init__(
 21        self,
 22        origin_widget: bui.Widget | None = None,
 23        on_success_callback: Callable[[], Any] | None = None,
 24    ):
 25        SendInfoWindow.__init__(
 26            self, modal=True, legacy_code_mode=True, origin_widget=origin_widget
 27        )
 28        self._on_success_callback = on_success_callback
 29
 30    def _on_import_response(self, response: dict[str, Any] | None) -> None:
 31        if response is None:
 32            bui.screenmessage(bui.Lstr(resource='errorText'), color=(1, 0, 0))
 33            bui.getsound('error').play()
 34            return
 35
 36        if response['playlistType'] == 'Team Tournament':
 37            playlist_type_name = bui.Lstr(resource='playModes.teamsText')
 38        elif response['playlistType'] == 'Free-for-All':
 39            playlist_type_name = bui.Lstr(resource='playModes.freeForAllText')
 40        else:
 41            playlist_type_name = bui.Lstr(value=response['playlistType'])
 42
 43        bui.screenmessage(
 44            bui.Lstr(
 45                resource='importPlaylistSuccessText',
 46                subs=[
 47                    ('${TYPE}', playlist_type_name),
 48                    ('${NAME}', response['playlistName']),
 49                ],
 50            ),
 51            color=(0, 1, 0),
 52        )
 53        bui.getsound('gunCocking').play()
 54        if self._on_success_callback is not None:
 55            self._on_success_callback()
 56        bui.containerwidget(
 57            edit=self._root_widget, transition=self._transition_out
 58        )
 59
 60    @override
 61    def _do_enter(self) -> None:
 62        plus = bui.app.plus
 63        assert plus is not None
 64
 65        plus.add_v1_account_transaction(
 66            {
 67                'type': 'IMPORT_PLAYLIST',
 68                'expire_time': time.time() + 5,
 69                'code': bui.textwidget(query=self._text_field),
 70            },
 71            callback=bui.WeakCall(self._on_import_response),
 72        )
 73        plus.run_v1_account_transactions()
 74        bui.screenmessage(bui.Lstr(resource='importingText'))
 75
 76
 77class SharePlaylistResultsWindow(bui.Window):
 78    """Window for sharing playlists."""
 79
 80    def __init__(
 81        self, name: str, data: str, origin: tuple[float, float] = (0.0, 0.0)
 82    ):
 83        del origin  # unused arg
 84        self._width = 450
 85        self._height = 300
 86        assert bui.app.classic is not None
 87        uiscale = bui.app.ui_v1.uiscale
 88        super().__init__(
 89            root_widget=bui.containerwidget(
 90                size=(self._width, self._height),
 91                color=(0.45, 0.63, 0.15),
 92                transition='in_scale',
 93                scale=(
 94                    1.8
 95                    if uiscale is bui.UIScale.SMALL
 96                    else 1.35 if uiscale is bui.UIScale.MEDIUM else 1.0
 97                ),
 98            )
 99        )
100        bui.getsound('cashRegister').play()
101        bui.getsound('swish').play()
102
103        self._cancel_button = bui.buttonwidget(
104            parent=self._root_widget,
105            scale=0.7,
106            position=(40, self._height - 40),
107            size=(50, 50),
108            label='',
109            on_activate_call=self.close,
110            autoselect=True,
111            color=(0.45, 0.63, 0.15),
112            icon=bui.gettexture('crossOut'),
113            iconscale=1.2,
114        )
115        bui.containerwidget(
116            edit=self._root_widget, cancel_button=self._cancel_button
117        )
118
119        bui.textwidget(
120            parent=self._root_widget,
121            position=(self._width * 0.5, self._height * 0.745),
122            size=(0, 0),
123            color=bui.app.ui_v1.infotextcolor,
124            scale=1.0,
125            flatness=1.0,
126            h_align='center',
127            v_align='center',
128            text=bui.Lstr(
129                resource='exportSuccessText', subs=[('${NAME}', name)]
130            ),
131            maxwidth=self._width * 0.85,
132        )
133
134        bui.textwidget(
135            parent=self._root_widget,
136            position=(self._width * 0.5, self._height * 0.645),
137            size=(0, 0),
138            color=bui.app.ui_v1.infotextcolor,
139            scale=0.6,
140            flatness=1.0,
141            h_align='center',
142            v_align='center',
143            text=bui.Lstr(resource='importPlaylistCodeInstructionsText'),
144            maxwidth=self._width * 0.85,
145        )
146
147        bui.textwidget(
148            parent=self._root_widget,
149            position=(self._width * 0.5, self._height * 0.4),
150            size=(0, 0),
151            color=(1.0, 3.0, 1.0),
152            scale=2.3,
153            h_align='center',
154            v_align='center',
155            text=data,
156            maxwidth=self._width * 0.85,
157        )
158
159    def close(self) -> None:
160        """Close the window."""
161        bui.containerwidget(edit=self._root_widget, transition='out_scale')
class SharePlaylistImportWindow(bauiv1lib.sendinfo.SendInfoWindow):
18class SharePlaylistImportWindow(SendInfoWindow):
19    """Window for importing a shared playlist."""
20
21    def __init__(
22        self,
23        origin_widget: bui.Widget | None = None,
24        on_success_callback: Callable[[], Any] | None = None,
25    ):
26        SendInfoWindow.__init__(
27            self, modal=True, legacy_code_mode=True, origin_widget=origin_widget
28        )
29        self._on_success_callback = on_success_callback
30
31    def _on_import_response(self, response: dict[str, Any] | None) -> None:
32        if response is None:
33            bui.screenmessage(bui.Lstr(resource='errorText'), color=(1, 0, 0))
34            bui.getsound('error').play()
35            return
36
37        if response['playlistType'] == 'Team Tournament':
38            playlist_type_name = bui.Lstr(resource='playModes.teamsText')
39        elif response['playlistType'] == 'Free-for-All':
40            playlist_type_name = bui.Lstr(resource='playModes.freeForAllText')
41        else:
42            playlist_type_name = bui.Lstr(value=response['playlistType'])
43
44        bui.screenmessage(
45            bui.Lstr(
46                resource='importPlaylistSuccessText',
47                subs=[
48                    ('${TYPE}', playlist_type_name),
49                    ('${NAME}', response['playlistName']),
50                ],
51            ),
52            color=(0, 1, 0),
53        )
54        bui.getsound('gunCocking').play()
55        if self._on_success_callback is not None:
56            self._on_success_callback()
57        bui.containerwidget(
58            edit=self._root_widget, transition=self._transition_out
59        )
60
61    @override
62    def _do_enter(self) -> None:
63        plus = bui.app.plus
64        assert plus is not None
65
66        plus.add_v1_account_transaction(
67            {
68                'type': 'IMPORT_PLAYLIST',
69                'expire_time': time.time() + 5,
70                'code': bui.textwidget(query=self._text_field),
71            },
72            callback=bui.WeakCall(self._on_import_response),
73        )
74        plus.run_v1_account_transactions()
75        bui.screenmessage(bui.Lstr(resource='importingText'))

Window for importing a shared playlist.

SharePlaylistImportWindow( origin_widget: _bauiv1.Widget | None = None, on_success_callback: Optional[Callable[[], Any]] = None)
21    def __init__(
22        self,
23        origin_widget: bui.Widget | None = None,
24        on_success_callback: Callable[[], Any] | None = None,
25    ):
26        SendInfoWindow.__init__(
27            self, modal=True, legacy_code_mode=True, origin_widget=origin_widget
28        )
29        self._on_success_callback = on_success_callback

Create a MainWindow given a root widget and transition info.

Automatically handles in and out transitions on the provided widget, so there is no need to set transitions when creating it.

class SharePlaylistResultsWindow(bauiv1._uitypes.Window):
 78class SharePlaylistResultsWindow(bui.Window):
 79    """Window for sharing playlists."""
 80
 81    def __init__(
 82        self, name: str, data: str, origin: tuple[float, float] = (0.0, 0.0)
 83    ):
 84        del origin  # unused arg
 85        self._width = 450
 86        self._height = 300
 87        assert bui.app.classic is not None
 88        uiscale = bui.app.ui_v1.uiscale
 89        super().__init__(
 90            root_widget=bui.containerwidget(
 91                size=(self._width, self._height),
 92                color=(0.45, 0.63, 0.15),
 93                transition='in_scale',
 94                scale=(
 95                    1.8
 96                    if uiscale is bui.UIScale.SMALL
 97                    else 1.35 if uiscale is bui.UIScale.MEDIUM else 1.0
 98                ),
 99            )
100        )
101        bui.getsound('cashRegister').play()
102        bui.getsound('swish').play()
103
104        self._cancel_button = bui.buttonwidget(
105            parent=self._root_widget,
106            scale=0.7,
107            position=(40, self._height - 40),
108            size=(50, 50),
109            label='',
110            on_activate_call=self.close,
111            autoselect=True,
112            color=(0.45, 0.63, 0.15),
113            icon=bui.gettexture('crossOut'),
114            iconscale=1.2,
115        )
116        bui.containerwidget(
117            edit=self._root_widget, cancel_button=self._cancel_button
118        )
119
120        bui.textwidget(
121            parent=self._root_widget,
122            position=(self._width * 0.5, self._height * 0.745),
123            size=(0, 0),
124            color=bui.app.ui_v1.infotextcolor,
125            scale=1.0,
126            flatness=1.0,
127            h_align='center',
128            v_align='center',
129            text=bui.Lstr(
130                resource='exportSuccessText', subs=[('${NAME}', name)]
131            ),
132            maxwidth=self._width * 0.85,
133        )
134
135        bui.textwidget(
136            parent=self._root_widget,
137            position=(self._width * 0.5, self._height * 0.645),
138            size=(0, 0),
139            color=bui.app.ui_v1.infotextcolor,
140            scale=0.6,
141            flatness=1.0,
142            h_align='center',
143            v_align='center',
144            text=bui.Lstr(resource='importPlaylistCodeInstructionsText'),
145            maxwidth=self._width * 0.85,
146        )
147
148        bui.textwidget(
149            parent=self._root_widget,
150            position=(self._width * 0.5, self._height * 0.4),
151            size=(0, 0),
152            color=(1.0, 3.0, 1.0),
153            scale=2.3,
154            h_align='center',
155            v_align='center',
156            text=data,
157            maxwidth=self._width * 0.85,
158        )
159
160    def close(self) -> None:
161        """Close the window."""
162        bui.containerwidget(edit=self._root_widget, transition='out_scale')

Window for sharing playlists.

SharePlaylistResultsWindow(name: str, data: str, origin: tuple[float, float] = (0.0, 0.0))
 81    def __init__(
 82        self, name: str, data: str, origin: tuple[float, float] = (0.0, 0.0)
 83    ):
 84        del origin  # unused arg
 85        self._width = 450
 86        self._height = 300
 87        assert bui.app.classic is not None
 88        uiscale = bui.app.ui_v1.uiscale
 89        super().__init__(
 90            root_widget=bui.containerwidget(
 91                size=(self._width, self._height),
 92                color=(0.45, 0.63, 0.15),
 93                transition='in_scale',
 94                scale=(
 95                    1.8
 96                    if uiscale is bui.UIScale.SMALL
 97                    else 1.35 if uiscale is bui.UIScale.MEDIUM else 1.0
 98                ),
 99            )
100        )
101        bui.getsound('cashRegister').play()
102        bui.getsound('swish').play()
103
104        self._cancel_button = bui.buttonwidget(
105            parent=self._root_widget,
106            scale=0.7,
107            position=(40, self._height - 40),
108            size=(50, 50),
109            label='',
110            on_activate_call=self.close,
111            autoselect=True,
112            color=(0.45, 0.63, 0.15),
113            icon=bui.gettexture('crossOut'),
114            iconscale=1.2,
115        )
116        bui.containerwidget(
117            edit=self._root_widget, cancel_button=self._cancel_button
118        )
119
120        bui.textwidget(
121            parent=self._root_widget,
122            position=(self._width * 0.5, self._height * 0.745),
123            size=(0, 0),
124            color=bui.app.ui_v1.infotextcolor,
125            scale=1.0,
126            flatness=1.0,
127            h_align='center',
128            v_align='center',
129            text=bui.Lstr(
130                resource='exportSuccessText', subs=[('${NAME}', name)]
131            ),
132            maxwidth=self._width * 0.85,
133        )
134
135        bui.textwidget(
136            parent=self._root_widget,
137            position=(self._width * 0.5, self._height * 0.645),
138            size=(0, 0),
139            color=bui.app.ui_v1.infotextcolor,
140            scale=0.6,
141            flatness=1.0,
142            h_align='center',
143            v_align='center',
144            text=bui.Lstr(resource='importPlaylistCodeInstructionsText'),
145            maxwidth=self._width * 0.85,
146        )
147
148        bui.textwidget(
149            parent=self._root_widget,
150            position=(self._width * 0.5, self._height * 0.4),
151            size=(0, 0),
152            color=(1.0, 3.0, 1.0),
153            scale=2.3,
154            h_align='center',
155            v_align='center',
156            text=data,
157            maxwidth=self._width * 0.85,
158        )
def close(self) -> None:
160    def close(self) -> None:
161        """Close the window."""
162        bui.containerwidget(edit=self._root_widget, transition='out_scale')

Close the window.