From c0dec156088e337b77fa9175f82b042277b3da89 Mon Sep 17 00:00:00 2001 From: Leonhard Kargl Date: Mon, 24 Nov 2025 17:31:29 +0100 Subject: [PATCH 1/4] Introduce an ItemGroup --- src/BaseItem.vala | 6 +- src/ItemGroup.vala | 134 +++++++++++++++++++++++++++++++++++++ src/ItemManager.vala | 153 +++++++++++-------------------------------- src/meson.build | 1 + 4 files changed, 177 insertions(+), 117 deletions(-) create mode 100644 src/ItemGroup.vala diff --git a/src/BaseItem.vala b/src/BaseItem.vala index c676093f..bdbea4c4 100644 --- a/src/BaseItem.vala +++ b/src/BaseItem.vala @@ -133,7 +133,7 @@ public class Dock.BaseItem : Gtk.Box { reveal.done.connect (set_revealed_finish); var animation_target = new Adw.CallbackAnimationTarget ((val) => { - ItemManager.get_default ().move (this, val, 0); + ((Gtk.Fixed) parent).move (this, val, 0); current_pos = val; }); @@ -219,6 +219,10 @@ public class Dock.BaseItem : Gtk.Box { * when moving a launcher so that its current_pos is always up to date. */ public void animate_move (double new_position) { + if (timed_animation.value_to == new_position) { + return; + } + timed_animation.value_from = current_pos; timed_animation.value_to = new_position; diff --git a/src/ItemGroup.vala b/src/ItemGroup.vala new file mode 100644 index 00000000..2ffa88e0 --- /dev/null +++ b/src/ItemGroup.vala @@ -0,0 +1,134 @@ +/* + * SPDX-License-Identifier: GPL-3.0 + * SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io) + * + * Authored by: Leonhard Kargl + */ + + public class Dock.ItemGroup : Gtk.Fixed { + private static Settings settings; + + public ListModel items { get; construct; } + + private Sequence item_store; + private ListStore current_children; + + private Adw.TimedAnimation resize_animation; + + private bool relayout_queued = false; + + public ItemGroup (ListModel items) { + Object (items: items); + } + + static construct { + settings = new Settings ("io.elementary.dock"); + } + + construct { + item_store = new Sequence (); + + current_children = new ListStore (typeof (BaseItem)); + current_children.items_changed.connect (queue_relayout); + + settings.changed["icon-size"].connect (queue_relayout); + + var animation_target = new Adw.PropertyAnimationTarget (this, "width-request"); + + resize_animation = new Adw.TimedAnimation (this, 0, 0, Granite.TRANSITION_DURATION_OPEN, animation_target); + resize_animation.done.connect (on_resized); + + items.items_changed.connect (on_items_changed); + on_items_changed (0, 0, items.get_n_items ()); + + overflow = VISIBLE; + } + + private void queue_relayout () { + if (relayout_queued) { + return; + } + + relayout_queued = true; + Idle.add_once (relayout); + } + + private void relayout () { + resize_animation.value_from = width_request; + resize_animation.value_to = get_launcher_size () * current_children.get_n_items (); + resize_animation.duration = resize_animation.value_from < resize_animation.value_to ? + Granite.TRANSITION_DURATION_OPEN : Granite.TRANSITION_DURATION_CLOSE; + resize_animation.play (); + + for (uint i = 0; i < current_children.get_n_items (); i++) { + var item = (BaseItem) current_children.get_item (i); + item.animate_move (get_launcher_size () * i); + } + + relayout_queued = false; + } + + private static int get_launcher_size () { + return settings.get_int ("icon-size") + Launcher.PADDING * 2; + } + + private void on_resized () { + // When we finished resizing we know we now have enough space for all new items + // so reveal them + for (uint i = 0; i < current_children.get_n_items (); i++) { + var item = (BaseItem) current_children.get_item (i); + if (!item.visible) { + item.visible = true; + item.set_revealed (true); + } + } + } + + private void on_items_changed (uint position, uint removed, uint added) { + var start_iter = item_store.get_iter_at_pos ((int) position); + var end_iter = start_iter.move ((int) removed); + start_iter.foreach_range (end_iter, remove_item); + start_iter.remove_range (end_iter); + + var insert_iter = item_store.get_iter_at_pos ((int) position); + for (int i = (int) position; i < position + added; i++) { + var item = (BaseItem) items.get_item (i); + insert_iter.insert_before (item); + + add_item (i, item); + } + } + + private void add_item (int pos, BaseItem item) { + if (item.parent == this) { + // The item was already in this group and is currently being removed + // so immediately finish the removal and add it as if it was new + // This happens when the items are repositioned via dnd + finish_remove (item); + } + + item.visible = false; + + var item_pos = get_launcher_size () * pos; + put (item, item_pos, 0); + item.current_pos = item_pos; + + current_children.insert (pos, item); + } + + private void remove_item (BaseItem item) { + item.revealed_done.connect (finish_remove); + item.set_revealed (false); + } + + private void finish_remove (BaseItem item) { + item.revealed_done.disconnect (finish_remove); + + remove (item); + + uint index; + if (current_children.find (item, out index)) { + current_children.remove (index); + } + } +} diff --git a/src/ItemManager.vala b/src/ItemManager.vala index ee5a25a6..0a008599 100644 --- a/src/ItemManager.vala +++ b/src/ItemManager.vala @@ -3,7 +3,7 @@ * SPDX-FileCopyrightText: 2023-2025 elementary, Inc. (https://elementary.io) */ - public class Dock.ItemManager : Gtk.Fixed { + public class Dock.ItemManager : Gtk.Box { private static Settings settings; private static GLib.Once instance; @@ -13,10 +13,9 @@ public Launcher? added_launcher { get; set; default = null; } - private Adw.TimedAnimation resize_animation; - private GLib.GenericArray launchers; // Only used to keep track of launcher indices + private ListStore launchers; private BackgroundItem background_item; - private GLib.GenericArray icon_groups; // Only used to keep track of icon group indices + private ListStore icon_groups; // Only used to keep track of icon group indices private DynamicWorkspaceIcon dynamic_workspace_item; #if WORKSPACE_SWITCHER @@ -28,34 +27,32 @@ } construct { - launchers = new GLib.GenericArray (); + launchers = new ListStore (typeof (Launcher)); background_item = new BackgroundItem (); background_item.apps_appeared.connect (add_item); - icon_groups = new GLib.GenericArray (); + icon_groups = new ListStore (typeof (WorkspaceIconGroup)); #if WORKSPACE_SWITCHER dynamic_workspace_item = new DynamicWorkspaceIcon (); - separator = new Gtk.Separator (VERTICAL); + separator = new Gtk.Separator (VERTICAL) { + valign = START, + margin_top = Launcher.PADDING, + }; settings.bind ("icon-size", separator, "height-request", GET); - put (separator, 0, 0); #endif + append (new ItemGroup (launchers)); + append (background_item); +#if WORKSPACE_SWITCHER + append (separator); + append (new ItemGroup (icon_groups)); + append (dynamic_workspace_item); +#endif overflow = VISIBLE; - resize_animation = new Adw.TimedAnimation ( - this, 0, 0, 0, - new Adw.CallbackAnimationTarget ((val) => { - width_request = (int) val; - }) - ); - - resize_animation.done.connect (() => width_request = -1); //Reset otherwise we stay to big when the launcher icon size changes - - settings.changed["icon-size"].connect (reposition_items); - var drop_target_file = new Gtk.DropTarget (typeof (File), COPY) { preload = true }; @@ -175,49 +172,10 @@ }); } - private void reposition_items () { - int index = 0; - foreach (var launcher in launchers) { - position_item (launcher, ref index); - } - - if (background_item.has_apps) { - position_item (background_item, ref index); - } - -#if WORKSPACE_SWITCHER - var separator_y = (get_launcher_size () - separator.height_request) / 2; - move (separator, index * get_launcher_size () - 1, separator_y); -#endif - - foreach (var icon_group in icon_groups) { - position_item (icon_group, ref index); - } - -#if WORKSPACE_SWITCHER - position_item (dynamic_workspace_item, ref index); -#endif - } - - private void position_item (BaseItem item, ref int index) { - var position = get_launcher_size () * index; - - if (item.parent != this) { - put (item, position, 0); - item.current_pos = position; - } else { - item.animate_move (position); - } - - index++; - } - private void add_launcher_via_dnd (Launcher launcher, int index) { launcher.removed.connect (remove_item); launchers.insert (index, launcher); - reposition_items (); - launcher.set_revealed (true); sync_pinned (); } @@ -225,85 +183,46 @@ item.removed.connect (remove_item); if (item is Launcher) { - launchers.add ((Launcher) item); + launchers.append (item); sync_pinned (); } else if (item is WorkspaceIconGroup) { - icon_groups.add ((WorkspaceIconGroup) item); + icon_groups.append (item); } - - ulong reveal_cb = 0; - reveal_cb = resize_animation.done.connect (() => { - resize_animation.disconnect (reveal_cb); - reposition_items (); - item.set_revealed (true); - }); - - resize_animation.easing = EASE_OUT_BACK; - resize_animation.duration = Granite.TRANSITION_DURATION_OPEN; - resize_animation.value_from = get_width (); - resize_animation.value_to = launchers.length * get_launcher_size (); - resize_animation.play (); } private void remove_item (BaseItem item) { + ListStore store; if (item is Launcher) { - launchers.remove ((Launcher) item); + store = launchers; } else if (item is WorkspaceIconGroup) { - icon_groups.remove ((WorkspaceIconGroup) item); + store = icon_groups; + } else { + return; } - item.removed.disconnect (remove_item); - item.revealed_done.connect (remove_finish); - item.set_revealed (false); - } - - private void remove_finish (BaseItem item) { - // Temporarily set the width request to avoid flicker until the animation calls the callback for the first time - width_request = get_width (); - - remove (item); - reposition_items (); - - resize_animation.easing = EASE_IN_OUT_QUAD; - resize_animation.duration = Granite.TRANSITION_DURATION_CLOSE; - resize_animation.value_from = get_width (); - resize_animation.value_to = launchers.length * get_launcher_size (); - resize_animation.play (); + uint index; + if (store.find (item, out index)) { + store.remove (index); + } - item.revealed_done.disconnect (remove_finish); + item.removed.disconnect (remove_item); item.cleanup (); } public void move_launcher_after (BaseItem source, int target_index) { - unowned GLib.GenericArray? list = null; - double offset = 0; + ListStore list; if (source is Launcher) { list = launchers; } else if (source is WorkspaceIconGroup) { list = icon_groups; - offset = (launchers.length + (background_item.has_apps ? 1 : 0)) * get_launcher_size (); // +1 for the background item } else { warning ("Tried to move neither launcher nor icon group"); return; } - if (target_index >= list.length) { - target_index = list.length - 1; - } - - uint source_index = 0; - list.find (source, out source_index); - - source.animate_move ((get_launcher_size () * target_index) + offset); - - bool right = source_index > target_index; - - // Move the launchers located between the source and the target with an animation - for (int i = (right ? target_index : (int) (source_index + 1)); i <= (right ? ((int) source_index) - 1 : target_index); i++) { - list.get (i).animate_move ((right ? (i + 1) * get_launcher_size () : (i - 1) * get_launcher_size ()) + offset); - } + var source_index = get_index_for_launcher (source); - list.remove (source); + list.remove ((uint) source_index); list.insert (target_index, source); sync_pinned (); @@ -325,7 +244,7 @@ return 0; } else if (item == dynamic_workspace_item) { //treat dynamic workspace icon as last icon group - return (int) icon_groups.length; + return (int) icon_groups.get_n_items (); } warning ("Tried to get index of neither launcher nor icon group"); @@ -335,7 +254,8 @@ public void sync_pinned () { string[] new_pinned_ids = {}; - foreach (var launcher in launchers) { + for (uint i = 0; i < launchers.get_n_items (); i++) { + var launcher = (Launcher) launchers.get_item (i); if (launcher.app.pinned) { new_pinned_ids += launcher.app.app_info.get_id (); } @@ -345,12 +265,13 @@ } public void launch (uint index) { - if (index < 1 || index > launchers.length) { + if (index < 1 || index > launchers.get_n_items ()) { return; } var context = Gdk.Display.get_default ().get_app_launch_context (); - launchers.get ((int) index - 1).app.launch (context); + var launcher = (Launcher) launchers.get_item (index - 1); + launcher.app.launch (context); } public static int get_launcher_size () { diff --git a/src/meson.build b/src/meson.build index 1b84b18f..14b8a4ba 100644 --- a/src/meson.build +++ b/src/meson.build @@ -4,6 +4,7 @@ sources = [ 'BaseItem.vala', 'BottomMargin.vala', 'ContainerItem.vala', + 'ItemGroup.vala', 'ItemManager.vala', 'MainWindow.vala', 'RenderNodeWalker.vala', From 1f2c181fc3acbb0a263a8364c6825107a4c30281 Mon Sep 17 00:00:00 2001 From: Leonhard Kargl Date: Fri, 26 Dec 2025 16:32:16 +0100 Subject: [PATCH 2/4] BaseItem: Introduce a get_index method --- src/AppSystem/Background/BackgroundItem.vala | 2 +- src/BaseItem.vala | 25 ++++++------ src/ItemGroup.vala | 9 +++++ src/ItemManager.vala | 39 +++---------------- src/WorkspaceSystem/DynamicWorkspaceItem.vala | 2 +- src/WorkspaceSystem/IconGroup.vala | 2 +- 6 files changed, 28 insertions(+), 51 deletions(-) diff --git a/src/AppSystem/Background/BackgroundItem.vala b/src/AppSystem/Background/BackgroundItem.vala index 502a1041..4d1557c0 100644 --- a/src/AppSystem/Background/BackgroundItem.vala +++ b/src/AppSystem/Background/BackgroundItem.vala @@ -20,7 +20,7 @@ public class Dock.BackgroundItem : BaseIconGroup { icons: new Gtk.MapListModel (background_monitor.background_apps, (app) => { return ((BackgroundApp) app).icon; }), - disallow_dnd: true + group: Group.NONE ); } diff --git a/src/BaseItem.vala b/src/BaseItem.vala index bdbea4c4..320ca3d0 100644 --- a/src/BaseItem.vala +++ b/src/BaseItem.vala @@ -11,6 +11,7 @@ public class Dock.BaseItem : Gtk.Box { } public enum Group { + NONE, LAUNCHER, WORKSPACE } @@ -24,7 +25,6 @@ public class Dock.BaseItem : Gtk.Box { public signal void removed (); public signal void revealed_done (); - public bool disallow_dnd { get; construct; default = false; } /** * The group in the dock this item belongs to. This is used to allow DND * only within that group. @@ -155,6 +155,10 @@ public class Dock.BaseItem : Gtk.Box { gesture_click = new Gtk.GestureClick (); add_controller (gesture_click); + if (group == NONE) { + return; + } + var drop_target = new Gtk.DropTarget (typeof (BaseItem), MOVE) { preload = true }; @@ -162,10 +166,6 @@ public class Dock.BaseItem : Gtk.Box { drop_target.enter.connect (on_drop_enter); drop_target.drop.connect (on_drop); - if (disallow_dnd) { - return; - } - var drag_source = new Gtk.DragSource () { actions = MOVE }; @@ -181,6 +181,11 @@ public class Dock.BaseItem : Gtk.Box { popover_tooltip.dispose (); } + public uint get_index () { + var item_group = get_ancestor (typeof (ItemGroup)) as ItemGroup; + return item_group?.get_index_for_item (this) ?? Gtk.INVALID_LIST_POSITION; + } + public void set_revealed (bool revealed) { fade.skip (); reveal.skip (); @@ -289,15 +294,7 @@ public class Dock.BaseItem : Gtk.Box { */ public void calculate_dnd_move (BaseItem source, double x, double y) { var launcher_manager = ItemManager.get_default (); - - int target_index = launcher_manager.get_index_for_launcher (this); - int source_index = launcher_manager.get_index_for_launcher (source); - - if (source_index == target_index) { - return; - } - - launcher_manager.move_launcher_after (source, target_index); + launcher_manager.move_launcher_after (source, (int) get_index ()); } private bool on_drop (Value val) { diff --git a/src/ItemGroup.vala b/src/ItemGroup.vala index 2ffa88e0..71cdf8d3 100644 --- a/src/ItemGroup.vala +++ b/src/ItemGroup.vala @@ -131,4 +131,13 @@ current_children.remove (index); } } + + public uint get_index_for_item (BaseItem item) { + uint index; + if (current_children.find (item, out index)) { + return index; + } + + return Gtk.INVALID_LIST_POSITION; + } } diff --git a/src/ItemManager.vala b/src/ItemManager.vala index 0a008599..921019d4 100644 --- a/src/ItemManager.vala +++ b/src/ItemManager.vala @@ -210,45 +210,16 @@ } public void move_launcher_after (BaseItem source, int target_index) { - ListStore list; if (source is Launcher) { - list = launchers; + launchers.remove ((uint) source.get_index ()); + launchers.insert (target_index, source); + sync_pinned (); } else if (source is WorkspaceIconGroup) { - list = icon_groups; + icon_groups.remove ((uint) source.get_index ()); + icon_groups.insert (target_index, source); } else { warning ("Tried to move neither launcher nor icon group"); - return; - } - - var source_index = get_index_for_launcher (source); - - list.remove ((uint) source_index); - list.insert (target_index, source); - - sync_pinned (); - } - - public int get_index_for_launcher (BaseItem item) { - if (item is Launcher) { - uint index; - if (launchers.find ((Launcher) item, out index)) { - return (int) index; - } - - return 0; - } else if (item is WorkspaceIconGroup) { - uint index; - if (icon_groups.find ((WorkspaceIconGroup) item, out index)) { - return (int) index; - } - - return 0; - } else if (item == dynamic_workspace_item) { //treat dynamic workspace icon as last icon group - return (int) icon_groups.get_n_items (); } - - warning ("Tried to get index of neither launcher nor icon group"); - return 0; } public void sync_pinned () { diff --git a/src/WorkspaceSystem/DynamicWorkspaceItem.vala b/src/WorkspaceSystem/DynamicWorkspaceItem.vala index 34361ab8..73300090 100644 --- a/src/WorkspaceSystem/DynamicWorkspaceItem.vala +++ b/src/WorkspaceSystem/DynamicWorkspaceItem.vala @@ -9,7 +9,7 @@ public class Dock.DynamicWorkspaceIcon : ContainerItem, WorkspaceItem { private Gtk.Image image; public DynamicWorkspaceIcon () { - Object (disallow_dnd: true, group: Group.WORKSPACE); + Object (group: Group.NONE); } construct { diff --git a/src/WorkspaceSystem/IconGroup.vala b/src/WorkspaceSystem/IconGroup.vala index 53605988..bb1274a3 100644 --- a/src/WorkspaceSystem/IconGroup.vala +++ b/src/WorkspaceSystem/IconGroup.vala @@ -48,7 +48,7 @@ public class Dock.WorkspaceIconGroup : BaseIconGroup, WorkspaceItem { private void on_moving_changed () { if (!moving) { - workspace.reorder (ItemManager.get_default ().get_index_for_launcher (this)); + workspace.reorder ((int) get_index ()); } } From 0323d9dd08b65a0b5baef3f7e22b429df2abfb15 Mon Sep 17 00:00:00 2001 From: Leonhard Kargl Date: Sat, 27 Dec 2025 00:39:25 +0100 Subject: [PATCH 3/4] Use ListModels for apps and workspaces --- src/AppSystem/App.vala | 1 - src/AppSystem/AppSystem.vala | 52 +++++++-- src/AppSystem/Background/BackgroundItem.vala | 10 +- src/AppSystem/Launcher.vala | 1 - src/BaseItem.vala | 3 +- src/ItemGroup.vala | 45 +++++++- src/ItemManager.vala | 109 +++--------------- src/WorkspaceSystem/DynamicWorkspaceItem.vala | 9 +- src/WorkspaceSystem/IconGroup.vala | 10 -- src/WorkspaceSystem/Workspace.vala | 12 -- src/WorkspaceSystem/WorkspaceSystem.vala | 34 +++--- 11 files changed, 135 insertions(+), 151 deletions(-) diff --git a/src/AppSystem/App.vala b/src/AppSystem/App.vala index 5ebadc82..a081ddd0 100644 --- a/src/AppSystem/App.vala +++ b/src/AppSystem/App.vala @@ -103,7 +103,6 @@ public class Dock.App : Object { notify["pinned"].connect (() => { check_remove (); - ItemManager.get_default ().sync_pinned (); }); WindowSystem.get_default ().notify["active-workspace"].connect (() => { diff --git a/src/AppSystem/AppSystem.vala b/src/AppSystem/AppSystem.vala index 6148b170..3ba2fc52 100644 --- a/src/AppSystem/AppSystem.vala +++ b/src/AppSystem/AppSystem.vala @@ -15,13 +15,17 @@ public class Dock.AppSystem : Object, UnityClient { return instance.once (() => { return new AppSystem (); }); } - public signal void app_added (App app); + private ListStore apps_store; + public ListModel apps { get { return apps_store; } } private GLib.HashTable id_to_app; private AppSystem () { } construct { + apps_store = new ListStore (typeof (App)); + apps_store.items_changed.connect (save_pinned); + id_to_app = new HashTable (str_hash, str_equal); } @@ -41,12 +45,46 @@ public class Dock.AppSystem : Object, UnityClient { private App add_app (DesktopAppInfo app_info, bool pinned) { var app = new App (app_info, pinned); + app.removed.connect (remove_app); + app.notify["pinned"].connect (save_pinned); id_to_app[app_info.get_id ()] = app; - app.removed.connect ((_app) => id_to_app.remove (_app.app_info.get_id ())); - app_added (app); + apps_store.append (app); return app; } + private void remove_app (App app) { + id_to_app.remove (app.app_info.get_id ()); + + uint pos; + if (apps_store.find (app, out pos)) { + apps_store.remove (pos); + } + } + + public void reorder_app (App app, uint new_index) { + uint pos; + if (!apps_store.find (app, out pos)) { + warning ("Tried to reorder an app that is not in the store"); + return; + } + + apps_store.remove (pos); + apps_store.insert (new_index, app); + } + + private void save_pinned () { + string[] new_pinned_ids = {}; + + for (uint i = 0; i < apps_store.get_n_items (); i++) { + var app = (App) apps_store.get_item (i); + if (app.pinned) { + new_pinned_ids += app.app_info.get_id (); + } + } + + settings.set_strv ("launchers", new_pinned_ids); + } + public async void sync_windows () { var windows = WindowSystem.get_default ().windows; @@ -79,20 +117,20 @@ public class Dock.AppSystem : Object, UnityClient { } } - public void add_app_for_id (string app_id) { + public App? add_app_for_id (string app_id) { if (app_id in id_to_app) { id_to_app[app_id].pinned = true; - return; + return id_to_app[app_id]; } var app_info = new DesktopAppInfo (app_id); if (app_info == null) { warning ("App not found: %s", app_id); - return; + return null; } - add_app (app_info, true); + return add_app (app_info, true); } public void remove_app_by_id (string app_id) { diff --git a/src/AppSystem/Background/BackgroundItem.vala b/src/AppSystem/Background/BackgroundItem.vala index 4d1557c0..c5fc26d1 100644 --- a/src/AppSystem/Background/BackgroundItem.vala +++ b/src/AppSystem/Background/BackgroundItem.vala @@ -6,10 +6,10 @@ */ public class Dock.BackgroundItem : BaseIconGroup { - public signal void apps_appeared (); + private ListStore group_store; + public ListModel group_model { get { return group_store; } } public BackgroundMonitor monitor { private get; construct; } - public bool has_apps { get { return monitor.background_apps.get_n_items () > 0; } } private Gtk.Popover popover; @@ -25,6 +25,8 @@ public class Dock.BackgroundItem : BaseIconGroup { } construct { + group_store = new ListStore (typeof (BackgroundItem)); + var list_box = new Gtk.ListBox () { selection_mode = BROWSE }; @@ -53,9 +55,9 @@ public class Dock.BackgroundItem : BaseIconGroup { monitor.background_apps.items_changed.connect ((pos, n_removed, n_added) => { if (monitor.background_apps.get_n_items () == 0) { popover.popdown (); - removed (); + group_store.remove (0); } else if (n_removed == 0 && n_added != 0 && n_added == monitor.background_apps.get_n_items ()) { - apps_appeared (); + group_store.append (this); } }); diff --git a/src/AppSystem/Launcher.vala b/src/AppSystem/Launcher.vala index e2b02411..182160de 100644 --- a/src/AppSystem/Launcher.vala +++ b/src/AppSystem/Launcher.vala @@ -148,7 +148,6 @@ public class Dock.Launcher : BaseItem { }); app.launched.connect (animate_launch); - app.removed.connect (() => removed ()); var bounce_animation_target = new Adw.CallbackAnimationTarget ((val) => { var height = overlay.get_height (); diff --git a/src/BaseItem.vala b/src/BaseItem.vala index 320ca3d0..a396cf38 100644 --- a/src/BaseItem.vala +++ b/src/BaseItem.vala @@ -22,7 +22,6 @@ public class Dock.BaseItem : Gtk.Box { dock_settings = new GLib.Settings ("io.elementary.dock"); } - public signal void removed (); public signal void revealed_done (); /** @@ -307,7 +306,7 @@ public class Dock.BaseItem : Gtk.Box { private bool is_allowed_drop (Value val) { var obj = val.get_object (); - return obj != null && obj is BaseItem && ((BaseItem) obj).group == group; + return obj != null && obj is BaseItem && ((BaseItem) obj).group == group && obj != this; } private class PopoverTooltip : Gtk.Popover { diff --git a/src/ItemGroup.vala b/src/ItemGroup.vala index 71cdf8d3..cc51fe5f 100644 --- a/src/ItemGroup.vala +++ b/src/ItemGroup.vala @@ -6,9 +6,15 @@ */ public class Dock.ItemGroup : Gtk.Fixed { + private const string OBJECT_DATA_KEY = "item-group-obj"; + + [CCode (has_target = false)] + public delegate BaseItem CreateBaseItemFunc (Object obj); + private static Settings settings; public ListModel items { get; construct; } + public CreateBaseItemFunc create_item_func { get; construct; } private Sequence item_store; private ListStore current_children; @@ -17,8 +23,11 @@ private bool relayout_queued = false; - public ItemGroup (ListModel items) { - Object (items: items); + private HashTable cached_items; + private uint clear_cache_id = 0; + + public ItemGroup (ListModel items, CreateBaseItemFunc create_item_func) { + Object (items: items, create_item_func: create_item_func); } static construct { @@ -42,6 +51,8 @@ on_items_changed (0, 0, items.get_n_items ()); overflow = VISIBLE; + + cached_items = new HashTable (null, null); } private void queue_relayout () { @@ -87,18 +98,44 @@ private void on_items_changed (uint position, uint removed, uint added) { var start_iter = item_store.get_iter_at_pos ((int) position); var end_iter = start_iter.move ((int) removed); + start_iter.foreach_range (end_iter, cache_item); start_iter.foreach_range (end_iter, remove_item); start_iter.remove_range (end_iter); var insert_iter = item_store.get_iter_at_pos ((int) position); for (int i = (int) position; i < position + added; i++) { - var item = (BaseItem) items.get_item (i); + var item = get_or_create_item (items.get_item (i)); insert_iter.insert_before (item); add_item (i, item); } } + // Make sure that if an item is removed and added again in the same + // mainloop iteration it gets mapped to the same BaseItem + private void cache_item (BaseItem item) { + cached_items[item.get_data (OBJECT_DATA_KEY)] = item; + + if (clear_cache_id == 0) { + clear_cache_id = Idle.add_once (clear_cache); + } + } + + private void clear_cache () { + cached_items.remove_all (); + clear_cache_id = 0; + } + + private BaseItem get_or_create_item (Object obj) { + if (obj in cached_items) { + return cached_items[obj]; + } + + var base_item = create_item_func (obj); + base_item.set_data (OBJECT_DATA_KEY, obj); + return base_item; + } + private void add_item (int pos, BaseItem item) { if (item.parent == this) { // The item was already in this group and is currently being removed @@ -119,6 +156,8 @@ private void remove_item (BaseItem item) { item.revealed_done.connect (finish_remove); item.set_revealed (false); + + cached_items[item.get_data ("dock-obj")] = item; } private void finish_remove (BaseItem item) { diff --git a/src/ItemManager.vala b/src/ItemManager.vala index 921019d4..983978b4 100644 --- a/src/ItemManager.vala +++ b/src/ItemManager.vala @@ -13,9 +13,6 @@ public Launcher? added_launcher { get; set; default = null; } - private ListStore launchers; - private BackgroundItem background_item; - private ListStore icon_groups; // Only used to keep track of icon group indices private DynamicWorkspaceIcon dynamic_workspace_item; #if WORKSPACE_SWITCHER @@ -27,12 +24,10 @@ } construct { - launchers = new ListStore (typeof (Launcher)); + var app_group = new ItemGroup (AppSystem.get_default ().apps, (obj) => new Launcher ((App) obj)); - background_item = new BackgroundItem (); - background_item.apps_appeared.connect (add_item); - - icon_groups = new ListStore (typeof (WorkspaceIconGroup)); + var background_item = new BackgroundItem (); + var background_group = new ItemGroup (background_item.group_model, (obj) => (BackgroundItem) obj); #if WORKSPACE_SWITCHER dynamic_workspace_item = new DynamicWorkspaceIcon (); @@ -44,11 +39,11 @@ settings.bind ("icon-size", separator, "height-request", GET); #endif - append (new ItemGroup (launchers)); - append (background_item); + append (app_group); + append (background_group); #if WORKSPACE_SWITCHER append (separator); - append (new ItemGroup (icon_groups)); + append (new ItemGroup (WorkspaceSystem.get_default ().workspaces, (obj) => new WorkspaceIconGroup ((Workspace) obj))); append (dynamic_workspace_item); #endif overflow = VISIBLE; @@ -94,7 +89,15 @@ return; } - app_system.add_app_for_id (app_info.get_id ()); + app = app_system.add_app_for_id (app_info.get_id ()); + + for (var child = app_group.get_first_child (); child != null; child = child.get_next_sibling ()) { + if (child is Launcher && child.app == app) { + added_launcher = (Launcher) child; + added_launcher.moving = true; + break; + } + } }); BaseItem? current_base_item = null; @@ -142,27 +145,6 @@ return false; }); - AppSystem.get_default ().app_added.connect ((app) => { - var launcher = new Launcher (app); - - if (drop_target_file.get_value () != null && added_launcher == null) { // The launcher is being added via dnd from wingpanel - var position = (int) Math.round (drop_x / get_launcher_size ()); - added_launcher = launcher; - launcher.moving = true; - - add_launcher_via_dnd (launcher, position); - return; - } - - add_item (launcher); - }); - -#if WORKSPACE_SWITCHER - WorkspaceSystem.get_default ().workspace_added.connect ((workspace) => { - add_item (new WorkspaceIconGroup (workspace)); - }); -#endif - map.connect (() => { AppSystem.get_default ().load.begin (); background_item.load (); @@ -172,76 +154,23 @@ }); } - private void add_launcher_via_dnd (Launcher launcher, int index) { - launcher.removed.connect (remove_item); - - launchers.insert (index, launcher); - sync_pinned (); - } - - private void add_item (BaseItem item) { - item.removed.connect (remove_item); - - if (item is Launcher) { - launchers.append (item); - sync_pinned (); - } else if (item is WorkspaceIconGroup) { - icon_groups.append (item); - } - } - - private void remove_item (BaseItem item) { - ListStore store; - if (item is Launcher) { - store = launchers; - } else if (item is WorkspaceIconGroup) { - store = icon_groups; - } else { - return; - } - - uint index; - if (store.find (item, out index)) { - store.remove (index); - } - - item.removed.disconnect (remove_item); - item.cleanup (); - } - public void move_launcher_after (BaseItem source, int target_index) { if (source is Launcher) { - launchers.remove ((uint) source.get_index ()); - launchers.insert (target_index, source); - sync_pinned (); + AppSystem.get_default ().reorder_app (source.app, target_index); } else if (source is WorkspaceIconGroup) { - icon_groups.remove ((uint) source.get_index ()); - icon_groups.insert (target_index, source); + WorkspaceSystem.get_default ().reorder_workspace (source.workspace, target_index); } else { warning ("Tried to move neither launcher nor icon group"); } } - public void sync_pinned () { - string[] new_pinned_ids = {}; - - for (uint i = 0; i < launchers.get_n_items (); i++) { - var launcher = (Launcher) launchers.get_item (i); - if (launcher.app.pinned) { - new_pinned_ids += launcher.app.app_info.get_id (); - } - } - - settings.set_strv ("launchers", new_pinned_ids); - } - public void launch (uint index) { - if (index < 1 || index > launchers.get_n_items ()) { + if (index < 1 || index > AppSystem.get_default ().apps.get_n_items ()) { return; } var context = Gdk.Display.get_default ().get_app_launch_context (); - var launcher = (Launcher) launchers.get_item (index - 1); + var launcher = (Launcher) AppSystem.get_default ().apps.get_item (index - 1); launcher.app.launch (context); } diff --git a/src/WorkspaceSystem/DynamicWorkspaceItem.vala b/src/WorkspaceSystem/DynamicWorkspaceItem.vala index 73300090..91899bae 100644 --- a/src/WorkspaceSystem/DynamicWorkspaceItem.vala +++ b/src/WorkspaceSystem/DynamicWorkspaceItem.vala @@ -4,7 +4,7 @@ */ public class Dock.DynamicWorkspaceIcon : ContainerItem, WorkspaceItem { - public int workspace_index { get { return WorkspaceSystem.get_default ().workspaces.length; } } + public int workspace_index { get { return (int) WorkspaceSystem.get_default ().workspaces.get_n_items (); } } private Gtk.Image image; @@ -27,8 +27,7 @@ public class Dock.DynamicWorkspaceIcon : ContainerItem, WorkspaceItem { _("New Workspace") ); - WorkspaceSystem.get_default ().workspace_added.connect (update_active_state); - WorkspaceSystem.get_default ().workspace_removed.connect (update_active_state); + WorkspaceSystem.get_default ().workspaces.items_changed.connect (update_active_state); WindowSystem.get_default ().notify["active-workspace"].connect (update_active_state); dock_settings.bind_with_mapping ( @@ -51,11 +50,11 @@ public class Dock.DynamicWorkspaceIcon : ContainerItem, WorkspaceItem { private void update_active_state () { unowned var workspace_system = WorkspaceSystem.get_default (); unowned var window_system = WindowSystem.get_default (); - state = (workspace_system.workspaces.length == window_system.active_workspace) ? State.ACTIVE : State.HIDDEN; + state = (workspace_system.workspaces.get_n_items () == window_system.active_workspace) ? State.ACTIVE : State.HIDDEN; } private async void switch_to_new_workspace () { - var n_workspaces = WorkspaceSystem.get_default ().workspaces.length; + var n_workspaces = (int) WorkspaceSystem.get_default ().workspaces.get_n_items (); var index = WindowSystem.get_default ().active_workspace; if (index == n_workspaces) { diff --git a/src/WorkspaceSystem/IconGroup.vala b/src/WorkspaceSystem/IconGroup.vala index bb1274a3..8c592b00 100644 --- a/src/WorkspaceSystem/IconGroup.vala +++ b/src/WorkspaceSystem/IconGroup.vala @@ -38,18 +38,8 @@ public class Dock.WorkspaceIconGroup : BaseIconGroup, WorkspaceItem { return true; }); - workspace.removed.connect (() => removed ()); - gesture_click.button = Gdk.BUTTON_PRIMARY; gesture_click.released.connect (workspace.activate); - - notify["moving"].connect (on_moving_changed); - } - - private void on_moving_changed () { - if (!moving) { - workspace.reorder ((int) get_index ()); - } } public void window_entered (Window window) { diff --git a/src/WorkspaceSystem/Workspace.vala b/src/WorkspaceSystem/Workspace.vala index a180f32b..13f1c7ee 100644 --- a/src/WorkspaceSystem/Workspace.vala +++ b/src/WorkspaceSystem/Workspace.vala @@ -4,9 +4,6 @@ */ public class Dock.Workspace : GLib.Object { - public signal void reordered (int new_index); - public signal void removed (); - private ListStore store; public ListModel windows { get { return store; } } public int index { get; set; } @@ -20,10 +17,6 @@ public class Dock.Workspace : GLib.Object { store.splice (0, store.get_n_items (), new_windows.data); } - public void remove () { - removed (); - } - public void update_active_workspace () { is_active_workspace = index == WindowSystem.get_default ().active_workspace; } @@ -35,9 +28,4 @@ public class Dock.Workspace : GLib.Object { WindowSystem.get_default ().desktop_integration.activate_workspace.begin (index); } } - - public void reorder (int new_index) { - reordered (new_index); - WindowSystem.get_default ().desktop_integration.reorder_workspace.begin (index, new_index); - } } diff --git a/src/WorkspaceSystem/WorkspaceSystem.vala b/src/WorkspaceSystem/WorkspaceSystem.vala index 420b7f83..36a8e643 100644 --- a/src/WorkspaceSystem/WorkspaceSystem.vala +++ b/src/WorkspaceSystem/WorkspaceSystem.vala @@ -9,15 +9,12 @@ public class Dock.WorkspaceSystem : Object { return instance.once (() => { return new WorkspaceSystem (); }); } - public signal void workspace_added (Workspace workspace); - public signal void workspace_removed (); - - public GLib.GenericArray workspaces { get; private owned set; } + public ListStore workspaces { get; private owned set; } private WorkspaceSystem () { } construct { - workspaces = new GLib.GenericArray (); + workspaces = new ListStore (typeof (Workspace)); } public async void load () { @@ -30,9 +27,7 @@ public class Dock.WorkspaceSystem : Object { private Workspace add_workspace () { var workspace = new Workspace (); - workspaces.add (workspace); - workspace.reordered.connect (on_workspace_reordered); - workspace_added (workspace); + workspaces.append (workspace); return workspace; } @@ -59,8 +54,8 @@ public class Dock.WorkspaceSystem : Object { // update windows in existing workspaces for (var i = 0; i < n_workspaces; i++) { Workspace workspace; - if (i < workspaces.length) { - workspace = workspaces[i]; + if (i < workspaces.get_n_items ()) { + workspace = (Workspace) workspaces.get_item (i); } else { workspace = add_workspace (); } @@ -78,7 +73,8 @@ public class Dock.WorkspaceSystem : Object { } private async void sync_active_workspace () { - foreach (var workspace in workspaces) { + for (uint i = 0; i < workspaces.get_n_items (); i++) { + var workspace = (Workspace) workspaces.get_item (i); workspace.update_active_workspace (); } } @@ -88,9 +84,7 @@ public class Dock.WorkspaceSystem : Object { index--; } - workspaces[index].remove (); - workspaces.remove_index (index); - workspace_removed (); + workspaces.remove (index); } private async int get_n_workspaces () { @@ -107,8 +101,16 @@ public class Dock.WorkspaceSystem : Object { } } - private void on_workspace_reordered (Workspace workspace, int new_index) { - workspaces.remove (workspace); + public void reorder_workspace (Workspace workspace, int new_index) { + uint pos; + if (!workspaces.find (workspace, out pos)) { + warning ("Tried to reorder a workspace that is not in the store"); + return; + } + + workspaces.remove (pos); workspaces.insert (new_index, workspace); + + WindowSystem.get_default ().desktop_integration.reorder_workspace.begin (workspace.index, new_index); } } From 42fcc5fdc8b29b0db89990ce3c6558c8c721f988 Mon Sep 17 00:00:00 2001 From: Leonhard Kargl Date: Sun, 28 Dec 2025 17:56:59 +0100 Subject: [PATCH 4/4] Make sure only a single app exists for an id --- src/AppSystem/App.vala | 21 ++----- src/AppSystem/AppSystem.vala | 112 ++++++++++++++++++----------------- src/ItemManager.vala | 12 ++-- 3 files changed, 70 insertions(+), 75 deletions(-) diff --git a/src/AppSystem/App.vala b/src/AppSystem/App.vala index a081ddd0..f80d1b2d 100644 --- a/src/AppSystem/App.vala +++ b/src/AppSystem/App.vala @@ -20,11 +20,10 @@ public class Dock.App : Object { } } - public signal void removed (); - - public bool pinned { get; construct set; } public GLib.DesktopAppInfo app_info { get; construct; } + public bool pinned { get; set; } + public bool count_visible { get; private set; default = false; } public int64 current_count { get; private set; default = 0; } public bool progress_visible { get; set; default = false; } @@ -52,8 +51,8 @@ public class Dock.App : Object { private static Dock.SwitcherooControl switcheroo_control; - public App (GLib.DesktopAppInfo app_info, bool pinned) { - Object (app_info: app_info, pinned: pinned); + public App (GLib.DesktopAppInfo app_info) { + Object (app_info: app_info); } static construct { @@ -101,10 +100,6 @@ public class Dock.App : Object { app_action_group.add_action (simple_action); } - notify["pinned"].connect (() => { - check_remove (); - }); - WindowSystem.get_default ().notify["active-workspace"].connect (() => { notify_property ("running-on-active-workspace"); }); @@ -165,12 +160,6 @@ public class Dock.App : Object { return false; } - private void check_remove () { - if (!pinned && !running) { - removed (); - } - } - public void update_windows (GLib.GenericArray? new_windows) { if (new_windows == null) { windows = new GLib.GenericArray (); @@ -184,8 +173,6 @@ public class Dock.App : Object { if (launching && running) { launching = false; } - - check_remove (); } public void perform_unity_update (VariantIter prop_iter) { diff --git a/src/AppSystem/AppSystem.vala b/src/AppSystem/AppSystem.vala index 3ba2fc52..f7cfde79 100644 --- a/src/AppSystem/AppSystem.vala +++ b/src/AppSystem/AppSystem.vala @@ -29,38 +29,51 @@ public class Dock.AppSystem : Object, UnityClient { id_to_app = new HashTable (str_hash, str_equal); } - public App? get_app (string id) { - return id_to_app[id]; - } + public App? get_app_by_id (string id) { + if (!(id in id_to_app)) { + var app_info = new DesktopAppInfo (id); - public async void load () { - foreach (string app_id in settings.get_strv ("launchers")) { - var app_info = new GLib.DesktopAppInfo (app_id); - add_app (app_info, true); - } + if (app_info == null) { + return null; + } - yield sync_windows (); - WindowSystem.get_default ().notify["windows"].connect (sync_windows); - } + var app = new App (app_info); + app.notify["running"].connect (check_app); + app.notify["pinned"].connect (check_app); + app.notify["pinned"].connect (save_pinned); + + id_to_app[app_info.get_id ()] = app; + } - private App add_app (DesktopAppInfo app_info, bool pinned) { - var app = new App (app_info, pinned); - app.removed.connect (remove_app); - app.notify["pinned"].connect (save_pinned); - id_to_app[app_info.get_id ()] = app; - apps_store.append (app); - return app; + return id_to_app[id]; } - private void remove_app (App app) { - id_to_app.remove (app.app_info.get_id ()); + private void check_app (Object obj, ParamSpec pspec) { + var app = (App) obj; uint pos; - if (apps_store.find (app, out pos)) { + var exists = apps_store.find (app, out pos); + + if ((app.pinned || app.running) && !exists) { + apps_store.append (app); + } else if ((!app.pinned && !app.running) && exists) { apps_store.remove (pos); } } + private void save_pinned () { + string[] new_pinned_ids = {}; + + for (uint i = 0; i < apps_store.get_n_items (); i++) { + var app = (App) apps_store.get_item (i); + if (app.pinned) { + new_pinned_ids += app.app_info.get_id (); + } + } + + settings.set_strv ("launchers", new_pinned_ids); + } + public void reorder_app (App app, uint new_index) { uint pos; if (!apps_store.find (app, out pos)) { @@ -72,17 +85,17 @@ public class Dock.AppSystem : Object, UnityClient { apps_store.insert (new_index, app); } - private void save_pinned () { - string[] new_pinned_ids = {}; - - for (uint i = 0; i < apps_store.get_n_items (); i++) { - var app = (App) apps_store.get_item (i); - if (app.pinned) { - new_pinned_ids += app.app_info.get_id (); + public async void load () { + foreach (string app_id in settings.get_strv ("launchers")) { + var app = get_app_by_id (app_id); + if (app == null) { + continue; } + app.pinned = true; } - settings.set_strv ("launchers", new_pinned_ids); + yield sync_windows (); + WindowSystem.get_default ().notify["windows"].connect (sync_windows); } public async void sync_windows () { @@ -90,14 +103,9 @@ public class Dock.AppSystem : Object, UnityClient { var app_window_list = new GLib.HashTable> (direct_hash, direct_equal); foreach (var window in windows) { - App? app = id_to_app[window.app_id]; + var app = get_app_by_id (window.app_id); if (app == null) { - var app_info = new GLib.DesktopAppInfo (window.app_id); - if (app_info == null) { - continue; - } - - app = add_app (app_info, false); + continue; } var window_list = app_window_list.get (app); @@ -117,25 +125,19 @@ public class Dock.AppSystem : Object, UnityClient { } } - public App? add_app_for_id (string app_id) { - if (app_id in id_to_app) { - id_to_app[app_id].pinned = true; - return id_to_app[app_id]; - } - - var app_info = new DesktopAppInfo (app_id); + public void add_app_for_id (string app_id) { + var app = get_app_by_id (app_id); - if (app_info == null) { - warning ("App not found: %s", app_id); - return null; + if (app != null) { + app.pinned = true; } - - return add_app (app_info, true); } public void remove_app_by_id (string app_id) { - if (app_id in id_to_app) { - id_to_app[app_id].pinned = false; + var app = get_app_by_id (app_id); + + if (app != null) { + app.pinned = false; } } @@ -160,8 +162,9 @@ public class Dock.AppSystem : Object, UnityClient { parameters.get ("(sa{sv})", out app_uri, out prop_iter); var app_id = app_uri.replace ("application://", ""); - if (id_to_app[app_id] != null) { - id_to_app[app_id].perform_unity_update (prop_iter); + var app = get_app_by_id (app_id); + if (app != null) { + app.perform_unity_update (prop_iter); } else { critical ("unable to update missing launcher: %s", app_id); } @@ -169,8 +172,9 @@ public class Dock.AppSystem : Object, UnityClient { private void remove_launcher_entry (string sender_name) { var app_id = sender_name + ".desktop"; - if (id_to_app[app_id] != null) { - id_to_app[app_id].remove_launcher_entry (); + var app = get_app_by_id (app_id); + if (app != null) { + app.remove_launcher_entry (); } } } diff --git a/src/ItemManager.vala b/src/ItemManager.vala index 983978b4..66ac22b4 100644 --- a/src/ItemManager.vala +++ b/src/ItemManager.vala @@ -82,15 +82,19 @@ var app_system = AppSystem.get_default (); - var app = app_system.get_app (app_info.get_id ()); - if (app != null) { + var app = app_system.get_app_by_id (app_info.get_id ()); + + if (app == null) { + return; + } + + if (app.pinned || app.running) { + // Already in the dock app.pinned = true; drop_target_file.reject (); return; } - app = app_system.add_app_for_id (app_info.get_id ()); - for (var child = app_group.get_first_child (); child != null; child = child.get_next_sibling ()) { if (child is Launcher && child.app == app) { added_launcher = (Launcher) child;