bastd.ui.qrcode

Provides functionality for displaying QR codes.

 1# Released under the MIT License. See LICENSE for details.
 2#
 3"""Provides functionality for displaying QR codes."""
 4from __future__ import annotations
 5
 6import ba
 7from bastd.ui import popup
 8
 9
10class QRCodeWindow(popup.PopupWindow):
11    """Popup window that shows a QR code."""
12
13    def __init__(self, origin_widget: ba.Widget, qr_tex: ba.Texture):
14
15        position = origin_widget.get_screen_space_center()
16        uiscale = ba.app.ui.uiscale
17        scale = (
18            2.3
19            if uiscale is ba.UIScale.SMALL
20            else 1.65
21            if uiscale is ba.UIScale.MEDIUM
22            else 1.23
23        )
24        self._transitioning_out = False
25        self._width = 450
26        self._height = 400
27        bg_color = (0.5, 0.4, 0.6)
28        popup.PopupWindow.__init__(
29            self,
30            position=position,
31            size=(self._width, self._height),
32            scale=scale,
33            bg_color=bg_color,
34        )
35        self._cancel_button = ba.buttonwidget(
36            parent=self.root_widget,
37            position=(50, self._height - 30),
38            size=(50, 50),
39            scale=0.5,
40            label='',
41            color=bg_color,
42            on_activate_call=self._on_cancel_press,
43            autoselect=True,
44            icon=ba.gettexture('crossOut'),
45            iconscale=1.2,
46        )
47        ba.imagewidget(
48            parent=self.root_widget,
49            position=(self._width * 0.5 - 150, self._height * 0.5 - 150),
50            size=(300, 300),
51            texture=qr_tex,
52        )
53
54    def _on_cancel_press(self) -> None:
55        self._transition_out()
56
57    def _transition_out(self) -> None:
58        if not self._transitioning_out:
59            self._transitioning_out = True
60            ba.containerwidget(edit=self.root_widget, transition='out_scale')
61
62    def on_popup_cancel(self) -> None:
63        ba.playsound(ba.getsound('swish'))
64        self._transition_out()
class QRCodeWindow(bastd.ui.popup.PopupWindow):
11class QRCodeWindow(popup.PopupWindow):
12    """Popup window that shows a QR code."""
13
14    def __init__(self, origin_widget: ba.Widget, qr_tex: ba.Texture):
15
16        position = origin_widget.get_screen_space_center()
17        uiscale = ba.app.ui.uiscale
18        scale = (
19            2.3
20            if uiscale is ba.UIScale.SMALL
21            else 1.65
22            if uiscale is ba.UIScale.MEDIUM
23            else 1.23
24        )
25        self._transitioning_out = False
26        self._width = 450
27        self._height = 400
28        bg_color = (0.5, 0.4, 0.6)
29        popup.PopupWindow.__init__(
30            self,
31            position=position,
32            size=(self._width, self._height),
33            scale=scale,
34            bg_color=bg_color,
35        )
36        self._cancel_button = ba.buttonwidget(
37            parent=self.root_widget,
38            position=(50, self._height - 30),
39            size=(50, 50),
40            scale=0.5,
41            label='',
42            color=bg_color,
43            on_activate_call=self._on_cancel_press,
44            autoselect=True,
45            icon=ba.gettexture('crossOut'),
46            iconscale=1.2,
47        )
48        ba.imagewidget(
49            parent=self.root_widget,
50            position=(self._width * 0.5 - 150, self._height * 0.5 - 150),
51            size=(300, 300),
52            texture=qr_tex,
53        )
54
55    def _on_cancel_press(self) -> None:
56        self._transition_out()
57
58    def _transition_out(self) -> None:
59        if not self._transitioning_out:
60            self._transitioning_out = True
61            ba.containerwidget(edit=self.root_widget, transition='out_scale')
62
63    def on_popup_cancel(self) -> None:
64        ba.playsound(ba.getsound('swish'))
65        self._transition_out()

Popup window that shows a QR code.

QRCodeWindow(origin_widget: _ba.Widget, qr_tex: _ba.Texture)
14    def __init__(self, origin_widget: ba.Widget, qr_tex: ba.Texture):
15
16        position = origin_widget.get_screen_space_center()
17        uiscale = ba.app.ui.uiscale
18        scale = (
19            2.3
20            if uiscale is ba.UIScale.SMALL
21            else 1.65
22            if uiscale is ba.UIScale.MEDIUM
23            else 1.23
24        )
25        self._transitioning_out = False
26        self._width = 450
27        self._height = 400
28        bg_color = (0.5, 0.4, 0.6)
29        popup.PopupWindow.__init__(
30            self,
31            position=position,
32            size=(self._width, self._height),
33            scale=scale,
34            bg_color=bg_color,
35        )
36        self._cancel_button = ba.buttonwidget(
37            parent=self.root_widget,
38            position=(50, self._height - 30),
39            size=(50, 50),
40            scale=0.5,
41            label='',
42            color=bg_color,
43            on_activate_call=self._on_cancel_press,
44            autoselect=True,
45            icon=ba.gettexture('crossOut'),
46            iconscale=1.2,
47        )
48        ba.imagewidget(
49            parent=self.root_widget,
50            position=(self._width * 0.5 - 150, self._height * 0.5 - 150),
51            size=(300, 300),
52            texture=qr_tex,
53        )
def on_popup_cancel(self) -> None:
63    def on_popup_cancel(self) -> None:
64        ba.playsound(ba.getsound('swish'))
65        self._transition_out()

Called when the popup is canceled.

Cancels can occur due to clicking outside the window, hitting escape, etc.