Skip to content

Commit

Permalink
Use the portal (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
leolost2605 authored Aug 12, 2024
1 parent 1f989fe commit 87d7563
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 412 deletions.
22 changes: 0 additions & 22 deletions data/gschema.xml
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
<schemalist>
<enum id="io.elementary.screenshot.capture-mode">
<value nick="screen" value="0"/>
<value nick="window" value="1"/>
<value nick="area" value="2"/>
</enum>

<schema id="io.elementary.screenshot" path="/io/elementary/screenshot/" gettext-domain="io.elementary.screenshot">
<key name="format" type="s">
<default>"png"</default>
</key>
<key name="mouse-pointer" type="b">
<default>false</default>
</key>
<key name="close-on-save" type="b">
<default>false</default>
</key>
<key name="redact" type="b">
<default>false</default>
</key>
<key name="delay" type="i">
<range min="0" max="15"/>
<default>1</default>
</key>
<key name="folder-dir" type="s">
<default>""</default>
</key>
<key name='last-capture-mode' enum='io.elementary.screenshot.capture-mode'>
<default>"screen"</default>
</key>
</schema>
</schemalist>
4 changes: 2 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ executable(
icon_res,
'src/Application.vala',
'src/CaptureType.vala',
'src/SaveDialog.vala',
'src/ScreenshotBackend.vala',
'src/ScreenshotProxy.vala',
'src/ScreenshotWindow.vala',
'src/Widgets/SaveDialog.vala',
config_file,
dependencies: [
dependency('gdk-pixbuf-2.0'),
Expand All @@ -45,6 +44,7 @@ executable(
dependency('gtk+-3.0'),
dependency('granite', version: '>=6.0.0'),
dependency('libhandy-1', version: '>=0.83.0'),
dependency('libportal'),
meson.get_compiler('vala').find_library('posix'),
],
install : true
Expand Down
3 changes: 1 addition & 2 deletions po/POTFILES
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
src/Application.vala
src/ScreenshotBackend.vala
src/ScreenshotWindow.vala
src/Widgets/SaveDialog.vala
src/SaveDialog.vala
128 changes: 117 additions & 11 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
public class Screenshot.Application : Gtk.Application {
public const string SAVE_FOLDER = _("Screenshots");

private ScreenshotWindow window = null;

private static bool area = false;
private static bool clipboard = false;
private static bool grab_pointer = false;
Expand All @@ -30,6 +28,8 @@ public class Screenshot.Application : Gtk.Application {
private static bool win = false;
private static int delay = 1;

private Settings settings = new Settings ("io.elementary.screenshot");

private const string CAPTURE_AREA = N_("Capture area");
private const string CAPTURE_STRING = N_("Capture the whole screen");
private const string CAPTURE_WIN = N_("Capture active window");
Expand Down Expand Up @@ -96,18 +96,124 @@ public class Screenshot.Application : Gtk.Application {
if (area) action = 3;

if (action == 0) {
if (window == null) {
window = new ScreenshotWindow ();
window.set_application (this);
window.show_all ();
take_screenshot.begin ();
} else {
take_screenshot_backend.begin (action - 1);
}
}

private async void take_screenshot () {
hold ();

var portal = new Xdp.Portal ();

try {
var file_uri = yield portal.take_screenshot (null, INTERACTIVE, null);

var pixbuf = new Gdk.Pixbuf.from_file (Filename.from_uri (file_uri, null));

show_save_dialog (pixbuf);
} catch (Error e) {
warning ("Failed to take screenshot via portal: %s", e.message);
} finally {
release ();
}
}

private async void take_screenshot_backend (CaptureType capture_type) {
hold ();

var backend = new ScreenshotBackend ();

try {
var pixbuf = yield backend.capture (capture_type, delay, grab_pointer, redact);

if (pixbuf != null) {
show_save_dialog (pixbuf);
}
} catch (GLib.IOError.CANCELLED e) {
//Do nothing
} catch (Error e) {
show_error_dialog (e.message);
} finally {
release ();
}
}

window.present ();
} else {
window = new ScreenshotWindow.from_cmd (action, delay, grab_pointer, redact, clipboard);
window.set_application (this);
window.take_clicked ();
private void show_save_dialog (Gdk.Pixbuf pixbuf) {
var save_dialog = new SaveDialog (pixbuf, settings);
save_dialog.set_application (this);

save_dialog.save_response.connect ((dialog, response, folder_dir, output_name, format) => {
dialog.destroy ();

if (response) {
string[] formats = {".png", ".jpg", ".jpeg", ".bmp", ".tiff"};
string output = output_name;

foreach (string type in formats) {
output = output.replace (type, "");
}

try {
save_file (dialog.pixbuf, output, format, folder_dir);
} catch (GLib.Error e) {
show_error_dialog (e.message);
}
} else {
take_screenshot.begin ();
}
});

save_dialog.present ();
}

private void save_file (Gdk.Pixbuf pixbuf, string file_name, string format, owned string folder_dir) throws GLib.Error {
if (pixbuf == null) {
critical ("Pixbuf is null");
return;
}

string full_file_name = "";
string folder_from_settings = "";

if (folder_dir == "") {
folder_from_settings = settings.get_string ("folder-dir");
if (folder_from_settings != "") {
folder_dir = folder_from_settings;
} else {
folder_dir = GLib.Environment.get_user_special_dir (GLib.UserDirectory.PICTURES)
+ "%c".printf (GLib.Path.DIR_SEPARATOR) + Application.SAVE_FOLDER;
}
create_dir_if_missing (folder_dir);
}

int attempt = 0;

do {
if (attempt == 0) {
full_file_name = Path.build_filename (folder_dir, "%s.%s".printf (file_name, format));
} else {
full_file_name = Path.build_filename (folder_dir, "%s (%d).%s".printf (file_name, attempt, format));
}

attempt++;
} while (File.new_for_path (full_file_name).query_exists ());

pixbuf.save (full_file_name, format);
}

private void show_error_dialog (string error_message) {
var dialog = new Granite.MessageDialog.with_image_from_icon_name (
_("Could not capture screenshot"),
_("Image not saved"),
"dialog-error",
Gtk.ButtonsType.CLOSE
);
dialog.set_application (this);
dialog.show_error_details (error_message);
dialog.response.connect (dialog.destroy);
dialog.present ();
}

public static void create_dir_if_missing (string path) {
Expand Down
5 changes: 2 additions & 3 deletions src/Widgets/SaveDialog.vala → src/SaveDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@ public class Screenshot.SaveDialog : Granite.Dialog {
private Gtk.Label folder_name;
private Gtk.Image folder_image;

public SaveDialog (Gdk.Pixbuf pixbuf, Settings settings, Gtk.Window parent) {
public SaveDialog (Gdk.Pixbuf pixbuf, Settings settings) {
Object (
deletable: false,
modal: true,
pixbuf: pixbuf,
settings: settings,
title: _("Screenshot"),
transient_for: parent
title: _("Screenshot")
);
}

Expand Down
Loading

0 comments on commit 87d7563

Please sign in to comment.