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

Window for conveniently testing various settings.

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