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.internal
 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 = (
 37            2.0
 38            if uiscale is ba.UIScale.SMALL
 39            else 1.6
 40            if uiscale is ba.UIScale.MEDIUM
 41            else 1.1
 42        )
 43        super().__init__(
 44            root_widget=ba.containerwidget(
 45                size=(self._width, self._height),
 46                transition=transition,
 47                scale=base_scale,
 48                scale_origin_stack_offset=scale_origin,
 49                stack_offset=(0, -10)
 50                if uiscale is ba.UIScale.SMALL
 51                else (0, 0),
 52            )
 53        )
 54        self._cancel_button = ba.buttonwidget(
 55            parent=self._root_widget,
 56            position=(30, self._height - 50),
 57            size=(50, 50),
 58            scale=0.7,
 59            label='',
 60            color=bg_color,
 61            on_activate_call=self._cancel,
 62            autoselect=True,
 63            icon=ba.gettexture('crossOut'),
 64            iconscale=1.2,
 65        )
 66        ba.textwidget(
 67            parent=self._root_widget,
 68            position=(self._width * 0.5, self._height * 0.88),
 69            size=(0, 0),
 70            text=ba.Lstr(
 71                resource='accountSettingsWindow.unlinkAccountsInstructionsText'
 72            ),
 73            maxwidth=self._width * 0.7,
 74            color=ba.app.ui.infotextcolor,
 75            h_align='center',
 76            v_align='center',
 77        )
 78        ba.containerwidget(
 79            edit=self._root_widget, cancel_button=self._cancel_button
 80        )
 81
 82        self._scrollwidget = ba.scrollwidget(
 83            parent=self._root_widget,
 84            highlight=False,
 85            position=(
 86                (self._width - self._scroll_width) * 0.5,
 87                self._height - 85 - self._scroll_height,
 88            ),
 89            size=(self._scroll_width, self._scroll_height),
 90        )
 91        ba.containerwidget(edit=self._scrollwidget, claims_left_right=True)
 92        self._columnwidget = ba.columnwidget(
 93            parent=self._scrollwidget, border=2, margin=0, left_border=10
 94        )
 95
 96        our_login_id = ba.internal.get_public_login_id()
 97        if our_login_id is None:
 98            entries = []
 99        else:
100            account_infos = ba.internal.get_v1_account_misc_read_val_2(
101                'linkedAccounts2', []
102            )
103            entries = [
104                {'name': ai['d'], 'id': ai['id']}
105                for ai in account_infos
106                if ai['id'] != our_login_id
107            ]
108
109        # (avoid getting our selection stuck on an empty column widget)
110        if not entries:
111            ba.containerwidget(edit=self._scrollwidget, selectable=False)
112        for i, entry in enumerate(entries):
113            txt = ba.textwidget(
114                parent=self._columnwidget,
115                selectable=True,
116                text=entry['name'],
117                size=(self._scroll_width - 30, 30),
118                autoselect=True,
119                click_activate=True,
120                on_activate_call=ba.Call(self._on_entry_selected, entry),
121            )
122            ba.widget(edit=txt, left_widget=self._cancel_button)
123            if i == 0:
124                ba.widget(edit=txt, up_widget=self._cancel_button)
125
126    def _on_entry_selected(self, entry: dict[str, Any]) -> None:
127        ba.screenmessage(
128            ba.Lstr(
129                resource='pleaseWaitText', fallback_resource='requestingText'
130            ),
131            color=(0, 1, 0),
132        )
133        ba.internal.add_transaction(
134            {
135                'type': 'ACCOUNT_UNLINK_REQUEST',
136                'accountID': entry['id'],
137                'expire_time': time.time() + 5,
138            }
139        )
140        ba.internal.run_transactions()
141        ba.containerwidget(
142            edit=self._root_widget, transition=self._transition_out
143        )
144
145    def _cancel(self) -> None:
146        ba.containerwidget(
147            edit=self._root_widget, transition=self._transition_out
148        )
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 = (
 38            2.0
 39            if uiscale is ba.UIScale.SMALL
 40            else 1.6
 41            if uiscale is ba.UIScale.MEDIUM
 42            else 1.1
 43        )
 44        super().__init__(
 45            root_widget=ba.containerwidget(
 46                size=(self._width, self._height),
 47                transition=transition,
 48                scale=base_scale,
 49                scale_origin_stack_offset=scale_origin,
 50                stack_offset=(0, -10)
 51                if uiscale is ba.UIScale.SMALL
 52                else (0, 0),
 53            )
 54        )
 55        self._cancel_button = ba.buttonwidget(
 56            parent=self._root_widget,
 57            position=(30, self._height - 50),
 58            size=(50, 50),
 59            scale=0.7,
 60            label='',
 61            color=bg_color,
 62            on_activate_call=self._cancel,
 63            autoselect=True,
 64            icon=ba.gettexture('crossOut'),
 65            iconscale=1.2,
 66        )
 67        ba.textwidget(
 68            parent=self._root_widget,
 69            position=(self._width * 0.5, self._height * 0.88),
 70            size=(0, 0),
 71            text=ba.Lstr(
 72                resource='accountSettingsWindow.unlinkAccountsInstructionsText'
 73            ),
 74            maxwidth=self._width * 0.7,
 75            color=ba.app.ui.infotextcolor,
 76            h_align='center',
 77            v_align='center',
 78        )
 79        ba.containerwidget(
 80            edit=self._root_widget, cancel_button=self._cancel_button
 81        )
 82
 83        self._scrollwidget = ba.scrollwidget(
 84            parent=self._root_widget,
 85            highlight=False,
 86            position=(
 87                (self._width - self._scroll_width) * 0.5,
 88                self._height - 85 - self._scroll_height,
 89            ),
 90            size=(self._scroll_width, self._scroll_height),
 91        )
 92        ba.containerwidget(edit=self._scrollwidget, claims_left_right=True)
 93        self._columnwidget = ba.columnwidget(
 94            parent=self._scrollwidget, border=2, margin=0, left_border=10
 95        )
 96
 97        our_login_id = ba.internal.get_public_login_id()
 98        if our_login_id is None:
 99            entries = []
100        else:
101            account_infos = ba.internal.get_v1_account_misc_read_val_2(
102                'linkedAccounts2', []
103            )
104            entries = [
105                {'name': ai['d'], 'id': ai['id']}
106                for ai in account_infos
107                if ai['id'] != our_login_id
108            ]
109
110        # (avoid getting our selection stuck on an empty column widget)
111        if not entries:
112            ba.containerwidget(edit=self._scrollwidget, selectable=False)
113        for i, entry in enumerate(entries):
114            txt = ba.textwidget(
115                parent=self._columnwidget,
116                selectable=True,
117                text=entry['name'],
118                size=(self._scroll_width - 30, 30),
119                autoselect=True,
120                click_activate=True,
121                on_activate_call=ba.Call(self._on_entry_selected, entry),
122            )
123            ba.widget(edit=txt, left_widget=self._cancel_button)
124            if i == 0:
125                ba.widget(edit=txt, up_widget=self._cancel_button)
126
127    def _on_entry_selected(self, entry: dict[str, Any]) -> None:
128        ba.screenmessage(
129            ba.Lstr(
130                resource='pleaseWaitText', fallback_resource='requestingText'
131            ),
132            color=(0, 1, 0),
133        )
134        ba.internal.add_transaction(
135            {
136                'type': 'ACCOUNT_UNLINK_REQUEST',
137                'accountID': entry['id'],
138                'expire_time': time.time() + 5,
139            }
140        )
141        ba.internal.run_transactions()
142        ba.containerwidget(
143            edit=self._root_widget, transition=self._transition_out
144        )
145
146    def _cancel(self) -> None:
147        ba.containerwidget(
148            edit=self._root_widget, transition=self._transition_out
149        )

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 = (
 38            2.0
 39            if uiscale is ba.UIScale.SMALL
 40            else 1.6
 41            if uiscale is ba.UIScale.MEDIUM
 42            else 1.1
 43        )
 44        super().__init__(
 45            root_widget=ba.containerwidget(
 46                size=(self._width, self._height),
 47                transition=transition,
 48                scale=base_scale,
 49                scale_origin_stack_offset=scale_origin,
 50                stack_offset=(0, -10)
 51                if uiscale is ba.UIScale.SMALL
 52                else (0, 0),
 53            )
 54        )
 55        self._cancel_button = ba.buttonwidget(
 56            parent=self._root_widget,
 57            position=(30, self._height - 50),
 58            size=(50, 50),
 59            scale=0.7,
 60            label='',
 61            color=bg_color,
 62            on_activate_call=self._cancel,
 63            autoselect=True,
 64            icon=ba.gettexture('crossOut'),
 65            iconscale=1.2,
 66        )
 67        ba.textwidget(
 68            parent=self._root_widget,
 69            position=(self._width * 0.5, self._height * 0.88),
 70            size=(0, 0),
 71            text=ba.Lstr(
 72                resource='accountSettingsWindow.unlinkAccountsInstructionsText'
 73            ),
 74            maxwidth=self._width * 0.7,
 75            color=ba.app.ui.infotextcolor,
 76            h_align='center',
 77            v_align='center',
 78        )
 79        ba.containerwidget(
 80            edit=self._root_widget, cancel_button=self._cancel_button
 81        )
 82
 83        self._scrollwidget = ba.scrollwidget(
 84            parent=self._root_widget,
 85            highlight=False,
 86            position=(
 87                (self._width - self._scroll_width) * 0.5,
 88                self._height - 85 - self._scroll_height,
 89            ),
 90            size=(self._scroll_width, self._scroll_height),
 91        )
 92        ba.containerwidget(edit=self._scrollwidget, claims_left_right=True)
 93        self._columnwidget = ba.columnwidget(
 94            parent=self._scrollwidget, border=2, margin=0, left_border=10
 95        )
 96
 97        our_login_id = ba.internal.get_public_login_id()
 98        if our_login_id is None:
 99            entries = []
100        else:
101            account_infos = ba.internal.get_v1_account_misc_read_val_2(
102                'linkedAccounts2', []
103            )
104            entries = [
105                {'name': ai['d'], 'id': ai['id']}
106                for ai in account_infos
107                if ai['id'] != our_login_id
108            ]
109
110        # (avoid getting our selection stuck on an empty column widget)
111        if not entries:
112            ba.containerwidget(edit=self._scrollwidget, selectable=False)
113        for i, entry in enumerate(entries):
114            txt = ba.textwidget(
115                parent=self._columnwidget,
116                selectable=True,
117                text=entry['name'],
118                size=(self._scroll_width - 30, 30),
119                autoselect=True,
120                click_activate=True,
121                on_activate_call=ba.Call(self._on_entry_selected, entry),
122            )
123            ba.widget(edit=txt, left_widget=self._cancel_button)
124            if i == 0:
125                ba.widget(edit=txt, up_widget=self._cancel_button)
Inherited Members
ba.ui.Window
get_root_widget