feat: add stop (#14)

This commit is contained in:
0xMRTT 2023-05-11 00:29:16 +02:00
parent 0fff84a2f9
commit be32379d81
Signed by: 0xMRTT
GPG Key ID: 19C1449A774028BD
5 changed files with 59 additions and 1 deletions

View File

@ -8,5 +8,6 @@
</gresource>
<gresource prefix="/io/github/Bavarder/Bavarder/icons/scalable/actions/">
<file preprocess="xml-stripblanks" alias="paper-plane-symbolic.svg">icons/scalable/actions/paper-plane-symbolic.svg</file>
<file preprocess="xml-stripblanks" alias="x-circular-symbolic.svg">icons/scalable/actions/x-circular-symbolic.svg</file>
</gresource>
</gresources>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg height="16px" viewBox="0 0 16 16" width="16px" xmlns="http://www.w3.org/2000/svg">
<path d="m 7.96875 1 c -3.851562 0 -6.96875 3.117188 -6.96875 6.96875 s 3.117188 6.96875 6.96875 6.96875 s 6.96875 -3.117188 6.96875 -6.96875 s -3.117188 -6.96875 -6.96875 -6.96875 z m -3 3.96875 h 1 h 0.03125 c 0.253906 0.011719 0.511719 0.128906 0.6875 0.3125 l 1.28125 1.28125 l 1.3125 -1.28125 c 0.265625 -0.230469 0.445312 -0.304688 0.6875 -0.3125 h 1 v 1 c 0 0.285156 -0.035156 0.550781 -0.25 0.75 l -1.28125 1.28125 l 1.25 1.25 c 0.1875 0.1875 0.28125 0.453125 0.28125 0.71875 v 1 h -1 c -0.265625 0 -0.53125 -0.09375 -0.71875 -0.28125 l -1.28125 -1.28125 l -1.28125 1.28125 c -0.1875 0.1875 -0.453125 0.28125 -0.71875 0.28125 h -1 v -1 c 0 -0.265625 0.09375 -0.53125 0.28125 -0.71875 l 1.28125 -1.25 l -1.28125 -1.28125 c -0.210938 -0.195312 -0.304688 -0.46875 -0.28125 -0.75 z m 0 0" fill="#2e3436"/>
</svg>

After

Width:  |  Height:  |  Size: 948 B

View File

@ -203,6 +203,17 @@ template BavarderWindow : Adw.ApplicationWindow {
halign: end;
action-name: "app.copy_bot";
}
Button stop_button {
visible: true;
sensitive: false;
icon-name: "x-circular-symbolic";
tooltip-text: _("Stop");
hexpand: true;
halign: end;
action-name: "app.stop";
styles ["suggested-action", "circular"]
}
}
}
}

View File

@ -50,6 +50,38 @@ class Step(IntEnum):
LOAD_WEBVIEW = auto()
RENDER = auto()
class KillableThread(threading.Thread):
def __init__(self, *args, **keywords):
threading.Thread.__init__(self, *args, **keywords)
self.killed = False
def start(self):
self.__run_backup = self.run
self.run = self.__run
threading.Thread.start(self)
def __run(self):
sys.settrace(self.globaltrace)
self.__run_backup()
self.run = self.__run_backup
def globaltrace(self, frame, event, arg):
if event == 'call':
return self.localtrace
else:
return None
def localtrace(self, frame, event, arg):
if self.killed:
if event == 'line':
raise SystemExit()
return self.localtrace
def kill(self):
self.killed = True
class BavarderApplication(Adw.Application):
@ -69,6 +101,7 @@ class BavarderApplication(Adw.Application):
self.create_action("copy_bot", self.on_copy_bot_action, ["<primary><shift>c"])
self.create_action("ask", self.on_ask_action, ["<primary>Return"])
self.create_action("clear", self.on_clear_action, ["<primary><shift>BackSpace"])
self.create_action("stop", self.on_stop_action, ["<primary>Escape"])
# self.create_action("speak", self.on_speak_action, ["<primary>S"])
# self.create_action("listen", self.on_listen_action, ["<primary>L"])
@ -995,9 +1028,17 @@ Providers: {self.enabled_providers}
if self.clear_after_send:
self.win.prompt_text_view.get_buffer().set_text("")
self.t = threading.Thread(target=thread_run)
self.t = KillableThread(target=thread_run)
self.t.start()
def on_stop_action(self, widget, _):
"""Callback for the app.stop action."""
self.win.spinner.stop()
self.win.ask_button.set_visible(True)
self.win.wait_button.set_visible(False)
self.t.kill()
self.t.join()
# def on_speak_action(self, widget, _):
# """Callback for the app.speak action."""
# print("app.speak action activated")

View File

@ -34,6 +34,7 @@ class BavarderWindow(Adw.ApplicationWindow):
bot_text_view = Gtk.Template.Child()
response_stack = Gtk.Template.Child()
banner = Gtk.Template.Child()
stop_button = Gtk.Template.Child()
# listen = Gtk.Template.Child()
# listen_wait = Gtk.Template.Child()
# listen_spinner = Gtk.Template.Child()