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 this->onSurfaceDestroyed(surface);
330 });
331}
332
333int TopLevelWindowModel::countWindowsWithApplication(lomiri::shell::application::ApplicationInfoInterface *application)
334{
335 int count = 0;
336
337 for (const auto & item : qAsConst(m_windowModel)) {
338 if (item.application == application)
339 count++;
340 }
341
342 return count;
343}
344
345void TopLevelWindowModel::onSurfaceDied(lomiriapi::MirSurfaceInterface *surface)
346{
347 if (surface->type() == Mir::InputMethodType) {
348 removeInputMethodWindow();
349 return;
350 }
351
352 int i = indexOf(surface);
353 if (i == -1) {
354 return;
355 }
356
357 auto application = m_windowModel[i].application;
358
359 DEBUG_MSG << " application->name()=" << application->name()
360 << " application->state()=" << application->state();
361
362 // assume a touch app got killed by the out-of-memory daemon.
363 //
364 // Leave at most 1 entry in the model and only remove its surface, so shell can display a screenshot
365 // in its place.
366 if (application->isTouchApp() && countWindowsWithApplication(application) == 1)
367 m_windowModel[i].removeOnceSurfaceDestroyed = false;
368 else
369 m_windowModel[i].removeOnceSurfaceDestroyed = true;
370}
371
372void TopLevelWindowModel::onSurfaceDestroyed(lomiriapi::MirSurfaceInterface *surface)
373{
374 int i = indexOf(surface);
375 if (i == -1) {
376 return;
377 }
378
379 auto application = m_windowModel[i].application;
380
381 // Clean up non-touch windows immediately, we cannot reliably restart them from an OOM situation
382 // and we cannot allow stuck windows to stick around after actually having closed them.
383 if (application->appId() == QStringLiteral("xwayland.qtmir") || !application->isTouchApp()) {
384 m_windowModel[i].removeOnceSurfaceDestroyed = true;
385 }
386
387 if (m_windowModel[i].removeOnceSurfaceDestroyed) {
388 deleteAt(i);
389 } else {
390 auto window = m_windowModel[i].window;
391 window->setFocused(false);
392 m_allSurfaces.remove(surface);
393 DEBUG_MSG << " Removed surface from entry. After: " << toString();
394 }
395}
396
397Window *TopLevelWindowModel::createWindow(lomiriapi::MirSurfaceInterface *surface)
398{
399 int id = m_nextId.fetchAndAddAcquire(1);
400 return createWindowWithId(surface, id);
401}
402
403Window *TopLevelWindowModel::createNullWindow()
404{
405 return createWindowWithId(nullptr, 0);
406}
407
408Window *TopLevelWindowModel::createWindowWithId(lomiriapi::MirSurfaceInterface *surface, int id)
409{
410 Window *qmlWindow = new Window(id, this);
411 connectWindow(qmlWindow);
412 if (surface) {
413 qmlWindow->setSurface(surface);
414 }
415 return qmlWindow;
416}
417
418void TopLevelWindowModel::onSurfacesAddedToWorkspace(const std::shared_ptr<miral::Workspace>& workspace,
419 const QVector<lomiri::shell::application::MirSurfaceInterface*> surfaces)
420{
421 if (!m_workspace || !m_applicationManager) return;
422 if (workspace != m_workspace->workspace()) {
423 removeSurfaces(surfaces);
424 return;
425 }
426
427 Q_FOREACH(auto surface, surfaces) {
428 if (m_allSurfaces.contains(surface)) continue;
429
430 if (surface->parentSurface()) {
431 // Wrap it in a Window so that we keep focusedWindow() up to date.
432 Window *window = createWindow(surface);
433 connect(surface, &QObject::destroyed, window, [=](){
434 window->setSurface(nullptr);
435 window->deleteLater();
436 });
437 } else {
438 if (surface->type() == Mir::InputMethodType) {
439 connectSurface(surface);
440 setInputMethodWindow(createWindow(surface));
441 } else {
442 auto *application = m_applicationManager->findApplicationWithSurface(surface);
443 if (application) {
444 if (surface->state() == Mir::HiddenState) {
445 // Ignore it until it's finally shown
446 connect(surface, &lomiriapi::MirSurfaceInterface::stateChanged, this, [=](Mir::State newState) {
447 Q_ASSERT(newState != Mir::HiddenState);
448 disconnect(surface, &lomiriapi::MirSurfaceInterface::stateChanged, this, 0);
449 prependSurface(surface, application);
450 });
451 } else {
452 prependSurface(surface, application);
453 }
454 } else {
455 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
456 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
457 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
458 Window *promptWindow = createWindow(surface);
459 connect(surface, &QObject::destroyed, promptWindow, [=](){
460 promptWindow->setSurface(nullptr);
461 promptWindow->deleteLater();
462 });
463 }
464 }
465 }
466 }
467}
468
469void TopLevelWindowModel::removeSurfaces(const QVector<lomiri::shell::application::MirSurfaceInterface *> surfaces)
470{
471 int start = -1;
472 int end = -1;
473 for (auto iter = surfaces.constBegin(); iter != surfaces.constEnd();) {
474 auto surface = *iter;
475 iter++;
476
477 // Do removals in adjacent blocks.
478 start = end = indexOf(surface);
479 if (start == -1) {
480 // could be a child surface
481 m_allSurfaces.remove(surface);
482 continue;
483 }
484 while(iter != surfaces.constEnd()) {
485 int index = indexOf(*iter);
486 if (index != end+1) {
487 break;
488 }
489 end++;
490 iter++;
491 }
492
493 if (m_modelState == IdleState) {
494 beginRemoveRows(QModelIndex(), start, end);
495 m_modelState = RemovingState;
496 } else {
497 Q_ASSERT(m_modelState == ResettingState);
498 // No point in signaling anything if we're resetting the whole model
499 }
500
501 for (int index = start; index <= end; index++) {
502 auto window = m_windowModel[start].window;
503 window->setSurface(nullptr);
504 window->setFocused(false);
505
506 if (window == m_previousWindow) {
507 m_previousWindow = nullptr;
508 }
509
510 m_windowModel.removeAt(start);
511 m_allSurfaces.remove(surface);
512 }
513
514 if (m_modelState == RemovingState) {
515 endRemoveRows();
516 Q_EMIT countChanged();
517 Q_EMIT listChanged();
518 m_modelState = IdleState;
519 }
520 }
521}
522
523void TopLevelWindowModel::deleteAt(int index)
524{
525 auto window = m_windowModel[index].window;
526
527 removeAt(index);
528
529 window->setSurface(nullptr);
530
531 delete window;
532}
533
534void TopLevelWindowModel::removeAt(int index)
535{
536 if (m_modelState == IdleState) {
537 beginRemoveRows(QModelIndex(), index, index);
538 m_modelState = RemovingState;
539 } else {
540 Q_ASSERT(m_modelState == ResettingState);
541 // No point in signaling anything if we're resetting the whole model
542 }
543
544 auto window = m_windowModel[index].window;
545 auto surface = window->surface();
546
547 if (!window->surface()) {
548 window->setFocused(false);
549 }
550
551 if (window == m_previousWindow) {
552 m_previousWindow = nullptr;
553 }
554
555 m_windowModel.removeAt(index);
556 m_allSurfaces.remove(surface);
557
558 if (m_modelState == RemovingState) {
559 endRemoveRows();
560 Q_EMIT countChanged();
561 Q_EMIT listChanged();
562 m_modelState = IdleState;
563 }
564
565 if (m_focusedWindow == window) {
566 setFocusedWindow(nullptr);
567 m_focusedWindowCleared = false;
568 }
569
570 if (m_previousWindow == window) {
571 m_previousWindow = nullptr;
572 }
573
574 if (m_closingAllApps) {
575 if (m_windowModel.isEmpty()) {
576 Q_EMIT closedAllWindows();
577 }
578 }
579
580 DEBUG_MSG << " after " << toString() << " apps left " << m_windowModel.count();
581}
582
583void TopLevelWindowModel::setInputMethodWindow(Window *window)
584{
585 if (m_inputMethodWindow) {
586 qWarning("Multiple Input Method Surfaces created, removing the old one!");
587 delete m_inputMethodWindow;
588 }
589 m_inputMethodWindow = window;
590 Q_EMIT inputMethodSurfaceChanged(m_inputMethodWindow->surface());
591 InputMethodManager::instance()->setWindow(window);
592}
593
594void TopLevelWindowModel::removeInputMethodWindow()
595{
596 if (m_inputMethodWindow) {
597 auto surface = m_inputMethodWindow->surface();
598 if (surface) {
599 m_allSurfaces.remove(surface);
600 }
601 if (m_focusedWindow == m_inputMethodWindow) {
602 setFocusedWindow(nullptr);
603 m_focusedWindowCleared = false;
604 }
605
606 delete m_inputMethodWindow;
607 m_inputMethodWindow = nullptr;
608 Q_EMIT inputMethodSurfaceChanged(nullptr);
609 InputMethodManager::instance()->setWindow(nullptr);
610 }
611}
612
613void TopLevelWindowModel::onSurfacesRaised(const QVector<lomiriapi::MirSurfaceInterface*> &surfaces)
614{
615 DEBUG_MSG << "(" << surfaces << ")";
616 const int raiseCount = surfaces.size();
617 for (int i = 0; i < raiseCount; i++) {
618 int fromIndex = indexOf(surfaces[i]);
619 if (fromIndex != -1) {
620 move(fromIndex, 0);
621 }
622 }
623}
624
625int TopLevelWindowModel::rowCount(const QModelIndex &/*parent*/) const
626{
627 return m_windowModel.count();
628}
629
630QVariant TopLevelWindowModel::data(const QModelIndex& index, int role) const
631{
632 if (index.row() < 0 || index.row() >= m_windowModel.size())
633 return QVariant();
634
635 if (role == WindowRole) {
636 Window *window = m_windowModel.at(index.row()).window;
637 return QVariant::fromValue(window);
638 } else if (role == ApplicationRole) {
639 return QVariant::fromValue(m_windowModel.at(index.row()).application);
640 } else {
641 return QVariant();
642 }
643}
644
645QString TopLevelWindowModel::toString()
646{
647 QString str;
648 for (int i = 0; i < m_windowModel.count(); ++i) {
649 auto item = m_windowModel.at(i);
650
651 QString itemStr = QString("(index=%1,appId=%2,surface=0x%3,id=%4)")
652 .arg(QString::number(i),
653 item.application->appId(),
654 QString::number((qintptr)item.window->surface(), 16),
655 QString::number(item.window->id()));
656
657 if (i > 0) {
658 str.append(",");
659 }
660 str.append(itemStr);
661 }
662 return str;
663}
664
665int TopLevelWindowModel::indexOf(lomiriapi::MirSurfaceInterface *surface)
666{
667 for (int i = 0; i < m_windowModel.count(); ++i) {
668 if (m_windowModel.at(i).window->surface() == surface) {
669 return i;
670 }
671 }
672 return -1;
673}
674
676{
677 for (int i = 0; i < m_windowModel.count(); ++i) {
678 if (m_windowModel[i].window->id() == id) {
679 return i;
680 }
681 }
682 return -1;
683}
684
686{
687 if (index >=0 && index < m_windowModel.count()) {
688 return m_windowModel[index].window;
689 } else {
690 return nullptr;
691 }
692}
693
694lomiriapi::MirSurfaceInterface *TopLevelWindowModel::surfaceAt(int index) const
695{
696 if (index >=0 && index < m_windowModel.count()) {
697 return m_windowModel[index].window->surface();
698 } else {
699 return nullptr;
700 }
701}
702
703lomiriapi::ApplicationInfoInterface *TopLevelWindowModel::applicationAt(int index) const
704{
705 if (index >=0 && index < m_windowModel.count()) {
706 return m_windowModel[index].application;
707 } else {
708 return nullptr;
709 }
710}
711
712int TopLevelWindowModel::idAt(int index) const
713{
714 if (index >=0 && index < m_windowModel.count()) {
715 return m_windowModel[index].window->id();
716 } else {
717 return 0;
718 }
719}
720
722{
723 if (m_modelState == IdleState) {
724 DEBUG_MSG << "(id=" << id << ") - do it now.";
725 doRaiseId(id);
726 } else {
727 DEBUG_MSG << "(id=" << id << ") - Model busy (modelState=" << m_modelState << "). Try again in the next event loop.";
728 // The model has just signalled some change. If we have a Repeater responding to this update, it will get nuts
729 // if we perform yet another model change straight away.
730 //
731 // A bad sympton of this problem is a Repeater.itemAt(index) call returning null event though Repeater.count says
732 // the index is definitely within bounds.
733 QMetaObject::invokeMethod(this, "raiseId", Qt::QueuedConnection, Q_ARG(int, id));
734 }
735}
736
737void TopLevelWindowModel::doRaiseId(int id)
738{
739 int fromIndex = indexForId(id);
740 // can't raise something that doesn't exist or that it's already on top
741 if (fromIndex != -1 && fromIndex != 0) {
742 auto surface = m_windowModel[fromIndex].window->surface();
743 if (surface && surface->live()) {
744 m_surfaceManager->raise(surface);
745 } else {
746 // move it ourselves. Since there's no mir::scene::Surface/miral::Window, there's nothing
747 // miral can do about it.
748 move(fromIndex, 0);
749 }
750 }
751}
752
753void TopLevelWindowModel::setFocusedWindow(Window *window)
754{
755 if (window != m_focusedWindow) {
756 DEBUG_MSG << "(" << window << ")";
757
758 m_previousWindow = m_focusedWindow;
759
760 m_focusedWindow = window;
761 Q_EMIT focusedWindowChanged(m_focusedWindow);
762
763 if (m_previousWindow && m_previousWindow->focused() && !m_previousWindow->surface()) {
764 // do it ourselves. miral doesn't know about this window
765 m_previousWindow->setFocused(false);
766 }
767 }
768
769 // Reset
770 m_pendingActivation = false;
771}
772
773lomiriapi::MirSurfaceInterface* TopLevelWindowModel::inputMethodSurface() const
774{
775 return m_inputMethodWindow ? m_inputMethodWindow->surface() : nullptr;
776}
777
779{
780 return m_focusedWindow;
781}
782
783void TopLevelWindowModel::move(int from, int to)
784{
785 if (from == to) return;
786 DEBUG_MSG << " from=" << from << " to=" << to;
787
788 if (from >= 0 && from < m_windowModel.size() && to >= 0 && to < m_windowModel.size()) {
789 QModelIndex parent;
790 /* When moving an item down, the destination index needs to be incremented
791 by one, as explained in the documentation:
792 http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#beginMoveRows */
793
794 Q_ASSERT(m_modelState == IdleState);
795 m_modelState = MovingState;
796
797 beginMoveRows(parent, from, from, parent, to + (to > from ? 1 : 0));
798#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
799 const auto &window = m_windowModel.takeAt(from);
800 m_windowModel.insert(to, window);
801#else
802 m_windowModel.move(from, to);
803#endif
804 endMoveRows();
805
806 Q_EMIT listChanged();
807 m_modelState = IdleState;
808
809 DEBUG_MSG << " after " << toString();
810 }
811}
812void TopLevelWindowModel::onModificationsStarted()
813{
814 m_surfaceManagerBusy = true;
815}
816
817void TopLevelWindowModel::onModificationsEnded()
818{
819 if (m_focusedWindowCleared) {
820 setFocusedWindow(nullptr);
821 }
822 // reset
823 m_focusedWindowCleared = false;
824 m_surfaceManagerBusy = false;
825}
826
827void TopLevelWindowModel::activateTopMostWindowWithoutId(int forbiddenId)
828{
829 DEBUG_MSG << "(" << forbiddenId << ")";
830
831 for (int i = 0; i < m_windowModel.count(); ++i) {
832 Window *window = m_windowModel[i].window;
833 if (window->id() != forbiddenId) {
834 window->activate();
835 break;
836 }
837 }
838}
839
840void TopLevelWindowModel::refreshWindows()
841{
842 DEBUG_MSG << "()";
843 clear();
844
845 if (!m_workspace || !m_applicationManager || !m_surfaceManager) return;
846
847 m_surfaceManager->forEachSurfaceInWorkspace(m_workspace->workspace(), [this](lomiri::shell::application::MirSurfaceInterface* surface) {
848 if (surface->parentSurface()) {
849 // Wrap it in a Window so that we keep focusedWindow() up to date.
850 Window *window = createWindow(surface);
851 connect(surface, &QObject::destroyed, window, [=](){
852 window->setSurface(nullptr);
853 window->deleteLater();
854 });
855 } else {
856 if (surface->type() == Mir::InputMethodType) {
857 setInputMethodWindow(createWindow(surface));
858 } else {
859 auto *application = m_applicationManager->findApplicationWithSurface(surface);
860 if (application) {
861 prependSurface(surface, application);
862 } else {
863 // Must be a prompt session. No need to do add it as a prompt surface is not top-level.
864 // It will show up in the ApplicationInfoInterface::promptSurfaceList of some application.
865 // Still wrap it in a Window though, so that we keep focusedWindow() up to date.
866 Window *promptWindow = createWindow(surface);
867 connect(surface, &QObject::destroyed, promptWindow, [=](){
868 promptWindow->setSurface(nullptr);
869 promptWindow->deleteLater();
870 });
871 }
872 }
873 }
874 });
875}
876
877void TopLevelWindowModel::clear()
878{
879 DEBUG_MSG << "()";
880
881 while(m_windowModel.count() > 0) {
882 ModelEntry entry = m_windowModel.takeAt(0);
883 disconnect(entry.window, 0, this, 0);
884 delete entry.window;
885 }
886 m_allSurfaces.clear();
887 setFocusedWindow(nullptr);
888 m_focusedWindowCleared = false;
889 m_previousWindow = nullptr;
890}
891
893{
894 m_closingAllApps = true;
895 for (auto win : m_windowModel) {
896 win.window->close();
897 }
898
899 // This is done after the for loop in the unlikely event that
900 // an app starts in between this
901 if (m_windowModel.isEmpty()) {
902 Q_EMIT closedAllWindows();
903 }
904}
905
907{
908 return !m_nullWindow->focused();
909}
910
911void TopLevelWindowModel::setRootFocus(bool focus)
912{
913 DEBUG_MSG << "(" << focus << "), surfaceManagerBusy is " << m_surfaceManagerBusy;
914
915 if (m_surfaceManagerBusy) {
916 // Something else is probably being focused already, let's not add to
917 // the noise.
918 return;
919 }
920
921 if (focus) {
922 // Give focus back to previous focused window, only if null window is focused.
923 // If null window is not focused, a different app had taken the focus and we
924 // should repect that, or if a pendingActivation is going on.
925 if (m_previousWindow && !m_previousWindow->focused() && !m_pendingActivation &&
926 m_nullWindow == m_focusedWindow && m_previousWindow != m_nullWindow) {
927 m_previousWindow->activate();
928 } else if (!m_pendingActivation) {
929 // The previous window does not exist any more, focus top window.
930 activateTopMostWindowWithoutId(-1);
931 }
932 } else {
933 if (!m_nullWindow->focused()) {
934 m_nullWindow->activate();
935 }
936 }
937}
938
939// Pending Activation will block refocus of previous focused window
940// this is needed since surface activation with miral is async,
941// and activation of placeholder is sync. This causes a race condition
942// between placeholder and prev window. This results in prev window
943// gets focused, as it will always be later than the placeholder.
945{
946 m_pendingActivation = true;
947}
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.
int count
Number of top-level surfaces in this model.
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