on b3c03fa help sign in
unity8/plugins/WindowManager/WorkspaceManager.cpp
111 lines 3.2 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"
21// Qt
22#include <QGuiApplication>
23#include <QScreen>
24#include <QQmlEngine>
26WorkspaceManager *WorkspaceManager::instance()
28 static WorkspaceManager* workspaceManager(new WorkspaceManager());
29 return workspaceManager;
32WorkspaceManager::WorkspaceManager()
33 : m_activeWorkspace(nullptr)
37Workspace *WorkspaceManager::createWorkspace()
39 auto workspace = new Workspace(this);
40 QQmlEngine::setObjectOwnership(workspace, QQmlEngine::CppOwnership);
41 m_allWorkspaces.insert(workspace);
42 m_floatingWorkspaces.append(workspace);
44 connect(workspace, &Workspace::assigned, this, [this, workspace]() {
45 m_floatingWorkspaces.removeOne(workspace);
46 Q_EMIT floatingWorkspacesChanged();
47 });
48 connect(workspace, &Workspace::unassigned, this, [this, workspace]() {
49 m_floatingWorkspaces.append(workspace);
50 Q_EMIT floatingWorkspacesChanged();
51 });
53 if (m_allWorkspaces.count() == 0 && m_activeWorkspace) {
54 setActiveWorkspace(nullptr);
55 Q_EMIT activeWorkspaceChanged();
56 } else if (m_allWorkspaces.count() == 1) {
57 setActiveWorkspace(workspace);
58 }
60 return workspace;
63void WorkspaceManager::destroyWorkspace(Workspace *workspace)
65 if (!workspace) return;
67 if (workspace->isAssigned()) {
68 workspace->unassign();
69 }
70 m_floatingWorkspaces.removeOne(workspace);
71 m_allWorkspaces.remove(workspace);
72 Q_EMIT floatingWorkspacesChanged();
74 if (m_activeWorkspace == workspace) {
75 Q_ASSERT(false); // Shouldn't happen, should have chosen something by now.
76 // just choose anything.
77 setActiveWorkspace(m_allWorkspaces.count() ? *m_allWorkspaces.begin() : nullptr);
78 }
79 if (m_activeWorkspace) {
80 workspace->moveWindowsTo(m_activeWorkspace);
81 }
83 disconnect(workspace, 0, this, 0);
84 workspace->release();
87QQmlListProperty<Workspace> WorkspaceManager::floatingWorkspaces()
89 return QQmlListProperty<Workspace>(this, m_floatingWorkspaces);
92void WorkspaceManager::destroyFloatingWorkspaces()
94 QList<Workspace*> dpCpy(m_floatingWorkspaces);
95 Q_FOREACH(auto workspace, dpCpy) {
96 destroyWorkspace(workspace);
97 }
100Workspace *WorkspaceManager::activeWorkspace() const
102 return m_activeWorkspace;
105void WorkspaceManager::setActiveWorkspace(Workspace *workspace)
107 if (workspace != m_activeWorkspace) {
108 m_activeWorkspace = workspace;
109 Q_EMIT activeWorkspaceChanged();
110 }