ui: rework window

This commit is contained in:
0xMRTT 2023-04-27 00:32:45 +02:00
parent 211ae80e93
commit 99bc7a6eca
4 changed files with 226 additions and 78 deletions

View File

@ -2,10 +2,11 @@ using Gtk 4.0;
using Adw 1;
template BavarderWindow : Adw.ApplicationWindow {
title: _("Bavarder");
default-width: 950;
default-height: 650;
title: _("Bavarder");
default-width: 650;
default-height: 250;
ShortcutController {
Shortcut {
trigger: "<primary>q";
@ -14,78 +15,126 @@ template BavarderWindow : Adw.ApplicationWindow {
}
Adw.ToastOverlay toast_overlay {
Adw.Leaflet leaflet {
can-navigate-back: true;
can-unfold: false;
Box {
orientation: vertical;
vexpand: true;
hexpand: true;
Adw.HeaderBar {
MenuButton {
primary: true;
menu-model: main-menu;
icon-name: "open-menu-symbolic";
tooltip-text: _("Main Menu");
}
}
Gtk.Box main_view {
Box main {
orientation: vertical;
vexpand: true;
hexpand: true;
margin-top: 0;
margin-bottom: 24;
margin-start: 24;
margin-end: 24;
spacing: 12;
Adw.HeaderBar titlebar {
centering-policy: strict;
Adw.PreferencesGroup prompt_group {
title: _("Prompt");
Button apply-button {
styles ["suggested-action"]
label: _("New");
tooltip-text: _("New chat");
action-name: "app.new_chat";
}
[title]
Adw.ViewSwitcherTitle title {
stack: view_stack;
}
[end]
Gtk.MenuButton {
tooltip-text: _("Main Menu");
icon-name: "open-menu-symbolic";
menu-model: main-menu;
}
}
Gtk.Box {
orientation: vertical;
Adw.ViewStack view_stack {
vexpand: true;
Box {
orientation: vertical;
hexpand: true;
vexpand: true;
Adw.ViewStackPage {
name: "colors";
title: _("_Colors");
icon-name: "larger-brush-symbolic";
styles ["card", "text-box"]
child: Adw.PreferencesPage content { };
use-underline: true;
ScrolledWindow {
margin-top:12;
margin-bottom:0;
margin-start:12;
margin-end:12;
TextView prompt_text_view {
wrap-mode: char;
hexpand: true;
vexpand: true;
buffer: TextBuffer { };
}
}
Box {
hexpand: true;
halign: end;
styles ["toolbar"]
Button {
sensitive: false;
icon-name: "document-send-symbolic";
tooltip-text: _("Ask");
action-name: "app.ask";
}
Button {
sensitive: false;
icon-name: "edit-copy-symbolic";
tooltip-text: _("Copy to Clipboard");
hexpand: true;
halign: end;
action-name: "app.copy_prompt";
}
}
Adw.ViewStackPage {
name: "monet";
title: _("_Monet");
icon-name: "color-picker-symbolic";
child: Adw.PreferencesPage content_monet { };
use-underline: true;
}
Adw.ViewStackPage {
name: "plugins";
title: _("_Advanced");
icon-name: "settings-symbolic";
child: Adw.PreferencesPage content_plugins { };
use-underline: true;
}
}
Adw.ViewSwitcherBar {
stack: view_stack;
reveal: bind title.title-visible;
}
}
Adw.PreferencesGroup bot_group {
title: _("Bot");
Box {
orientation: vertical;
hexpand: true;
vexpand: true;
styles ["card", "text-box"]
ScrolledWindow {
margin-top:12;
margin-bottom:0;
margin-start:12;
margin-end:12;
TextView bot_text_view {
wrap-mode: char;
hexpand: true;
vexpand: true;
editable: false;
}
}
Box {
hexpand: true;
halign: end;
styles ["toolbar"]
Label status {
label: _("Ready");
}
Button {
sensitive: false;
icon-name: "edit-copy-symbolic";
tooltip-text: _("Copy to Clipboard");
hexpand: true;
halign: end;
action-name: "app.copy_bot";
}
}
}
}
}
}
}

View File

@ -19,12 +19,19 @@
import sys
import gi
import sys
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
gi.require_version('Gdk', '4.0')
from gi.repository import Gtk, Gio, Adw
from gi.repository import Gtk, Gio, Adw, Gdk
from .window import BavarderWindow
from .preferences import Preferences
from .constants import app_id, version
from baichat_py import BAIChat
class BavarderApplication(Adw.Application):
@ -36,6 +43,14 @@ class BavarderApplication(Adw.Application):
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):
"""Called when the application is activated.
@ -43,26 +58,91 @@ class BavarderApplication(Adw.Application):
We raise the application's main window, creating it if
necessary.
"""
win = self.props.active_window
if not win:
win = BavarderWindow(application=self)
win.present()
self.win = self.props.active_window
if not self.win:
self.win = BavarderWindow(application=self)
self.win.present()
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='com.github.Bavarder.Bavarder',
developer_name='Me',
version='0.1.0',
developers=['Me'],
copyright='© 2023 Me')
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.present()
def on_preferences_action(self, widget, _):
"""Callback for the app.preferences action."""
print('app.preferences action activated')
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')
(start, end) = text.get_bounds()
text = text.get_text(start, end, False)
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, _):
"""Callback for the app.copy_bot action."""
toast = Adw.Toast()
text = self.win.bot_text_view.get_buffer()
toast.set_title('Text copied')
(start, end) = text.get_bounds()
text = text.get_text(start, end, False)
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
def on_ask_action(self, widget, _):
"""Callback for the app.ask action."""
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 create_action(self, name, callback, shortcuts=None):
"""Add an application action.

View File

@ -27,13 +27,14 @@ configure_file(
'LOCALE_DIR': conf.get('LOCALE_DIR'),
}),
install: true,
install_dir: PY_INSTALLDIR.get_install_dir() / backenddir
install_dir: moduledir
)
bavarder_sources = [
'__init__.py',
'main.py',
'preferences.py',
'window.py',
]
install_data(bavarder_sources, install_dir: moduledir)
PY_INSTALLDIR.install_sources(bavarder_sources, subdir: moduledir)

View File

@ -18,11 +18,29 @@
# SPDX-License-Identifier: GPL-3.0-or-later
from gi.repository import Adw
from gi.repository import Gtk
from gi.repository import Gtk, Gio
@Gtk.Template(resource_path='/com/github/Bavarder/Bavarder/ui/window.ui')
class BavarderWindow(Adw.ApplicationWindow):
__gtype_name__ = 'BavarderWindow'
toast_overlay = Gtk.Template.Child()
prompt_text_view = Gtk.Template.Child()
bot_text_view = Gtk.Template.Child()
status = Gtk.Template.Child()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.settings = Gio.Settings(schema_id="com.github.Bavarder.Bavarder")
self.settings.bind("width", self, "default-width",
Gio.SettingsBindFlags.DEFAULT)
self.settings.bind("height", self, "default-height",
Gio.SettingsBindFlags.DEFAULT)
self.settings.bind("is-maximized", self, "maximized",
Gio.SettingsBindFlags.DEFAULT)
self.settings.bind("is-fullscreen", self, "fullscreened",
Gio.SettingsBindFlags.DEFAULT)