Save-Dialog: Added a Save Dialog for Ctrl+Q Unsaved Changes
Changed the logic of app.quit accelerator, introduced a dialog in it. Also made functions such is_save_senstive() and save_overwrite() publicly accessible for logic building. Fixes: #537 Part-of: <https://gitlab.gnome.org/GNOME/loupe/-/merge_requests/576>
This commit is contained in:
parent
1088a7128f
commit
f2d4fa6e0d
3 changed files with 73 additions and 3 deletions
|
|
@ -20,6 +20,7 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
use crate::util::gettext::*;
|
||||||
use adw::prelude::*;
|
use adw::prelude::*;
|
||||||
use adw::subclass::prelude::*;
|
use adw::subclass::prelude::*;
|
||||||
|
|
||||||
|
|
@ -133,7 +134,55 @@ impl LpApplication {
|
||||||
.activate(|app: &Self, _, _| app.show_help())
|
.activate(|app: &Self, _, _| app.show_help())
|
||||||
.build(),
|
.build(),
|
||||||
gio::ActionEntryBuilder::new("quit")
|
gio::ActionEntryBuilder::new("quit")
|
||||||
.activate(|app: &Self, _, _| app.quit())
|
.activate(|app: &Self, _, _| {
|
||||||
|
if let Some(win) = app.active_window() {
|
||||||
|
if let Some(lp_window) = win.downcast_ref::<LpWindow>() {
|
||||||
|
if let Some(edit_window) = lp_window.edit_window() {
|
||||||
|
if edit_window.has_unsaved_changes() {
|
||||||
|
let dialog = adw::AlertDialog::new(
|
||||||
|
Some(&gettext("Unsaved Changes")),
|
||||||
|
Some(&gettext(
|
||||||
|
"Changes which are not lost will be permanently lost",
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
|
||||||
|
dialog.add_response("cancel", &gettext("Cancel"));
|
||||||
|
dialog.add_response("discard", &gettext("Discard"));
|
||||||
|
dialog.add_response("save", &gettext("Save"));
|
||||||
|
|
||||||
|
let app_clone = app.clone();
|
||||||
|
let win_clone = win.clone();
|
||||||
|
let edit_window_clone = edit_window.clone();
|
||||||
|
let dialog_clone = dialog.clone();
|
||||||
|
|
||||||
|
dialog.set_response_appearance(
|
||||||
|
"discard",
|
||||||
|
adw::ResponseAppearance::Destructive,
|
||||||
|
);
|
||||||
|
|
||||||
|
glib::MainContext::default().spawn_local(async move {
|
||||||
|
let res = dialog_clone.choose_future(&win_clone).await;
|
||||||
|
println!("User response: {}", res);
|
||||||
|
match res.as_str() {
|
||||||
|
"cancel" => {}
|
||||||
|
"discard" => {
|
||||||
|
app_clone.quit();
|
||||||
|
}
|
||||||
|
"save" => {
|
||||||
|
edit_window_clone.save_as();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
dialog.present(Some(&win));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
app.quit();
|
||||||
|
}
|
||||||
|
})
|
||||||
.build(),
|
.build(),
|
||||||
gio::ActionEntryBuilder::new("new-window")
|
gio::ActionEntryBuilder::new("new-window")
|
||||||
.activate(|app: &Self, _, _| {
|
.activate(|app: &Self, _, _| {
|
||||||
|
|
|
||||||
|
|
@ -244,7 +244,7 @@ mod imp {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn save_overwrite(&self) {
|
pub(super) async fn save_overwrite(&self) {
|
||||||
let obj = self.obj();
|
let obj = self.obj();
|
||||||
self.save.popdown();
|
self.save.popdown();
|
||||||
|
|
||||||
|
|
@ -434,7 +434,7 @@ mod imp {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_save_sensitive(&self) -> bool {
|
pub(super) fn is_save_sensitive(&self) -> bool {
|
||||||
let obj = self.obj();
|
let obj = self.obj();
|
||||||
|
|
||||||
self.operations
|
self.operations
|
||||||
|
|
@ -485,6 +485,20 @@ impl LpEditWindow {
|
||||||
imp.update_save_state(false);
|
imp.update_save_state(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn has_unsaved_changes(&self) -> bool {
|
||||||
|
self.imp().is_save_sensitive()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save_as(&self) {
|
||||||
|
glib::spawn_future_local(glib::clone!(
|
||||||
|
#[weak(rename_to=win)]
|
||||||
|
self,
|
||||||
|
async move {
|
||||||
|
win.imp().save_overwrite().await;
|
||||||
|
}
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_operation(&self, operation: glycin::Operation) {
|
pub fn add_operation(&self, operation: glycin::Operation) {
|
||||||
let imp = self.imp();
|
let imp = self.imp();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -268,6 +268,13 @@ impl LpWindow {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn edit_window(&self) -> Option<LpEditWindow> {
|
||||||
|
self.imp()
|
||||||
|
.edit_window_child
|
||||||
|
.child()
|
||||||
|
.and_then(|w| w.downcast::<LpEditWindow>().ok())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn image_view(&self) -> LpImageView {
|
pub fn image_view(&self) -> LpImageView {
|
||||||
self.imp().image_window.image_view()
|
self.imp().image_window.image_view()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue