bastd.ui.url

UI functionality related to URLs.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""UI functionality related to URLs."""
  4
  5from __future__ import annotations
  6
  7import ba
  8import ba.internal
  9
 10
 11class ShowURLWindow(ba.Window):
 12    """A window presenting a URL to the user visually."""
 13
 14    def __init__(self, address: str):
 15
 16        # in some cases we might want to show it as a qr code
 17        # (for long URLs especially)
 18        app = ba.app
 19        uiscale = app.ui.uiscale
 20        self._address = address
 21
 22        self._width = 800
 23        self._height = 450
 24        super().__init__(
 25            root_widget=ba.containerwidget(
 26                size=(self._width, self._height + 40),
 27                transition='in_right',
 28                scale=(
 29                    1.25
 30                    if uiscale is ba.UIScale.SMALL
 31                    else 1.25
 32                    if uiscale is ba.UIScale.MEDIUM
 33                    else 1.25
 34                ),
 35            )
 36        )
 37        ba.textwidget(
 38            parent=self._root_widget,
 39            position=(self._width * 0.5, self._height - 10),
 40            size=(0, 0),
 41            color=ba.app.ui.title_color,
 42            h_align='center',
 43            v_align='center',
 44            text=ba.Lstr(resource='directBrowserToURLText'),
 45            maxwidth=self._width * 0.95,
 46        )
 47        ba.textwidget(
 48            parent=self._root_widget,
 49            position=(self._width * 0.5, self._height - 60),
 50            size=(0, 0),
 51            scale=1.3,
 52            color=ba.app.ui.infotextcolor,
 53            h_align='center',
 54            v_align='center',
 55            text=address,
 56            maxwidth=self._width * 0.95,
 57        )
 58        button_width = 200
 59
 60        qr_size = 220
 61        ba.imagewidget(
 62            parent=self._root_widget,
 63            position=(
 64                self._width * 0.5 - qr_size * 0.5,
 65                self._height * 0.5 - qr_size * 0.5 + 10,
 66            ),
 67            size=(qr_size, qr_size),
 68            texture=ba.internal.get_qrcode_texture(address),
 69        )
 70
 71        xoffs = 0
 72        if ba.clipboard_is_supported():
 73            xoffs = -150
 74            btn = ba.buttonwidget(
 75                parent=self._root_widget,
 76                position=(
 77                    self._width * 0.5 - button_width * 0.5 + xoffs,
 78                    20,
 79                ),
 80                size=(button_width, 65),
 81                autoselect=True,
 82                label=ba.Lstr(resource='copyText'),
 83                on_activate_call=self._copy,
 84            )
 85            xoffs = 150
 86
 87        btn = ba.buttonwidget(
 88            parent=self._root_widget,
 89            position=(self._width * 0.5 - button_width * 0.5 + xoffs, 20),
 90            size=(button_width, 65),
 91            autoselect=True,
 92            label=ba.Lstr(resource='doneText'),
 93            on_activate_call=self._done,
 94        )
 95        # we have no 'cancel' button but still want to be able to
 96        # hit back/escape/etc to leave..
 97        ba.containerwidget(
 98            edit=self._root_widget,
 99            selected_child=btn,
100            start_button=btn,
101            on_cancel_call=btn.activate,
102        )
103
104    def _copy(self) -> None:
105        ba.clipboard_set_text(self._address)
106        ba.screenmessage(ba.Lstr(resource='copyConfirmText'), color=(0, 1, 0))
107
108    def _done(self) -> None:
109        ba.containerwidget(edit=self._root_widget, transition='out_left')
class ShowURLWindow(ba.ui.Window):
 12class ShowURLWindow(ba.Window):
 13    """A window presenting a URL to the user visually."""
 14
 15    def __init__(self, address: str):
 16
 17        # in some cases we might want to show it as a qr code
 18        # (for long URLs especially)
 19        app = ba.app
 20        uiscale = app.ui.uiscale
 21        self._address = address
 22
 23        self._width = 800
 24        self._height = 450
 25        super().__init__(
 26            root_widget=ba.containerwidget(
 27                size=(self._width, self._height + 40),
 28                transition='in_right',
 29                scale=(
 30                    1.25
 31                    if uiscale is ba.UIScale.SMALL
 32                    else 1.25
 33                    if uiscale is ba.UIScale.MEDIUM
 34                    else 1.25
 35                ),
 36            )
 37        )
 38        ba.textwidget(
 39            parent=self._root_widget,
 40            position=(self._width * 0.5, self._height - 10),
 41            size=(0, 0),
 42            color=ba.app.ui.title_color,
 43            h_align='center',
 44            v_align='center',
 45            text=ba.Lstr(resource='directBrowserToURLText'),
 46            maxwidth=self._width * 0.95,
 47        )
 48        ba.textwidget(
 49            parent=self._root_widget,
 50            position=(self._width * 0.5, self._height - 60),
 51            size=(0, 0),
 52            scale=1.3,
 53            color=ba.app.ui.infotextcolor,
 54            h_align='center',
 55            v_align='center',
 56            text=address,
 57            maxwidth=self._width * 0.95,
 58        )
 59        button_width = 200
 60
 61        qr_size = 220
 62        ba.imagewidget(
 63            parent=self._root_widget,
 64            position=(
 65                self._width * 0.5 - qr_size * 0.5,
 66                self._height * 0.5 - qr_size * 0.5 + 10,
 67            ),
 68            size=(qr_size, qr_size),
 69            texture=ba.internal.get_qrcode_texture(address),
 70        )
 71
 72        xoffs = 0
 73        if ba.clipboard_is_supported():
 74            xoffs = -150
 75            btn = ba.buttonwidget(
 76                parent=self._root_widget,
 77                position=(
 78                    self._width * 0.5 - button_width * 0.5 + xoffs,
 79                    20,
 80                ),
 81                size=(button_width, 65),
 82                autoselect=True,
 83                label=ba.Lstr(resource='copyText'),
 84                on_activate_call=self._copy,
 85            )
 86            xoffs = 150
 87
 88        btn = ba.buttonwidget(
 89            parent=self._root_widget,
 90            position=(self._width * 0.5 - button_width * 0.5 + xoffs, 20),
 91            size=(button_width, 65),
 92            autoselect=True,
 93            label=ba.Lstr(resource='doneText'),
 94            on_activate_call=self._done,
 95        )
 96        # we have no 'cancel' button but still want to be able to
 97        # hit back/escape/etc to leave..
 98        ba.containerwidget(
 99            edit=self._root_widget,
100            selected_child=btn,
101            start_button=btn,
102            on_cancel_call=btn.activate,
103        )
104
105    def _copy(self) -> None:
106        ba.clipboard_set_text(self._address)
107        ba.screenmessage(ba.Lstr(resource='copyConfirmText'), color=(0, 1, 0))
108
109    def _done(self) -> None:
110        ba.containerwidget(edit=self._root_widget, transition='out_left')

A window presenting a URL to the user visually.

ShowURLWindow(address: str)
 15    def __init__(self, address: str):
 16
 17        # in some cases we might want to show it as a qr code
 18        # (for long URLs especially)
 19        app = ba.app
 20        uiscale = app.ui.uiscale
 21        self._address = address
 22
 23        self._width = 800
 24        self._height = 450
 25        super().__init__(
 26            root_widget=ba.containerwidget(
 27                size=(self._width, self._height + 40),
 28                transition='in_right',
 29                scale=(
 30                    1.25
 31                    if uiscale is ba.UIScale.SMALL
 32                    else 1.25
 33                    if uiscale is ba.UIScale.MEDIUM
 34                    else 1.25
 35                ),
 36            )
 37        )
 38        ba.textwidget(
 39            parent=self._root_widget,
 40            position=(self._width * 0.5, self._height - 10),
 41            size=(0, 0),
 42            color=ba.app.ui.title_color,
 43            h_align='center',
 44            v_align='center',
 45            text=ba.Lstr(resource='directBrowserToURLText'),
 46            maxwidth=self._width * 0.95,
 47        )
 48        ba.textwidget(
 49            parent=self._root_widget,
 50            position=(self._width * 0.5, self._height - 60),
 51            size=(0, 0),
 52            scale=1.3,
 53            color=ba.app.ui.infotextcolor,
 54            h_align='center',
 55            v_align='center',
 56            text=address,
 57            maxwidth=self._width * 0.95,
 58        )
 59        button_width = 200
 60
 61        qr_size = 220
 62        ba.imagewidget(
 63            parent=self._root_widget,
 64            position=(
 65                self._width * 0.5 - qr_size * 0.5,
 66                self._height * 0.5 - qr_size * 0.5 + 10,
 67            ),
 68            size=(qr_size, qr_size),
 69            texture=ba.internal.get_qrcode_texture(address),
 70        )
 71
 72        xoffs = 0
 73        if ba.clipboard_is_supported():
 74            xoffs = -150
 75            btn = ba.buttonwidget(
 76                parent=self._root_widget,
 77                position=(
 78                    self._width * 0.5 - button_width * 0.5 + xoffs,
 79                    20,
 80                ),
 81                size=(button_width, 65),
 82                autoselect=True,
 83                label=ba.Lstr(resource='copyText'),
 84                on_activate_call=self._copy,
 85            )
 86            xoffs = 150
 87
 88        btn = ba.buttonwidget(
 89            parent=self._root_widget,
 90            position=(self._width * 0.5 - button_width * 0.5 + xoffs, 20),
 91            size=(button_width, 65),
 92            autoselect=True,
 93            label=ba.Lstr(resource='doneText'),
 94            on_activate_call=self._done,
 95        )
 96        # we have no 'cancel' button but still want to be able to
 97        # hit back/escape/etc to leave..
 98        ba.containerwidget(
 99            edit=self._root_widget,
100            selected_child=btn,
101            start_button=btn,
102            on_cancel_call=btn.activate,
103        )
Inherited Members
ba.ui.Window
get_root_widget