Skip to content

Commit 40d8a87

Browse files
committed
Make sure only a single app exists for an id
1 parent 0323d9d commit 40d8a87

3 files changed

Lines changed: 62 additions & 64 deletions

File tree

src/AppSystem/App.vala

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ public class Dock.App : Object {
2020
}
2121
}
2222

23-
public signal void removed ();
24-
25-
public bool pinned { get; construct set; }
2623
public GLib.DesktopAppInfo app_info { get; construct; }
2724

25+
public bool pinned { get; set; }
26+
2827
public bool count_visible { get; private set; default = false; }
2928
public int64 current_count { get; private set; default = 0; }
3029
public bool progress_visible { get; set; default = false; }
@@ -52,8 +51,8 @@ public class Dock.App : Object {
5251

5352
private static Dock.SwitcherooControl switcheroo_control;
5453

55-
public App (GLib.DesktopAppInfo app_info, bool pinned) {
56-
Object (app_info: app_info, pinned: pinned);
54+
public App (GLib.DesktopAppInfo app_info) {
55+
Object (app_info: app_info);
5756
}
5857

5958
static construct {
@@ -101,10 +100,6 @@ public class Dock.App : Object {
101100
app_action_group.add_action (simple_action);
102101
}
103102

104-
notify["pinned"].connect (() => {
105-
check_remove ();
106-
});
107-
108103
WindowSystem.get_default ().notify["active-workspace"].connect (() => {
109104
notify_property ("running-on-active-workspace");
110105
});
@@ -165,12 +160,6 @@ public class Dock.App : Object {
165160
return false;
166161
}
167162

168-
private void check_remove () {
169-
if (!pinned && !running) {
170-
removed ();
171-
}
172-
}
173-
174163
public void update_windows (GLib.GenericArray<Window>? new_windows) {
175164
if (new_windows == null) {
176165
windows = new GLib.GenericArray<Window> ();
@@ -184,8 +173,6 @@ public class Dock.App : Object {
184173
if (launching && running) {
185174
launching = false;
186175
}
187-
188-
check_remove ();
189176
}
190177

191178
public void perform_unity_update (VariantIter prop_iter) {

src/AppSystem/AppSystem.vala

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,51 @@ public class Dock.AppSystem : Object, UnityClient {
2929
id_to_app = new HashTable<unowned string, App> (str_hash, str_equal);
3030
}
3131

32-
public App? get_app (string id) {
33-
return id_to_app[id];
34-
}
32+
public App? get_app_by_id (string id) {
33+
if (!(id in id_to_app)) {
34+
var app_info = new DesktopAppInfo (id);
3535

36-
public async void load () {
37-
foreach (string app_id in settings.get_strv ("launchers")) {
38-
var app_info = new GLib.DesktopAppInfo (app_id);
39-
add_app (app_info, true);
40-
}
36+
if (app_info == null) {
37+
return null;
38+
}
4139

42-
yield sync_windows ();
43-
WindowSystem.get_default ().notify["windows"].connect (sync_windows);
44-
}
40+
var app = new App (app_info);
41+
app.notify["pinned"].connect (save_pinned);
42+
app.notify["pinned"].connect (check_app);
43+
app.notify["running"].connect (check_app);
4544

46-
private App add_app (DesktopAppInfo app_info, bool pinned) {
47-
var app = new App (app_info, pinned);
48-
app.removed.connect (remove_app);
49-
app.notify["pinned"].connect (save_pinned);
50-
id_to_app[app_info.get_id ()] = app;
51-
apps_store.append (app);
52-
return app;
45+
id_to_app[app_info.get_id ()] = app;
46+
}
47+
48+
return id_to_app[id];
5349
}
5450

55-
private void remove_app (App app) {
56-
id_to_app.remove (app.app_info.get_id ());
51+
private void check_app (Object obj, ParamSpec pspec) {
52+
var app = (App) obj;
5753

5854
uint pos;
59-
if (apps_store.find (app, out pos)) {
55+
var exists = apps_store.find (app, out pos);
56+
57+
if ((app.pinned || app.running) && !exists) {
58+
apps_store.append (app);
59+
} else if ((!app.pinned && !app.running) && exists) {
6060
apps_store.remove (pos);
6161
}
6262
}
6363

64+
public async void load () {
65+
foreach (string app_id in settings.get_strv ("launchers")) {
66+
var app = get_app_by_id (app_id);
67+
if (app == null) {
68+
continue;
69+
}
70+
app.pinned = true;
71+
}
72+
73+
yield sync_windows ();
74+
WindowSystem.get_default ().notify["windows"].connect (sync_windows);
75+
}
76+
6477
public void reorder_app (App app, uint new_index) {
6578
uint pos;
6679
if (!apps_store.find (app, out pos)) {
@@ -90,14 +103,9 @@ public class Dock.AppSystem : Object, UnityClient {
90103

91104
var app_window_list = new GLib.HashTable<App, GLib.GenericArray<Window>> (direct_hash, direct_equal);
92105
foreach (var window in windows) {
93-
App? app = id_to_app[window.app_id];
106+
var app = get_app_by_id (window.app_id);
94107
if (app == null) {
95-
var app_info = new GLib.DesktopAppInfo (window.app_id);
96-
if (app_info == null) {
97-
continue;
98-
}
99-
100-
app = add_app (app_info, false);
108+
continue;
101109
}
102110

103111
var window_list = app_window_list.get (app);
@@ -118,24 +126,21 @@ public class Dock.AppSystem : Object, UnityClient {
118126
}
119127

120128
public App? add_app_for_id (string app_id) {
121-
if (app_id in id_to_app) {
122-
id_to_app[app_id].pinned = true;
123-
return id_to_app[app_id];
124-
}
129+
var app = get_app_by_id (app_id);
125130

126-
var app_info = new DesktopAppInfo (app_id);
127-
128-
if (app_info == null) {
129-
warning ("App not found: %s", app_id);
131+
if (app == null) {
130132
return null;
131133
}
132134

133-
return add_app (app_info, true);
135+
app.pinned = true;
136+
return app;
134137
}
135138

136139
public void remove_app_by_id (string app_id) {
137-
if (app_id in id_to_app) {
138-
id_to_app[app_id].pinned = false;
140+
var app = get_app_by_id (app_id);
141+
142+
if (app != null) {
143+
app.pinned = false;
139144
}
140145
}
141146

@@ -160,17 +165,19 @@ public class Dock.AppSystem : Object, UnityClient {
160165
parameters.get ("(sa{sv})", out app_uri, out prop_iter);
161166

162167
var app_id = app_uri.replace ("application://", "");
163-
if (id_to_app[app_id] != null) {
164-
id_to_app[app_id].perform_unity_update (prop_iter);
168+
var app = get_app_by_id (app_id);
169+
if (app != null) {
170+
app.perform_unity_update (prop_iter);
165171
} else {
166172
critical ("unable to update missing launcher: %s", app_id);
167173
}
168174
}
169175

170176
private void remove_launcher_entry (string sender_name) {
171177
var app_id = sender_name + ".desktop";
172-
if (id_to_app[app_id] != null) {
173-
id_to_app[app_id].remove_launcher_entry ();
178+
var app = get_app_by_id (app_id);
179+
if (app != null) {
180+
app.remove_launcher_entry ();
174181
}
175182
}
176183
}

src/ItemManager.vala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,19 @@
8282

8383
var app_system = AppSystem.get_default ();
8484

85-
var app = app_system.get_app (app_info.get_id ());
86-
if (app != null) {
85+
var app = app_system.get_app_by_id (app_info.get_id ());
86+
87+
if (app == null) {
88+
return;
89+
}
90+
91+
if (app.pinned || app.running) {
92+
// Already in the dock
8793
app.pinned = true;
8894
drop_target_file.reject ();
8995
return;
9096
}
9197

92-
app = app_system.add_app_for_id (app_info.get_id ());
93-
9498
for (var child = app_group.get_first_child (); child != null; child = child.get_next_sibling ()) {
9599
if (child is Launcher && child.app == app) {
96100
added_launcher = (Launcher) child;

0 commit comments

Comments
 (0)