bastd.ui.account.unlink

UI functionality for unlinking accounts.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""UI functionality for unlinking accounts."""
  4
  5from __future__ import annotations
  6
  7import time
  8from typing import TYPE_CHECKING
  9
 10import _ba
 11import ba
 12
 13if TYPE_CHECKING:
 14    from typing import Any
 15
 16
 17class AccountUnlinkWindow(ba.Window):
 18    """A window to kick off account unlinks."""
 19
 20    def __init__(self, origin_widget: ba.Widget | None = None):
 21        scale_origin: tuple[float, float] | None
 22        if origin_widget is not None:
 23            self._transition_out = 'out_scale'
 24            scale_origin = origin_widget.get_screen_space_center()
 25            transition = 'in_scale'
 26        else:
 27            self._transition_out = 'out_right'
 28            scale_origin = None
 29            transition = 'in_right'
 30        bg_color = (0.4, 0.4, 0.5)
 31        self._width = 540
 32        self._height = 350
 33        self._scroll_width = 400
 34        self._scroll_height = 200
 35        uiscale = ba.app.ui.uiscale
 36        base_scale = (2.0 if uiscale is ba.UIScale.SMALL else
 37                      1.6 if uiscale is ba.UIScale.MEDIUM else 1.1)
 38        super().__init__(root_widget=ba.containerwidget(
 39            size=(self._width, self._height),
 40            transition=transition,
 41            scale=base_scale,
 42            scale_origin_stack_offset=scale_origin,
 43            stack_offset=(0, -10) if uiscale is ba.UIScale.SMALL else (0, 0)))
 44        self._cancel_button = ba.buttonwidget(parent=self._root_widget,
 45                                              position=(30, self._height - 50),
 46                                              size=(50, 50),
 47                                              scale=0.7,
 48                                              label='',
 49                                              color=bg_color,
 50                                              on_activate_call=self._cancel,
 51                                              autoselect=True,
 52                                              icon=ba.gettexture('crossOut'),
 53                                              iconscale=1.2)
 54        ba.textwidget(
 55            parent=self._root_widget,
 56            position=(self._width * 0.5, self._height * 0.88),
 57            size=(0, 0),
 58            text=ba.Lstr(
 59                resource='accountSettingsWindow.unlinkAccountsInstructionsText'
 60            ),
 61            maxwidth=self._width * 0.7,
 62            color=ba.app.ui.infotextcolor,
 63            h_align='center',
 64            v_align='center')
 65        ba.containerwidget(edit=self._root_widget,
 66                           cancel_button=self._cancel_button)
 67
 68        self._scrollwidget = ba.scrollwidget(
 69            parent=self._root_widget,
 70            highlight=False,
 71            position=((self._width - self._scroll_width) * 0.5,
 72                      self._height - 85 - self._scroll_height),
 73            size=(self._scroll_width, self._scroll_height))
 74        ba.containerwidget(edit=self._scrollwidget, claims_left_right=True)
 75        self._columnwidget = ba.columnwidget(parent=self._scrollwidget,
 76                                             border=2,
 77                                             margin=0,
 78                                             left_border=10)
 79
 80        our_login_id = _ba.get_public_login_id()
 81        if our_login_id is None:
 82            entries = []
 83        else:
 84            account_infos = _ba.get_v1_account_misc_read_val_2(
 85                'linkedAccounts2', [])
 86            entries = [{
 87                'name': ai['d'],
 88                'id': ai['id']
 89            } for ai in account_infos if ai['id'] != our_login_id]
 90
 91        # (avoid getting our selection stuck on an empty column widget)
 92        if not entries:
 93            ba.containerwidget(edit=self._scrollwidget, selectable=False)
 94        for i, entry in enumerate(entries):
 95            txt = ba.textwidget(parent=self._columnwidget,
 96                                selectable=True,
 97                                text=entry['name'],
 98                                size=(self._scroll_width - 30, 30),
 99                                autoselect=True,
100                                click_activate=True,
101                                on_activate_call=ba.Call(
102                                    self._on_entry_selected, entry))
103            ba.widget(edit=txt, left_widget=self._cancel_button)
104            if i == 0:
105                ba.widget(edit=txt, up_widget=self._cancel_button)
106
107    def _on_entry_selected(self, entry: dict[str, Any]) -> None:
108        ba.screenmessage(ba.Lstr(resource='pleaseWaitText',
109                                 fallback_resource='requestingText'),
110                         color=(0, 1, 0))
111        _ba.add_transaction({
112            'type': 'ACCOUNT_UNLINK_REQUEST',
113            'accountID': entry['id'],
114            'expire_time': time.time() + 5
115        })
116        _ba.run_transactions()
117        ba.containerwidget(edit=self._root_widget,
118                           transition=self._transition_out)
119
120    def _cancel(self) -> None:
121        ba.containerwidget(edit=self._root_widget,
122                           transition=self._transition_out)
class AccountUnlinkWindow(ba.ui.Window):
 18class AccountUnlinkWindow(ba.Window):
 19    """A window to kick off account unlinks."""
 20
 21    def __init__(self, origin_widget: ba.Widget | None = None):
 22        scale_origin: tuple[float, float] | None
 23        if origin_widget is not None:
 24            self._transition_out = 'out_scale'
 25            scale_origin = origin_widget.get_screen_space_center()
 26            transition = 'in_scale'
 27        else:
 28            self._transition_out = 'out_right'
 29            scale_origin = None
 30            transition = 'in_right'
 31        bg_color = (0.4, 0.4, 0.5)
 32        self._width = 540
 33        self._height = 350
 34        self._scroll_width = 400
 35        self._scroll_height = 200
 36        uiscale = ba.app.ui.uiscale
 37        base_scale = (2.0 if uiscale is ba.UIScale.SMALL else
 38                      1.6 if uiscale is ba.UIScale.MEDIUM else 1.1)
 39        super().__init__(root_widget=ba.containerwidget(
 40            size=(self._width, self._height),
 41            transition=transition,
 42            scale=base_scale,
 43            scale_origin_stack_offset=scale_origin,
 44            stack_offset=(0, -10) if uiscale is ba.UIScale.SMALL else (0, 0)))
 45        self._cancel_button = ba.buttonwidget(parent=self._root_widget,
 46                                              position=(30, self._height - 50),
 47                                              size=(50, 50),
 48                                              scale=0.7,
 49                                              label='',
 50                                              color=bg_color,
 51                                              on_activate_call=self._cancel,
 52                                              autoselect=True,
 53                                              icon=ba.gettexture('crossOut'),
 54                                              iconscale=1.2)
 55        ba.textwidget(
 56            parent=self._root_widget,
 57            position=(self._width * 0.5, self._height * 0.88),
 58            size=(0, 0),
 59            text=ba.Lstr(
 60                resource='accountSettingsWindow.unlinkAccountsInstructionsText'
 61            ),
 62            maxwidth=self._width * 0.7,
 63            color=ba.app.ui.infotextcolor,
 64            h_align='center',
 65            v_align='center')
 66        ba.containerwidget(edit=self._root_widget,
 67                           cancel_button=self._cancel_button)
 68
 69        self._scrollwidget = ba.scrollwidget(
 70            parent=self._root_widget,
 71            highlight=False,
 72            position=((self._width - self._scroll_width) * 0.5,
 73                      self._height - 85 - self._scroll_height),
 74            size=(self._scroll_width, self._scroll_height))
 75        ba.containerwidget(edit=self._scrollwidget, claims_left_right=True)
 76        self._columnwidget = ba.columnwidget(parent=self._scrollwidget,
 77                                             border=2,
 78                                             margin=0,
 79                                             left_border=10)
 80
 81        our_login_id = _ba.get_public_login_id()
 82        if our_login_id is None:
 83            entries = []
 84        else:
 85            account_infos = _ba.get_v1_account_misc_read_val_2(
 86                'linkedAccounts2', [])
 87            entries = [{
 88                'name': ai['d'],
 89                'id': ai['id']
 90            } for ai in account_infos if ai['id'] != our_login_id]
 91
 92        # (avoid getting our selection stuck on an empty column widget)
 93        if not entries:
 94            ba.containerwidget(edit=self._scrollwidget, selectable=False)
 95        for i, entry in enumerate(entries):
 96            txt = ba.textwidget(parent=self._columnwidget,
 97                                selectable=True,
 98                                text=entry['name'],
 99                                size=(self._scroll_width - 30, 30),
100                                autoselect=True,
101                                click_activate=True,
102                                on_activate_call=ba.Call(
103                                    self._on_entry_selected, entry))
104            ba.widget(edit=txt, left_widget=self._cancel_button)
105            if i == 0:
106                ba.widget(edit=txt, up_widget=self._cancel_button)
107
108    def _on_entry_selected(self, entry: dict[str, Any]) -> None:
109        ba.screenmessage(ba.Lstr(resource='pleaseWaitText',
110                                 fallback_resource='requestingText'),
111                         color=(0, 1, 0))
112        _ba.add_transaction({
113            'type': 'ACCOUNT_UNLINK_REQUEST',
114            'accountID': entry['id'],
115            'expire_time': time.time() + 5
116        })
117        _ba.run_transactions()
118        ba.containerwidget(edit=self._root_widget,
119                           transition=self._transition_out)
120
121    def _cancel(self) -> None:
122        ba.containerwidget(edit=self._root_widget,
123                           transition=self._transition_out)

A window to kick off account unlinks.

AccountUnlinkWindow(origin_widget: _ba.Widget | None = None)
 21    def __init__(self, origin_widget: ba.Widget | None = None):
 22        scale_origin: tuple[float, float] | None
 23        if origin_widget is not None:
 24            self._transition_out = 'out_scale'
 25            scale_origin = origin_widget.get_screen_space_center()
 26            transition = 'in_scale'
 27        else:
 28            self._transition_out = 'out_right'
 29            scale_origin = None
 30            transition = 'in_right'
 31        bg_color = (0.4, 0.4, 0.5)
 32        self._width = 540
 33        self._height = 350
 34        self._scroll_width = 400
 35        self._scroll_height = 200
 36        uiscale = ba.app.ui.uiscale
 37        base_scale = (2.0 if uiscale is ba.UIScale.SMALL else
 38                      1.6 if uiscale is ba.UIScale.MEDIUM else 1.1)
 39        super().__init__(root_widget=ba.containerwidget(
 40            size=(self._width, self._height),
 41            transition=transition,
 42            scale=base_scale,
 43            scale_origin_stack_offset=scale_origin,
 44            stack_offset=(0, -10) if uiscale is ba.UIScale.SMALL else (0, 0)))
 45        self._cancel_button = ba.buttonwidget(parent=self._root_widget,
 46                                              position=(30, self._height - 50),
 47                                              size=(50, 50),
 48                                              scale=0.7,
 49                                              label='',
 50                                              color=bg_color,
 51                                              on_activate_call=self._cancel,
 52                                              autoselect=True,
 53                                              icon=ba.gettexture('crossOut'),
 54                                              iconscale=1.2)
 55        ba.textwidget(
 56            parent=self._root_widget,
 57            position=(self._width * 0.5, self._height * 0.88),
 58            size=(0, 0),
 59            text=ba.Lstr(
 60                resource='accountSettingsWindow.unlinkAccountsInstructionsText'
 61            ),
 62            maxwidth=self._width * 0.7,
 63            color=ba.app.ui.infotextcolor,
 64            h_align='center',
 65            v_align='center')
 66        ba.containerwidget(edit=self._root_widget,
 67                           cancel_button=self._cancel_button)
 68
 69        self._scrollwidget = ba.scrollwidget(
 70            parent=self._root_widget,
 71            highlight=False,
 72            position=((self._width - self._scroll_width) * 0.5,
 73                      self._height - 85 - self._scroll_height),
 74            size=(self._scroll_width, self._scroll_height))
 75        ba.containerwidget(edit=self._scrollwidget, claims_left_right=True)
 76        self._columnwidget = ba.columnwidget(parent=self._scrollwidget,
 77                                             border=2,
 78                                             margin=0,
 79                                             left_border=10)
 80
 81        our_login_id = _ba.get_public_login_id()
 82        if our_login_id is None:
 83            entries = []
 84        else:
 85            account_infos = _ba.get_v1_account_misc_read_val_2(
 86                'linkedAccounts2', [])
 87            entries = [{
 88                'name': ai['d'],
 89                'id': ai['id']
 90            } for ai in account_infos if ai['id'] != our_login_id]
 91
 92        # (avoid getting our selection stuck on an empty column widget)
 93        if not entries:
 94            ba.containerwidget(edit=self._scrollwidget, selectable=False)
 95        for i, entry in enumerate(entries):
 96            txt = ba.textwidget(parent=self._columnwidget,
 97                                selectable=True,
 98                                text=entry['name'],
 99                                size=(self._scroll_width - 30, 30),
100                                autoselect=True,
101                                click_activate=True,
102                                on_activate_call=ba.Call(
103                                    self._on_entry_selected, entry))
104            ba.widget(edit=txt, left_widget=self._cancel_button)
105            if i == 0:
106                ba.widget(edit=txt, up_widget=self._cancel_button)
Inherited Members
ba.ui.Window
get_root_widget