feat: add Ctrl + {W,Q} for closing window(s) (fix #24)

This commit is contained in:
0xMRTT 2023-05-14 15:23:46 +02:00
parent d977c0ebfb
commit 62c5b4e788
Signed by: 0xMRTT
GPG Key ID: 19C1449A774028BD
6 changed files with 113 additions and 2 deletions

View File

@ -28,5 +28,8 @@
<key name="use-text-view" type="b">
<default>false</default>
</key>
<key name="close-all-without-dialog" type="b">
<default>false</default>
</key>
</schema>
</schemalist>

View File

@ -27,6 +27,12 @@
<property name="action-name">app.quit</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut window">Close all windows</property>
<property name="action-name">app.quit_all</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut window">Preferences</property>

View File

@ -28,6 +28,15 @@ template Preferences : Adw.PreferencesWindow {
valign: center;
}
}
Adw.ActionRow {
title: _("Close all windows without warning");
subtitle: _("All windows will be closed without warnnings, this can lead to data lose");
activatable-widget: close_all_without_dialog_switch;
Gtk.Switch close_all_without_dialog_switch {
valign: center;
}
}
}
Adw.PreferencesGroup provider_group {

View File

@ -93,7 +93,8 @@ class BavarderApplication(Adw.Application):
application_id="io.github.Bavarder.Bavarder",
flags=Gio.ApplicationFlags.DEFAULT_FLAGS,
)
self.create_action("quit", self.on_quit, ["<primary>q"])
self.create_action("quit_all", self.on_close_all, ["<primary>q"])
self.create_action("quit", self.on_quit, ["<primary>w"])
self.create_action("about", self.on_about_action)
self.create_action(
"preferences", self.on_preferences_action, ["<primary>comma"]
@ -116,6 +117,9 @@ class BavarderApplication(Adw.Application):
set(self.settings.get_strv("enabled-providers"))
)
self.latest_provider = self.settings.get_string("latest-provider")
self.close_all_without_dialog = self.settings.get_boolean(
"close-all-without-dialog"
)
self.use_theme = False
def quitting(self, *args, **kwargs):
@ -126,7 +130,6 @@ class BavarderApplication(Adw.Application):
print("Saving providers data...")
self.save_providers()
self.win.close()
@property
def win(self):
@ -158,9 +161,42 @@ class BavarderApplication(Adw.Application):
win.present()
def on_close_all(self, action, param):
print("Closing all windows...")
def close_all():
self.quitting()
for w in self.get_windows():
w.close()
if len(self.get_windows()) == 1:
self.on_quit(action, param)
elif self.close_all_without_dialog:
close_all()
else:
dialog = Adw.MessageDialog(
body="Are you sure you want to close all windows?",
transient_for=self.props.active_window,
)
dialog.add_response("cancel", "Cancel")
dialog.add_response("close", "Close")
dialog.set_response_appearance("close", Adw.ResponseAppearance.DESTRUCTIVE)
dialog.set_default_response("cancel")
dialog.set_close_response("cancel")
dialog.connect("response", self.on_close_all_response)
dialog.present()
def on_close_all_response(self, dialog, response):
if response == "close":
close_all()
dialog.close()
def on_quit(self, action, param):
"""Called when the user activates the Quit action."""
print("Closing active window...")
self.quitting()
self.win.close()
def save_providers(self):
r = {}

View File

@ -10,6 +10,7 @@ class Preferences(Adw.PreferencesWindow):
clear_after_send_switch = Gtk.Template.Child()
provider_group = Gtk.Template.Child()
use_text_view_switch = Gtk.Template.Child()
close_all_without_dialog_switch = Gtk.Template.Child()
def __init__(self, application, **kwargs):
super().__init__(**kwargs)
@ -27,6 +28,11 @@ class Preferences(Adw.PreferencesWindow):
"notify::active", self.on_use_text_view_switch_toggled
)
self.close_all_without_dialog_switch.set_active(self.app.close_all_without_dialog)
self.close_all_without_dialog_switch.connect(
"notify::active", self.on_close_all_without_dialog_switch_toggled
)
self.setup_providers()
def on_clear_after_send_switch_toggled(self, widget, *args):
@ -47,6 +53,15 @@ class Preferences(Adw.PreferencesWindow):
self.settings.set_boolean("use-text-view", False)
self.app.use_text_view = False
def on_close_all_without_dialog_switch_toggled(self, widget, *args):
"""Callback for the close_all_without_dialog_switch toggled event."""
if widget.get_active():
self.settings.set_boolean("close-all-without-dialog", True)
self.app.close_all_without_dialog = True
else:
self.settings.set_boolean("close-all-without-dialog", False)
self.app.close_all_without_dialog = False
def setup_providers(self):
# for provider in self.app.providers.values():
# try:

View File

@ -0,0 +1,42 @@
from .base import BavarderProvider
import requests
import shutil
class BaseOfflineProvider(BavarderProvider):
data = {
"setup": False,
"weight_path": "",
}
download_url = ""
def save(self):
return data
def load(self, data):
self.data = data
def download_file(self, url, filename=None):
if not filename:
filename = url.split('/')[-1]
with requests.get(url, stream=True) as r:
with open(filename, 'wb') as f:
shutil.copyfileobj(r.raw, f)
return filename
def setup(self):
if self.data["setup"]:
return
else:
self.data["setup"] = True
self.data["weight_path"] = self.download_file(self.download_url)
def ask(self, prompt):
self.setup()
return self._ask(prompt)
def _ask(self, prompt):
raise NotImplementedError()