File size: 5,360 Bytes
9b674e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import pyautogui
from .signal import *
import threading

try:
    from ..audio.record import *
    from ..screen.shot import *
    from ..agent.process import *
    from ..agent.chat_history import clear_chat_history
    from ..utils.db import (
        screenshot_path,
        save_api_key,
        load_api_key,
        activate_just_text_model,
        deactivate_just_text_model,
        is_just_text_model_active,
        set_profile,
        get_profile,
    )
    from ..screen.shot import take_screenshot
except ImportError:
    from audio.record import *
    from screen.shot import *
    from agent.process import *
    from agent.chat_history import clear_chat_history
    from utils.db import (
        screenshot_path,
        save_api_key,
        load_api_key,
        activate_just_text_model,
        deactivate_just_text_model,
        is_just_text_model_active,
        set_profile,
        get_profile,
    )
    from screen.shot import take_screenshot

recording_thread = None


class ButtonHandler:
    """Handles button click events and corresponding actions."""

    def __init__(self, main_window):
        """Initialize the ButtonHandler."""
        self.recording = False
        self.main_window = main_window
        self.process_audio_thread = None

        signal_handler.recording_started.connect(self.on_recording_started)
        signal_handler.recording_stopped.connect(self.on_recording_stopped)
        signal_handler.assistant_thinking.connect(self.on_assistant_thinking)
        signal_handler.assistant_response_ready.connect(
            self.on_assistant_response_ready
        )
        signal_handler.assistant_response_stopped.connect(
            self.on_assistant_response_stopped
        )

    def toggle_recording(
        self, no_screenshot=False, take_system_audio=False, dont_save_image=False, new_record=False
    ):
        """Toggle audio recording."""

        if self.recording and not new_record:
            stop_recording()
            self.recording = False
        else:
            if not no_screenshot:
                screenshot = pyautogui.screenshot()
                screenshot.save(screenshot_path)

            self.no_screenshot = no_screenshot
            self.take_system_audio = take_system_audio
            self.dont_save_image = dont_save_image

            global recording_thread
            if recording_thread is None or not recording_thread.is_alive() or new_record:
                recording_thread = threading.Thread(
                    target=start_recording, args=(take_system_audio,self,)
                )
                recording_thread.start()
            signal_handler.recording_started.emit()

    def on_recording_started(self):
        """Handle event when recording starts."""

        self.recording = True
        self.main_window.update_state("talking")

    def on_recording_stopped(self):
        """Handle event when recording stops."""

        print("ON RECORDING STOPPED")
        self.recording = False
        self.main_window.update_state("thinking")
        if (
            self.process_audio_thread is None
            or not self.process_audio_thread.is_alive()
        ):
            signal_handler.assistant_thinking.emit()
            self.process_audio_thread = threading.Thread(
                target=process_audio,
                args=(
                    not self.no_screenshot,
                    self.take_system_audio,
                    self.dont_save_image,
                ),
            )
            self.process_audio_thread.start()

    def just_screenshot(self):
        """Take a screenshot."""

        take_screenshot()
        self.process_audio_thread = threading.Thread(target=process_screenshot)
        self.process_audio_thread.start()

    def on_assistant_response_stopped(self):
        """Handle event when assistant's response stops."""

        self.main_window.update_state("idle")

    def on_assistant_thinking(self):
        """Handle event when assistant is thinking."""

        self.main_window.update_state("thinking")

    def on_assistant_response_ready(self):
        """Handle event when assistant's response is ready."""

        self.main_window.update_state("aitalking")

    def input_text(self, text):
        """Handle input text."""

        self.main_window.update_state("thinking")
        if (
            self.process_audio_thread is None
            or not self.process_audio_thread.is_alive()
        ):
            signal_handler.assistant_thinking.emit()
            self.process_audio_thread = threading.Thread(
                target=process_text, args=(text,)
            )
            self.process_audio_thread.start()

    def input_text_screenshot(self, text):
        """Handle input text with screenshot."""

        screenshot = pyautogui.screenshot()
        screenshot.save(screenshot_path)

        self.main_window.update_state("thinking")
        if (
            self.process_audio_thread is None
            or not self.process_audio_thread.is_alive()
        ):
            signal_handler.assistant_thinking.emit()
            self.process_audio_thread = threading.Thread(
                target=process_text,
                args=(text,),
                kwargs={"screenshot_path": screenshot_path},
            )
            self.process_audio_thread.start()