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 502a1041..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; @@ -20,11 +20,13 @@ public class Dock.BackgroundItem : BaseIconGroup { icons: new Gtk.MapListModel (background_monitor.background_apps, (app) => { return ((BackgroundApp) app).icon; }), - disallow_dnd: true + group: Group.NONE ); } 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 c676093f..a396cf38 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 } @@ -21,10 +22,8 @@ public class Dock.BaseItem : Gtk.Box { dock_settings = new GLib.Settings ("io.elementary.dock"); } - 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. @@ -133,7 +132,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; }); @@ -155,6 +154,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 +165,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 +180,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 (); @@ -219,6 +223,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; @@ -285,15 +293,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) { @@ -306,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 new file mode 100644 index 00000000..cc51fe5f --- /dev/null +++ b/src/ItemGroup.vala @@ -0,0 +1,182 @@ +/* + * 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 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; + + private Adw.TimedAnimation resize_animation; + + private bool relayout_queued = false; + + 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 { + 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; + + cached_items = new HashTable (null, null); + } + + 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, 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 = 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 + // 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); + + cached_items[item.get_data ("dock-obj")] = item; + } + + 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); + } + } + + 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 ee5a25a6..983978b4 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,6 @@ 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 BackgroundItem background_item; - private GLib.GenericArray icon_groups; // Only used to keep track of icon group indices private DynamicWorkspaceIcon dynamic_workspace_item; #if WORKSPACE_SWITCHER @@ -28,34 +24,30 @@ } construct { - launchers = new GLib.GenericArray (); + 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 GLib.GenericArray (); + 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 (); - 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 (app_group); + append (background_group); +#if WORKSPACE_SWITCHER + append (separator); + append (new ItemGroup (WorkspaceSystem.get_default ().workspaces, (obj) => new WorkspaceIconGroup ((Workspace) obj))); + 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 }; @@ -97,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; @@ -145,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 (); @@ -175,182 +154,24 @@ }); } - 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 (); - } - - private void add_item (BaseItem item) { - item.removed.connect (remove_item); - - if (item is Launcher) { - launchers.add ((Launcher) item); - sync_pinned (); - } else if (item is WorkspaceIconGroup) { - icon_groups.add ((WorkspaceIconGroup) 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) { - if (item is Launcher) { - launchers.remove ((Launcher) item); - } else if (item is WorkspaceIconGroup) { - icon_groups.remove ((WorkspaceIconGroup) item); - } - - 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 (); - - item.revealed_done.disconnect (remove_finish); - item.cleanup (); - } - public void move_launcher_after (BaseItem source, int target_index) { - unowned GLib.GenericArray? list = null; - double offset = 0; if (source is Launcher) { - list = launchers; + AppSystem.get_default ().reorder_app (source.app, target_index); } 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 + WorkspaceSystem.get_default ().reorder_workspace (source.workspace, target_index); } 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); - } - - list.remove (source); - 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.length; - } - - warning ("Tried to get index of neither launcher nor icon group"); - return 0; - } - - public void sync_pinned () { - string[] new_pinned_ids = {}; - - foreach (var launcher in launchers) { - 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.length) { + if (index < 1 || index > AppSystem.get_default ().apps.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) AppSystem.get_default ().apps.get_item (index - 1); + launcher.app.launch (context); } public static int get_launcher_size () { diff --git a/src/WorkspaceSystem/DynamicWorkspaceItem.vala b/src/WorkspaceSystem/DynamicWorkspaceItem.vala index 34361ab8..91899bae 100644 --- a/src/WorkspaceSystem/DynamicWorkspaceItem.vala +++ b/src/WorkspaceSystem/DynamicWorkspaceItem.vala @@ -4,12 +4,12 @@ */ 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; public DynamicWorkspaceIcon () { - Object (disallow_dnd: true, group: Group.WORKSPACE); + Object (group: Group.NONE); } construct { @@ -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 53605988..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 (ItemManager.get_default ().get_index_for_launcher (this)); - } } 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); } } 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',