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