Skip to content

Commit

Permalink
ScreenshotManager: Fix saving to clipboard (#2017)
Browse files Browse the repository at this point in the history
  • Loading branch information
lenemter authored Aug 12, 2024
1 parent ee00380 commit e0624ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
27 changes: 20 additions & 7 deletions src/ScreenshotManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ namespace Gala {
return Environment.get_home_dir ();
}

private static async bool save_image (Cairo.ImageSurface image, string filename, float scale, out string used_filename) {
private async bool save_image (Cairo.ImageSurface image, string filename, float scale, out string used_filename) {
return (filename != "")
? yield save_image_to_file (image, filename, scale, out used_filename)
: save_image_to_clipboard (image, filename, out used_filename);
Expand Down Expand Up @@ -342,19 +342,32 @@ namespace Gala {
}
}

private static bool save_image_to_clipboard (Cairo.ImageSurface image, string filename, out string used_filename) {
private bool save_image_to_clipboard (Cairo.ImageSurface image, string filename, out string used_filename) {
used_filename = filename;

unowned Gdk.Display display = Gdk.Display.get_default ();
unowned Gtk.Clipboard clipboard = Gtk.Clipboard.get_default (display);

var screenshot = Gdk.pixbuf_get_from_surface (image, 0, 0, image.get_width (), image.get_height ());
if (screenshot == null) {
warning ("could not save screenshot to clipboard: null pixbuf");
warning ("Could not save screenshot to clipboard: null pixbuf");
return false;
}

uint8[] buffer;
try {
screenshot.save_to_buffer (out buffer, "png");
} catch (Error e) {
warning ("Could not save screenshot to clipboard: failed to save image to buffer: %s", e.message);
return false;
}

try {
unowned var selection = wm.get_display ().get_selection ();
var source = new Meta.SelectionSourceMemory ("image/png", new GLib.Bytes.take (buffer));
selection.set_owner (Meta.SelectionType.SELECTION_CLIPBOARD, source);
} catch (Error e) {
warning ("Could not save screenshot to clipboard: failed to create new Meta.SelectionSourceMemory: %s", e.message);
return false;
}

clipboard.set_image (screenshot);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion vapi/libmutter.vapi
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ namespace Meta {
[CCode (cheader_filename = "meta/meta-selection-source-memory.h", type_id = "meta_selection_source_memory_get_type ()")]
public sealed class SelectionSourceMemory : Meta.SelectionSource {
[CCode (has_construct_function = false, type = "MetaSelectionSource*")]
public SelectionSourceMemory (string mimetype, GLib.Bytes content);
public SelectionSourceMemory (string mimetype, GLib.Bytes content) throws GLib.Error;
}
[CCode (cheader_filename = "meta/meta-settings.h", has_type_id = false)]
[Compact]
Expand Down

0 comments on commit e0624ac

Please sign in to comment.