bauiv1lib.settings.testing

Provides UI for test settings.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""Provides UI for test settings."""
  4
  5from __future__ import annotations
  6
  7import copy
  8from typing import TYPE_CHECKING
  9
 10import bauiv1 as bui
 11
 12if TYPE_CHECKING:
 13    from typing import Any, Callable
 14
 15
 16class TestingWindow(bui.MainWindow):
 17    """Window for conveniently testing various settings."""
 18
 19    def __init__(
 20        self,
 21        title: bui.Lstr,
 22        entries: list[dict[str, Any]],
 23        transition: str | None = 'in_right',
 24        origin_widget: bui.Widget | None = None,
 25    ):
 26        assert bui.app.classic is not None
 27        uiscale = bui.app.ui_v1.uiscale
 28        self._width = 700 if uiscale is bui.UIScale.SMALL else 600
 29        self._height = 324 if uiscale is bui.UIScale.SMALL else 400
 30        self._entries = copy.deepcopy(entries)
 31        super().__init__(
 32            root_widget=bui.containerwidget(
 33                size=(self._width, self._height),
 34                scale=(
 35                    2.27
 36                    if uiscale is bui.UIScale.SMALL
 37                    else 1.2 if uiscale is bui.UIScale.MEDIUM else 1.0
 38                ),
 39                stack_offset=(
 40                    (0, -20) if uiscale is bui.UIScale.SMALL else (0, 0)
 41                ),
 42                toolbar_visibility=(
 43                    'menu_minimal'
 44                    if uiscale is bui.UIScale.SMALL
 45                    else 'menu_full'
 46                ),
 47            ),
 48            transition=transition,
 49            origin_widget=origin_widget,
 50        )
 51
 52        if uiscale is bui.UIScale.SMALL:
 53            self._back_button = bui.get_special_widget('back_button')
 54            bui.containerwidget(
 55                edit=self._root_widget, on_cancel_call=self.main_window_back
 56            )
 57        else:
 58            self._back_button = btn = bui.buttonwidget(
 59                parent=self._root_widget,
 60                autoselect=True,
 61                position=(65, self._height - 59),
 62                size=(130, 60),
 63                scale=0.8,
 64                text_scale=1.2,
 65                label=bui.Lstr(resource='backText'),
 66                button_type='back',
 67                on_activate_call=self.main_window_back,
 68            )
 69            bui.buttonwidget(
 70                edit=self._back_button,
 71                button_type='backSmall',
 72                size=(60, 60),
 73                label=bui.charstr(bui.SpecialChar.BACK),
 74            )
 75            bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 76
 77        bui.textwidget(
 78            parent=self._root_widget,
 79            position=(
 80                self._width * 0.5,
 81                self._height - (42 if uiscale is bui.UIScale.SMALL else 35),
 82            ),
 83            size=(0, 0),
 84            color=bui.app.ui_v1.title_color,
 85            h_align='center',
 86            v_align='center',
 87            maxwidth=245,
 88            text=title,
 89        )
 90
 91        bui.textwidget(
 92            parent=self._root_widget,
 93            position=(
 94                self._width * 0.5,
 95                self._height - (80 if uiscale is bui.UIScale.SMALL else 80),
 96            ),
 97            size=(0, 0),
 98            color=bui.app.ui_v1.infotextcolor,
 99            h_align='center',
100            v_align='center',
101            maxwidth=self._width * 0.75,
102            text=bui.Lstr(resource='settingsWindowAdvanced.forTestingText'),
103        )
104        self._scroll_width = self._width - 130
105        self._scroll_height = self._height - 140
106        self._scrollwidget = bui.scrollwidget(
107            parent=self._root_widget,
108            size=(self._scroll_width, self._scroll_height),
109            highlight=False,
110            position=((self._width - self._scroll_width) * 0.5, 40),
111        )
112        bui.containerwidget(edit=self._scrollwidget, claims_left_right=True)
113
114        self._spacing = 50
115
116        self._sub_width = self._scroll_width * 0.95
117        self._sub_height = 50 + len(self._entries) * self._spacing + 60
118        self._subcontainer = bui.containerwidget(
119            parent=self._scrollwidget,
120            size=(self._sub_width, self._sub_height),
121            background=False,
122        )
123
124        h = 230
125        v = self._sub_height - 48
126
127        for i, entry in enumerate(self._entries):
128            entry_name = entry['name']
129
130            # If we haven't yet, record the default value for this name so
131            # we can reset if we want..
132            if entry_name not in bui.app.classic.value_test_defaults:
133                bui.app.classic.value_test_defaults[entry_name] = (
134                    bui.app.classic.value_test(entry_name)
135                )
136
137            bui.textwidget(
138                parent=self._subcontainer,
139                position=(h, v),
140                size=(0, 0),
141                h_align='right',
142                v_align='center',
143                maxwidth=200,
144                text=entry['label'],
145            )
146            btn = bui.buttonwidget(
147                parent=self._subcontainer,
148                position=(h + 20, v - 19),
149                size=(40, 40),
150                autoselect=True,
151                repeat=True,
152                left_widget=self._back_button,
153                button_type='square',
154                label='-',
155                on_activate_call=bui.Call(self._on_minus_press, entry['name']),
156            )
157            if i == 0:
158                bui.widget(edit=btn, up_widget=self._back_button)
159            entry['widget'] = bui.textwidget(
160                parent=self._subcontainer,
161                position=(h + 100, v),
162                size=(0, 0),
163                h_align='center',
164                v_align='center',
165                maxwidth=60,
166                text=f'{bui.app.classic.value_test(entry_name):.4g}',
167            )
168            btn = bui.buttonwidget(
169                parent=self._subcontainer,
170                position=(h + 140, v - 19),
171                size=(40, 40),
172                autoselect=True,
173                repeat=True,
174                button_type='square',
175                label='+',
176                on_activate_call=bui.Call(self._on_plus_press, entry['name']),
177            )
178            if i == 0:
179                bui.widget(edit=btn, up_widget=self._back_button)
180            v -= self._spacing
181        v -= 35
182        bui.buttonwidget(
183            parent=self._subcontainer,
184            autoselect=True,
185            size=(200, 50),
186            position=(self._sub_width * 0.5 - 100, v),
187            label=bui.Lstr(resource='settingsWindowAdvanced.resetText'),
188            right_widget=btn,
189            on_activate_call=self._on_reset_press,
190        )
191
192    def _get_entry(self, name: str) -> dict[str, Any]:
193        for entry in self._entries:
194            if entry['name'] == name:
195                return entry
196        raise bui.NotFoundError(f'Entry not found: {name}')
197
198    def _on_reset_press(self) -> None:
199        assert bui.app.classic is not None
200        for entry in self._entries:
201            bui.app.classic.value_test(
202                entry['name'],
203                absolute=bui.app.classic.value_test_defaults[entry['name']],
204            )
205            bui.textwidget(
206                edit=entry['widget'],
207                text=f'{bui.app.classic.value_test(entry['name']):.4g}',
208            )
209
210    def _on_minus_press(self, entry_name: str) -> None:
211        assert bui.app.classic is not None
212        entry = self._get_entry(entry_name)
213        bui.app.classic.value_test(entry['name'], change=-entry['increment'])
214        # pylint: disable=consider-using-f-string
215        bui.textwidget(
216            edit=entry['widget'],
217            text='%.4g' % bui.app.classic.value_test(entry['name']),
218        )
219
220    def _on_plus_press(self, entry_name: str) -> None:
221        assert bui.app.classic is not None
222        entry = self._get_entry(entry_name)
223        bui.app.classic.value_test(entry['name'], change=entry['increment'])
224        # pylint: disable=consider-using-f-string
225        bui.textwidget(
226            edit=entry['widget'],
227            text='%.4g' % bui.app.classic.value_test(entry['name']),
228        )
class TestingWindow(bauiv1._uitypes.MainWindow):
 17class TestingWindow(bui.MainWindow):
 18    """Window for conveniently testing various settings."""
 19
 20    def __init__(
 21        self,
 22        title: bui.Lstr,
 23        entries: list[dict[str, Any]],
 24        transition: str | None = 'in_right',
 25        origin_widget: bui.Widget | None = None,
 26    ):
 27        assert bui.app.classic is not None
 28        uiscale = bui.app.ui_v1.uiscale
 29        self._width = 700 if uiscale is bui.UIScale.SMALL else 600
 30        self._height = 324 if uiscale is bui.UIScale.SMALL else 400
 31        self._entries = copy.deepcopy(entries)
 32        super().__init__(
 33            root_widget=bui.containerwidget(
 34                size=(self._width, self._height),
 35                scale=(
 36                    2.27
 37                    if uiscale is bui.UIScale.SMALL
 38                    else 1.2 if uiscale is bui.UIScale.MEDIUM else 1.0
 39                ),
 40                stack_offset=(
 41                    (0, -20) if uiscale is bui.UIScale.SMALL else (0, 0)
 42                ),
 43                toolbar_visibility=(
 44                    'menu_minimal'
 45                    if uiscale is bui.UIScale.SMALL
 46                    else 'menu_full'
 47                ),
 48            ),
 49            transition=transition,
 50            origin_widget=origin_widget,
 51        )
 52
 53        if uiscale is bui.UIScale.SMALL:
 54            self._back_button = bui.get_special_widget('back_button')
 55            bui.containerwidget(
 56                edit=self._root_widget, on_cancel_call=self.main_window_back
 57            )
 58        else:
 59            self._back_button = btn = bui.buttonwidget(
 60                parent=self._root_widget,
 61                autoselect=True,
 62                position=(65, self._height - 59),
 63                size=(130, 60),
 64                scale=0.8,
 65                text_scale=1.2,
 66                label=bui.Lstr(resource='backText'),
 67                button_type='back',
 68                on_activate_call=self.main_window_back,
 69            )
 70            bui.buttonwidget(
 71                edit=self._back_button,
 72                button_type='backSmall',
 73                size=(60, 60),
 74                label=bui.charstr(bui.SpecialChar.BACK),
 75            )
 76            bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 77
 78        bui.textwidget(
 79            parent=self._root_widget,
 80            position=(
 81                self._width * 0.5,
 82                self._height - (42 if uiscale is bui.UIScale.SMALL else 35),
 83            ),
 84            size=(0, 0),
 85            color=bui.app.ui_v1.title_color,
 86            h_align='center',
 87            v_align='center',
 88            maxwidth=245,
 89            text=title,
 90        )
 91
 92        bui.textwidget(
 93            parent=self._root_widget,
 94            position=(
 95                self._width * 0.5,
 96                self._height - (80 if uiscale is bui.UIScale.SMALL else 80),
 97            ),
 98            size=(0, 0),
 99            color=bui.app.ui_v1.infotextcolor,
100            h_align='center',
101            v_align='center',
102            maxwidth=self._width * 0.75,
103            text=bui.Lstr(resource='settingsWindowAdvanced.forTestingText'),
104        )
105        self._scroll_width = self._width - 130
106        self._scroll_height = self._height - 140
107        self._scrollwidget = bui.scrollwidget(
108            parent=self._root_widget,
109            size=(self._scroll_width, self._scroll_height),
110            highlight=False,
111            position=((self._width - self._scroll_width) * 0.5, 40),
112        )
113        bui.containerwidget(edit=self._scrollwidget, claims_left_right=True)
114
115        self._spacing = 50
116
117        self._sub_width = self._scroll_width * 0.95
118        self._sub_height = 50 + len(self._entries) * self._spacing + 60
119        self._subcontainer = bui.containerwidget(
120            parent=self._scrollwidget,
121            size=(self._sub_width, self._sub_height),
122            background=False,
123        )
124
125        h = 230
126        v = self._sub_height - 48
127
128        for i, entry in enumerate(self._entries):
129            entry_name = entry['name']
130
131            # If we haven't yet, record the default value for this name so
132            # we can reset if we want..
133            if entry_name not in bui.app.classic.value_test_defaults:
134                bui.app.classic.value_test_defaults[entry_name] = (
135                    bui.app.classic.value_test(entry_name)
136                )
137
138            bui.textwidget(
139                parent=self._subcontainer,
140                position=(h, v),
141                size=(0, 0),
142                h_align='right',
143                v_align='center',
144                maxwidth=200,
145                text=entry['label'],
146            )
147            btn = bui.buttonwidget(
148                parent=self._subcontainer,
149                position=(h + 20, v - 19),
150                size=(40, 40),
151                autoselect=True,
152                repeat=True,
153                left_widget=self._back_button,
154                button_type='square',
155                label='-',
156                on_activate_call=bui.Call(self._on_minus_press, entry['name']),
157            )
158            if i == 0:
159                bui.widget(edit=btn, up_widget=self._back_button)
160            entry['widget'] = bui.textwidget(
161                parent=self._subcontainer,
162                position=(h + 100, v),
163                size=(0, 0),
164                h_align='center',
165                v_align='center',
166                maxwidth=60,
167                text=f'{bui.app.classic.value_test(entry_name):.4g}',
168            )
169            btn = bui.buttonwidget(
170                parent=self._subcontainer,
171                position=(h + 140, v - 19),
172                size=(40, 40),
173                autoselect=True,
174                repeat=True,
175                button_type='square',
176                label='+',
177                on_activate_call=bui.Call(self._on_plus_press, entry['name']),
178            )
179            if i == 0:
180                bui.widget(edit=btn, up_widget=self._back_button)
181            v -= self._spacing
182        v -= 35
183        bui.buttonwidget(
184            parent=self._subcontainer,
185            autoselect=True,
186            size=(200, 50),
187            position=(self._sub_width * 0.5 - 100, v),
188            label=bui.Lstr(resource='settingsWindowAdvanced.resetText'),
189            right_widget=btn,
190            on_activate_call=self._on_reset_press,
191        )
192
193    def _get_entry(self, name: str) -> dict[str, Any]:
194        for entry in self._entries:
195            if entry['name'] == name:
196                return entry
197        raise bui.NotFoundError(f'Entry not found: {name}')
198
199    def _on_reset_press(self) -> None:
200        assert bui.app.classic is not None
201        for entry in self._entries:
202            bui.app.classic.value_test(
203                entry['name'],
204                absolute=bui.app.classic.value_test_defaults[entry['name']],
205            )
206            bui.textwidget(
207                edit=entry['widget'],
208                text=f'{bui.app.classic.value_test(entry['name']):.4g}',
209            )
210
211    def _on_minus_press(self, entry_name: str) -> None:
212        assert bui.app.classic is not None
213        entry = self._get_entry(entry_name)
214        bui.app.classic.value_test(entry['name'], change=-entry['increment'])
215        # pylint: disable=consider-using-f-string
216        bui.textwidget(
217            edit=entry['widget'],
218            text='%.4g' % bui.app.classic.value_test(entry['name']),
219        )
220
221    def _on_plus_press(self, entry_name: str) -> None:
222        assert bui.app.classic is not None
223        entry = self._get_entry(entry_name)
224        bui.app.classic.value_test(entry['name'], change=entry['increment'])
225        # pylint: disable=consider-using-f-string
226        bui.textwidget(
227            edit=entry['widget'],
228            text='%.4g' % bui.app.classic.value_test(entry['name']),
229        )

Window for conveniently testing various settings.

TestingWindow( title: babase.Lstr, entries: list[dict[str, typing.Any]], transition: str | None = 'in_right', origin_widget: _bauiv1.Widget | None = None)
 20    def __init__(
 21        self,
 22        title: bui.Lstr,
 23        entries: list[dict[str, Any]],
 24        transition: str | None = 'in_right',
 25        origin_widget: bui.Widget | None = None,
 26    ):
 27        assert bui.app.classic is not None
 28        uiscale = bui.app.ui_v1.uiscale
 29        self._width = 700 if uiscale is bui.UIScale.SMALL else 600
 30        self._height = 324 if uiscale is bui.UIScale.SMALL else 400
 31        self._entries = copy.deepcopy(entries)
 32        super().__init__(
 33            root_widget=bui.containerwidget(
 34                size=(self._width, self._height),
 35                scale=(
 36                    2.27
 37                    if uiscale is bui.UIScale.SMALL
 38                    else 1.2 if uiscale is bui.UIScale.MEDIUM else 1.0
 39                ),
 40                stack_offset=(
 41                    (0, -20) if uiscale is bui.UIScale.SMALL else (0, 0)
 42                ),
 43                toolbar_visibility=(
 44                    'menu_minimal'
 45                    if uiscale is bui.UIScale.SMALL
 46                    else 'menu_full'
 47                ),
 48            ),
 49            transition=transition,
 50            origin_widget=origin_widget,
 51        )
 52
 53        if uiscale is bui.UIScale.SMALL:
 54            self._back_button = bui.get_special_widget('back_button')
 55            bui.containerwidget(
 56                edit=self._root_widget, on_cancel_call=self.main_window_back
 57            )
 58        else:
 59            self._back_button = btn = bui.buttonwidget(
 60                parent=self._root_widget,
 61                autoselect=True,
 62                position=(65, self._height - 59),
 63                size=(130, 60),
 64                scale=0.8,
 65                text_scale=1.2,
 66                label=bui.Lstr(resource='backText'),
 67                button_type='back',
 68                on_activate_call=self.main_window_back,
 69            )
 70            bui.buttonwidget(
 71                edit=self._back_button,
 72                button_type='backSmall',
 73                size=(60, 60),
 74                label=bui.charstr(bui.SpecialChar.BACK),
 75            )
 76            bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 77
 78        bui.textwidget(
 79            parent=self._root_widget,
 80            position=(
 81                self._width * 0.5,
 82                self._height - (42 if uiscale is bui.UIScale.SMALL else 35),
 83            ),
 84            size=(0, 0),
 85            color=bui.app.ui_v1.title_color,
 86            h_align='center',
 87            v_align='center',
 88            maxwidth=245,
 89            text=title,
 90        )
 91
 92        bui.textwidget(
 93            parent=self._root_widget,
 94            position=(
 95                self._width * 0.5,
 96                self._height - (80 if uiscale is bui.UIScale.SMALL else 80),
 97            ),
 98            size=(0, 0),
 99            color=bui.app.ui_v1.infotextcolor,
100            h_align='center',
101            v_align='center',
102            maxwidth=self._width * 0.75,
103            text=bui.Lstr(resource='settingsWindowAdvanced.forTestingText'),
104        )
105        self._scroll_width = self._width - 130
106        self._scroll_height = self._height - 140
107        self._scrollwidget = bui.scrollwidget(
108            parent=self._root_widget,
109            size=(self._scroll_width, self._scroll_height),
110            highlight=False,
111            position=((self._width - self._scroll_width) * 0.5, 40),
112        )
113        bui.containerwidget(edit=self._scrollwidget, claims_left_right=True)
114
115        self._spacing = 50
116
117        self._sub_width = self._scroll_width * 0.95
118        self._sub_height = 50 + len(self._entries) * self._spacing + 60
119        self._subcontainer = bui.containerwidget(
120            parent=self._scrollwidget,
121            size=(self._sub_width, self._sub_height),
122            background=False,
123        )
124
125        h = 230
126        v = self._sub_height - 48
127
128        for i, entry in enumerate(self._entries):
129            entry_name = entry['name']
130
131            # If we haven't yet, record the default value for this name so
132            # we can reset if we want..
133            if entry_name not in bui.app.classic.value_test_defaults:
134                bui.app.classic.value_test_defaults[entry_name] = (
135                    bui.app.classic.value_test(entry_name)
136                )
137
138            bui.textwidget(
139                parent=self._subcontainer,
140                position=(h, v),
141                size=(0, 0),
142                h_align='right',
143                v_align='center',
144                maxwidth=200,
145                text=entry['label'],
146            )
147            btn = bui.buttonwidget(
148                parent=self._subcontainer,
149                position=(h + 20, v - 19),
150                size=(40, 40),
151                autoselect=True,
152                repeat=True,
153                left_widget=self._back_button,
154                button_type='square',
155                label='-',
156                on_activate_call=bui.Call(self._on_minus_press, entry['name']),
157            )
158            if i == 0:
159                bui.widget(edit=btn, up_widget=self._back_button)
160            entry['widget'] = bui.textwidget(
161                parent=self._subcontainer,
162                position=(h + 100, v),
163                size=(0, 0),
164                h_align='center',
165                v_align='center',
166                maxwidth=60,
167                text=f'{bui.app.classic.value_test(entry_name):.4g}',
168            )
169            btn = bui.buttonwidget(
170                parent=self._subcontainer,
171                position=(h + 140, v - 19),
172                size=(40, 40),
173                autoselect=True,
174                repeat=True,
175                button_type='square',
176                label='+',
177                on_activate_call=bui.Call(self._on_plus_press, entry['name']),
178            )
179            if i == 0:
180                bui.widget(edit=btn, up_widget=self._back_button)
181            v -= self._spacing
182        v -= 35
183        bui.buttonwidget(
184            parent=self._subcontainer,
185            autoselect=True,
186            size=(200, 50),
187            position=(self._sub_width * 0.5 - 100, v),
188            label=bui.Lstr(resource='settingsWindowAdvanced.resetText'),
189            right_widget=btn,
190            on_activate_call=self._on_reset_press,
191        )

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.

Inherited Members
bauiv1._uitypes.MainWindow
main_window_back_state
main_window_close
can_change_main_window
main_window_back
main_window_replace
on_main_window_close
get_main_window_state
bauiv1._uitypes.Window
get_root_widget