Skip to content

Commit

Permalink
WorkspaceManager: Fixes and cleanup (#1768)
Browse files Browse the repository at this point in the history
Co-authored-by: Danielle Foré <danielle@elementary.io>
  • Loading branch information
lenemter and danirabbit authored Oct 26, 2023
1 parent 493dd89 commit 11c3125
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/WorkspaceManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ namespace Gala {
}

private void window_added (Meta.Workspace? workspace, Meta.Window window) {
if (workspace == null || !Meta.Prefs.get_dynamic_workspaces ()
|| window.on_all_workspaces)
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;
Expand All @@ -133,31 +133,41 @@ namespace Gala {
}

private void window_removed (Meta.Workspace? workspace, Meta.Window window) {
if (workspace == null || !Meta.Prefs.get_dynamic_workspaces () || window.on_all_workspaces)
if (workspace == null || !Meta.Prefs.get_dynamic_workspaces () || window.on_all_workspaces) {
return;
}

unowned Meta.WorkspaceManager manager = workspace.get_display ().get_workspace_manager ();
var index = workspace.index ();
bool is_active_workspace = workspace == manager.get_active_workspace ();
int last_workspace = manager.get_n_workspaces () - 1;
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)
return;

// has already been removed
if (index < 0)
if (workspace.index () < 0) {
return;
}

// 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) < 1
&& index != last_workspace) {
&& Utils.get_n_windows (workspace) == 0
&& workspace != last_workspace) {
remove_workspace (workspace);
}

// 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) == 0
&& workspace.index () == last_workspace_index - 1) {
remove_workspace (last_workspace);
}
}

private void window_entered_monitor (Meta.Display display, int monitor, Meta.Window window) {
Expand Down Expand Up @@ -247,19 +257,15 @@ namespace Gala {
* cleanup after an operation that required stable workspace/window indices
*/
public void cleanup () {
if (!Meta.Prefs.get_dynamic_workspaces ())
if (!Meta.Prefs.get_dynamic_workspaces ()) {
return;

unowned Meta.Display display = wm.get_display ();
unowned Meta.WorkspaceManager manager = display.get_workspace_manager ();
List<Meta.Workspace> workspaces = null;
for (int i = 0; i < manager.get_n_workspaces (); i++) {
workspaces.append (manager.get_workspace_by_index (i));
}

foreach (var workspace in workspaces) {
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) < 1
if (Utils.get_n_windows (workspace) == 0
&& workspace.index () != last_index) {
remove_workspace (workspace);
}
Expand Down

0 comments on commit 11c3125

Please sign in to comment.