@@ -39,13 +39,30 @@ Screen::Screen(qtmir::Screen* screen)
3939
4040 // Connect the active workspace to activate the screen.
4141 connect(m_workspaces.data(), &WorkspaceModel::workspaceAdded, this, [this](Workspace* workspace) {
42− connect(workspace, &Workspace::activeChanged, this, [this](bool active) {
43− if (active) activate();
42+ connect(workspace, &Workspace::activeChanged, this, [this, workspace](bool active) {
43+ if (active) {
44+ setCurrentWorkspace(workspace);
45+ activate();
46+ }
4447 });
45− if (workspace->isActive()) activate();
48+ if (workspace->isActive()) {
49+ activate();
50+ setCurrentWorkspace(workspace);
51+ }
52+ if (!m_currentWorspace) {
53+ setCurrentWorkspace(workspace);
54+ }
4655 });
4756 connect(m_workspaces.data(), &WorkspaceModel::workspaceRemoved, this, [this](Workspace* workspace) {
4857 disconnect(workspace, &Workspace::activeChanged, this, 0);
58+ if (workspace == m_currentWorspace) {
59+ resetCurrentWorkspace();
60+ }
61+ });
62+ connect(this, &Screen::activeChanged, this, [this](bool active) {
63+ if (active && m_currentWorspace) {
64+ m_currentWorspace->activate();
65+ }
4966 });
5067
5168 WorkspaceManager::instance()->createWorkspace()->assign(m_workspaces.data());
@@ -71,6 +88,15 @@ Screen::Screen(const Screen &other)
7188 connect(m_wrapped, &qtmir::Screen::availableModesChanged, this, &Screen::availableModesChanged);
7289}
7390
91+void Screen::resetCurrentWorkspace()
92+{
93+ auto newCurrent = m_workspaces->rowCount() > 0 ? m_workspaces->get(0) : nullptr;
94+ if (m_currentWorspace != newCurrent) {
95+ m_currentWorspace = newCurrent;
96+ Q_EMIT currentWorkspaceChanged(newCurrent);
97+ }
98+}
99+
74100qtmir::OutputId Screen::outputId() const
75101{
76102 return m_wrapped->outputId();
@@ -151,6 +177,19 @@ WorkspaceModel *Screen::workspaces() const
151177 return m_workspaces.data();
152178}
153179
180+Workspace *Screen::currentWorkspace() const
181+{
182+ return m_currentWorspace.data();
183+}
184+
185+void Screen::setCurrentWorkspace(Workspace *workspace)
186+{
187+ if (m_currentWorspace != workspace) {
188+ m_currentWorspace = workspace;
189+ Q_EMIT currentWorkspaceChanged(workspace);
190+ }
191+}
192+
154193void Screen::sync(Screen *proxy)
155194{
156195 if (!proxy) return;