Skip to content
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

Notification workflow #253

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 0 additions & 3 deletions data/gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
<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>
Expand Down
1 change: 0 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ executable(
'src/ScreenshotBackend.vala',
'src/ScreenshotProxy.vala',
'src/ScreenshotWindow.vala',
'src/Widgets/SaveDialog.vala',
config_file,
dependencies: [
dependency('gdk-pixbuf-2.0'),
Expand Down
1 change: 0 additions & 1 deletion 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
13 changes: 13 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ public class Screenshot.Application : Gtk.Application {
}
});

var open_action = new SimpleAction ("open", VariantType.STRING);
open_action.activate.connect ((parameter) => {
var context = Gdk.Display.get_default ().get_app_launch_context ();
context.set_timestamp (Gdk.CURRENT_TIME);

try {
AppInfo.launch_default_for_uri ("file:///" + parameter.get_string (), context);
} catch (Error e) {
critical (e.message);
}
});

add_action (open_action);
add_action (quit_action);
set_accels_for_action ("app.quit", {"<Control>q", "Escape"});
}
Expand Down
115 changes: 42 additions & 73 deletions src/ScreenshotWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

public class Screenshot.ScreenshotWindow : Hdy.ApplicationWindow {
public bool to_clipboard { get; construct; }
public bool close_on_save { get; set; }
public bool mouse_pointer { get; set; }
public bool redact { get; set; }

Expand All @@ -46,7 +45,6 @@ public class Screenshot.ScreenshotWindow : Hdy.ApplicationWindow {
to_clipboard: clipboard
);

close_on_save = true;
from_command = true;
mouse_pointer = grab_pointer;
this.delay = int.max (0, delay);
Expand Down Expand Up @@ -95,12 +93,6 @@ public class Screenshot.ScreenshotWindow : Hdy.ApplicationWindow {
pointer_switch = new Gtk.Switch ();
pointer_switch.halign = Gtk.Align.START;

var close_label = new Gtk.Label (_("Close after saving:"));
close_label.halign = Gtk.Align.END;

var close_switch = new Gtk.Switch ();
close_switch.halign = Gtk.Align.START;

var redact_label = new Gtk.Label (_("Conceal text:"));
redact_label.halign = Gtk.Align.END;

Expand Down Expand Up @@ -139,8 +131,6 @@ public class Screenshot.ScreenshotWindow : Hdy.ApplicationWindow {
};
option_grid.attach (pointer_label, 0, 0);
option_grid.attach (pointer_switch, 1, 0);
option_grid.attach (close_label, 0, 1);
option_grid.attach (close_switch, 1, 1);
option_grid.attach (redact_label, 0, 2);
option_grid.attach (redact_switch, 1, 2);
option_grid.attach (delay_label, 0, 3);
Expand Down Expand Up @@ -170,8 +160,6 @@ public class Screenshot.ScreenshotWindow : Hdy.ApplicationWindow {
settings = new Settings ("io.elementary.screenshot");
settings.bind ("mouse-pointer", pointer_switch, "active", GLib.SettingsBindFlags.DEFAULT);
settings.bind ("mouse-pointer", this, "mouse-pointer", GLib.SettingsBindFlags.DEFAULT);
settings.bind ("close-on-save", close_switch, "active", GLib.SettingsBindFlags.DEFAULT);
settings.bind ("close-on-save", this, "close-on-save", GLib.SettingsBindFlags.DEFAULT);
settings.bind ("delay", delay_spin, "value", GLib.SettingsBindFlags.DEFAULT);
settings.bind ("redact", redact_switch, "active", GLib.SettingsBindFlags.DEFAULT);
settings.bind ("redact", this, "redact", GLib.SettingsBindFlags.DEFAULT);
Expand Down Expand Up @@ -242,23 +230,30 @@ public class Screenshot.ScreenshotWindow : Hdy.ApplicationWindow {
pointer_switch.sensitive = sensitive;
}

private void save_file (string file_name, string format, owned string folder_dir, Gdk.Pixbuf screenshot) throws GLib.Error {
private void save_file (Gdk.Pixbuf screenshot) throws GLib.Error {
var date_time = new DateTime.now_local ().format ("%Y-%m-%d %H.%M.%S");

/// TRANSLATORS: %s represents a timestamp here
string file_name = _("Screenshot from %s").printf (date_time);

if (scale_factor > 1) {
file_name += "@%ix".printf (scale_factor);
}

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

var folder_dir = settings.get_string ("folder-dir");
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;
}
Application.create_dir_if_missing (folder_dir);
folder_dir = Environment.get_user_special_dir (GLib.UserDirectory.PICTURES)
+ "%c".printf (Path.DIR_SEPARATOR) + Application.SAVE_FOLDER;
}

Application.create_dir_if_missing (folder_dir);

int attempt = 0;

string format = settings.get_string ("format");

do {
if (attempt == 0) {
full_file_name = Path.build_filename (folder_dir, "%s.%s".printf (file_name, format));
Expand All @@ -270,57 +265,36 @@ public class Screenshot.ScreenshotWindow : Hdy.ApplicationWindow {
} while (File.new_for_path (full_file_name).query_exists ());

screenshot.save (full_file_name, format);

var file_icon = new FileIcon (File.new_for_path (full_file_name));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not getting a notification when running as Flatpak although it works OK native. It seems to be linked to using a FileIcon (or at least, this one). Using a ThemedIcon works OK in Flatpak.


var readable_path = folder_dir.replace (Environment.get_home_dir () + "%c".printf (Path.DIR_SEPARATOR), "");

var notification = new Notification (_("Screenshot saved"));
notification.add_button (
_("Open"),
Action.print_detailed_name ("app.open", new Variant ("s", full_file_name))
);
notification.set_body (_("Saved to “%s”").printf (readable_path));
notification.set_icon (file_icon);
notification.set_priority (NotificationPriority.LOW);

GLib.Application.get_default ().send_notification (null, notification);
}

private void save_pixbuf (Gdk.Pixbuf screenshot) {
if (from_command == false) {
var save_dialog = new Screenshot.SaveDialog (screenshot, settings, this);
save_dialog.save_response.connect ((response, folder_dir, output_name, format) => {
save_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 (output, format, folder_dir, screenshot);

if (close_on_save) {
this.destroy ();
}
} catch (GLib.Error e) {
show_error_dialog (e.message);
}
}
});

save_dialog.close.connect (() => {
if (close_on_save) {
this.destroy ();
}
});

save_dialog.show_all ();
if (to_clipboard) {
Gtk.Clipboard.get_default (this.get_display ()).set_image (screenshot);
} else {
if (to_clipboard) {
Gtk.Clipboard.get_default (this.get_display ()).set_image (screenshot);
} else {
var date_time = new GLib.DateTime.now_local ().format ("%Y-%m-%d %H.%M.%S");

/// TRANSLATORS: %s represents a timestamp here
string file_name = _("Screenshot from %s").printf (date_time);
string format = settings.get_string ("format");
try {
save_file (file_name, format, "", screenshot);
} catch (GLib.Error e) {
show_error_dialog (e.message);
}
try {
save_file (screenshot);
} catch (GLib.Error e) {
show_error_dialog (e.message);
}
this.destroy ();
}

if (from_command) {
destroy ();
}
}

Expand All @@ -335,11 +309,6 @@ public class Screenshot.ScreenshotWindow : Hdy.ApplicationWindow {
Gdk.Pixbuf? pixbuf = null;
try {
pixbuf = backend.capture.end (res);
} catch (GLib.IOError.CANCELLED e) {
if (close_on_save) {
this.destroy ();
return;
}
} catch (Error e) {
show_error_dialog (e.message);
}
Expand Down
Loading