Skip to content

Commit 229b261

Browse files
Merge branch 'main' into lenemter/set-open-in-photos-as-default-action
2 parents 2df49b5 + 5bbf6ed commit 229b261

File tree

4 files changed

+129
-18
lines changed

4 files changed

+129
-18
lines changed

lib/AppSystem.vala

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,64 @@ public class Gala.AppSystem : GLib.Object {
1414
private GLib.HashTable<unowned string, Gala.App> id_to_app;
1515
private GLib.HashTable<string, string> startup_wm_class_to_id;
1616
private Gala.AppCache app_cache;
17+
private string[] all_desktop_files = {};
18+
private GLib.FileMonitor[]? directory_monitors;
1719

1820
construct {
1921
id_to_app = new GLib.HashTable<unowned string, Gala.App> (str_hash, str_equal);
2022
startup_wm_class_to_id = new GLib.HashTable<string, string> (str_hash, str_equal);
2123
running_apps = new GLib.HashTable<Gala.App, unowned Gala.App> (null, null);
2224
app_cache = new AppCache ();
25+
26+
update_desktop_files ();
27+
}
28+
29+
private void update_desktop_files () {
30+
var data_dirs = Environment.get_system_data_dirs ();
31+
data_dirs += Environment.get_user_data_dir ();
32+
33+
var create_monitors = directory_monitors == null;
34+
if (create_monitors) {
35+
directory_monitors = {};
36+
}
37+
38+
foreach (unowned string data_dir in data_dirs) {
39+
var app_dir = Path.build_filename (data_dir, "applications");
40+
if (FileUtils.test (app_dir, FileTest.EXISTS)) {
41+
try {
42+
foreach (var name in enumerate_children (app_dir)) {
43+
if (!name.contains ("~") && name.has_suffix (".desktop")) {
44+
all_desktop_files += name;
45+
}
46+
}
47+
48+
if (!create_monitors) {
49+
continue;
50+
}
51+
52+
var monitor = File.new_for_path (app_dir).monitor (GLib.FileMonitorFlags.NONE, null);
53+
monitor.changed.connect ((file, other_file, event_type) => {
54+
if (event_type == GLib.FileMonitorEvent.CHANGES_DONE_HINT) {
55+
update_desktop_files ();
56+
}
57+
});
58+
directory_monitors += monitor;
59+
} catch (Error e) {
60+
debug ("Error inside %s: %s", app_dir, e.message);
61+
}
62+
}
63+
}
64+
}
65+
66+
private string[] enumerate_children (string dir) throws Error {
67+
string[] result = {};
68+
FileInfo? file_info;
69+
var enumerator = File.new_for_path (dir).enumerate_children (FileAttribute.STANDARD_NAME, 0);
70+
while ((file_info = enumerator.next_file ()) != null) {
71+
result += file_info.get_name ();
72+
}
73+
74+
return result;
2375
}
2476

2577
public unowned Gala.App? lookup_app (string id) {
@@ -105,6 +157,20 @@ public class Gala.AppSystem : GLib.Object {
105157
return lookup_heuristic_basename (desktop_file);
106158
}
107159

160+
public unowned Gala.App? guess_app_by_id (string _id) {
161+
var id = _id.ascii_down ();
162+
unowned Gala.App? result = null;
163+
164+
foreach (var name in all_desktop_files) {
165+
// Try to find desktop file based on the application name
166+
if (name.contains (id) && (result = lookup_app (name)) != null) {
167+
return result;
168+
}
169+
}
170+
171+
return null;
172+
}
173+
108174
public void notify_app_state_changed (Gala.App app) {
109175
if (app.state == Gala.AppState.RUNNING) {
110176
running_apps.insert (app, app);

lib/Drawing/StyleManager.vala

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ public class Gala.Drawing.StyleManager : Object {
1818
}
1919

2020
[DBus (name="io.elementary.pantheon.AccountsService")]
21-
private interface AccountsService : DBusProxy {
21+
private interface PantheonAccountsService : DBusProxy {
2222
public abstract int prefers_color_scheme { get; set; }
2323
public abstract int prefers_accent_color { get; set; }
2424
}
2525

26+
[DBus (name="io.elementary.SettingsDaemon.AccountsService")]
27+
private interface SettingsDaemonAccountsService : DBusProxy {
28+
public abstract int accent_color { get; set; }
29+
}
30+
2631
private const string FDO_ACCOUNTS_NAME = "org.freedesktop.Accounts";
2732
private const string FDO_ACCOUNTS_PATH = "/org/freedesktop/Accounts";
2833

@@ -37,10 +42,18 @@ public class Gala.Drawing.StyleManager : Object {
3742
public ColorScheme prefers_color_scheme { get; private set; default = LIGHT; }
3843
public Gdk.RGBA theme_accent_color { get; private set; default = DEFAULT_ACCENT_COLOR; }
3944

40-
private AccountsService? accounts_service_proxy;
45+
private PantheonAccountsService? pantheon_proxy;
46+
private SettingsDaemonAccountsService? settings_daemon_proxy;
4147

4248
construct {
43-
Bus.watch_name (SYSTEM, FDO_ACCOUNTS_NAME, NONE, () => connect_to_accounts_service.begin (), () => accounts_service_proxy = null);
49+
Bus.watch_name (
50+
SYSTEM, FDO_ACCOUNTS_NAME, NONE,
51+
() => connect_to_accounts_service.begin (),
52+
() => {
53+
pantheon_proxy = null;
54+
settings_daemon_proxy = null;
55+
}
56+
);
4457
}
4558

4659
private async void connect_to_accounts_service () {
@@ -49,24 +62,27 @@ public class Gala.Drawing.StyleManager : Object {
4962

5063
var path = yield accounts.find_user_by_name (Environment.get_user_name ());
5164

52-
accounts_service_proxy = yield Bus.get_proxy<AccountsService> (SYSTEM, FDO_ACCOUNTS_NAME, path, GET_INVALIDATED_PROPERTIES);
65+
pantheon_proxy = yield Bus.get_proxy<PantheonAccountsService> (SYSTEM, FDO_ACCOUNTS_NAME, path, GET_INVALIDATED_PROPERTIES);
66+
settings_daemon_proxy = yield Bus.get_proxy<SettingsDaemonAccountsService> (SYSTEM, FDO_ACCOUNTS_NAME, path, GET_INVALIDATED_PROPERTIES);
5367
} catch {
5468
warning ("Could not connect to AccountsService. Default accent color will be used");
5569
return;
5670
}
5771

58-
update_color_scheme (accounts_service_proxy.prefers_color_scheme);
59-
update_color (accounts_service_proxy.prefers_accent_color);
72+
update_color_scheme (pantheon_proxy.prefers_color_scheme);
73+
update_color (settings_daemon_proxy.accent_color);
6074

61-
accounts_service_proxy.g_properties_changed.connect ((changed, invalid) => {
62-
var value = changed.lookup_value ("PrefersAccentColor", new VariantType ("i"));
75+
pantheon_proxy.g_properties_changed.connect ((changed, invalid) => {
76+
var value = changed.lookup_value ("PrefersColorScheme", new VariantType ("i"));
6377
if (value != null) {
64-
update_color (value.get_int32 ());
78+
update_color_scheme (value.get_int32 ());
6579
}
80+
});
6681

67-
value = changed.lookup_value ("PrefersColorScheme", new VariantType ("i"));
82+
settings_daemon_proxy.g_properties_changed.connect ((changed, invalid) => {
83+
var value = changed.lookup_value ("AccentColor", new VariantType ("i"));
6884
if (value != null) {
69-
update_color_scheme (value.get_int32 ());
85+
update_color (value.get_int32 ());
7086
}
7187
});
7288
}

src/DesktopIntegration.vala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ public class Gala.DesktopIntegration : GLib.Object {
3737
unowned var display = wm.get_display ();
3838
unowned var workspace_manager = display.get_workspace_manager ();
3939

40-
workspace_manager.active_workspace_changed.connect (() => {
41-
active_workspace_changed ();
42-
windows_changed (); // windows have 'on-active-workspace' property that we need to update
43-
});
40+
workspace_manager.active_workspace_changed.connect (() => active_workspace_changed ());
4441
workspace_manager.workspaces_reordered.connect (() => windows_changed ());
4542
workspace_manager.workspace_added.connect (() => windows_changed ());
4643
workspace_manager.workspace_removed.connect ((index) => {
@@ -99,7 +96,6 @@ public class Gala.DesktopIntegration : GLib.Object {
9996
public Window[] get_windows () throws GLib.DBusError, GLib.IOError {
10097
Window[] returned_windows = {};
10198
var apps = Gala.AppSystem.get_default ().get_running_apps ();
102-
unowned var active_workspace = wm.get_display ().get_workspace_manager ().get_active_workspace ();
10399
foreach (unowned var app in apps) {
104100
foreach (weak Meta.Window window in app.get_windows ()) {
105101
if (!is_eligible_window (window)) {
@@ -116,7 +112,6 @@ public class Gala.DesktopIntegration : GLib.Object {
116112
properties.insert ("client-type", new GLib.Variant.uint32 (window.get_client_type ()));
117113
properties.insert ("is-hidden", new GLib.Variant.boolean (window.is_hidden ()));
118114
properties.insert ("has-focus", new GLib.Variant.boolean (window.has_focus ()));
119-
properties.insert ("on-active-workspace", new GLib.Variant.boolean (window.located_on_workspace (active_workspace)));
120115
properties.insert ("workspace-index", new GLib.Variant.int32 (window.get_workspace ().index ()));
121116
properties.insert ("width", new GLib.Variant.uint32 (frame_rect.width));
122117
properties.insert ("height", new GLib.Variant.uint32 (frame_rect.height));

src/WindowTracker.vala

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,34 @@ public class Gala.WindowTracker : GLib.Object {
199199
return null;
200200
}
201201

202+
private unowned Gala.App? get_app_by_guessing (Meta.Window window) {
203+
unowned var app_system = Gala.AppSystem.get_default ();
204+
unowned Gala.App? result = null;
205+
206+
unowned string? id = window.get_gtk_application_id ();
207+
// if id contains '.' it's probably a valid app id but it doesn't provide the desktop file
208+
if (id != null && !id.contains (".") && (result = app_system.guess_app_by_id (id)) != null) {
209+
return result;
210+
}
211+
212+
id = window.get_sandboxed_app_id ();
213+
if (id != null && !id.contains (".") && (result = app_system.guess_app_by_id (id)) != null) {
214+
return result;
215+
}
216+
217+
id = window.get_wm_class ();
218+
if (id != null && !id.contains (".") && (result = app_system.guess_app_by_id (id)) != null) {
219+
return result;
220+
}
221+
222+
id = window.get_wm_class_instance ();
223+
if (id != null && !id.contains (".") && (result = app_system.guess_app_by_id (id)) != null) {
224+
return result;
225+
}
226+
227+
return null;
228+
}
229+
202230
public Gala.App get_app_for_window (Meta.Window window) {
203231
unowned Meta.Window? transient_for = window.get_transient_for ();
204232
if (transient_for != null) {
@@ -279,9 +307,15 @@ public class Gala.WindowTracker : GLib.Object {
279307
return result;
280308
}
281309

310+
/* Try to carefully guess .desktop file name based on application ids and wm class
311+
*/
312+
result = get_app_by_guessing (window);
313+
if (result != null) {
314+
return result;
315+
}
316+
282317
/* Our last resort - we create a fake app from the window */
283318
return new Gala.App.for_window (window);
284-
285319
}
286320

287321
private void tracked_window_changed (Meta.Window window) {

0 commit comments

Comments
 (0)