edit_window: Add save and cancel shortcuts

This commit is contained in:
Sophie Herold 2025-01-31 13:49:20 +01:00
parent 11553bdab0
commit 8bb6f8da88
4 changed files with 70 additions and 11 deletions

View file

@ -286,6 +286,30 @@
<child> <child>
<object class="GtkShortcutsGroup"> <object class="GtkShortcutsGroup">
<property name="title" translatable="yes" context="shortcut window">Editing</property> <property name="title" translatable="yes" context="shortcut window">Editing</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut window">Open Editing View</property>
<property name="accelerator">E</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut window">Replace Original</property>
<property name="accelerator">&lt;Ctrl&gt;S</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut window">Save As</property>
<property name="accelerator">&lt;Ctrl&gt;&lt;Shift&gt;S</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut window">Cancel</property>
<property name="accelerator">Escape</property>
</object>
</child>
<child> <child>
<object class="GtkShortcutsShortcut"> <object class="GtkShortcutsShortcut">
<property name="title" translatable="yes" context="shortcut window">Rotate Clockwise</property> <property name="title" translatable="yes" context="shortcut window">Rotate Clockwise</property>

View file

@ -0,0 +1 @@
edit: `Ctrl+S` and `Ctrl+Shift+S` shortcuts save images and `Esc` to exit the editing view.

View file

@ -48,6 +48,8 @@ mod imp {
saving_status: TemplateChild<gtk::Label>, saving_status: TemplateChild<gtk::Label>,
#[template_child] #[template_child]
saving_info: TemplateChild<gtk::Revealer>, saving_info: TemplateChild<gtk::Revealer>,
#[template_child]
shortcut_controller: TemplateChild<gtk::ShortcutController>,
#[property(get, construct_only)] #[property(get, construct_only)]
original_image: OnceCell<LpImage>, original_image: OnceCell<LpImage>,
@ -82,6 +84,10 @@ mod imp {
async move { obj.imp().save_overwrite().await } async move { obj.imp().save_overwrite().await }
)); ));
}); });
klass.install_action("edit.cancel", None, move |obj, _, _| {
obj.imp().cancel();
});
} }
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) { fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
@ -96,17 +102,6 @@ mod imp {
let obj = self.obj(); let obj = self.obj();
self.cancel.connect_clicked(glib::clone!(
#[weak]
obj,
move |_| {
if let Some(cancellable) = obj.imp().save_cancellable.replace(None) {
cancellable.cancel();
}
obj.window().show_image();
}
));
glib::spawn_future_local(glib::clone!( glib::spawn_future_local(glib::clone!(
#[weak] #[weak]
obj, obj,
@ -115,6 +110,30 @@ mod imp {
obj.action_set_enabled("edit.save-overwrite", can_trash); obj.action_set_enabled("edit.save-overwrite", can_trash);
} }
)); ));
self.shortcut_controller.add_shortcut(gtk::Shortcut::new(
Some(gtk::KeyvalTrigger::new(
gdk::Key::S,
gdk::ModifierType::CONTROL_MASK,
)),
Some(gtk::NamedAction::new("edit.save-overwrite")),
));
self.shortcut_controller.add_shortcut(gtk::Shortcut::new(
Some(gtk::KeyvalTrigger::new(
gdk::Key::S,
gdk::ModifierType::CONTROL_MASK.union(gdk::ModifierType::SHIFT_MASK),
)),
Some(gtk::NamedAction::new("edit.save-copy")),
));
self.shortcut_controller.add_shortcut(gtk::Shortcut::new(
Some(gtk::KeyvalTrigger::new(
gdk::Key::Escape,
gdk::ModifierType::NO_MODIFIER_MASK,
)),
Some(gtk::NamedAction::new("edit.cancel")),
));
} }
} }
@ -136,6 +155,15 @@ mod imp {
impl BinImpl for LpEditWindow {} impl BinImpl for LpEditWindow {}
impl LpEditWindow { impl LpEditWindow {
fn cancel(&self) {
let obj = self.obj();
if let Some(cancellable) = obj.imp().save_cancellable.replace(None) {
cancellable.cancel();
}
obj.window().show_image();
}
async fn save_copy(&self) { async fn save_copy(&self) {
let obj = self.obj(); let obj = self.obj();
self.done.popdown(); self.done.popdown();

View file

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<interface> <interface>
<template class="LpEditWindow" parent="AdwBin"> <template class="LpEditWindow" parent="AdwBin">
<child>
<object class="GtkShortcutController" id="shortcut_controller">
<property name="scope">global</property>
</object>
</child>
<property name="child"> <property name="child">
<object class="AdwToolbarView" id="toolbar_view"> <object class="AdwToolbarView" id="toolbar_view">
<property name="top-bar-style">raised</property> <property name="top-bar-style">raised</property>
@ -58,6 +63,7 @@
<object class="GtkButton" id="cancel"> <object class="GtkButton" id="cancel">
<property name="label" translatable="true">_Cancel</property> <property name="label" translatable="true">_Cancel</property>
<property name="use-underline">true</property> <property name="use-underline">true</property>
<property name="action-name">edit.cancel</property>
</object> </object>
</child> </child>
<child type="end"> <child type="end">