from textual import on from textual.app import ComposeResult from textual.widgets import Button, Label, Switch from textual.containers import Vertical, Horizontal, VerticalScroll from ..back.detector_profiles_manager import DetectorProfilesManager from .detector_profiles_tab_pushscreens import ConfirmDeletePushScreen, ShowLogsPushScreen, ShowProfilePushScreen, SetDetectorNotificationPushScreen class DetectorProfilesTab(Vertical): def __init__(self, manager: DetectorProfilesManager, *args, **kwargs): super().__init__(*args, **kwargs) self.manager = manager self.manager.on_refresh = self.refresh_profiles self.manager.on_message = self.on_manager_message def compose(self) -> ComposeResult: yield VerticalScroll(id="profiles-list") def on_mount(self) -> None: self.refresh_profiles() def refresh_profiles(self) -> None: profiles_list = self.query_one("#profiles-list", VerticalScroll) profiles_list.remove_children() if not self.manager.profiles: return for profile in self.manager.profiles: row = Horizontal( Switch(id=f"activate-profile-switch-{profile.profile_name}", value=profile.is_active), Label(f"{profile.profile_name}", classes="profile-profile_name"), Button("Show logs", id=f"show-logs-button-{profile.profile_name}", classes="profile-action", variant="primary"), Button("Show Profile", id=f"show-profile-button-{profile.profile_name}", classes="profile-action", variant="default"), Button("Notifications", id=f"set-notifications-button-{profile.profile_name}", classes="profile-action", variant="default"), Button("Delete", id=f"delete-{profile.profile_name}", classes="profile-action button-delete", variant="error"), classes="profile-row" ) profiles_list.mount(row) @on(Switch.Changed) def any_switch_changed(self, event: Switch.Changed) -> None: switch_id = event.switch.id if not switch_id: return elif switch_id.startswith("activate-profile-switch"): profile_name = switch_id.removeprefix("activate-profile-switch-") if event.switch.value: is_turned_on = self.manager.turn_on_profile(profile_name) if not is_turned_on: event.switch.value = False else: is_turned_off = self.manager.turn_off_profile(profile_name) if not is_turned_off: event.switch.value = False @on(Button.Pressed) def on_any_button_pressed(self, event: Button.Pressed) -> None: button_id = event.button.id if not button_id: return elif button_id.startswith("show-profile-button-"): profile_name = button_id.removeprefix("show-profile-button-") self.app.push_screen(ShowProfilePushScreen(self.manager, profile_name)) elif button_id.startswith("set-notifications-button-"): profile_name = button_id.removeprefix("set-notifications-button-") self.app.push_screen(SetDetectorNotificationPushScreen(self.manager, profile_name)) elif button_id.startswith("show-logs-button-"): profile_name = button_id.removeprefix("show-logs-button-") self.app.push_screen(ShowLogsPushScreen(self.manager, profile_name)) elif button_id.startswith("delete-"): profile_name = button_id.removeprefix("delete-") self.app.push_screen(ConfirmDeletePushScreen(self.manager, profile_name)) def on_manager_message(self, msg: str, title: str, severity): self.app.notify(message=msg, title=title, severity=severity)