|
| 1 | +from typing import override |
| 2 | + |
| 3 | +from archinstall.lib.menu.abstract_menu import AbstractSubMenu |
| 4 | +from archinstall.lib.models.application import ApplicationConfiguration, BluetoothConfiguration |
| 5 | +from archinstall.lib.translationhandler import tr |
| 6 | +from archinstall.tui.curses_menu import SelectMenu |
| 7 | +from archinstall.tui.menu_item import MenuItem, MenuItemGroup |
| 8 | +from archinstall.tui.types import Alignment, Orientation |
| 9 | + |
| 10 | + |
| 11 | +class ApplicationMenu(AbstractSubMenu[ApplicationConfiguration]): |
| 12 | + def __init__( |
| 13 | + self, |
| 14 | + preset: ApplicationConfiguration | None = None, |
| 15 | + ): |
| 16 | + if preset: |
| 17 | + self._app_config = preset |
| 18 | + else: |
| 19 | + self._app_config = ApplicationConfiguration() |
| 20 | + |
| 21 | + menu_optioons = self._define_menu_options() |
| 22 | + self._item_group = MenuItemGroup(menu_optioons, checkmarks=True) |
| 23 | + |
| 24 | + super().__init__( |
| 25 | + self._item_group, |
| 26 | + config=self._app_config, |
| 27 | + allow_reset=True, |
| 28 | + ) |
| 29 | + |
| 30 | + @override |
| 31 | + def run(self, additional_title: str | None = None) -> ApplicationConfiguration: |
| 32 | + super().run(additional_title=additional_title) |
| 33 | + return self._app_config |
| 34 | + |
| 35 | + def _define_menu_options(self) -> list[MenuItem]: |
| 36 | + return [ |
| 37 | + MenuItem( |
| 38 | + text=tr('Bluetooth'), |
| 39 | + action=select_bluetooth, |
| 40 | + value=self._app_config.bluetooth_config, |
| 41 | + preview_action=self._prev_bluetooth, |
| 42 | + key='bluetooth_config', |
| 43 | + ), |
| 44 | + ] |
| 45 | + |
| 46 | + def _prev_bluetooth(self, item: MenuItem) -> str | None: |
| 47 | + if item.value is not None: |
| 48 | + bluetooth_config: BluetoothConfiguration = item.value |
| 49 | + |
| 50 | + output = 'Bluetooth: ' |
| 51 | + output += tr('Enabled') if bluetooth_config.enabled else tr('Disabled') |
| 52 | + return output |
| 53 | + return None |
| 54 | + |
| 55 | + |
| 56 | +def select_bluetooth(preset: BluetoothConfiguration | None) -> BluetoothConfiguration | None: |
| 57 | + group = MenuItemGroup.yes_no() |
| 58 | + group.focus_item = MenuItem.no() |
| 59 | + |
| 60 | + if preset is not None: |
| 61 | + group.set_selected_by_value(preset.enabled) |
| 62 | + |
| 63 | + header = tr('Would you like to configure Bluetooth?') + '\n' |
| 64 | + |
| 65 | + result = SelectMenu[bool]( |
| 66 | + group, |
| 67 | + header=header, |
| 68 | + alignment=Alignment.CENTER, |
| 69 | + columns=2, |
| 70 | + orientation=Orientation.HORIZONTAL, |
| 71 | + allow_skip=True, |
| 72 | + ).run() |
| 73 | + |
| 74 | + enabled = result.item() == MenuItem.yes() |
| 75 | + |
| 76 | + return BluetoothConfiguration(enabled) |
0 commit comments