Skip to content

Commit 0323d9d

Browse files
committed
Use ListModels for apps and workspaces
1 parent 1f2c181 commit 0323d9d

11 files changed

Lines changed: 135 additions & 151 deletions

File tree

src/AppSystem/App.vala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public class Dock.App : Object {
103103

104104
notify["pinned"].connect (() => {
105105
check_remove ();
106-
ItemManager.get_default ().sync_pinned ();
107106
});
108107

109108
WindowSystem.get_default ().notify["active-workspace"].connect (() => {

src/AppSystem/AppSystem.vala

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ public class Dock.AppSystem : Object, UnityClient {
1515
return instance.once (() => { return new AppSystem (); });
1616
}
1717

18-
public signal void app_added (App app);
18+
private ListStore apps_store;
19+
public ListModel apps { get { return apps_store; } }
1920

2021
private GLib.HashTable<unowned string, App> id_to_app;
2122

2223
private AppSystem () { }
2324

2425
construct {
26+
apps_store = new ListStore (typeof (App));
27+
apps_store.items_changed.connect (save_pinned);
28+
2529
id_to_app = new HashTable<unowned string, App> (str_hash, str_equal);
2630
}
2731

@@ -41,12 +45,46 @@ public class Dock.AppSystem : Object, UnityClient {
4145

4246
private App add_app (DesktopAppInfo app_info, bool pinned) {
4347
var app = new App (app_info, pinned);
48+
app.removed.connect (remove_app);
49+
app.notify["pinned"].connect (save_pinned);
4450
id_to_app[app_info.get_id ()] = app;
45-
app.removed.connect ((_app) => id_to_app.remove (_app.app_info.get_id ()));
46-
app_added (app);
51+
apps_store.append (app);
4752
return app;
4853
}
4954

55+
private void remove_app (App app) {
56+
id_to_app.remove (app.app_info.get_id ());
57+
58+
uint pos;
59+
if (apps_store.find (app, out pos)) {
60+
apps_store.remove (pos);
61+
}
62+
}
63+
64+
public void reorder_app (App app, uint new_index) {
65+
uint pos;
66+
if (!apps_store.find (app, out pos)) {
67+
warning ("Tried to reorder an app that is not in the store");
68+
return;
69+
}
70+
71+
apps_store.remove (pos);
72+
apps_store.insert (new_index, app);
73+
}
74+
75+
private void save_pinned () {
76+
string[] new_pinned_ids = {};
77+
78+
for (uint i = 0; i < apps_store.get_n_items (); i++) {
79+
var app = (App) apps_store.get_item (i);
80+
if (app.pinned) {
81+
new_pinned_ids += app.app_info.get_id ();
82+
}
83+
}
84+
85+
settings.set_strv ("launchers", new_pinned_ids);
86+
}
87+
5088
public async void sync_windows () {
5189
var windows = WindowSystem.get_default ().windows;
5290

@@ -79,20 +117,20 @@ public class Dock.AppSystem : Object, UnityClient {
79117
}
80118
}
81119

82-
public void add_app_for_id (string app_id) {
120+
public App? add_app_for_id (string app_id) {
83121
if (app_id in id_to_app) {
84122
id_to_app[app_id].pinned = true;
85-
return;
123+
return id_to_app[app_id];
86124
}
87125

88126
var app_info = new DesktopAppInfo (app_id);
89127

90128
if (app_info == null) {
91129
warning ("App not found: %s", app_id);
92-
return;
130+
return null;
93131
}
94132

95-
add_app (app_info, true);
133+
return add_app (app_info, true);
96134
}
97135

98136
public void remove_app_by_id (string app_id) {

src/AppSystem/Background/BackgroundItem.vala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
*/
77

88
public class Dock.BackgroundItem : BaseIconGroup {
9-
public signal void apps_appeared ();
9+
private ListStore group_store;
10+
public ListModel group_model { get { return group_store; } }
1011

1112
public BackgroundMonitor monitor { private get; construct; }
12-
public bool has_apps { get { return monitor.background_apps.get_n_items () > 0; } }
1313

1414
private Gtk.Popover popover;
1515

@@ -25,6 +25,8 @@ public class Dock.BackgroundItem : BaseIconGroup {
2525
}
2626

2727
construct {
28+
group_store = new ListStore (typeof (BackgroundItem));
29+
2830
var list_box = new Gtk.ListBox () {
2931
selection_mode = BROWSE
3032
};
@@ -53,9 +55,9 @@ public class Dock.BackgroundItem : BaseIconGroup {
5355
monitor.background_apps.items_changed.connect ((pos, n_removed, n_added) => {
5456
if (monitor.background_apps.get_n_items () == 0) {
5557
popover.popdown ();
56-
removed ();
58+
group_store.remove (0);
5759
} else if (n_removed == 0 && n_added != 0 && n_added == monitor.background_apps.get_n_items ()) {
58-
apps_appeared ();
60+
group_store.append (this);
5961
}
6062
});
6163

src/AppSystem/Launcher.vala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ public class Dock.Launcher : BaseItem {
148148
});
149149

150150
app.launched.connect (animate_launch);
151-
app.removed.connect (() => removed ());
152151

153152
var bounce_animation_target = new Adw.CallbackAnimationTarget ((val) => {
154153
var height = overlay.get_height ();

src/BaseItem.vala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class Dock.BaseItem : Gtk.Box {
2222
dock_settings = new GLib.Settings ("io.elementary.dock");
2323
}
2424

25-
public signal void removed ();
2625
public signal void revealed_done ();
2726

2827
/**
@@ -307,7 +306,7 @@ public class Dock.BaseItem : Gtk.Box {
307306

308307
private bool is_allowed_drop (Value val) {
309308
var obj = val.get_object ();
310-
return obj != null && obj is BaseItem && ((BaseItem) obj).group == group;
309+
return obj != null && obj is BaseItem && ((BaseItem) obj).group == group && obj != this;
311310
}
312311

313312
private class PopoverTooltip : Gtk.Popover {

src/ItemGroup.vala

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
*/
77

88
public class Dock.ItemGroup : Gtk.Fixed {
9+
private const string OBJECT_DATA_KEY = "item-group-obj";
10+
11+
[CCode (has_target = false)]
12+
public delegate BaseItem CreateBaseItemFunc (Object obj);
13+
914
private static Settings settings;
1015

1116
public ListModel items { get; construct; }
17+
public CreateBaseItemFunc create_item_func { get; construct; }
1218

1319
private Sequence<BaseItem> item_store;
1420
private ListStore current_children;
@@ -17,8 +23,11 @@
1723

1824
private bool relayout_queued = false;
1925

20-
public ItemGroup (ListModel items) {
21-
Object (items: items);
26+
private HashTable<Object, BaseItem> cached_items;
27+
private uint clear_cache_id = 0;
28+
29+
public ItemGroup (ListModel items, CreateBaseItemFunc create_item_func) {
30+
Object (items: items, create_item_func: create_item_func);
2231
}
2332

2433
static construct {
@@ -42,6 +51,8 @@
4251
on_items_changed (0, 0, items.get_n_items ());
4352

4453
overflow = VISIBLE;
54+
55+
cached_items = new HashTable<Object, BaseItem> (null, null);
4556
}
4657

4758
private void queue_relayout () {
@@ -87,18 +98,44 @@
8798
private void on_items_changed (uint position, uint removed, uint added) {
8899
var start_iter = item_store.get_iter_at_pos ((int) position);
89100
var end_iter = start_iter.move ((int) removed);
101+
start_iter.foreach_range (end_iter, cache_item);
90102
start_iter.foreach_range (end_iter, remove_item);
91103
start_iter.remove_range (end_iter);
92104

93105
var insert_iter = item_store.get_iter_at_pos ((int) position);
94106
for (int i = (int) position; i < position + added; i++) {
95-
var item = (BaseItem) items.get_item (i);
107+
var item = get_or_create_item (items.get_item (i));
96108
insert_iter.insert_before (item);
97109

98110
add_item (i, item);
99111
}
100112
}
101113

114+
// Make sure that if an item is removed and added again in the same
115+
// mainloop iteration it gets mapped to the same BaseItem
116+
private void cache_item (BaseItem item) {
117+
cached_items[item.get_data<Object> (OBJECT_DATA_KEY)] = item;
118+
119+
if (clear_cache_id == 0) {
120+
clear_cache_id = Idle.add_once (clear_cache);
121+
}
122+
}
123+
124+
private void clear_cache () {
125+
cached_items.remove_all ();
126+
clear_cache_id = 0;
127+
}
128+
129+
private BaseItem get_or_create_item (Object obj) {
130+
if (obj in cached_items) {
131+
return cached_items[obj];
132+
}
133+
134+
var base_item = create_item_func (obj);
135+
base_item.set_data<Object> (OBJECT_DATA_KEY, obj);
136+
return base_item;
137+
}
138+
102139
private void add_item (int pos, BaseItem item) {
103140
if (item.parent == this) {
104141
// The item was already in this group and is currently being removed
@@ -119,6 +156,8 @@
119156
private void remove_item (BaseItem item) {
120157
item.revealed_done.connect (finish_remove);
121158
item.set_revealed (false);
159+
160+
cached_items[item.get_data<Object> ("dock-obj")] = item;
122161
}
123162

124163
private void finish_remove (BaseItem item) {

0 commit comments

Comments
 (0)