main: add spinner

This commit is contained in:
0xMRTT 2023-04-27 13:48:50 +02:00
parent db65ef7ad4
commit b3799c0484

View File

@ -20,12 +20,13 @@
import sys
import gi
import sys
import threading
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
gi.require_version('Gdk', '4.0')
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
gi.require_version("Gdk", "4.0")
from gi.repository import Gtk, Gio, Adw, Gdk
from gi.repository import Gtk, Gio, Adw, Gdk, GLib
from .window import BavarderWindow
from .preferences import Preferences
@ -38,18 +39,19 @@ class BavarderApplication(Adw.Application):
"""The main application singleton class."""
def __init__(self):
super().__init__(application_id='com.github.Bavarder.Bavarder',
flags=Gio.ApplicationFlags.DEFAULT_FLAGS)
self.create_action('quit', lambda *_: self.quit(), ['<primary>q'])
self.create_action('about', self.on_about_action)
self.create_action('preferences', self.on_preferences_action)
self.create_action('copy_prompt', self.on_copy_prompt_action)
self.create_action('copy_bot', self.on_copy_bot_action)
self.create_action('ask', self.on_ask_action)
super().__init__(
application_id="com.github.Bavarder.Bavarder",
flags=Gio.ApplicationFlags.DEFAULT_FLAGS,
)
self.create_action("quit", lambda *_: self.quit(), ["<primary>q"])
self.create_action("about", self.on_about_action)
self.create_action("preferences", self.on_preferences_action)
self.create_action("copy_prompt", self.on_copy_prompt_action)
self.create_action("copy_bot", self.on_copy_bot_action)
self.create_action("ask", self.on_ask_action)
self.settings = Gio.Settings(schema_id="com.github.Bavarder.Bavarder")
self.clear_after_send = self.settings.get_boolean("clear-after-send")
def do_activate(self):
@ -65,44 +67,53 @@ class BavarderApplication(Adw.Application):
def on_about_action(self, widget, _):
"""Callback for the app.about action."""
about = Adw.AboutWindow(transient_for=self.props.active_window,
application_name='Bavarder',
application_icon=app_id,
developer_name='0xMRTT',
developers = ['0xMRTT https://github.com/0xMRTT'],
designers = [],
documenters = [],
license_type=Gtk.License.GPL_3_0,
version=version,
copyright='© 2023 0xMRTT')
about = Adw.AboutWindow(
transient_for=self.props.active_window,
application_name="Bavarder",
application_icon=app_id,
developer_name="0xMRTT",
developers=["0xMRTT https://github.com/0xMRTT"],
designers=[],
documenters=[],
license_type=Gtk.License.GPL_3_0,
version=version,
copyright="© 2023 0xMRTT",
)
about.add_acknowledgement_section('Special thanks to',['Telegraph https://apps.gnome.org/app/io.github.fkinoshita.Telegraph', 'BAIChat https://chatbot.theb.ai/'])
about.add_acknowledgement_section(
"Special thanks to",
[
"Telegraph https://apps.gnome.org/app/io.github.fkinoshita.Telegraph",
"BAIChat https://chatbot.theb.ai/",
],
)
about.present()
def on_preferences_action(self, widget, _):
"""Callback for the app.preferences action."""
print('app.preferences action activated')
print("app.preferences action activated")
preferences = Preferences(application=self, transient_for=self.props.active_window)
preferences = Preferences(
application=self, transient_for=self.props.active_window
)
preferences.present()
def on_copy_prompt_action(self, widget, _):
"""Callback for the app.copy_prompt action."""
toast = Adw.Toast()
text = self.win.prompt_text_view.get_buffer()
toast.set_title('Text copied')
toast.set_title("Text copied")
(start, end) = text.get_bounds()
text = text.get_text(start, end, False)
if (len(text) == 0):
if len(text) == 0:
return
Gdk.Display.get_default().get_clipboard().set(text)
self.win.toast_overlay.add_toast(toast)
def on_copy_bot_action(self, widget, _):
@ -111,37 +122,60 @@ class BavarderApplication(Adw.Application):
toast = Adw.Toast()
text = self.win.bot_text_view.get_buffer()
toast.set_title('Text copied')
toast.set_title("Text copied")
(start, end) = text.get_bounds()
text = text.get_text(start, end, False)
if (len(text) == 0):
if len(text) == 0:
return
Gdk.Display.get_default().get_clipboard().set(text)
self.win.toast_overlay.add_toast(toast)
def ask(self, prompt):
with BAIChat() as (loop, chat):
response = chat.ask(self.prompt)
return response.text
chat = BAIChat(sync=True)
try:
response = chat.sync_ask(self.prompt)
except KeyError:
return ""
return response.text
@Gtk.Template.Callback()
def on_key_press_event(self, widget, event):
if gtk.gdk.keyval_name(event.keyval) == "Return":
self.on_ask_action(widget, event)
return True
return False
def on_ask_action(self, widget, _):
"""Callback for the app.ask action."""
self.win.spinner.start()
self.win.ask_button.set_visible(False)
self.win.spinner.set_visible(True)
self.win.status.set_text("Loading…")
self.prompt = self.win.prompt_text_view.get_buffer().props.text
self.win.bot_text_view.get_buffer().set_text(self.ask(self.prompt))
self.win.status.set_text("Ready")
def thread_run():
# call heavy here
response = self.ask(self.prompt)
GLib.idle_add(cleanup, response)
def cleanup(response):
self.win.spinner.stop()
self.win.ask_button.set_visible(True)
self.win.spinner.set_visible(False)
t.join()
self.win.bot_text_view.get_buffer().set_text(response)
self.win.status.set_text("Ready")
if self.clear_after_send:
self.win.prompt_text_view.get_buffer().set_text("")
t = threading.Thread(target=thread_run)
t.start()
def create_action(self, name, callback, shortcuts=None):
"""Add an application action.