Lomiri
Loading...
Searching...
No Matches
TopLevelWindowModel.cpp
1/*
2 * Copyright (C) 2016-2017 Canonical Ltd.
3 * Copyright 2019 UBports Foundation
4 *
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License version 3, as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
11 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "TopLevelWindowModel.h"
19#include "WindowManagerObjects.h"
20
21// lomiri-api
22#include <lomiri/shell/application/ApplicationInfoInterface.h>
23#include <lomiri/shell/application/ApplicationManagerInterface.h>
24#include <lomiri/shell/application/MirSurfaceInterface.h>
25#include <lomiri/shell/application/MirSurfaceListInterface.h>
26#include <lomiri/shell/application/SurfaceManagerInterface.h>
27
28// Qt
29#include <QDebug>
30
31// local
32#include "Window.h"
33#include "Workspace.h"
34#include "InputMethodManager.h"
35
36Q_LOGGING_CATEGORY(TOPLEVELWINDOWMODEL, "toplevelwindowmodel", QtInfoMsg)
37
38#define DEBUG_MSG qCDebug(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
39#define INFO_MSG qCInfo(TOPLEVELWINDOWMODEL).nospace().noquote() << __func__
40
41namespace lomiriapi = lomiri::shell::application;
42
43TopLevelWindowModel::TopLevelWindowModel(Workspace* workspace)
44 : m_nullWindow(createWindow(nullptr)),
45 m_workspace(workspace),
46 m_surfaceManagerBusy(false)
47{
48 connect(WindowManagerObjects::instance(), &WindowManagerObjects::applicationManagerChanged,
49 this, &TopLevelWindowModel::setApplicationManager);
50 connect(WindowManagerObjects::instance(), &WindowManagerObjects::surfaceManagerChanged,
51 this, &TopLevelWindowModel::setSurfaceManager);
52
53 setApplicationManager(WindowManagerObjects::instance()->applicationManager());
54 setSurfaceManager(WindowManagerObjects::instance()->surfaceManager());
55
56 connect(m_nullWindow, &Window::focusedChanged, this, [this] {
57 Q_EMIT rootFocusChanged();
58 });
59}
60
61TopLevelWindowModel::~TopLevelWindowModel()
62{
63}
64
65void TopLevelWindowModel::setApplicationManager(lomiriapi::ApplicationManagerInterface* value)
66{
67 if (m_applicationManager == value) {
68 return;
69 }
70
71 DEBUG_MSG << "(" << value << ")";
72
73 Q_ASSERT(m_modelState == IdleState);
74 m_modelState = ResettingState;
75
76 beginResetModel();
77
78 if (m_applicationManager) {
79 disconnect(m_applicationManager, 0, this, 0);
80 }
81
82 m_applicationManager = value;
83
84 if (m_applicationManager) {
85 connect(m_applicationManager, &QAbstractItemModel::rowsInserted,
86 this, [this](const QModelIndex &/*parent*/, int first, int last) {
87 if (!m_workspace || !m_workspace->isActive())
88 return;
89
90 for (int i = first; i <= last; ++i) {
91 auto application = m_applicationManager->get(i);
92 addApplication(application);
93 }
94 });
95
96 connect(m_applicationManager, &QAbstractItemModel::rowsAboutToBeRemoved,
97 this, [this](const QModelIndex &/*parent*/, int first, int last) {
98 for (int i = first; i <= last; ++i) {
99 auto application = m_applicationManager->get(i);
100 removeApplication(application);
101 }
102 });
103 }
104
105 refreshWindows();
106
107 endResetModel();
108 m_modelState = IdleState;
109}
110
111void TopLevelWindowModel::setSurfaceManager(lomiriapi::SurfaceManagerInterface *surfaceManager)
112{
113 if (surfaceManager == m_surfaceManager) {
114 return;
115 }
116
117 DEBUG_MSG << "(" << surfaceManager << ")";
118
119 Q_ASSERT(m_modelState == IdleState);
120 m_modelState = ResettingState;
121
122 beginResetModel();
123
124 if (m_surfaceManager) {
125 disconnect(m_surfaceManager, 0, this, 0);
126 }
127
128 m_surfaceManager = surfaceManager;
129
130 if (m_surfaceManager) {
131 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::surfacesAddedToWorkspace, this, &TopLevelWindowModel::onSurfacesAddedToWorkspace);
132 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::surfacesRaised, this, &TopLevelWindowModel::onSurfacesRaised);
133 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::surfaceRemoved, this, &TopLevelWindowModel::onSurfaceDestroyed);
134 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::modificationsStarted, this, &TopLevelWindowModel::onModificationsStarted);
135 connect(m_surfaceManager, &lomiriapi::SurfaceManagerInterface::modificationsEnded, this, &TopLevelWindowModel::onModificationsEnded);
136 }
137
138 refreshWindows();
139
140 endResetModel();
141 m_modelState = IdleState;
142}
143
144void TopLevelWindowModel::addApplication(lomiriapi::ApplicationInfoInterface *application)
145{
146 DEBUG_MSG << "(" << application->appId() << ")";
147
148 if (application->state() != lomiriapi::ApplicationInfoInterface::Stopped && application->surfaceList()->count() == 0) {
149 prependPlaceholder(application);
150 }
151}
152
153void TopLevelWindowModel::removeApplication(lomiriapi::ApplicationInfoInterface *application)
154{
155 DEBUG_MSG << "(" << application->appId() << ")";
156
157 Q_ASSERT(m_modelState == IdleState);
158
159 int i = 0;
160 while (i < m_windowModel.count()) {
161 if (m_windowModel.at(i).application == application) {
162 deleteAt(i);
163 } else {
164 ++i;
165 }
166 }
167}
168
169void TopLevelWindowModel::prependPlaceholder(lomiriapi::ApplicationInfoInterface *application)
170{
171 DEBUG_MSG << "(" << application->appId() << ")";
172
173 if (!application->showSplash())
174 return;
175
176 prependSurfaceHelper(nullptr, application);
177}
178
179void TopLevelWindowModel::prependSurface(lomiriapi::MirSurfaceInterface *surface, lomiriapi::ApplicationInfoInterface *application)
180{
181 Q_ASSERT(surface != nullptr);
182
183 connectSurface(surface);
184 m_allSurfaces.insert(surface);
185
186 bool filledPlaceholder = false;
187 for (int i = 0; i < m_windowModel.count() && !filledPlaceholder; ++i) {
188 ModelEntry &entry = m_windowModel[i];
189 if (entry.application == application && (entry.window->surface() == nullptr || !entry.window->surface()->live())) {
190 entry.window->setSurface(surface);
191 DEBUG_MSG << " appId=" << application->appId() << " surface=" << surface
192 << ", filling out placeholder. after: " << toString();
193 filledPlaceholder = true;
194 }
195 }
196
197 if (!filledPlaceholder) {
198 DEBUG_MSG << " appId=" << application->appId() << " surface=" << surface << ", adding new row";
199 prependSurfaceHelper(surface, application);
200 }
201}
202
203void TopLevelWindowModel::prependSurfaceHelper(lomiriapi::MirSurfaceInterface *surface, lomiriapi::ApplicationInfoInterface *application)
204{
205
206 Window *window = createWindow(surface);
207
208 connect(window, &Window::stateChanged, this, [=](Mir::State newState) {
209 if (newState == Mir::HiddenState) {
210 // Comply, removing it from our model. Just as if it didn't exist anymore.
211 removeAt(indexForId(window->id()));
212 } else {
213 if (indexForId(window->id()) == -1) {
214 // was probably hidden before. put it back on the list
215 auto *application = m_applicationManager->findApplicationWithSurface(window->surface());
216 Q_ASSERT(application);
217 prependWindow(window, application);
218 }
219 }
220 });
221
222 prependWindow(window, application);
223
224 // Activate the newly-prepended window.
225 window->activate();
226
227 DEBUG_MSG << " after " << toString();
228}
229
230void TopLevelWindowModel::prependWindow(Window *window, lomiriapi::ApplicationInfoInterface *application)
231{
232 if (m_modelState == IdleState) {
233 m_modelState = InsertingState;
234 beginInsertRows(QModelIndex(), 0 /*first*/, 0 /*last*/);
235 } else {
236 Q_ASSERT(m_modelState == ResettingState);
237 // No point in signaling anything if we're resetting the whole model
238 }
239
240 m_windowModel.prepend(ModelEntry(window, application));
241
242 if (m_modelState == InsertingState) {
243 endInsertRows();
244 Q_EMIT countChanged();
245 Q_EMIT listChanged();
246 m_modelState = IdleState;
247 }
248}
249
250void TopLevelWindowModel::connectWindow(Window *window)
251{
252 connect(window, &Window::focusRequested, this, [this, window]() {
253 if (!window->surface()) {
254 activateEmptyWindow(window);
255 }
256 });
257
258 connect(window, &Window::focusedChanged, this, [this, window](bool focused) {
259 if (window->surface()) {
260 if (focused) {
261 setFocusedWindow(window);
262 m_focusedWindowCleared = false;
263 } else if (m_focusedWindow == window) {
264 // Condense changes to the focused window
265 // eg: Do focusedWindow=A to focusedWindow=B instead of
266 // focusedWindow=A to focusedWindow=null to focusedWindow=B
267 m_focusedWindowCleared = true;
268 } else {
269 // don't clear the focused window if you were not there in the first place
270 // happens when a filled window gets replaced with an empty one (no surface) as the focused window.
271 }
272 }
273 });
274
275 connect(window, &Window::closeRequested, this, [this, window]() {
276 if (!window->surface()) {
277 // do things ourselves as miral doesn't know about this window
278 int id = window->id();
279 int index = indexForId(id);
280 bool focusOther = false;
281 Q_ASSERT(index >= 0);
282 if (window->focused()) {
283 focusOther = true;
284 }
285 m_windowModel[index].application->close();
286 if (focusOther) {
287 activateTopMostWindowWithoutId(id);
288 }
289 }
290 });
291
292 connect(window, &Window::emptyWindowActivated, this, [this, window]() {
293 activateEmptyWindow(window);
294 });
295
296 connect(window, &Window::liveChanged, this, [this, window](bool isAlive) {
297 if (!isAlive && window->state() == Mir::HiddenState) {
298 // Hidden windows are not in the model. So just delete it right away.
299 delete window;
300 }
301 });
302}
303
304void TopLevelWindowModel::activateEmptyWindow(Window *window)
305{
306 Q_ASSERT(!window->surface());
307 DEBUG_MSG << "(" << window << ")";
308
309 // miral doesn't know about empty windows (ie, windows that are not backed up by MirSurfaces)
310 // So we have to activate them ourselves (instead of asking SurfaceManager to do it for us).
311
312 window->setFocused(true);
313 raiseId(window->id());
314 Window *previousWindow = m_focusedWindow;
315 setFocusedWindow(window);
316 if (previousWindow && previousWindow->surface() && previousWindow->surface()->focused()) {
317 m_surfaceManager->activate(nullptr);
318 }
319}
320
321void TopLevelWindowModel::connectSurface(lomiriapi::MirSurfaceInterface *surface)
322{
323 connect(surface, &lomiriapi::MirSurfaceInterface::liveChanged, this, [this, surface](bool live){
324 if (!live) {
325 onSurfaceDied(surface);
326 }
327 });
328 connect(surface, &QObject::destroyed, this, [this, surface](QObject*){
329 int i = indexOf(surface);
330 if (i >= 0) {
331 m_windowModel[i].removeOnceSurfaceDestroyed = true;
332 }
333 this->onSurfaceDestroyed(surface);
334 });
335}
336
337void TopLevelWindowModel::onSurfaceDied(lomiriapi::MirSurfaceInterface *surface)
338{
339 if (surface->type() == Mir::InputMethodType) {
340 removeInputMethodWindow();
341 return;
342 }
343
344 int i = indexOf(surface);
345 if (i == -1) {
346 return;
347 }
348
349 auto application = m_windowModel[i].application;
350
351 DEBUG_MSG << " application->name()=" << application->name()
352 << " application->state()=" << application->state();
353
354 // assume a touch app got killed by the out-of-memory daemon.
355 //
356 // Leave at most 1 entry in the model and only remove its surface, so shell can display a screenshot
357 // in its place.
358 if (application->isTouchApp() && application->surfaceList()->count() == 1)
359 m_windowModel[i].removeOnceSurfaceDestroyed = false;
360 else
361 m_windowModel[i].removeOnceSurfaceDestroyed = true;
362}
363
364void TopLevelWindowModel::onSurfaceDestroyed(lomiriapi::MirSurfaceInterface *surface)
365{
366 int i = indexOf(surface);
367 if (i == -1) {
368 return;
369 }
370
371 auto application = m_windowModel[i].application;
372
373 // Clean up windows immediately, we cannot reliably restart them from an OOM situation
374 // and we cannot allow stuck windows to stick around after actually having closed them.
375 if (application->appId() == QStringLiteral("xwayland.qtmir")) {
376 m_windowModel[i].removeOnceSurfaceDestroyed = true;
377 }
378
379 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
380 deleteAt(i);
381 } else {
382 auto window = m_windowModel[i].window;
383 window->setFocused(false);
384 m_allSurfaces.remove(surface);
385 DEBUG_MSG << " Removed surface from entry. After: " << toString();
386 }
387}
388
389Window *TopLevelWindowModel::createWindow(lomiriapi::MirSurfaceInterface *surface)
390{
391 int id = m_nextId.fetchAndAddAcquire(1);
392 return createWindowWithId(surface, id);
393}
394
395Window *TopLevelWindowModel::createNullWindow()
396{
397 return createWindowWithId(nullptr, 0);
398}
399
400Window *TopLevelWindowModel::createWindowWithId(lomiriapi::MirSurfaceInterface *surface, int id)
401{
402 Window *qmlWindow = new Window(id, this);
403 connectWindow(qmlWindow);
404 if (surface) {
405 qmlWindow->setSurface(surface);
406 }
407 return qmlWindow;
408}
409
410void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
411 const QVector<lomiri::shell::application::MirSurfaceInterface*> surfaces)
412{
413 if (!m_workspace || !m_applicationManager) return;
414 if (workspace != m_workspace->workspace()) {
415 removeSurfaces(surfaces);
416 return;
417 }
418
419 Q_FOREACH(auto surface, surfaces) {
420 if (m_allSurfaces.contains(surface)) continue;
421
422 if (surface->parentSurface()) {
423 // Wrap it in a Window so that we keep focusedWindow() up to date.
424 Window *window = createWindow(surface);
425 connect(surface, &QObject::destroyed, window, [=](){
426 window->setSurface(nullptr);
427 window->deleteLater();
428 });
429 } else {
430 if (surface->type() == Mir::InputMethodType) {
431 connectSurface(surface);
432 setInputMethodWindow(createWindow(surface));
433 } else {
434 auto *application = m_applicationManager->findApplicationWithSurface(surface);
435 if (application) {
436 if (surface->state() == Mir::HiddenState) {
437 // Ignore it until it's finally shown
438 connect(surface, &lomiriapi::MirSurfaceInterface::stateChanged, this, [=](Mir::State newState) {
439 Q_ASSERT(newState != Mir::HiddenState);
440 disconnect(surface, &lomiriapi::MirSurfaceInterface::stateChanged, this, 0);
441 prependSurface(surface, application);
442 });
443 } else {
444 prependSurface(surface, application);
445 }
446 } else {
447 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
448 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
449 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
450 Window *promptWindow = createWindow(surface);
451 connect(surface, &QObject::destroyed, promptWindow, [=](){
452 promptWindow->setSurface(nullptr);
453 promptWindow->deleteLater();
454 });
455 }
456 }
457 }
458 }
459}
460
461void TopLevelWindowModel::removeSurfaces(const QVector<lomiri::shell::application::MirSurfaceInterface *> surfaces)
462{
463 int start = -1;
464 int end = -1;
465 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
466 auto surface = *iter;
467 iter++;
468
469 // Do removals in adjacent blocks.
470 start = end = indexOf(surface);
471 if (start == -1) {
472 // could be a child surface
473 m_allSurfaces.remove(surface);
474 continue;
475 }
476 while(iter != surfaces.constEnd()) {
477 int index = indexOf(*iter);
478 if (index != end+1) {
479 break;
480 }
481 end++;
482 iter++;
483 }
484
485 if (m_modelState == IdleState) {
486 beginRemoveRows(QModelIndex(), start, end);
487 m_modelState = RemovingState;
488 } else {
489 Q_ASSERT(m_modelState == ResettingState);
490 // No point in signaling anything if we're resetting the whole model
491 }
492
493 for (int index = start; index <= end; index++) {
494 auto window = m_windowModel[start].window;
495 window->setSurface(nullptr);
496 window->setFocused(false);
497
498 if (window == m_previousWindow) {
499 m_previousWindow = nullptr;
500 }
501
502 m_windowModel.removeAt(start);
503 m_allSurfaces.remove(surface);
504 }
505
506 if (m_modelState == RemovingState) {
507 endRemoveRows();
508 Q_EMIT countChanged();
509 Q_EMIT listChanged();
510 m_modelState = IdleState;
511 }
512 }
513}
514
515void TopLevelWindowModel::deleteAt(int index)
516{
517 auto window = m_windowModel[index].window;
518
519 removeAt(index);
520
521 window->setSurface(nullptr);
522
523 delete window;
524}
525
526void TopLevelWindowModel::removeAt(int index)
527{
528 if (m_modelState == IdleState) {
529 beginRemoveRows(QModelIndex(), index, index);
530 m_modelState = RemovingState;
531 } else {
532 Q_ASSERT(m_modelState == ResettingState);
533 // No point in signaling anything if we're resetting the whole model
534 }
535
536 auto window = m_windowModel[index].window;
537 auto surface = window->surface();
538
539 if (!window->surface()) {
540 window->setFocused(false);
541 }
542
543 if (window == m_previousWindow) {
544 m_previousWindow = nullptr;
545 }
546
547 m_windowModel.removeAt(index);
548 m_allSurfaces.remove(surface);
549
550 if (m_modelState == RemovingState) {
551 endRemoveRows();
552 Q_EMIT countChanged();
553 Q_EMIT listChanged();
554 m_modelState = IdleState;
555 }
556
557 if (m_focusedWindow == window) {
558 setFocusedWindow(nullptr);
559 m_focusedWindowCleared = false;
560 }
561
562 if (m_previousWindow == window) {
563 m_previousWindow = nullptr;
564 }
565
566 if (m_closingAllApps) {
567 if (m_windowModel.isEmpty()) {
568 Q_EMIT closedAllWindows();
569 }
570 }
571
572 DEBUG_MSG << " after " << toString() << " apps left " << m_windowModel.count();
573}
574
575void TopLevelWindowModel::setInputMethodWindow(Window *window)
576{
577 if (m_inputMethodWindow) {
578 qWarning("Multiple Input Method Surfaces created, removing the old one!");
579 delete m_inputMethodWindow;
580 }
581 m_inputMethodWindow = window;
582 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
583 InputMethodManager::instance()->setWindow(window);
584}
585
586void TopLevelWindowModel::removeInputMethodWindow()
587{
588 if (m_inputMethodWindow) {
589 auto surface = m_inputMethodWindow->surface();
590 if (surface) {
591 m_allSurfaces.remove(surface);
592 }
593 if (m_focusedWindow == m_inputMethodWindow) {
594 setFocusedWindow(nullptr);
595 m_focusedWindowCleared = false;
596 }
597
598 delete m_inputMethodWindow;
599 m_inputMethodWindow = nullptr;
600 Q_EMIT inputMethodSurfaceChanged(nullptr);
601 InputMethodManager::instance()->setWindow(nullptr);
602 }
603}
604
605void TopLevelWindowModel::onSurfacesRaised(const QVector<lomiriapi::MirSurfaceInterface*> &surfaces)
606{
607 DEBUG_MSG << "(" << surfaces << ")";
608 const int raiseCount = surfaces.size();
609 for (int i = 0; i < raiseCount; i++) {
610 int fromIndex = indexOf(surfaces[i]);
611 if (fromIndex != -1) {
612 move(fromIndex, 0);
613 }
614 }
615}
616
617int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
618{
619 return m_windowModel.count();
620}
621
622QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
623{
624 if (index.row() < 0 || index.row() >= m_windowModel.size())
625 return QVariant();
626
627 if (role == WindowRole) {
628 Window *window = m_windowModel.at(index.row()).window;
629 return QVariant::fromValue(window);
630 } else if (role == ApplicationRole) {
631 return QVariant::fromValue(m_windowModel.at(index.row()).application);
632 } else {
633 return QVariant();
634 }
635}
636
637QString TopLevelWindowModel::toString()
638{
639 QString str;
640 for (int i = 0; i < m_windowModel.count(); ++i) {
641 auto item = m_windowModel.at(i);
642
643 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
644 .arg(QString::number(i),
645 item.application->appId(),
646 QString::number((qintptr)item.window->surface(), 16),
647 QString::number(item.window->id()));
648
649 if (i > 0) {
650 str.append(",");
651 }
652 str.append(itemStr);
653 }
654 return str;
655}
656
657int TopLevelWindowModel::indexOf(lomiriapi::MirSurfaceInterface *surface)
658{
659 for (int i = 0; i < m_windowModel.count(); ++i) {
660 if (m_windowModel.at(i).window->surface() == surface) {
661 return i;
662 }
663 }
664 return -1;
665}
666
668{
669 for (int i = 0; i < m_windowModel.count(); ++i) {
670 if (m_windowModel[i].window->id() == id) {
671 return i;
672 }
673 }
674 return -1;
675}
676
678{
679 if (index >=0 && index < m_windowModel.count()) {
680 return m_windowModel[index].window;
681 } else {
682 return nullptr;
683 }
684}
685
686lomiriapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
687{
688 if (index >=0 && index < m_windowModel.count()) {
689 return m_windowModel[index].window->surface();
690 } else {
691 return nullptr;
692 }
693}
694
695lomiriapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
696{
697 if (index >=0 && index < m_windowModel.count()) {
698 return m_windowModel[index].application;
699 } else {
700 return nullptr;
701 }
702}
703
704int TopLevelWindowModel::idAt(int index) const
705{
706 if (index >=0 && index < m_windowModel.count()) {
707 return m_windowModel[index].window->id();
708 } else {
709 return 0;
710 }
711}
712
714{
715 if (m_modelState == IdleState) {
716 DEBUG_MSG << "(id=" << id << ") - do it now.";
717 doRaiseId(id);
718 } else {
719 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
720 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
721 // if we perform yet another model change straight away.
722 //
723 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
724 // the index is definitely within bounds.
725 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
726 }
727}
728
729void TopLevelWindowModel::doRaiseId(int id)
730{
731 int fromIndex = indexForId(id);
732 // can't raise something that doesn't exist or that it's already on top
733 if (fromIndex != -1 && fromIndex != 0) {
734 auto surface = m_windowModel[fromIndex].window->surface();
735 if (surface && surface->live()) {
736 m_surfaceManager->raise(surface);
737 } else {
738 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
739 // miral can do about it.
740 move(fromIndex, 0);
741 }
742 }
743}
744
745void TopLevelWindowModel::setFocusedWindow(Window *window)
746{
747 if (window != m_focusedWindow) {
748 DEBUG_MSG << "(" << window << ")";
749
750 m_previousWindow = m_focusedWindow;
751
752 m_focusedWindow = window;
753 Q_EMIT focusedWindowChanged(m_focusedWindow);
754
755 if (m_previousWindow && m_previousWindow->focused() && !m_previousWindow->surface()) {
756 // do it ourselves. miral doesn't know about this window
757 m_previousWindow->setFocused(false);
758 }
759 }
760
761 // Reset
762 m_pendingActivation = false;
763}
764
765lomiriapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
766{
767 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
768}
769
771{
772 return m_focusedWindow;
773}
774
775void TopLevelWindowModel::move(int from, int to)
776{
777 if (from == to) return;
778 DEBUG_MSG << " from=" << from << " to=" << to;
779
780 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
781 QModelIndex parent;
782 /* When moving an item down, the destination index needs to be incremented
783 by one, as explained in the documentation:
784 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
785
786 Q_ASSERT(m_modelState == IdleState);
787 m_modelState = MovingState;
788
789 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
790#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
791 const auto &window = m_windowModel.takeAt(from);
792 m_windowModel.insert(to, window);
793#else
794 m_windowModel.move(from, to);
795#endif
796 endMoveRows();
797
798 Q_EMIT listChanged();
799 m_modelState = IdleState;
800
801 DEBUG_MSG << " after " << toString();
802 }
803}
804void TopLevelWindowModel::onModificationsStarted()
805{
806 m_surfaceManagerBusy = true;
807}
808
809void TopLevelWindowModel::onModificationsEnded()
810{
811 if (m_focusedWindowCleared) {
812 setFocusedWindow(nullptr);
813 }
814 // reset
815 m_focusedWindowCleared = false;
816 m_surfaceManagerBusy = false;
817}
818
819void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
820{
821 DEBUG_MSG << "(" << forbiddenId << ")";
822
823 for (int i = 0; i < m_windowModel.count(); ++i) {
824 Window *window = m_windowModel[i].window;
825 if (window->id() != forbiddenId) {
826 window->activate();
827 break;
828 }
829 }
830}
831
832void TopLevelWindowModel::refreshWindows()
833{
834 DEBUG_MSG << "()";
835 clear();
836
837 if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
838
839 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](lomiri::shell::application::MirSurfaceInterface* surface) {
840 if (surface->parentSurface()) {
841 // Wrap it in a Window so that we keep focusedWindow() up to date.
842 Window *window = createWindow(surface);
843 connect(surface, &QObject::destroyed, window, [=](){
844 window->setSurface(nullptr);
845 window->deleteLater();
846 });
847 } else {
848 if (surface->type() == Mir::InputMethodType) {
849 setInputMethodWindow(createWindow(surface));
850 } else {
851 auto *application = m_applicationManager->findApplicationWithSurface(surface);
852 if (application) {
853 prependSurface(surface, application);
854 } else {
855 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
856 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
857 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
858 Window *promptWindow = createWindow(surface);
859 connect(surface, &QObject::destroyed, promptWindow, [=](){
860 promptWindow->setSurface(nullptr);
861 promptWindow->deleteLater();
862 });
863 }
864 }
865 }
866 });
867}
868
869void TopLevelWindowModel::clear()
870{
871 DEBUG_MSG << "()";
872
873 while(m_windowModel.count() > 0) {
874 ModelEntry entry = m_windowModel.takeAt(0);
875 disconnect(entry.window, 0, this, 0);
876 delete entry.window;
877 }
878 m_allSurfaces.clear();
879 setFocusedWindow(nullptr);
880 m_focusedWindowCleared = false;
881 m_previousWindow = nullptr;
882}
883
885{
886 m_closingAllApps = true;
887 for (auto win : m_windowModel) {
888 win.window->close();
889 }
890
891 // This is done after the for loop in the unlikely event that
892 // an app starts in between this
893 if (m_windowModel.isEmpty()) {
894 Q_EMIT closedAllWindows();
895 }
896}
897
899{
900 return !m_nullWindow->focused();
901}
902
903void TopLevelWindowModel::setRootFocus(bool focus)
904{
905 DEBUG_MSG << "(" << focus << "), surfaceManagerBusy is " << m_surfaceManagerBusy;
906
907 if (m_surfaceManagerBusy) {
908 // Something else is probably being focused already, let's not add to
909 // the noise.
910 return;
911 }
912
913 if (focus) {
914 // Give focus back to previous focused window, only if null window is focused.
915 // If null window is not focused, a different app had taken the focus and we
916 // should repect that, or if a pendingActivation is going on.
917 if (m_previousWindow && !m_previousWindow->focused() && !m_pendingActivation &&
918 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
919 m_previousWindow->activate();
920 } else if (!m_pendingActivation) {
921 // The previous window does not exist any more, focus top window.
922 activateTopMostWindowWithoutId(-1);
923 }
924 } else {
925 if (!m_nullWindow->focused()) {
926 m_nullWindow->activate();
927 }
928 }
929}
930
931// Pending Activation will block refocus of previous focused window
932// this is needed since surface activation with miral is async,
933// and activation of placeholder is sync. This causes a race condition
934// between placeholder and prev window. This results in prev window
935// gets focused, as it will always be later than the placeholder.
937{
938 m_pendingActivation = true;
939}
Q_INVOKABLE void raiseId(int id)
Raises the row with the given id to the top of the window stack (index == count-1)
bool rootFocus
Sets whether a user Window or "nothing" should be focused.
Q_INVOKABLE Window * windowAt(int index) const
Returns the window at the given index.
Q_INVOKABLE lomiri::shell::application::MirSurfaceInterface * surfaceAt(int index) const
Returns the surface at the given index.
Q_INVOKABLE void pendingActivation()
Sets pending activation flag.
Q_INVOKABLE int indexForId(int id) const
Returns the index where the row with the given id is located.
lomiri::shell::application::MirSurfaceInterface * inputMethodSurface
The input method surface, if any.
Q_INVOKABLE int idAt(int index) const
Returns the unique id of the element at the given index.
Q_INVOKABLE void closeAllWindows()
Closes all windows, emits closedAllWindows when done.
void listChanged()
Emitted when the list changes.
Q_INVOKABLE lomiri::shell::application::ApplicationInfoInterface * applicationAt(int index) const
Returns the application at the given index.
Window * focusedWindow
The currently focused window, if any.
A slightly higher concept than MirSurface.
Definition Window.h:48
int id
A unique identifier for this window. Useful for telling windows apart in a list model as they get mov...
Definition Window.h:84
void focusRequested()
Emitted when focus for this window is requested by an external party.
lomiri::shell::application::MirSurfaceInterface * surface
Surface backing up this window It might be null if a surface hasn't been created yet (application is ...
Definition Window.h:92
bool focused
Whether the surface is focused.
Definition Window.h:71
void activate()
Focuses and raises the window.
Definition Window.cpp:137
Mir::State state
State of the surface.
Definition Window.h:64