image: Implement Copy to clipboard

This commit is contained in:
Maximiliano Sandoval R 2022-01-08 14:02:16 +01:00
parent 7b760f85f4
commit be09d49756
No known key found for this signature in database
GPG key ID: D64A8D747F6FE706
5 changed files with 36 additions and 0 deletions

View file

@ -101,6 +101,10 @@
<attribute name="label" translatable="yes">_Print…</attribute>
<attribute name="action">win.print</attribute>
</item>
<item>
<attribute name="label" translatable="yes">_Copy</attribute>
<attribute name="action">win.copy</attribute>
</item>
<!--<item>
<attribute name="label" translatable="yes">_Rotate</attribute>
<attribute name="action">win.rotate-image</attribute>

View file

@ -164,6 +164,7 @@ impl LpApplication {
self.set_accels_for_action("app.new-window", &["<Primary>N"]);
self.set_accels_for_action("win.open", &["<Primary>O"]);
self.set_accels_for_action("win.print", &["<Primary>P"]);
self.set_accels_for_action("win.copy", &["<Primary>C"]);
self.set_accels_for_action("win.show-help-overlay", &["<Primary>question"]);
self.set_accels_for_action("win.toggle-fullscreen", &["F11"]);
self.set_accels_for_action("window.close", &["<Primary>W"]);

View file

@ -205,4 +205,9 @@ impl LpImage {
let imp = self.imp();
imp.image_height.get()
}
pub fn texture(&self) -> Option<gdk::Texture> {
let imp = self.imp();
imp.texture.borrow().clone()
}
}

View file

@ -342,6 +342,19 @@ impl LpImageView {
Ok(())
}
pub fn copy(&self) -> anyhow::Result<()> {
let clipboard = self.clipboard();
let imp = self.imp();
if let Some(texture) = imp.picture.texture() {
clipboard.set_texture(&texture);
} else {
anyhow::bail!("No Image displayed.");
}
Ok(())
}
pub fn uri(&self) -> Option<String> {
let imp = self.imp();
imp.uri.borrow().to_owned()

View file

@ -107,6 +107,10 @@ mod imp {
win.print();
});
klass.install_action("win.copy", None, move |win, _, _| {
win.copy();
});
klass.install_action("win.show-toast", Some("(si)"), move |win, _, var| {
if let Some((ref toast, i)) = var.map(|v| v.get::<(String, i32)>()).flatten() {
win.show_toast(toast, adw::ToastPriority::__Unknown(i));
@ -303,6 +307,14 @@ impl LpWindow {
}
}
fn copy(&self) {
let imp = self.imp();
if let Err(e) = imp.image_view.copy() {
log::error!("Failed to copy to clipboard: {}", e);
}
}
fn show_toast(&self, text: &impl AsRef<str>, priority: adw::ToastPriority) {
let imp = self.imp();
@ -335,6 +347,7 @@ impl LpWindow {
self.action_set_enabled("win.set-wallpaper", enabled);
self.action_set_enabled("win.toggle-fullscreen", enabled);
self.action_set_enabled("win.print", enabled);
self.action_set_enabled("win.copy", enabled);
}
// Adapted from https://gitlab.gnome.org/GNOME/eog/-/blob/master/src/eog-window.c:eog_window_obtain_desired_size