Refactored Rendering

This commit is contained in:
CooperWang0912 2026-07-31 14:24:27 +08:00
parent b58c15ffdc
commit 50f7a63603
2 changed files with 20 additions and 67 deletions

View File

@ -1,7 +1,4 @@
import inspect
from collections.abc import Callable
from functools import wraps
from typing import Any, override
from typing import override
from archinstall.default_profiles.profile import GreeterType
from archinstall.lib.applications.application_menu import ApplicationMenu
@ -35,7 +32,7 @@ from archinstall.lib.pacman.config import PacmanConfig
from archinstall.lib.pacman.pacman_menu import PacmanMenu
from archinstall.lib.translationhandler import Language, tr, translation_handler
from archinstall.lib.utils.format import as_table
from archinstall.tui.components import get_status_prefix, tui
from archinstall.tui.components import tui
from archinstall.tui.menu_item import MenuItem, MenuItemGroup, MsgLevelType, PreviewResult
@ -53,7 +50,7 @@ class GlobalMenu(AbstractMenu[None]):
self._skip_boot = skip_boot
self._advanced = advanced
self._uefi = SysInfo.has_uefi()
menu_options = self._get_menu_options(wrap_actions=True)
menu_options = self._get_menu_options()
self._item_group = MenuItemGroup(
menu_options,
@ -62,42 +59,8 @@ class GlobalMenu(AbstractMenu[None]):
)
super().__init__(self._item_group, config=arch_config, title=title)
self._update_item_labels()
def _update_item_labels(self) -> None:
"""
Re-applies translated titles and status prefixes to all active menu items.
"""
raw_options = self._get_menu_options(wrap_actions=False)
new_options = {o.key: o.text for o in raw_options if o.key is not None}
for item in self._item_group.items:
if item.key in new_options:
base_title = new_options[item.key]
prefix = get_status_prefix(item)
item.text = f'{prefix}{base_title}'
def _wrap_action(self, item: MenuItem, action: Callable[..., Any]) -> Callable[..., Any]:
@wraps(action)
async def wrapper(*args: Any, **kwargs: Any) -> Any:
try:
if inspect.iscoroutinefunction(action):
result = await action(*args, **kwargs)
else:
result = action(*args, **kwargs)
if inspect.isawaitable(result):
result = await result
item.value = result
return result
finally:
self._update_item_labels()
return wrapper
def _get_menu_options(self, wrap_actions: bool = True) -> list[MenuItem]:
def _get_menu_options(self) -> list[MenuItem]:
menu_options = [
MenuItem(
text=tr('Archinstall language'),
@ -183,7 +146,7 @@ class GlobalMenu(AbstractMenu[None]):
MenuItem(
text=tr('Pacman'),
action=self._pacman_configuration,
value=PacmanConfiguration.default(),
value=PacmanConfiguration(),
preview_action=self._prev_pacman_config,
key='pacman_config',
),
@ -228,11 +191,6 @@ class GlobalMenu(AbstractMenu[None]):
),
]
if wrap_actions:
for item in menu_options:
if item.key and item.action:
item.action = self._wrap_action(item, item.action)
return menu_options
async def _safe_config(self) -> None:
@ -271,14 +229,11 @@ class GlobalMenu(AbstractMenu[None]):
tr('The selected desktop profile requires a regular user to log in via the greeter'),
)
raw_options = {o.key: o.text for o in self._get_menu_options(wrap_actions=False) if o.key is not None}
for item in self._item_group.items:
if item.mandatory:
assert item.key is not None
if not check(item.key):
raw_title = raw_options.get(item.key, item.text)
missing.add(raw_title)
missing.add(item.text)
return list(missing)
@ -318,9 +273,15 @@ class GlobalMenu(AbstractMenu[None]):
def _update_lang_text(self) -> None:
"""
Updates option titles and status prefixes when language changes or settings are modified.
The options for the global menu are generated with a static text;
each entry of the menu needs to be updated with the new translation
"""
self._update_item_labels()
new_options = self._get_menu_options()
for o in new_options:
if o.key is not None:
self._item_group.find_by_key(o.key).text = o.text
tui.translate_bindings()
async def _locale_selection(self, preset: LocaleConfiguration) -> LocaleConfiguration | None:
@ -610,6 +571,7 @@ class GlobalMenu(AbstractMenu[None]):
preset = BootloaderConfiguration.get_default(self._uefi, self._skip_boot)
bootloader_config = await BootloaderMenu(preset, self._uefi, self._skip_boot).show()
return bootloader_config
async def _select_profile(self, current_profile: ProfileConfiguration | None) -> ProfileConfiguration | None:

View File

@ -20,8 +20,6 @@ from textual.widgets.selection_list import Selection
from textual.worker import WorkerCancelled
from archinstall.lib.log import debug
from archinstall.lib.menu.abstract_menu import SpecialMenuKey
from archinstall.lib.models.authentication import AuthenticationConfiguration
from archinstall.lib.translationhandler import tr
from archinstall.tui.menu_item import MenuItem, MenuItemGroup, MsgLevelType, PreviewResult
from archinstall.tui.result import Result, ResultType
@ -57,21 +55,12 @@ def _translate_bindings(source: BindingsMap | None, target: BindingsMap) -> None
target.key_to_bindings[key] = [replace(b, description=tr(b.description)) if b.description else b for b in bindings]
def get_status_prefix(item: MenuItem) -> str:
def _get_status_prefix(item: MenuItem) -> str:
"""
Returns a rich-formatted status prefix icon depending on item state:
- Space for configured items
- ! (Yellow) for unconfigured items
"""
if item.read_only or item.key in (SpecialMenuKey.SAVE.value, SpecialMenuKey.INSTALL.value, SpecialMenuKey.ABORT.value):
return ''
if item.key == 'auth_config':
auth_config: AuthenticationConfiguration | None = item.value
is_auth_valid = auth_config is not None and (auth_config.root_enc_password is not None or auth_config.has_superuser())
if is_auth_valid:
return ' '
return '[bold yellow][!][/bold yellow] '
if item.has_value():
return ' '
@ -311,7 +300,8 @@ class OptionListScreen(BaseScreen[ValueT]):
for item in self._group.get_enabled_items():
disabled = True if item.read_only else False
options.append(Option(item.text, id=item.get_id(), disabled=disabled))
option_text = _get_status_prefix(item) + item.text
options.append(Option(option_text, id=item.get_id(), disabled=disabled))
return options
@ -544,7 +534,8 @@ class SelectListScreen(BaseScreen[ValueT]):
for item in self._group.get_enabled_items():
is_selected = item in self._selected_items
selection = Selection(item.text, item, is_selected)
selection_text = _get_status_prefix(item) + item.text
selection = Selection(selection_text, item, is_selected)
selections.append(selection)
return selections