bauiv1lib.settings.benchmarks

UIs for debugging purposes.

  1# Released under the MIT License. See LICENSE for details.
  2#
  3"""UIs for debugging purposes."""
  4
  5from __future__ import annotations
  6
  7import logging
  8from typing import cast, override
  9
 10import bauiv1 as bui
 11import bascenev1 as bs
 12
 13
 14class BenchmarksAndStressTestsWindow(bui.MainWindow):
 15    """Window for launching benchmarks or stress tests."""
 16
 17    def __init__(
 18        self,
 19        transition: str | None = 'in_right',
 20        origin_widget: bui.Widget | None = None,
 21    ):
 22        # pylint: disable=too-many-statements
 23        # pylint: disable=cyclic-import
 24        from bauiv1lib import popup
 25
 26        uiscale = bui.app.ui_v1.uiscale
 27        self._width = width = 650 if uiscale is bui.UIScale.SMALL else 580
 28        self._height = height = (
 29            400
 30            if uiscale is bui.UIScale.SMALL
 31            else 420 if uiscale is bui.UIScale.MEDIUM else 520
 32        )
 33        yoffs = -30 if uiscale is bui.UIScale.SMALL else 0
 34
 35        self._scroll_width = self._width - 100
 36        self._scroll_height = self._height - (
 37            180 if uiscale is bui.UIScale.SMALL else 120
 38        )
 39
 40        self._sub_width = self._scroll_width * 0.95
 41        self._sub_height = 520
 42
 43        self._stress_test_game_type = 'Random'
 44        self._stress_test_playlist = '__default__'
 45        self._stress_test_player_count = 8
 46        self._stress_test_round_duration = 30
 47
 48        self._r = 'debugWindow'
 49        uiscale = bui.app.ui_v1.uiscale
 50        super().__init__(
 51            root_widget=bui.containerwidget(
 52                size=(width, height),
 53                scale=(
 54                    2.32
 55                    if uiscale is bui.UIScale.SMALL
 56                    else 1.55 if uiscale is bui.UIScale.MEDIUM else 1.0
 57                ),
 58                stack_offset=(
 59                    (0, -30) if uiscale is bui.UIScale.SMALL else (0, 0)
 60                ),
 61                toolbar_visibility=(
 62                    'menu_minimal'
 63                    if uiscale is bui.UIScale.SMALL
 64                    else 'menu_full'
 65                ),
 66            ),
 67            transition=transition,
 68            origin_widget=origin_widget,
 69        )
 70
 71        if bui.app.ui_v1.uiscale is bui.UIScale.SMALL:
 72            bui.containerwidget(
 73                edit=self._root_widget, on_cancel_call=self.main_window_back
 74            )
 75            self._done_button = bui.get_special_widget('back_button')
 76        else:
 77            self._done_button = btn = bui.buttonwidget(
 78                parent=self._root_widget,
 79                position=(40, height - 67 + yoffs),
 80                size=(120, 60),
 81                scale=0.8,
 82                autoselect=True,
 83                label=bui.Lstr(resource='doneText'),
 84                on_activate_call=self.main_window_back,
 85            )
 86            bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 87
 88        bui.textwidget(
 89            parent=self._root_widget,
 90            position=(0, height - 60 + yoffs),
 91            size=(width, 30),
 92            text=bui.Lstr(resource=f'{self._r}.titleText'),
 93            h_align='center',
 94            color=bui.app.ui_v1.title_color,
 95            v_align='center',
 96            maxwidth=260,
 97        )
 98
 99        self._scrollwidget = bui.scrollwidget(
100            parent=self._root_widget,
101            highlight=False,
102            size=(self._scroll_width, self._scroll_height),
103            position=(
104                (self._width - self._scroll_width) * 0.5,
105                (115 if uiscale is bui.UIScale.SMALL else 50) + yoffs,
106            ),
107        )
108        bui.containerwidget(edit=self._scrollwidget, claims_left_right=True)
109
110        self._subcontainer = bui.containerwidget(
111            parent=self._scrollwidget,
112            size=(self._sub_width, self._sub_height),
113            background=False,
114        )
115
116        v = self._sub_height - 70
117        button_width = 300
118        btn = bui.buttonwidget(
119            parent=self._subcontainer,
120            position=((self._sub_width - button_width) * 0.5, v),
121            size=(button_width, 60),
122            autoselect=True,
123            label=bui.Lstr(resource=f'{self._r}.runCPUBenchmarkText'),
124            on_activate_call=self._run_cpu_benchmark_pressed,
125        )
126        bui.widget(
127            edit=btn, up_widget=self._done_button, left_widget=self._done_button
128        )
129        v -= 60
130
131        bui.buttonwidget(
132            parent=self._subcontainer,
133            position=((self._sub_width - button_width) * 0.5, v),
134            size=(button_width, 60),
135            autoselect=True,
136            label=bui.Lstr(resource=f'{self._r}.runMediaReloadBenchmarkText'),
137            on_activate_call=self._run_media_reload_benchmark_pressed,
138        )
139        v -= 60
140
141        bui.textwidget(
142            parent=self._subcontainer,
143            position=(self._sub_width * 0.5, v + 22),
144            size=(0, 0),
145            text=bui.Lstr(resource=f'{self._r}.stressTestTitleText'),
146            maxwidth=200,
147            color=bui.app.ui_v1.heading_color,
148            scale=0.85,
149            h_align='center',
150            v_align='center',
151        )
152        v -= 45
153
154        x_offs = 165
155        bui.textwidget(
156            parent=self._subcontainer,
157            position=(x_offs - 10, v + 22),
158            size=(0, 0),
159            text=bui.Lstr(resource=f'{self._r}.stressTestPlaylistTypeText'),
160            maxwidth=130,
161            color=bui.app.ui_v1.heading_color,
162            scale=0.65,
163            h_align='right',
164            v_align='center',
165        )
166
167        popup.PopupMenu(
168            parent=self._subcontainer,
169            position=(x_offs, v),
170            width=150,
171            choices=['Random', 'Teams', 'Free-For-All'],
172            choices_display=[
173                bui.Lstr(resource=a)
174                for a in [
175                    'randomText',
176                    'playModes.teamsText',
177                    'playModes.freeForAllText',
178                ]
179            ],
180            current_choice='Auto',
181            on_value_change_call=self._stress_test_game_type_selected,
182        )
183
184        v -= 46
185        bui.textwidget(
186            parent=self._subcontainer,
187            position=(x_offs - 10, v + 22),
188            size=(0, 0),
189            text=bui.Lstr(resource=f'{self._r}.stressTestPlaylistNameText'),
190            maxwidth=130,
191            color=bui.app.ui_v1.heading_color,
192            scale=0.65,
193            h_align='right',
194            v_align='center',
195        )
196
197        self._stress_test_playlist_name_field = bui.textwidget(
198            parent=self._subcontainer,
199            position=(x_offs + 5, v - 5),
200            size=(250, 46),
201            text=self._stress_test_playlist,
202            h_align='left',
203            v_align='center',
204            autoselect=True,
205            color=(0.9, 0.9, 0.9, 1.0),
206            description=bui.Lstr(
207                resource=f'{self._r}.stressTestPlaylistDescriptionText'
208            ),
209            editable=True,
210            padding=4,
211        )
212        v -= 29
213        x_sub = 60
214
215        # Player count.
216        bui.textwidget(
217            parent=self._subcontainer,
218            position=(x_offs - 10, v),
219            size=(0, 0),
220            text=bui.Lstr(resource=f'{self._r}.stressTestPlayerCountText'),
221            color=(0.8, 0.8, 0.8, 1.0),
222            h_align='right',
223            v_align='center',
224            scale=0.65,
225            maxwidth=130,
226        )
227        self._stress_test_player_count_text = bui.textwidget(
228            parent=self._subcontainer,
229            position=(246 - x_sub, v - 14),
230            size=(60, 28),
231            editable=False,
232            color=(0.3, 1.0, 0.3, 1.0),
233            h_align='right',
234            v_align='center',
235            text=str(self._stress_test_player_count),
236            padding=2,
237        )
238        bui.buttonwidget(
239            parent=self._subcontainer,
240            position=(330 - x_sub, v - 11),
241            size=(28, 28),
242            label='-',
243            autoselect=True,
244            on_activate_call=bui.Call(self._stress_test_player_count_decrement),
245            repeat=True,
246            enable_sound=True,
247        )
248        bui.buttonwidget(
249            parent=self._subcontainer,
250            position=(380 - x_sub, v - 11),
251            size=(28, 28),
252            label='+',
253            autoselect=True,
254            on_activate_call=bui.Call(self._stress_test_player_count_increment),
255            repeat=True,
256            enable_sound=True,
257        )
258        v -= 42
259
260        # Round duration.
261        bui.textwidget(
262            parent=self._subcontainer,
263            position=(x_offs - 10, v),
264            size=(0, 0),
265            text=bui.Lstr(resource=f'{self._r}.stressTestRoundDurationText'),
266            color=(0.8, 0.8, 0.8, 1.0),
267            h_align='right',
268            v_align='center',
269            scale=0.65,
270            maxwidth=130,
271        )
272        self._stress_test_round_duration_text = bui.textwidget(
273            parent=self._subcontainer,
274            position=(246 - x_sub, v - 14),
275            size=(60, 28),
276            editable=False,
277            color=(0.3, 1.0, 0.3, 1.0),
278            h_align='right',
279            v_align='center',
280            text=str(self._stress_test_round_duration),
281            padding=2,
282        )
283        bui.buttonwidget(
284            parent=self._subcontainer,
285            position=(330 - x_sub, v - 11),
286            size=(28, 28),
287            label='-',
288            autoselect=True,
289            on_activate_call=bui.Call(
290                self._stress_test_round_duration_decrement
291            ),
292            repeat=True,
293            enable_sound=True,
294        )
295        bui.buttonwidget(
296            parent=self._subcontainer,
297            position=(380 - x_sub, v - 11),
298            size=(28, 28),
299            label='+',
300            autoselect=True,
301            on_activate_call=bui.Call(
302                self._stress_test_round_duration_increment
303            ),
304            repeat=True,
305            enable_sound=True,
306        )
307        v -= 82
308        btn = bui.buttonwidget(
309            parent=self._subcontainer,
310            position=((self._sub_width - button_width) * 0.5, v),
311            size=(button_width, 60),
312            autoselect=True,
313            label=bui.Lstr(resource=f'{self._r}.runStressTestText'),
314            on_activate_call=self._stress_test_pressed,
315        )
316        bui.widget(edit=btn, show_buffer_bottom=50)
317
318    @override
319    def get_main_window_state(self) -> bui.MainWindowState:
320        # Support recreating our window for back/refresh purposes.
321        cls = type(self)
322        return bui.BasicMainWindowState(
323            create_call=lambda transition, origin_widget: cls(
324                transition=transition, origin_widget=origin_widget
325            )
326        )
327
328    def _stress_test_player_count_decrement(self) -> None:
329        self._stress_test_player_count = max(
330            1, self._stress_test_player_count - 1
331        )
332        bui.textwidget(
333            edit=self._stress_test_player_count_text,
334            text=str(self._stress_test_player_count),
335        )
336
337    def _stress_test_player_count_increment(self) -> None:
338        self._stress_test_player_count = self._stress_test_player_count + 1
339        bui.textwidget(
340            edit=self._stress_test_player_count_text,
341            text=str(self._stress_test_player_count),
342        )
343
344    def _stress_test_round_duration_decrement(self) -> None:
345        self._stress_test_round_duration = max(
346            10, self._stress_test_round_duration - 10
347        )
348        bui.textwidget(
349            edit=self._stress_test_round_duration_text,
350            text=str(self._stress_test_round_duration),
351        )
352
353    def _stress_test_round_duration_increment(self) -> None:
354        self._stress_test_round_duration = self._stress_test_round_duration + 10
355        bui.textwidget(
356            edit=self._stress_test_round_duration_text,
357            text=str(self._stress_test_round_duration),
358        )
359
360    def _stress_test_game_type_selected(self, game_type: str) -> None:
361        self._stress_test_game_type = game_type
362
363    def _run_cpu_benchmark_pressed(self) -> None:
364        if bui.app.classic is None:
365            logging.warning('run-cpu-benchmark requires classic')
366            return
367        bui.app.classic.run_cpu_benchmark()
368
369    def _run_media_reload_benchmark_pressed(self) -> None:
370        if bui.app.classic is None:
371            logging.warning('run-media-reload-benchmark requires classic')
372            return
373        bui.app.classic.run_media_reload_benchmark()
374
375    def _stress_test_pressed(self) -> None:
376        from bascenev1lib.mainmenu import MainMenuActivity
377
378        if bui.app.classic is None:
379            logging.warning('stress-test requires classic')
380            return
381
382        activity = bs.get_foreground_host_activity()
383        if isinstance(activity, MainMenuActivity):
384            bui.app.classic.run_stress_test(
385                playlist_type=self._stress_test_game_type,
386                playlist_name=cast(
387                    str,
388                    bui.textwidget(query=self._stress_test_playlist_name_field),
389                ),
390                player_count=self._stress_test_player_count,
391                round_duration=self._stress_test_round_duration,
392            )
393            bui.containerwidget(edit=self._root_widget, transition='out_right')
394        else:
395            bui.screenmessage(
396                bui.Lstr(value='Already present in another activity.')
397            )
class BenchmarksAndStressTestsWindow(bauiv1._uitypes.MainWindow):
 15class BenchmarksAndStressTestsWindow(bui.MainWindow):
 16    """Window for launching benchmarks or stress tests."""
 17
 18    def __init__(
 19        self,
 20        transition: str | None = 'in_right',
 21        origin_widget: bui.Widget | None = None,
 22    ):
 23        # pylint: disable=too-many-statements
 24        # pylint: disable=cyclic-import
 25        from bauiv1lib import popup
 26
 27        uiscale = bui.app.ui_v1.uiscale
 28        self._width = width = 650 if uiscale is bui.UIScale.SMALL else 580
 29        self._height = height = (
 30            400
 31            if uiscale is bui.UIScale.SMALL
 32            else 420 if uiscale is bui.UIScale.MEDIUM else 520
 33        )
 34        yoffs = -30 if uiscale is bui.UIScale.SMALL else 0
 35
 36        self._scroll_width = self._width - 100
 37        self._scroll_height = self._height - (
 38            180 if uiscale is bui.UIScale.SMALL else 120
 39        )
 40
 41        self._sub_width = self._scroll_width * 0.95
 42        self._sub_height = 520
 43
 44        self._stress_test_game_type = 'Random'
 45        self._stress_test_playlist = '__default__'
 46        self._stress_test_player_count = 8
 47        self._stress_test_round_duration = 30
 48
 49        self._r = 'debugWindow'
 50        uiscale = bui.app.ui_v1.uiscale
 51        super().__init__(
 52            root_widget=bui.containerwidget(
 53                size=(width, height),
 54                scale=(
 55                    2.32
 56                    if uiscale is bui.UIScale.SMALL
 57                    else 1.55 if uiscale is bui.UIScale.MEDIUM else 1.0
 58                ),
 59                stack_offset=(
 60                    (0, -30) if uiscale is bui.UIScale.SMALL else (0, 0)
 61                ),
 62                toolbar_visibility=(
 63                    'menu_minimal'
 64                    if uiscale is bui.UIScale.SMALL
 65                    else 'menu_full'
 66                ),
 67            ),
 68            transition=transition,
 69            origin_widget=origin_widget,
 70        )
 71
 72        if bui.app.ui_v1.uiscale is bui.UIScale.SMALL:
 73            bui.containerwidget(
 74                edit=self._root_widget, on_cancel_call=self.main_window_back
 75            )
 76            self._done_button = bui.get_special_widget('back_button')
 77        else:
 78            self._done_button = btn = bui.buttonwidget(
 79                parent=self._root_widget,
 80                position=(40, height - 67 + yoffs),
 81                size=(120, 60),
 82                scale=0.8,
 83                autoselect=True,
 84                label=bui.Lstr(resource='doneText'),
 85                on_activate_call=self.main_window_back,
 86            )
 87            bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 88
 89        bui.textwidget(
 90            parent=self._root_widget,
 91            position=(0, height - 60 + yoffs),
 92            size=(width, 30),
 93            text=bui.Lstr(resource=f'{self._r}.titleText'),
 94            h_align='center',
 95            color=bui.app.ui_v1.title_color,
 96            v_align='center',
 97            maxwidth=260,
 98        )
 99
100        self._scrollwidget = bui.scrollwidget(
101            parent=self._root_widget,
102            highlight=False,
103            size=(self._scroll_width, self._scroll_height),
104            position=(
105                (self._width - self._scroll_width) * 0.5,
106                (115 if uiscale is bui.UIScale.SMALL else 50) + yoffs,
107            ),
108        )
109        bui.containerwidget(edit=self._scrollwidget, claims_left_right=True)
110
111        self._subcontainer = bui.containerwidget(
112            parent=self._scrollwidget,
113            size=(self._sub_width, self._sub_height),
114            background=False,
115        )
116
117        v = self._sub_height - 70
118        button_width = 300
119        btn = bui.buttonwidget(
120            parent=self._subcontainer,
121            position=((self._sub_width - button_width) * 0.5, v),
122            size=(button_width, 60),
123            autoselect=True,
124            label=bui.Lstr(resource=f'{self._r}.runCPUBenchmarkText'),
125            on_activate_call=self._run_cpu_benchmark_pressed,
126        )
127        bui.widget(
128            edit=btn, up_widget=self._done_button, left_widget=self._done_button
129        )
130        v -= 60
131
132        bui.buttonwidget(
133            parent=self._subcontainer,
134            position=((self._sub_width - button_width) * 0.5, v),
135            size=(button_width, 60),
136            autoselect=True,
137            label=bui.Lstr(resource=f'{self._r}.runMediaReloadBenchmarkText'),
138            on_activate_call=self._run_media_reload_benchmark_pressed,
139        )
140        v -= 60
141
142        bui.textwidget(
143            parent=self._subcontainer,
144            position=(self._sub_width * 0.5, v + 22),
145            size=(0, 0),
146            text=bui.Lstr(resource=f'{self._r}.stressTestTitleText'),
147            maxwidth=200,
148            color=bui.app.ui_v1.heading_color,
149            scale=0.85,
150            h_align='center',
151            v_align='center',
152        )
153        v -= 45
154
155        x_offs = 165
156        bui.textwidget(
157            parent=self._subcontainer,
158            position=(x_offs - 10, v + 22),
159            size=(0, 0),
160            text=bui.Lstr(resource=f'{self._r}.stressTestPlaylistTypeText'),
161            maxwidth=130,
162            color=bui.app.ui_v1.heading_color,
163            scale=0.65,
164            h_align='right',
165            v_align='center',
166        )
167
168        popup.PopupMenu(
169            parent=self._subcontainer,
170            position=(x_offs, v),
171            width=150,
172            choices=['Random', 'Teams', 'Free-For-All'],
173            choices_display=[
174                bui.Lstr(resource=a)
175                for a in [
176                    'randomText',
177                    'playModes.teamsText',
178                    'playModes.freeForAllText',
179                ]
180            ],
181            current_choice='Auto',
182            on_value_change_call=self._stress_test_game_type_selected,
183        )
184
185        v -= 46
186        bui.textwidget(
187            parent=self._subcontainer,
188            position=(x_offs - 10, v + 22),
189            size=(0, 0),
190            text=bui.Lstr(resource=f'{self._r}.stressTestPlaylistNameText'),
191            maxwidth=130,
192            color=bui.app.ui_v1.heading_color,
193            scale=0.65,
194            h_align='right',
195            v_align='center',
196        )
197
198        self._stress_test_playlist_name_field = bui.textwidget(
199            parent=self._subcontainer,
200            position=(x_offs + 5, v - 5),
201            size=(250, 46),
202            text=self._stress_test_playlist,
203            h_align='left',
204            v_align='center',
205            autoselect=True,
206            color=(0.9, 0.9, 0.9, 1.0),
207            description=bui.Lstr(
208                resource=f'{self._r}.stressTestPlaylistDescriptionText'
209            ),
210            editable=True,
211            padding=4,
212        )
213        v -= 29
214        x_sub = 60
215
216        # Player count.
217        bui.textwidget(
218            parent=self._subcontainer,
219            position=(x_offs - 10, v),
220            size=(0, 0),
221            text=bui.Lstr(resource=f'{self._r}.stressTestPlayerCountText'),
222            color=(0.8, 0.8, 0.8, 1.0),
223            h_align='right',
224            v_align='center',
225            scale=0.65,
226            maxwidth=130,
227        )
228        self._stress_test_player_count_text = bui.textwidget(
229            parent=self._subcontainer,
230            position=(246 - x_sub, v - 14),
231            size=(60, 28),
232            editable=False,
233            color=(0.3, 1.0, 0.3, 1.0),
234            h_align='right',
235            v_align='center',
236            text=str(self._stress_test_player_count),
237            padding=2,
238        )
239        bui.buttonwidget(
240            parent=self._subcontainer,
241            position=(330 - x_sub, v - 11),
242            size=(28, 28),
243            label='-',
244            autoselect=True,
245            on_activate_call=bui.Call(self._stress_test_player_count_decrement),
246            repeat=True,
247            enable_sound=True,
248        )
249        bui.buttonwidget(
250            parent=self._subcontainer,
251            position=(380 - x_sub, v - 11),
252            size=(28, 28),
253            label='+',
254            autoselect=True,
255            on_activate_call=bui.Call(self._stress_test_player_count_increment),
256            repeat=True,
257            enable_sound=True,
258        )
259        v -= 42
260
261        # Round duration.
262        bui.textwidget(
263            parent=self._subcontainer,
264            position=(x_offs - 10, v),
265            size=(0, 0),
266            text=bui.Lstr(resource=f'{self._r}.stressTestRoundDurationText'),
267            color=(0.8, 0.8, 0.8, 1.0),
268            h_align='right',
269            v_align='center',
270            scale=0.65,
271            maxwidth=130,
272        )
273        self._stress_test_round_duration_text = bui.textwidget(
274            parent=self._subcontainer,
275            position=(246 - x_sub, v - 14),
276            size=(60, 28),
277            editable=False,
278            color=(0.3, 1.0, 0.3, 1.0),
279            h_align='right',
280            v_align='center',
281            text=str(self._stress_test_round_duration),
282            padding=2,
283        )
284        bui.buttonwidget(
285            parent=self._subcontainer,
286            position=(330 - x_sub, v - 11),
287            size=(28, 28),
288            label='-',
289            autoselect=True,
290            on_activate_call=bui.Call(
291                self._stress_test_round_duration_decrement
292            ),
293            repeat=True,
294            enable_sound=True,
295        )
296        bui.buttonwidget(
297            parent=self._subcontainer,
298            position=(380 - x_sub, v - 11),
299            size=(28, 28),
300            label='+',
301            autoselect=True,
302            on_activate_call=bui.Call(
303                self._stress_test_round_duration_increment
304            ),
305            repeat=True,
306            enable_sound=True,
307        )
308        v -= 82
309        btn = bui.buttonwidget(
310            parent=self._subcontainer,
311            position=((self._sub_width - button_width) * 0.5, v),
312            size=(button_width, 60),
313            autoselect=True,
314            label=bui.Lstr(resource=f'{self._r}.runStressTestText'),
315            on_activate_call=self._stress_test_pressed,
316        )
317        bui.widget(edit=btn, show_buffer_bottom=50)
318
319    @override
320    def get_main_window_state(self) -> bui.MainWindowState:
321        # Support recreating our window for back/refresh purposes.
322        cls = type(self)
323        return bui.BasicMainWindowState(
324            create_call=lambda transition, origin_widget: cls(
325                transition=transition, origin_widget=origin_widget
326            )
327        )
328
329    def _stress_test_player_count_decrement(self) -> None:
330        self._stress_test_player_count = max(
331            1, self._stress_test_player_count - 1
332        )
333        bui.textwidget(
334            edit=self._stress_test_player_count_text,
335            text=str(self._stress_test_player_count),
336        )
337
338    def _stress_test_player_count_increment(self) -> None:
339        self._stress_test_player_count = self._stress_test_player_count + 1
340        bui.textwidget(
341            edit=self._stress_test_player_count_text,
342            text=str(self._stress_test_player_count),
343        )
344
345    def _stress_test_round_duration_decrement(self) -> None:
346        self._stress_test_round_duration = max(
347            10, self._stress_test_round_duration - 10
348        )
349        bui.textwidget(
350            edit=self._stress_test_round_duration_text,
351            text=str(self._stress_test_round_duration),
352        )
353
354    def _stress_test_round_duration_increment(self) -> None:
355        self._stress_test_round_duration = self._stress_test_round_duration + 10
356        bui.textwidget(
357            edit=self._stress_test_round_duration_text,
358            text=str(self._stress_test_round_duration),
359        )
360
361    def _stress_test_game_type_selected(self, game_type: str) -> None:
362        self._stress_test_game_type = game_type
363
364    def _run_cpu_benchmark_pressed(self) -> None:
365        if bui.app.classic is None:
366            logging.warning('run-cpu-benchmark requires classic')
367            return
368        bui.app.classic.run_cpu_benchmark()
369
370    def _run_media_reload_benchmark_pressed(self) -> None:
371        if bui.app.classic is None:
372            logging.warning('run-media-reload-benchmark requires classic')
373            return
374        bui.app.classic.run_media_reload_benchmark()
375
376    def _stress_test_pressed(self) -> None:
377        from bascenev1lib.mainmenu import MainMenuActivity
378
379        if bui.app.classic is None:
380            logging.warning('stress-test requires classic')
381            return
382
383        activity = bs.get_foreground_host_activity()
384        if isinstance(activity, MainMenuActivity):
385            bui.app.classic.run_stress_test(
386                playlist_type=self._stress_test_game_type,
387                playlist_name=cast(
388                    str,
389                    bui.textwidget(query=self._stress_test_playlist_name_field),
390                ),
391                player_count=self._stress_test_player_count,
392                round_duration=self._stress_test_round_duration,
393            )
394            bui.containerwidget(edit=self._root_widget, transition='out_right')
395        else:
396            bui.screenmessage(
397                bui.Lstr(value='Already present in another activity.')
398            )

Window for launching benchmarks or stress tests.

BenchmarksAndStressTestsWindow( transition: str | None = 'in_right', origin_widget: _bauiv1.Widget | None = None)
 18    def __init__(
 19        self,
 20        transition: str | None = 'in_right',
 21        origin_widget: bui.Widget | None = None,
 22    ):
 23        # pylint: disable=too-many-statements
 24        # pylint: disable=cyclic-import
 25        from bauiv1lib import popup
 26
 27        uiscale = bui.app.ui_v1.uiscale
 28        self._width = width = 650 if uiscale is bui.UIScale.SMALL else 580
 29        self._height = height = (
 30            400
 31            if uiscale is bui.UIScale.SMALL
 32            else 420 if uiscale is bui.UIScale.MEDIUM else 520
 33        )
 34        yoffs = -30 if uiscale is bui.UIScale.SMALL else 0
 35
 36        self._scroll_width = self._width - 100
 37        self._scroll_height = self._height - (
 38            180 if uiscale is bui.UIScale.SMALL else 120
 39        )
 40
 41        self._sub_width = self._scroll_width * 0.95
 42        self._sub_height = 520
 43
 44        self._stress_test_game_type = 'Random'
 45        self._stress_test_playlist = '__default__'
 46        self._stress_test_player_count = 8
 47        self._stress_test_round_duration = 30
 48
 49        self._r = 'debugWindow'
 50        uiscale = bui.app.ui_v1.uiscale
 51        super().__init__(
 52            root_widget=bui.containerwidget(
 53                size=(width, height),
 54                scale=(
 55                    2.32
 56                    if uiscale is bui.UIScale.SMALL
 57                    else 1.55 if uiscale is bui.UIScale.MEDIUM else 1.0
 58                ),
 59                stack_offset=(
 60                    (0, -30) if uiscale is bui.UIScale.SMALL else (0, 0)
 61                ),
 62                toolbar_visibility=(
 63                    'menu_minimal'
 64                    if uiscale is bui.UIScale.SMALL
 65                    else 'menu_full'
 66                ),
 67            ),
 68            transition=transition,
 69            origin_widget=origin_widget,
 70        )
 71
 72        if bui.app.ui_v1.uiscale is bui.UIScale.SMALL:
 73            bui.containerwidget(
 74                edit=self._root_widget, on_cancel_call=self.main_window_back
 75            )
 76            self._done_button = bui.get_special_widget('back_button')
 77        else:
 78            self._done_button = btn = bui.buttonwidget(
 79                parent=self._root_widget,
 80                position=(40, height - 67 + yoffs),
 81                size=(120, 60),
 82                scale=0.8,
 83                autoselect=True,
 84                label=bui.Lstr(resource='doneText'),
 85                on_activate_call=self.main_window_back,
 86            )
 87            bui.containerwidget(edit=self._root_widget, cancel_button=btn)
 88
 89        bui.textwidget(
 90            parent=self._root_widget,
 91            position=(0, height - 60 + yoffs),
 92            size=(width, 30),
 93            text=bui.Lstr(resource=f'{self._r}.titleText'),
 94            h_align='center',
 95            color=bui.app.ui_v1.title_color,
 96            v_align='center',
 97            maxwidth=260,
 98        )
 99
100        self._scrollwidget = bui.scrollwidget(
101            parent=self._root_widget,
102            highlight=False,
103            size=(self._scroll_width, self._scroll_height),
104            position=(
105                (self._width - self._scroll_width) * 0.5,
106                (115 if uiscale is bui.UIScale.SMALL else 50) + yoffs,
107            ),
108        )
109        bui.containerwidget(edit=self._scrollwidget, claims_left_right=True)
110
111        self._subcontainer = bui.containerwidget(
112            parent=self._scrollwidget,
113            size=(self._sub_width, self._sub_height),
114            background=False,
115        )
116
117        v = self._sub_height - 70
118        button_width = 300
119        btn = bui.buttonwidget(
120            parent=self._subcontainer,
121            position=((self._sub_width - button_width) * 0.5, v),
122            size=(button_width, 60),
123            autoselect=True,
124            label=bui.Lstr(resource=f'{self._r}.runCPUBenchmarkText'),
125            on_activate_call=self._run_cpu_benchmark_pressed,
126        )
127        bui.widget(
128            edit=btn, up_widget=self._done_button, left_widget=self._done_button
129        )
130        v -= 60
131
132        bui.buttonwidget(
133            parent=self._subcontainer,
134            position=((self._sub_width - button_width) * 0.5, v),
135            size=(button_width, 60),
136            autoselect=True,
137            label=bui.Lstr(resource=f'{self._r}.runMediaReloadBenchmarkText'),
138            on_activate_call=self._run_media_reload_benchmark_pressed,
139        )
140        v -= 60
141
142        bui.textwidget(
143            parent=self._subcontainer,
144            position=(self._sub_width * 0.5, v + 22),
145            size=(0, 0),
146            text=bui.Lstr(resource=f'{self._r}.stressTestTitleText'),
147            maxwidth=200,
148            color=bui.app.ui_v1.heading_color,
149            scale=0.85,
150            h_align='center',
151            v_align='center',
152        )
153        v -= 45
154
155        x_offs = 165
156        bui.textwidget(
157            parent=self._subcontainer,
158            position=(x_offs - 10, v + 22),
159            size=(0, 0),
160            text=bui.Lstr(resource=f'{self._r}.stressTestPlaylistTypeText'),
161            maxwidth=130,
162            color=bui.app.ui_v1.heading_color,
163            scale=0.65,
164            h_align='right',
165            v_align='center',
166        )
167
168        popup.PopupMenu(
169            parent=self._subcontainer,
170            position=(x_offs, v),
171            width=150,
172            choices=['Random', 'Teams', 'Free-For-All'],
173            choices_display=[
174                bui.Lstr(resource=a)
175                for a in [
176                    'randomText',
177                    'playModes.teamsText',
178                    'playModes.freeForAllText',
179                ]
180            ],
181            current_choice='Auto',
182            on_value_change_call=self._stress_test_game_type_selected,
183        )
184
185        v -= 46
186        bui.textwidget(
187            parent=self._subcontainer,
188            position=(x_offs - 10, v + 22),
189            size=(0, 0),
190            text=bui.Lstr(resource=f'{self._r}.stressTestPlaylistNameText'),
191            maxwidth=130,
192            color=bui.app.ui_v1.heading_color,
193            scale=0.65,
194            h_align='right',
195            v_align='center',
196        )
197
198        self._stress_test_playlist_name_field = bui.textwidget(
199            parent=self._subcontainer,
200            position=(x_offs + 5, v - 5),
201            size=(250, 46),
202            text=self._stress_test_playlist,
203            h_align='left',
204            v_align='center',
205            autoselect=True,
206            color=(0.9, 0.9, 0.9, 1.0),
207            description=bui.Lstr(
208                resource=f'{self._r}.stressTestPlaylistDescriptionText'
209            ),
210            editable=True,
211            padding=4,
212        )
213        v -= 29
214        x_sub = 60
215
216        # Player count.
217        bui.textwidget(
218            parent=self._subcontainer,
219            position=(x_offs - 10, v),
220            size=(0, 0),
221            text=bui.Lstr(resource=f'{self._r}.stressTestPlayerCountText'),
222            color=(0.8, 0.8, 0.8, 1.0),
223            h_align='right',
224            v_align='center',
225            scale=0.65,
226            maxwidth=130,
227        )
228        self._stress_test_player_count_text = bui.textwidget(
229            parent=self._subcontainer,
230            position=(246 - x_sub, v - 14),
231            size=(60, 28),
232            editable=False,
233            color=(0.3, 1.0, 0.3, 1.0),
234            h_align='right',
235            v_align='center',
236            text=str(self._stress_test_player_count),
237            padding=2,
238        )
239        bui.buttonwidget(
240            parent=self._subcontainer,
241            position=(330 - x_sub, v - 11),
242            size=(28, 28),
243            label='-',
244            autoselect=True,
245            on_activate_call=bui.Call(self._stress_test_player_count_decrement),
246            repeat=True,
247            enable_sound=True,
248        )
249        bui.buttonwidget(
250            parent=self._subcontainer,
251            position=(380 - x_sub, v - 11),
252            size=(28, 28),
253            label='+',
254            autoselect=True,
255            on_activate_call=bui.Call(self._stress_test_player_count_increment),
256            repeat=True,
257            enable_sound=True,
258        )
259        v -= 42
260
261        # Round duration.
262        bui.textwidget(
263            parent=self._subcontainer,
264            position=(x_offs - 10, v),
265            size=(0, 0),
266            text=bui.Lstr(resource=f'{self._r}.stressTestRoundDurationText'),
267            color=(0.8, 0.8, 0.8, 1.0),
268            h_align='right',
269            v_align='center',
270            scale=0.65,
271            maxwidth=130,
272        )
273        self._stress_test_round_duration_text = bui.textwidget(
274            parent=self._subcontainer,
275            position=(246 - x_sub, v - 14),
276            size=(60, 28),
277            editable=False,
278            color=(0.3, 1.0, 0.3, 1.0),
279            h_align='right',
280            v_align='center',
281            text=str(self._stress_test_round_duration),
282            padding=2,
283        )
284        bui.buttonwidget(
285            parent=self._subcontainer,
286            position=(330 - x_sub, v - 11),
287            size=(28, 28),
288            label='-',
289            autoselect=True,
290            on_activate_call=bui.Call(
291                self._stress_test_round_duration_decrement
292            ),
293            repeat=True,
294            enable_sound=True,
295        )
296        bui.buttonwidget(
297            parent=self._subcontainer,
298            position=(380 - x_sub, v - 11),
299            size=(28, 28),
300            label='+',
301            autoselect=True,
302            on_activate_call=bui.Call(
303                self._stress_test_round_duration_increment
304            ),
305            repeat=True,
306            enable_sound=True,
307        )
308        v -= 82
309        btn = bui.buttonwidget(
310            parent=self._subcontainer,
311            position=((self._sub_width - button_width) * 0.5, v),
312            size=(button_width, 60),
313            autoselect=True,
314            label=bui.Lstr(resource=f'{self._r}.runStressTestText'),
315            on_activate_call=self._stress_test_pressed,
316        )
317        bui.widget(edit=btn, show_buffer_bottom=50)

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.

@override
def get_main_window_state(self) -> bauiv1.MainWindowState:
319    @override
320    def get_main_window_state(self) -> bui.MainWindowState:
321        # Support recreating our window for back/refresh purposes.
322        cls = type(self)
323        return bui.BasicMainWindowState(
324            create_call=lambda transition, origin_widget: cls(
325                transition=transition, origin_widget=origin_widget
326            )
327        )

Return a WindowState to recreate this window, if supported.