From 89a30b100c980bcc4bfe143f956af959ab1f5412 Mon Sep 17 00:00:00 2001 From: Leonhard Kargl Date: Thu, 9 Jan 2025 02:01:04 +0100 Subject: [PATCH 1/2] WorkspaceManager: Inline namespace --- src/WorkspaceManager.vala | 398 +++++++++++++++++++------------------- 1 file changed, 198 insertions(+), 200 deletions(-) diff --git a/src/WorkspaceManager.vala b/src/WorkspaceManager.vala index 7d0eb4314..08490a924 100644 --- a/src/WorkspaceManager.vala +++ b/src/WorkspaceManager.vala @@ -15,262 +15,260 @@ // along with this program. If not, see . // -namespace Gala { - public class WorkspaceManager : Object { - public static void init (WindowManager wm) requires (instance == null) { - instance = new WorkspaceManager (wm); - } +public class Gala.WorkspaceManager : Object { + public static void init (WindowManager wm) requires (instance == null) { + instance = new WorkspaceManager (wm); + } - public static unowned WorkspaceManager get_default () requires (instance != null) { - return instance; - } + public static unowned WorkspaceManager get_default () requires (instance != null) { + return instance; + } - private static WorkspaceManager? instance = null; + private static WorkspaceManager? instance = null; - public WindowManager wm { get; construct; } + public WindowManager wm { get; construct; } - private Gee.LinkedList workspaces_marked_removed; - private int remove_freeze_count = 0; + private Gee.LinkedList workspaces_marked_removed; + private int remove_freeze_count = 0; - private WorkspaceManager (WindowManager wm) { - Object (wm: wm); - } + private WorkspaceManager (WindowManager wm) { + Object (wm: wm); + } - construct { - workspaces_marked_removed = new Gee.LinkedList (); - unowned Meta.Display display = wm.get_display (); - unowned Meta.WorkspaceManager manager = display.get_workspace_manager (); + construct { + workspaces_marked_removed = new Gee.LinkedList (); + unowned Meta.Display display = wm.get_display (); + unowned Meta.WorkspaceManager manager = display.get_workspace_manager (); - // There are some empty workspace at startup - cleanup (); + // There are some empty workspace at startup + cleanup (); - if (Meta.Prefs.get_dynamic_workspaces ()) - manager.override_workspace_layout (Meta.DisplayCorner.TOPLEFT, false, 1, -1); + if (Meta.Prefs.get_dynamic_workspaces ()) + manager.override_workspace_layout (Meta.DisplayCorner.TOPLEFT, false, 1, -1); - for (var i = 0; i < manager.get_n_workspaces (); i++) - workspace_added (manager, i); + for (var i = 0; i < manager.get_n_workspaces (); i++) + workspace_added (manager, i); - Meta.Prefs.add_listener (prefs_listener); + Meta.Prefs.add_listener (prefs_listener); - manager.workspace_switched.connect_after (workspace_switched); - manager.workspace_added.connect (workspace_added); - manager.workspace_removed.connect_after (workspace_removed); - display.window_entered_monitor.connect (window_entered_monitor); - display.window_left_monitor.connect (window_left_monitor); + manager.workspace_switched.connect_after (workspace_switched); + manager.workspace_added.connect (workspace_added); + manager.workspace_removed.connect_after (workspace_removed); + display.window_entered_monitor.connect (window_entered_monitor); + display.window_left_monitor.connect (window_left_monitor); - // make sure the last workspace has no windows on it - if (Meta.Prefs.get_dynamic_workspaces () - && Utils.get_n_windows (manager.get_workspace_by_index (manager.get_n_workspaces () - 1)) > 0) - append_workspace (); - } + // make sure the last workspace has no windows on it + if (Meta.Prefs.get_dynamic_workspaces () + && Utils.get_n_windows (manager.get_workspace_by_index (manager.get_n_workspaces () - 1)) > 0) + append_workspace (); + } - ~WorkspaceManager () { - Meta.Prefs.remove_listener (prefs_listener); + ~WorkspaceManager () { + Meta.Prefs.remove_listener (prefs_listener); - unowned Meta.Display display = wm.get_display (); - unowned Meta.WorkspaceManager manager = display.get_workspace_manager (); - manager.workspace_added.disconnect (workspace_added); - manager.workspace_switched.disconnect (workspace_switched); - manager.workspace_removed.disconnect (workspace_removed); - display.window_entered_monitor.disconnect (window_entered_monitor); - display.window_left_monitor.disconnect (window_left_monitor); - } + unowned Meta.Display display = wm.get_display (); + unowned Meta.WorkspaceManager manager = display.get_workspace_manager (); + manager.workspace_added.disconnect (workspace_added); + manager.workspace_switched.disconnect (workspace_switched); + manager.workspace_removed.disconnect (workspace_removed); + display.window_entered_monitor.disconnect (window_entered_monitor); + display.window_left_monitor.disconnect (window_left_monitor); + } - private void workspace_added (Meta.WorkspaceManager manager, int index) { - var workspace = manager.get_workspace_by_index (index); - if (workspace == null) - return; + private void workspace_added (Meta.WorkspaceManager manager, int index) { + var workspace = manager.get_workspace_by_index (index); + if (workspace == null) + return; - workspace.window_added.connect (window_added); - workspace.window_removed.connect (window_removed); - } + workspace.window_added.connect (window_added); + workspace.window_removed.connect (window_removed); + } - private void workspace_removed (Meta.WorkspaceManager manager, int index) { - List existing_workspaces = null; - for (int i = 0; i < manager.get_n_workspaces (); i++) { - existing_workspaces.append (manager.get_workspace_by_index (i)); - } + private void workspace_removed (Meta.WorkspaceManager manager, int index) { + List existing_workspaces = null; + for (int i = 0; i < manager.get_n_workspaces (); i++) { + existing_workspaces.append (manager.get_workspace_by_index (i)); + } - var it = workspaces_marked_removed.iterator (); - while (it.next ()) { - var workspace = it.@get (); + var it = workspaces_marked_removed.iterator (); + while (it.next ()) { + var workspace = it.@get (); - if (existing_workspaces.index (workspace) < 0) - it.remove (); - } + if (existing_workspaces.index (workspace) < 0) + it.remove (); } + } - private void workspace_switched (Meta.WorkspaceManager manager, int from, int to, Meta.MotionDirection direction) { - if (!Meta.Prefs.get_dynamic_workspaces ()) - return; + private void workspace_switched (Meta.WorkspaceManager manager, int from, int to, Meta.MotionDirection direction) { + if (!Meta.Prefs.get_dynamic_workspaces ()) + return; - // remove empty workspaces after we switched away from them unless it's the last one - var prev_workspace = manager.get_workspace_by_index (from); - if (Utils.get_n_windows (prev_workspace) < 1 - && from != manager.get_n_workspaces () - 1) { + // remove empty workspaces after we switched away from them unless it's the last one + var prev_workspace = manager.get_workspace_by_index (from); + if (Utils.get_n_windows (prev_workspace) < 1 + && from != manager.get_n_workspaces () - 1) { - // If we're about to remove a workspace, cancel any DnD going on in the multitasking view - // or else things might get broke - DragDropAction.cancel_all_by_id ("multitaskingview-window"); + // If we're about to remove a workspace, cancel any DnD going on in the multitasking view + // or else things might get broke + DragDropAction.cancel_all_by_id ("multitaskingview-window"); - remove_workspace (prev_workspace); - } + remove_workspace (prev_workspace); } + } - private void window_added (Meta.Workspace? workspace, Meta.Window window) { - if (workspace == null || !Meta.Prefs.get_dynamic_workspaces () || window.on_all_workspaces) { - return; - } - - unowned Meta.WorkspaceManager manager = workspace.get_display ().get_workspace_manager (); - int last_workspace = manager.get_n_workspaces () - 1; - - if ((window.window_type == Meta.WindowType.NORMAL - || window.window_type == Meta.WindowType.DIALOG - || window.window_type == Meta.WindowType.MODAL_DIALOG) - && workspace.index () == last_workspace) - append_workspace (); + private void window_added (Meta.Workspace? workspace, Meta.Window window) { + if (workspace == null || !Meta.Prefs.get_dynamic_workspaces () || window.on_all_workspaces) { + return; } - private void window_removed (Meta.Workspace? workspace, Meta.Window window) { - if (workspace == null || !Meta.Prefs.get_dynamic_workspaces () || window.on_all_workspaces) { - return; - } + unowned Meta.WorkspaceManager manager = workspace.get_display ().get_workspace_manager (); + int last_workspace = manager.get_n_workspaces () - 1; - unowned Meta.WorkspaceManager manager = workspace.get_display ().get_workspace_manager (); - bool is_active_workspace = workspace == manager.get_active_workspace (); - var last_workspace_index = manager.get_n_workspaces () - 1; - unowned var last_workspace = manager.get_workspace_by_index (last_workspace_index); + if ((window.window_type == Meta.WindowType.NORMAL + || window.window_type == Meta.WindowType.DIALOG + || window.window_type == Meta.WindowType.MODAL_DIALOG) + && workspace.index () == last_workspace) + append_workspace (); + } - if (window.window_type != Meta.WindowType.NORMAL - && window.window_type != Meta.WindowType.DIALOG - && window.window_type != Meta.WindowType.MODAL_DIALOG) - return; + private void window_removed (Meta.Workspace? workspace, Meta.Window window) { + if (workspace == null || !Meta.Prefs.get_dynamic_workspaces () || window.on_all_workspaces) { + return; + } - // has already been removed - if (workspace.index () < 0) { - return; - } + unowned Meta.WorkspaceManager manager = workspace.get_display ().get_workspace_manager (); + bool is_active_workspace = workspace == manager.get_active_workspace (); + var last_workspace_index = manager.get_n_workspaces () - 1; + unowned var last_workspace = manager.get_workspace_by_index (last_workspace_index); - // remove it right away if it was the active workspace and it's not the very last - // or we are in modal-mode - if ((!is_active_workspace || wm.is_modal ()) - && remove_freeze_count < 1 - && Utils.get_n_windows (workspace, true, window) == 0 - && workspace != last_workspace) { - remove_workspace (workspace); - } + if (window.window_type != Meta.WindowType.NORMAL + && window.window_type != Meta.WindowType.DIALOG + && window.window_type != Meta.WindowType.MODAL_DIALOG) + return; - // if window is the second last and empty, make it the last workspace - if (is_active_workspace - && remove_freeze_count < 1 - && Utils.get_n_windows (workspace, true, window) == 0 - && workspace.index () == last_workspace_index - 1) { - remove_workspace (last_workspace); - } + // has already been removed + if (workspace.index () < 0) { + return; } - private void window_entered_monitor (Meta.Display display, int monitor, Meta.Window window) { - if (InternalUtils.workspaces_only_on_primary () - && monitor == display.get_primary_monitor ()) - window_added (window.get_workspace (), window); + // remove it right away if it was the active workspace and it's not the very last + // or we are in modal-mode + if ((!is_active_workspace || wm.is_modal ()) + && remove_freeze_count < 1 + && Utils.get_n_windows (workspace, true, window) == 0 + && workspace != last_workspace) { + remove_workspace (workspace); } - private void window_left_monitor (Meta.Display display, int monitor, Meta.Window window) { - if (InternalUtils.workspaces_only_on_primary () - && monitor == display.get_primary_monitor ()) - window_removed (window.get_workspace (), window); + // if window is the second last and empty, make it the last workspace + if (is_active_workspace + && remove_freeze_count < 1 + && Utils.get_n_windows (workspace, true, window) == 0 + && workspace.index () == last_workspace_index - 1) { + remove_workspace (last_workspace); } + } - private void prefs_listener (Meta.Preference pref) { - unowned Meta.WorkspaceManager manager = wm.get_display ().get_workspace_manager (); + private void window_entered_monitor (Meta.Display display, int monitor, Meta.Window window) { + if (InternalUtils.workspaces_only_on_primary () + && monitor == display.get_primary_monitor ()) + window_added (window.get_workspace (), window); + } - if (pref == Meta.Preference.DYNAMIC_WORKSPACES && Meta.Prefs.get_dynamic_workspaces ()) { - // if the last workspace has a window, we need to append a new workspace - if (Utils.get_n_windows (manager.get_workspace_by_index (manager.get_n_workspaces () - 1)) > 0) - append_workspace (); - } + private void window_left_monitor (Meta.Display display, int monitor, Meta.Window window) { + if (InternalUtils.workspaces_only_on_primary () + && monitor == display.get_primary_monitor ()) + window_removed (window.get_workspace (), window); + } + + private void prefs_listener (Meta.Preference pref) { + unowned Meta.WorkspaceManager manager = wm.get_display ().get_workspace_manager (); + + if (pref == Meta.Preference.DYNAMIC_WORKSPACES && Meta.Prefs.get_dynamic_workspaces ()) { + // if the last workspace has a window, we need to append a new workspace + if (Utils.get_n_windows (manager.get_workspace_by_index (manager.get_n_workspaces () - 1)) > 0) + append_workspace (); } + } + + private void append_workspace () { + unowned Meta.Display display = wm.get_display (); + unowned Meta.WorkspaceManager manager = display.get_workspace_manager (); - private void append_workspace () { - unowned Meta.Display display = wm.get_display (); - unowned Meta.WorkspaceManager manager = display.get_workspace_manager (); + manager.append_new_workspace (false, display.get_current_time ()); + } - manager.append_new_workspace (false, display.get_current_time ()); + /** + * Make sure we switch to a different workspace and remove the given one + * + * @param workspace The workspace to remove + */ + private void remove_workspace (Meta.Workspace workspace) { + unowned Meta.Display display = workspace.get_display (); + unowned Meta.WorkspaceManager manager = display.get_workspace_manager (); + var time = display.get_current_time (); + unowned Meta.Workspace active_workspace = manager.get_active_workspace (); + + if (workspace == active_workspace) { + Meta.Workspace? next = null; + + next = workspace.get_neighbor (Meta.MotionDirection.LEFT); + // if it's the first one we may have another one to the right + if (next == workspace || next == null) + next = workspace.get_neighbor (Meta.MotionDirection.RIGHT); + + if (next != null) + next.activate (time); } - /** - * Make sure we switch to a different workspace and remove the given one - * - * @param workspace The workspace to remove - */ - private void remove_workspace (Meta.Workspace workspace) { - unowned Meta.Display display = workspace.get_display (); - unowned Meta.WorkspaceManager manager = display.get_workspace_manager (); - var time = display.get_current_time (); - unowned Meta.Workspace active_workspace = manager.get_active_workspace (); - - if (workspace == active_workspace) { - Meta.Workspace? next = null; - - next = workspace.get_neighbor (Meta.MotionDirection.LEFT); - // if it's the first one we may have another one to the right - if (next == workspace || next == null) - next = workspace.get_neighbor (Meta.MotionDirection.RIGHT); - - if (next != null) - next.activate (time); - } + // workspace has already been removed + if (workspace in workspaces_marked_removed) { + return; + } - // workspace has already been removed - if (workspace in workspaces_marked_removed) { - return; - } + workspace.window_added.disconnect (window_added); + workspace.window_removed.disconnect (window_removed); - workspace.window_added.disconnect (window_added); - workspace.window_removed.disconnect (window_removed); + workspaces_marked_removed.add (workspace); - workspaces_marked_removed.add (workspace); + manager.remove_workspace (workspace, time); + } - manager.remove_workspace (workspace, time); - } + /** + * Temporarily disables removing workspaces when they are empty + */ + public void freeze_remove () { + GLib.AtomicInt.inc (ref remove_freeze_count); + } - /** - * Temporarily disables removing workspaces when they are empty - */ - public void freeze_remove () { - GLib.AtomicInt.inc (ref remove_freeze_count); + /** + * Undo the effect of freeze_remove() + */ + public void thaw_remove () { + if (GLib.AtomicInt.dec_and_test (ref remove_freeze_count)) { + cleanup (); } - /** - * Undo the effect of freeze_remove() - */ - public void thaw_remove () { - if (GLib.AtomicInt.dec_and_test (ref remove_freeze_count)) { - cleanup (); - } + assert (remove_freeze_count >= 0); + } - assert (remove_freeze_count >= 0); + /** + * If workspaces are dynamic, checks if there are empty workspaces that should + * be removed. Particularly useful in conjunction with freeze/thaw_remove to + * cleanup after an operation that required stable workspace/window indices + */ + private void cleanup () { + if (!Meta.Prefs.get_dynamic_workspaces ()) { + return; } - /** - * If workspaces are dynamic, checks if there are empty workspaces that should - * be removed. Particularly useful in conjunction with freeze/thaw_remove to - * cleanup after an operation that required stable workspace/window indices - */ - private void cleanup () { - if (!Meta.Prefs.get_dynamic_workspaces ()) { - return; - } - - unowned Meta.WorkspaceManager manager = wm.get_display ().get_workspace_manager (); + unowned Meta.WorkspaceManager manager = wm.get_display ().get_workspace_manager (); - foreach (var workspace in manager.get_workspaces ()) { - var last_index = manager.get_n_workspaces () - 1; - if (Utils.get_n_windows (workspace) == 0 - && workspace.index () != last_index) { - remove_workspace (workspace); - } + foreach (var workspace in manager.get_workspaces ()) { + var last_index = manager.get_n_workspaces () - 1; + if (Utils.get_n_windows (workspace) == 0 + && workspace.index () != last_index) { + remove_workspace (workspace); } } } From 9d876a2132cfb31a6a3716ab8234ea6329f05b33 Mon Sep 17 00:00:00 2001 From: Leonhard Kargl Date: Thu, 9 Jan 2025 02:05:58 +0100 Subject: [PATCH 2/2] WorkspaceManager: Always use braces --- src/WorkspaceManager.vala | 56 +++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/WorkspaceManager.vala b/src/WorkspaceManager.vala index 08490a924..5245a50d4 100644 --- a/src/WorkspaceManager.vala +++ b/src/WorkspaceManager.vala @@ -43,11 +43,13 @@ public class Gala.WorkspaceManager : Object { // There are some empty workspace at startup cleanup (); - if (Meta.Prefs.get_dynamic_workspaces ()) + if (Meta.Prefs.get_dynamic_workspaces ()) { manager.override_workspace_layout (Meta.DisplayCorner.TOPLEFT, false, 1, -1); + } - for (var i = 0; i < manager.get_n_workspaces (); i++) + for (var i = 0; i < manager.get_n_workspaces (); i++) { workspace_added (manager, i); + } Meta.Prefs.add_listener (prefs_listener); @@ -59,8 +61,10 @@ public class Gala.WorkspaceManager : Object { // make sure the last workspace has no windows on it if (Meta.Prefs.get_dynamic_workspaces () - && Utils.get_n_windows (manager.get_workspace_by_index (manager.get_n_workspaces () - 1)) > 0) + && Utils.get_n_windows (manager.get_workspace_by_index (manager.get_n_workspaces () - 1)) > 0 + ) { append_workspace (); + } } ~WorkspaceManager () { @@ -77,8 +81,9 @@ public class Gala.WorkspaceManager : Object { private void workspace_added (Meta.WorkspaceManager manager, int index) { var workspace = manager.get_workspace_by_index (index); - if (workspace == null) + if (workspace == null) { return; + } workspace.window_added.connect (window_added); workspace.window_removed.connect (window_removed); @@ -94,19 +99,22 @@ public class Gala.WorkspaceManager : Object { while (it.next ()) { var workspace = it.@get (); - if (existing_workspaces.index (workspace) < 0) + if (existing_workspaces.index (workspace) < 0) { it.remove (); + } } } private void workspace_switched (Meta.WorkspaceManager manager, int from, int to, Meta.MotionDirection direction) { - if (!Meta.Prefs.get_dynamic_workspaces ()) + if (!Meta.Prefs.get_dynamic_workspaces ()) { return; + } // remove empty workspaces after we switched away from them unless it's the last one var prev_workspace = manager.get_workspace_by_index (from); if (Utils.get_n_windows (prev_workspace) < 1 - && from != manager.get_n_workspaces () - 1) { + && from != manager.get_n_workspaces () - 1 + ) { // If we're about to remove a workspace, cancel any DnD going on in the multitasking view // or else things might get broke @@ -127,8 +135,10 @@ public class Gala.WorkspaceManager : Object { if ((window.window_type == Meta.WindowType.NORMAL || window.window_type == Meta.WindowType.DIALOG || window.window_type == Meta.WindowType.MODAL_DIALOG) - && workspace.index () == last_workspace) + && workspace.index () == last_workspace + ) { append_workspace (); + } } private void window_removed (Meta.Workspace? workspace, Meta.Window window) { @@ -143,8 +153,10 @@ public class Gala.WorkspaceManager : Object { if (window.window_type != Meta.WindowType.NORMAL && window.window_type != Meta.WindowType.DIALOG - && window.window_type != Meta.WindowType.MODAL_DIALOG) + && window.window_type != Meta.WindowType.MODAL_DIALOG + ) { return; + } // has already been removed if (workspace.index () < 0) { @@ -156,7 +168,8 @@ public class Gala.WorkspaceManager : Object { if ((!is_active_workspace || wm.is_modal ()) && remove_freeze_count < 1 && Utils.get_n_windows (workspace, true, window) == 0 - && workspace != last_workspace) { + && workspace != last_workspace + ) { remove_workspace (workspace); } @@ -164,21 +177,22 @@ public class Gala.WorkspaceManager : Object { if (is_active_workspace && remove_freeze_count < 1 && Utils.get_n_windows (workspace, true, window) == 0 - && workspace.index () == last_workspace_index - 1) { + && workspace.index () == last_workspace_index - 1 + ) { remove_workspace (last_workspace); } } private void window_entered_monitor (Meta.Display display, int monitor, Meta.Window window) { - if (InternalUtils.workspaces_only_on_primary () - && monitor == display.get_primary_monitor ()) + if (InternalUtils.workspaces_only_on_primary () && monitor == display.get_primary_monitor ()) { window_added (window.get_workspace (), window); + } } private void window_left_monitor (Meta.Display display, int monitor, Meta.Window window) { - if (InternalUtils.workspaces_only_on_primary () - && monitor == display.get_primary_monitor ()) + if (InternalUtils.workspaces_only_on_primary () && monitor == display.get_primary_monitor ()) { window_removed (window.get_workspace (), window); + } } private void prefs_listener (Meta.Preference pref) { @@ -186,8 +200,9 @@ public class Gala.WorkspaceManager : Object { if (pref == Meta.Preference.DYNAMIC_WORKSPACES && Meta.Prefs.get_dynamic_workspaces ()) { // if the last workspace has a window, we need to append a new workspace - if (Utils.get_n_windows (manager.get_workspace_by_index (manager.get_n_workspaces () - 1)) > 0) + if (Utils.get_n_windows (manager.get_workspace_by_index (manager.get_n_workspaces () - 1)) > 0) { append_workspace (); + } } } @@ -214,11 +229,13 @@ public class Gala.WorkspaceManager : Object { next = workspace.get_neighbor (Meta.MotionDirection.LEFT); // if it's the first one we may have another one to the right - if (next == workspace || next == null) + if (next == workspace || next == null) { next = workspace.get_neighbor (Meta.MotionDirection.RIGHT); + } - if (next != null) + if (next != null) { next.activate (time); + } } // workspace has already been removed @@ -266,8 +283,7 @@ public class Gala.WorkspaceManager : Object { foreach (var workspace in manager.get_workspaces ()) { var last_index = manager.get_n_workspaces () - 1; - if (Utils.get_n_windows (workspace) == 0 - && workspace.index () != last_index) { + if (Utils.get_n_windows (workspace) == 0 && workspace.index () != last_index) { remove_workspace (workspace); } }