Skip to content

Commit 602568b

Browse files
authored
Add button to the screenshot notification (#2241)
1 parent a99f429 commit 602568b

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

src/DBusAccelerator.vala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ public class Gala.DBusAccelerator {
167167
icon,
168168
label,
169169
"",
170+
{},
170171
hints
171172
);
172173
}

src/NotificationsManager.vala

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
public class Gala.NotificationsManager : GLib.Object {
1111
[DBus (name = "org.freedesktop.Notifications")]
1212
private interface DBusNotifications : GLib.Object {
13+
public signal void action_invoked (uint32 id, string action_key);
14+
1315
public abstract async uint32 notify (string app_name, uint32 replaces_id, string app_icon, string summary,
1416
string body, string[] actions, HashTable<string, Variant> hints, int32 expire_timeout) throws DBusError, IOError;
1517
}
1618

1719
private const int EXPIRE_TIMEOUT = 2000;
1820

21+
private GLib.SimpleActionGroup action_group = new GLib.SimpleActionGroup ();
1922
private DBusNotifications? notifications = null;
2023
private GLib.HashTable<string, uint32> replaces_id_table = new GLib.HashTable<string, uint32> (str_hash, str_equal);
2124

@@ -29,6 +32,7 @@ public class Gala.NotificationsManager : GLib.Object {
2932
(obj, res) => {
3033
try {
3134
notifications = ((DBusConnection) obj).get_proxy.end<DBusNotifications> (res);
35+
notifications.action_invoked.connect (handle_action_invoked);
3236
} catch (Error e) {
3337
warning ("NotificationsManager: Couldn't connect to notifications server: %s", e.message);
3438
notifications = null;
@@ -42,11 +46,32 @@ public class Gala.NotificationsManager : GLib.Object {
4246
notifications = null;
4347
}
4448

49+
private void handle_action_invoked (uint32 id, string action_name) {
50+
string name;
51+
GLib.Variant? target_value;
52+
53+
try {
54+
GLib.Action.parse_detailed_name (action_name, out name, out target_value);
55+
} catch (Error e) {
56+
warning ("NotificationsManager: Couldn't parse action: %s", e.message);
57+
return;
58+
}
59+
60+
if (action_group.has_action (name)) {
61+
action_group.activate_action (name, target_value);
62+
}
63+
}
64+
65+
public void add_action (GLib.Action action) {
66+
action_group.add_action (action);
67+
}
68+
4569
public async void send (
4670
string component_name,
4771
string icon,
4872
string summary,
4973
string body,
74+
string[] actions,
5075
GLib.HashTable<string, Variant> hints
5176
) {
5277
if (notifications == null) {
@@ -66,7 +91,7 @@ public class Gala.NotificationsManager : GLib.Object {
6691
icon,
6792
summary,
6893
body,
69-
{},
94+
actions,
7095
hints,
7196
EXPIRE_TIMEOUT
7297
);

src/ScreenshotManager.vala

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ namespace Gala {
2222

2323
[DBus (name="org.gnome.Shell.Screenshot")]
2424
public class ScreenshotManager : Object {
25-
private WindowManager wm;
25+
[DBus (visible = false)]
26+
public WindowManager wm { get; construct; }
27+
[DBus (visible = false)]
28+
public NotificationsManager notifications_manager { get; construct; }
29+
2630
private Settings desktop_settings;
2731

2832
private string prev_font_regular;
@@ -31,12 +35,29 @@ namespace Gala {
3135
private uint conceal_timeout;
3236

3337
[DBus (visible = false)]
34-
public ScreenshotManager (WindowManager _wm) {
35-
wm = _wm;
38+
public ScreenshotManager (WindowManager wm, NotificationsManager notifications_manager) {
39+
Object (wm: wm, notifications_manager: notifications_manager);
3640
}
3741

3842
construct {
3943
desktop_settings = new Settings ("org.gnome.desktop.interface");
44+
45+
var show_in_files_action = new GLib.SimpleAction ("show-in-files", GLib.VariantType.STRING);
46+
show_in_files_action.activate.connect (show_in_files);
47+
notifications_manager.add_action (show_in_files_action);
48+
}
49+
50+
private void show_in_files (GLib.Variant? variant) requires (variant != null && variant.is_of_type (GLib.VariantType.STRING)) {
51+
var files_list = new GLib.List<GLib.File> ();
52+
files_list.append (GLib.File.new_for_path (variant.get_string ()));
53+
54+
var files_appinfo = AppInfo.get_default_for_type ("inode/directory", true);
55+
56+
try {
57+
files_appinfo.launch (files_list, null);
58+
} catch (Error e) {
59+
warning (e.message);
60+
}
4061
}
4162

4263
public void flash_area (int x, int y, int width, int height) throws DBusError, IOError {

src/WindowManager.vala

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ namespace Gala {
193193
unowned Meta.Display display = get_display ();
194194

195195
notifications_manager = new NotificationsManager ();
196-
screenshot_manager = new ScreenshotManager (this);
196+
screenshot_manager = new ScreenshotManager (this, notifications_manager);
197197
DBus.init (this, notifications_manager, screenshot_manager);
198198

199199
WindowListener.init (display);
@@ -2324,7 +2324,7 @@ namespace Gala {
23242324
yield screenshot_manager.screenshot_window (true, false, true, filename, out success, out filename_used);
23252325

23262326
if (success) {
2327-
send_screenshot_notification (clipboard);
2327+
send_screenshot_notification (filename_used);
23282328
}
23292329
} catch (Error e) {
23302330
// Ignore this error
@@ -2342,7 +2342,7 @@ namespace Gala {
23422342
yield screenshot_manager.screenshot_area (x, y, w, h, true, filename, out success, out filename_used);
23432343

23442344
if (success) {
2345-
send_screenshot_notification (clipboard);
2345+
send_screenshot_notification (filename_used);
23462346
}
23472347
} catch (Error e) {
23482348
// Ignore this error
@@ -2357,19 +2357,28 @@ namespace Gala {
23572357
yield screenshot_manager.screenshot (false, true, filename, out success, out filename_used);
23582358

23592359
if (success) {
2360-
send_screenshot_notification (clipboard);
2360+
send_screenshot_notification (filename_used);
23612361
}
23622362
} catch (Error e) {
23632363
// Ignore this error
23642364
}
23652365
}
23662366

2367-
private void send_screenshot_notification (bool clipboard) {
2367+
private void send_screenshot_notification (string filename_used) {
2368+
var clipboard = filename_used == null;
2369+
2370+
string[] actions = {};
2371+
if (!clipboard) {
2372+
/// TRANSLATORS: 'Files' is the name of file manager used by elementary OS
2373+
actions = { GLib.Action.print_detailed_name ("show-in-files", new Variant ("s", filename_used)), _("Show in Files") };
2374+
}
2375+
23682376
notifications_manager.send.begin (
23692377
"ScreenshotManager",
23702378
"image-x-generic",
23712379
_("Screenshot taken"),
23722380
clipboard ? _("Screenshot is saved to clipboard") : _("Screenshot saved to screenshots folder"),
2381+
actions,
23732382
new GLib.HashTable<string, Variant> (null, null)
23742383
);
23752384
}

0 commit comments

Comments
 (0)