Skip to content

ScreenshotManager: Fix saving to clipboard #2017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading