Lomiri
WorkspaceManager.cpp
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  */
16 
17 #include "WorkspaceManager.h"
18 #include "Workspace.h"
19 #include "TopLevelWindowModel.h"
20 #include "WindowManagerObjects.h"
21 #include <lomiri/shell/application/SurfaceManagerInterface.h>
22 
23 // Qt
24 #include <QGuiApplication>
25 #include <QScreen>
26 #include <QQmlEngine>
27 
28 WorkspaceManager *WorkspaceManager::instance()
29 {
30  static WorkspaceManager* workspaceManager = ([]() {
31  auto w = new WorkspaceManager();
32  QQmlEngine::setObjectOwnership(w, QQmlEngine::CppOwnership);
33  return w;
34  }());
35  return workspaceManager;
36 }
37 
38 WorkspaceManager::WorkspaceManager()
39  : m_activeWorkspace(nullptr),
40  m_surfaceManager(nullptr)
41 {
42  connect(WindowManagerObjects::instance(), &WindowManagerObjects::surfaceManagerChanged,
43  this, &WorkspaceManager::setSurfaceManager);
44 
45  setSurfaceManager(WindowManagerObjects::instance()->surfaceManager());
46 }
47 
48 WorkspaceManager::~WorkspaceManager()
49 {
50  m_allWorkspaces.clear();
51 }
52 
53 void WorkspaceManager::setSurfaceManager(lomiri::shell::application::SurfaceManagerInterface *surfaceManager)
54 {
55  if (m_surfaceManager == surfaceManager) return;
56 
57  if (m_surfaceManager) {
58  disconnect(m_surfaceManager, &QObject::destroyed, this, 0);
59  }
60 
61  m_surfaceManager = surfaceManager;
62 
63  if (m_surfaceManager) {
64  connect(m_surfaceManager, &QObject::destroyed, this, [this](){
65  setSurfaceManager(nullptr);
66  });
67  }
68 }
69 
70 Workspace *WorkspaceManager::createWorkspace()
71 {
72  auto workspace = new ConcreteWorkspace(this);
73  QQmlEngine::setObjectOwnership(workspace, QQmlEngine::CppOwnership);
74  m_allWorkspaces.insert(workspace);
75 
76  if (m_allWorkspaces.count() == 0 && m_activeWorkspace) {
77  setActiveWorkspace(nullptr);
78  } else if (m_allWorkspaces.count() == 1) {
79  setActiveWorkspace(workspace);
80  }
81 
82  return workspace;
83 }
84 
85 void WorkspaceManager::destroyWorkspace(Workspace *workspace)
86 {
87  if (!workspace) return;
88 
89  if (workspace->isAssigned()) {
90  workspace->unassign();
91  }
92  m_allWorkspaces.remove(workspace);
93 
94  if (m_activeWorkspace == workspace) {
95  setActiveWorkspace(m_allWorkspaces.count() ? *m_allWorkspaces.begin() : nullptr);
96  }
97  if (m_activeWorkspace) {
98  moveWorkspaceContentToWorkspace(m_activeWorkspace, workspace);
99  }
100 
101  disconnect(workspace, 0, this, 0);
102 }
103 
104 void WorkspaceManager::moveSurfaceToWorkspace(lomiri::shell::application::MirSurfaceInterface *surface, Workspace *workspace)
105 {
106  if (m_surfaceManager) {
107  m_surfaceManager->moveSurfaceToWorkspace(surface, workspace->workspace());
108  }
109 }
110 
111 void WorkspaceManager::moveWorkspaceContentToWorkspace(Workspace *to, Workspace *from)
112 {
113  if (m_surfaceManager) {
114  m_surfaceManager->moveWorkspaceContentToWorkspace(to->workspace(), from->workspace());
115  }
116 }
117 
118 Workspace *WorkspaceManager::activeWorkspace() const
119 {
120  return m_activeWorkspace;
121 }
122 
123 void WorkspaceManager::setActiveWorkspace(Workspace *workspace)
124 {
125  if (workspace != m_activeWorkspace) {
126  m_activeWorkspace = workspace;
127  Q_EMIT activeWorkspaceChanged(workspace);
128  }
129 }
130 
131 void WorkspaceManager::setActiveWorkspace2(Workspace *workspace)
132 {
133  if (!workspace) return;
134  workspace->activate();
135 }