Skip to content

Commit eef0467

Browse files
authored
Set "Open in Photos" as default action (#2440)
1 parent a7d79cc commit eef0467

File tree

2 files changed

+56
-46
lines changed

2 files changed

+56
-46
lines changed

src/NotificationsManager.vala

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ public class Gala.NotificationsManager : GLib.Object {
1111
[DBus (name = "org.freedesktop.Notifications")]
1212
private interface DBusNotifications : GLib.Object {
1313
public signal void action_invoked (uint32 id, string action_key);
14+
public signal void notification_closed (uint32 id, uint32 reason);
1415

1516
public abstract async uint32 notify (string app_name, uint32 replaces_id, string app_icon, string summary,
1617
string body, string[] actions, HashTable<string, Variant> hints, int32 expire_timeout) throws DBusError, IOError;
1718
}
1819

1920
private const int EXPIRE_TIMEOUT = 2000;
2021

21-
private GLib.SimpleActionGroup action_group = new GLib.SimpleActionGroup ();
22+
public signal void action_invoked (uint32 id, string name, GLib.Variant? target_value);
23+
public signal void notification_closed (uint32 id);
24+
2225
private DBusNotifications? notifications = null;
2326
private GLib.HashTable<string, uint32> replaces_id_table = new GLib.HashTable<string, uint32> (str_hash, str_equal);
2427

@@ -33,6 +36,7 @@ public class Gala.NotificationsManager : GLib.Object {
3336
try {
3437
notifications = ((DBusConnection) obj).get_proxy.end<DBusNotifications> (res);
3538
notifications.action_invoked.connect (handle_action_invoked);
39+
notifications.notification_closed.connect (handle_notification_closed);
3640
} catch (Error e) {
3741
warning ("NotificationsManager: Couldn't connect to notifications server: %s", e.message);
3842
notifications = null;
@@ -57,16 +61,14 @@ public class Gala.NotificationsManager : GLib.Object {
5761
return;
5862
}
5963

60-
if (action_group.has_action (name)) {
61-
action_group.activate_action (name, target_value);
62-
}
64+
action_invoked (id, name, target_value);
6365
}
6466

65-
public void add_action (GLib.Action action) {
66-
action_group.add_action (action);
67+
private void handle_notification_closed (uint32 id, uint32 reason) {
68+
notification_closed (id);
6769
}
6870

69-
public async void send (
71+
public async uint32? send (
7072
string component_name,
7173
string icon,
7274
string summary,
@@ -76,7 +78,7 @@ public class Gala.NotificationsManager : GLib.Object {
7678
) {
7779
if (notifications == null) {
7880
warning ("NotificationsManager: Unable to send notification. No connection to notification server");
79-
return;
81+
return null;
8082
}
8183

8284
uint32? replaces_id = replaces_id_table.get (component_name);
@@ -97,8 +99,11 @@ public class Gala.NotificationsManager : GLib.Object {
9799
);
98100

99101
replaces_id_table.insert (component_name, notification_id);
102+
103+
return notification_id;
100104
} catch (Error e) {
101105
critical ("NotificationsManager: There was an error sending a notification: %s", e.message);
106+
return null;
102107
}
103108
}
104109
}

src/ScreenshotManager.vala

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class Gala.ScreenshotManager : Object {
3838
}
3939

4040
private Settings desktop_settings;
41+
private GLib.HashTable<uint32, string?> notifications_id_to_path;
4142

4243
private string prev_font_regular;
4344
private string prev_font_document;
@@ -50,6 +51,7 @@ public class Gala.ScreenshotManager : Object {
5051

5152
construct {
5253
desktop_settings = new Settings ("org.gnome.desktop.interface");
54+
notifications_id_to_path = new GLib.HashTable<uint32, string> (GLib.direct_hash, GLib.direct_equal);
5355

5456
var keybinding_settings = new GLib.Settings ("io.elementary.desktop.wm.keybindings");
5557
unowned var display = wm.get_display ();
@@ -61,13 +63,8 @@ public class Gala.ScreenshotManager : Object {
6163
display.add_keybinding ("window-screenshot-clip", keybinding_settings, IGNORE_AUTOREPEAT, handle_screenshot);
6264
display.add_keybinding ("area-screenshot-clip", keybinding_settings, IGNORE_AUTOREPEAT, handle_screenshot);
6365

64-
var show_in_files_action = new GLib.SimpleAction ("show-in-files", GLib.VariantType.STRING);
65-
show_in_files_action.activate.connect (show_in_files);
66-
notifications_manager.add_action (show_in_files_action);
67-
68-
var open_in_photos_action = new GLib.SimpleAction ("open-in-photos", GLib.VariantType.STRING);
69-
open_in_photos_action.activate.connect (open_in_photos);
70-
notifications_manager.add_action (open_in_photos_action);
66+
notifications_manager.action_invoked.connect (handle_action_invoked);
67+
notifications_manager.notification_closed.connect ((id) => notifications_id_to_path.remove (id));
7168
}
7269

7370
[CCode (instance_pos = -1)]
@@ -113,7 +110,7 @@ public class Gala.ScreenshotManager : Object {
113110
yield screenshot (false, true, filename, out success, out filename_used);
114111

115112
if (success) {
116-
send_screenshot_notification (filename_used);
113+
send_screenshot_notification.begin (filename_used);
117114
}
118115
} catch (Error e) {
119116
warning (e.message);
@@ -131,7 +128,7 @@ public class Gala.ScreenshotManager : Object {
131128
yield screenshot_area (x, y, w, h, true, filename, out success, out filename_used);
132129

133130
if (success) {
134-
send_screenshot_notification (filename_used);
131+
send_screenshot_notification.begin (filename_used);
135132
}
136133
} catch (Error e) {
137134
warning (e.message);
@@ -147,73 +144,81 @@ public class Gala.ScreenshotManager : Object {
147144
yield screenshot_window (true, false, true, filename, out success, out filename_used);
148145

149146
if (success) {
150-
send_screenshot_notification (filename_used);
147+
send_screenshot_notification.begin (filename_used);
151148
}
152149
} catch (Error e) {
153150
warning (e.message);
154151
}
155152
}
156153

157-
private void send_screenshot_notification (string filename_used) {
154+
private async void send_screenshot_notification (string filename_used) {
158155
var clipboard = filename_used == "";
159156

160157
string[] actions = {};
161158
if (!clipboard) {
162159
var files_appinfo = AppInfo.get_default_for_type ("inode/directory", true);
163-
var photos_appinfo = AppInfo.get_default_for_type ("image/png", true);
164-
165-
var open_in_photos_action = GLib.Action.print_detailed_name (
166-
"open-in-photos",
167-
new Variant ("s", filename_used)
168-
);
169-
170-
/// TRANSLATORS: %s represents a name of image viewer
171-
var open_in_photos_label = _("Open in %s").printf (photos_appinfo.get_display_name ());
172160

173161
actions = {
174-
GLib.Action.print_detailed_name (
175-
"show-in-files",
176-
new Variant ("s", filename_used)),
177-
/// TRANSLATORS: %s represents a name of file manager
178-
_("Show in %s").printf (files_appinfo.get_display_name ()
179-
),
180-
// TODO: uncomment when https://github.com/elementary/notifications/issues/237 is fixed
181-
// open_in_photo_action
182-
// open_in_photos_label
162+
"default",
163+
"",
164+
165+
"show-in-files",
166+
/// TRANSLATORS: %s represents a name of file manager
167+
_("Show in %s").printf (files_appinfo.get_display_name ())
183168
};
184169
}
185170

186-
notifications_manager.send.begin (
171+
var notification_id = yield notifications_manager.send (
187172
"ScreenshotManager",
188173
"image-x-generic",
189174
_("Screenshot taken"),
190175
clipboard ? _("Screenshot is saved to clipboard") : _("Screenshot saved to screenshots folder"),
191176
actions,
192177
new GLib.HashTable<string, Variant> (null, null)
193178
);
179+
180+
if (notification_id != null && !clipboard) {
181+
notifications_id_to_path[notification_id] = filename_used;
182+
}
183+
}
184+
185+
private void handle_action_invoked (uint32 id, string name, GLib.Variant? target_value) {
186+
var path = notifications_id_to_path[id];
187+
if (path == null) {
188+
return;
189+
}
190+
191+
switch (name) {
192+
case "default":
193+
open_in_photo_viewer (path);
194+
break;
195+
case "show-in-files":
196+
show_in_files (path);
197+
break;
198+
}
194199
}
195200

196-
private void show_in_files (GLib.Variant? variant) requires (variant != null && variant.is_of_type (GLib.VariantType.STRING)) {
201+
private void open_in_photo_viewer (string path) {
197202
var files_list = new GLib.List<GLib.File> ();
198-
files_list.append (GLib.File.new_for_path (variant.get_string ()));
203+
files_list.append (GLib.File.new_for_path (path));
199204

200-
var files_appinfo = AppInfo.get_default_for_type ("inode/directory", true);
205+
var photos_appinfo = AppInfo.get_default_for_type ("image/png", true);
201206

202207
try {
203-
files_appinfo.launch (files_list, null);
208+
photos_appinfo.launch (files_list, null);
204209
} catch (Error e) {
205210
warning (e.message);
206211
}
207212
}
208213

209-
private void open_in_photos (GLib.Variant? variant) requires (variant != null && variant.is_of_type (GLib.VariantType.STRING)) {
214+
private void show_in_files (string path) {
210215
var files_list = new GLib.List<GLib.File> ();
211-
files_list.append (GLib.File.new_for_path (variant.get_string ()));
216+
files_list.append (GLib.File.new_for_path (path));
212217

213-
var photos_appinfo = AppInfo.get_default_for_type ("image/png", true);
218+
var files_appinfo = AppInfo.get_default_for_type ("inode/directory", true);
214219

215220
try {
216-
photos_appinfo.launch (files_list, null);
221+
files_appinfo.launch (files_list, null);
217222
} catch (Error e) {
218223
warning (e.message);
219224
}

0 commit comments

Comments
 (0)