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