public void MoveWorkspaceToMonitor(Workspace workspace, Monitor newMonitor, bool showOnNewMonitor = true, bool switchTo = true) { var oldMonitor = workspace.Monitor; if (oldMonitor != newMonitor && oldMonitor.Workspaces.Count() > 1) { // unswitch the current workspace if (CurrentWorkspace != workspace && switchTo) { CurrentWorkspace.IsCurrentWorkspace = false; // remove windows from ALT-TAB menu and Taskbar if (CurrentWorkspace.hideFromAltTabWhenOnInactiveWorkspaceCount > 0) { CurrentWorkspace.GetWindows().Where(w => w.hideFromAltTabAndTaskbarWhenOnInactiveWorkspace).ForEach(w => w.ShowInAltTabAndTaskbar(false)); } } // if the workspace to be moved is visible on the old monitor, switch to another one if (oldMonitor.CurrentVisibleWorkspace == workspace) { var oldMonitorNewWorkspace = oldMonitor.Workspaces.First(ws => !ws.IsWorkspaceVisible); ShowHideWindows(workspace, oldMonitorNewWorkspace, false); oldMonitor.SwitchToWorkspace(oldMonitorNewWorkspace); } // remove from old/add to new monitor oldMonitor.RemoveWorkspace(workspace); workspace.Monitor = newMonitor; newMonitor.AddWorkspace(workspace); if (switchTo || showOnNewMonitor) // if the workspace must be switched to, it must be shown too { // switch to the workspace now on the new monitor ShowHideWindows(newMonitor.CurrentVisibleWorkspace, workspace, true); newMonitor.SwitchToWorkspace(workspace); } // switch to the moved workspace if (CurrentWorkspace != workspace && switchTo) { workspace.IsCurrentWorkspace = true; // add windows to ALT-TAB menu and Taskbar if (workspace.hideFromAltTabWhenOnInactiveWorkspaceCount > 0) { workspace.GetWindows().Where(w => w.hideFromAltTabAndTaskbarWhenOnInactiveWorkspace).ForEach(w => w.ShowInAltTabAndTaskbar(true)); } CurrentWorkspace = workspace; } if (CurrentWorkspace == workspace && config.MoveMouseOverMonitorsOnSwitch) { Utilities.MoveMouseToMiddleOf(workspace.Monitor.Bounds); } // reposition the windows on the workspace workspace.Reposition(); Workspace.DoWorkspaceMonitorChanged(workspace, oldMonitor, newMonitor); } }
private void SwapWorkspacesMonitorsAndSwitchTo(Workspace workspace) { var currentWorkspaceMonitor = CurrentWorkspace.Monitor; var newWorkspaceMonitor = workspace.Monitor; currentWorkspaceMonitor.HideBars(workspace, CurrentWorkspace); newWorkspaceMonitor.HideBars(CurrentWorkspace, workspace); // first add the workspaces to their new monitors newWorkspaceMonitor.AddWorkspace(CurrentWorkspace); currentWorkspaceMonitor.AddWorkspace(workspace); // show the new bars while hiding the old ones newWorkspaceMonitor.ShowHideAppBars(workspace, CurrentWorkspace); currentWorkspaceMonitor.ShowHideAppBars(CurrentWorkspace, workspace); // only then remove the old workspaces from the monitors newWorkspaceMonitor.RemoveWorkspace(workspace); currentWorkspaceMonitor.RemoveWorkspace(CurrentWorkspace); // swap the workspaces' monitors CurrentWorkspace.Monitor = newWorkspaceMonitor; workspace.Monitor = currentWorkspaceMonitor; // swap the monitors' current visible workspaces currentWorkspaceMonitor.CurrentVisibleWorkspace = workspace; newWorkspaceMonitor.CurrentVisibleWorkspace = CurrentWorkspace; currentWorkspaceMonitor.ShowBars(workspace); newWorkspaceMonitor.ShowBars(CurrentWorkspace); // reposition the windows in the two workspaces workspace.Reposition(); CurrentWorkspace.Reposition(); // finally switch to the workspace SwitchToWorkspace(workspace.id); Workspace.DoWorkspaceMonitorChanged(workspace, newWorkspaceMonitor, currentWorkspaceMonitor); Workspace.DoWorkspaceMonitorChanged(CurrentWorkspace, currentWorkspaceMonitor, newWorkspaceMonitor); }