on bcbc40d help sign in
unity8/plugins/WindowManager/WorkspaceManager.cpp
141 lines 4.1 kB history raw
1/*
2 * Copyright (C) 2017 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "WorkspaceManager.h"
18#include "Workspace.h"
19#include "TopLevelWindowModel.h"
20#include <unity/shell/application/SurfaceManagerInterface.h>
22// Qt
23#include <QGuiApplication>
24#include <QScreen>
25#include <QQmlEngine>
27WorkspaceManager *WorkspaceManager::instance()
29 static WorkspaceManager* workspaceManager(new WorkspaceManager());
30 return workspaceManager;
33WorkspaceManager::WorkspaceManager()
34 : m_activeWorkspace(nullptr)
35 , m_surfaceManager(nullptr)
39Workspace *WorkspaceManager::createWorkspace()
41 auto workspace = new Workspace(this);
42 QQmlEngine::setObjectOwnership(workspace, QQmlEngine::CppOwnership);
43 m_allWorkspaces.insert(workspace);
44 m_floatingWorkspaces.append(workspace);
46 connect(workspace, &Workspace::assigned, this, [this, workspace]() {
47 m_floatingWorkspaces.removeOne(workspace);
48 Q_EMIT floatingWorkspacesChanged();
49 });
50 connect(workspace, &Workspace::unassigned, this, [this, workspace]() {
51 m_floatingWorkspaces.append(workspace);
52 Q_EMIT floatingWorkspacesChanged();
53 });
55 if (m_allWorkspaces.count() == 0 && m_activeWorkspace) {
56 setActiveWorkspace(nullptr);
57 Q_EMIT activeWorkspaceChanged();
58 } else if (m_allWorkspaces.count() == 1) {
59 setActiveWorkspace(workspace);
60 }
62 return workspace;
65void WorkspaceManager::destroyWorkspace(Workspace *workspace)
67 if (!workspace) return;
69 if (workspace->isAssigned()) {
70 workspace->unassign();
71 }
72 m_floatingWorkspaces.removeOne(workspace);
73 m_allWorkspaces.remove(workspace);
74 Q_EMIT floatingWorkspacesChanged();
76 if (m_activeWorkspace == workspace) {
77 Q_ASSERT(false); // Shouldn't happen, should have chosen something by now.
78 // just choose anything.
79 setActiveWorkspace(m_allWorkspaces.count() ? *m_allWorkspaces.begin() : nullptr);
80 }
81 if (m_activeWorkspace) {
82 moveWorkspaceContentToWorkspace(workspace, m_activeWorkspace);
83 }
85 disconnect(workspace, 0, this, 0);
86 workspace->release();
89QQmlListProperty<Workspace> WorkspaceManager::floatingWorkspaces()
91 return QQmlListProperty<Workspace>(this, m_floatingWorkspaces);
94void WorkspaceManager::destroyFloatingWorkspaces()
96 QList<Workspace*> dpCpy(m_floatingWorkspaces);
97 Q_FOREACH(auto workspace, dpCpy) {
98 destroyWorkspace(workspace);
99 }
102void WorkspaceManager::setSurfaceManager(unity::shell::application::SurfaceManagerInterface *surfaceManager)
104 if (m_surfaceManager == surfaceManager) return;
106 m_surfaceManager = surfaceManager;
107 Q_EMIT surfaceManagerChanged();
110void WorkspaceManager::moveSurfaceToWorkspace(unity::shell::application::MirSurfaceInterface *surface, Workspace *workspace)
112 if (m_surfaceManager) {
113 m_surfaceManager->moveSurfaceToWorkspace(surface, workspace->workspace());
114 }
117void WorkspaceManager::moveWorkspaceContentToWorkspace(Workspace *from, Workspace *to)
119 if (m_surfaceManager) {
120 m_surfaceManager->moveWorkspaceContentToWorkspace(from->workspace(), to->workspace());
121 }
124Workspace *WorkspaceManager::activeWorkspace() const
126 return m_activeWorkspace;
129void WorkspaceManager::setActiveWorkspace(Workspace *workspace)
131 if (workspace != m_activeWorkspace) {
132 m_activeWorkspace = workspace;
133 Q_EMIT activeWorkspaceChanged();
134 }
137void WorkspaceManager::setActiveWorkspace2(Workspace *workspace)
139 if (!workspace) return;
140 workspace->activate();