summaryrefslogtreecommitdiff
path: root/src/streamml/front/detector_profiles_tab.py
blob: 7860e1699e7c0be67ba715614f7286385467cb31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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)