Debian Codegraph @

tests/mocks/QtMir/Application/MirSurface.cpp

raw
1 /*
2 * Copyright (C) 2015-2016 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include "MirSurface.h"
18
19 #include <QDebug>
20 #include <QQmlEngine>
21
22 // local
23 #include "SurfaceManager.h"
24
25 #define MIRSURFACE_DEBUG 0
26
27 #if MIRSURFACE_DEBUG
28 #define DEBUG_MSG(params) qDebug().nospace() << "MirSurface[" << (void*)this << "," << m_name << "]::" << __func__ << " " << params
29 #define XDEBUG_MSG(params) qDebug().nospace() << "MirSurface[" << (void*)this << "," << m_name << "]::" << params
30
31 const char *stateToStr(Mir::State state)
32 {
33 switch (state) {
34 case Mir::UnknownState:
35 return "unknown";
36 case Mir::RestoredState:
37 return "restored";
38 case Mir::MinimizedState:
39 return "minimized";
40 case Mir::MaximizedState:
41 return "maximized";
42 case Mir::VertMaximizedState:
43 return "vertMaximized";
44 case Mir::FullscreenState:
45 return "fullscreen";
46 case Mir::HorizMaximizedState:
47 return "horizMaximized";
48 case Mir::MaximizedLeftState:
49 return "maximizedLeft";
50 case Mir::MaximizedRightState:
51 return "maximizedRight";
52 case Mir::MaximizedTopLeftState:
53 return "maximizedTopLeft";
54 case Mir::MaximizedTopRightState:
55 return "maximizedTopRight";
56 case Mir::MaximizedBottomLeftState:
57 return "maximizedBottomLeft";
58 case Mir::MaximizedBottomRightState:
59 return "maximizedBottomRight";
60 case Mir::HiddenState:
61 return "hidden";
62 default:
63 return "???";
64 }
65 }
66
67 #else
68 #define DEBUG_MSG(params) ((void)0)
69 #define XDEBUG_MSG(params) ((void)0)
70 #endif
71
72 using namespace lomiri::shell::application;
73
74 MirSurface::MirSurface(const QString& name,
75 Mir::Type type,
76 Mir::State state,
77 MirSurface *parentSurface,
78 const QUrl& screenshot,
79 const QUrl &qmlFilePath)
80 : lomiri::shell::application::MirSurfaceInterface(nullptr)
81 , m_name(name)
82 , m_type(type)
83 , m_state(state)
84 , m_orientationAngle(Mir::Angle0)
85 , m_screenshotUrl(screenshot)
86 , m_qmlFilePath(qmlFilePath)
87 , m_live(true)
88 , m_ready(false)
89 , m_focused(false)
90 , m_activeFocus(false)
91 , m_width(-1)
92 , m_height(-1)
93 , m_slowToResize(false)
94 , m_shellChrome(Mir::NormalChrome)
95 , m_parentSurface(parentSurface)
96 , m_childSurfaceList(new MirSurfaceListModel(this))
97 {
98 DEBUG_MSG("state=" << stateToStr(state));
99
100 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
101
102 m_delayedResizeTimer.setInterval(600);
103 m_delayedResizeTimer.setSingleShot(true);
104 connect(&m_delayedResizeTimer, &QTimer::timeout, this, &MirSurface::applyDelayedResize);
105
106 m_zombieTimer.setInterval(100);
107 m_zombieTimer.setSingleShot(true);
108 connect(&m_zombieTimer, &QTimer::timeout, this, [this](){ this->setLive(false); });
109
110 m_readyTimer.setInterval(200);
111 m_readyTimer.setSingleShot(true);
112 connect(&m_readyTimer, &QTimer::timeout, this, [this](){ this->setReady(); });
113
114 updateInputBoundsAfterResize();
115 }
116
117 MirSurface::~MirSurface()
118 {
119 DEBUG_MSG("");
120
121 // Early warning, while MirSurface methods can still be accessed.
122 Q_EMIT destroyed(this);
123 }
124
125 QString MirSurface::name() const
126 {
127 return m_name;
128 }
129
130 QString MirSurface::persistentId() const
131 {
132 return m_name+"Id";
133 }
134
135 QString MirSurface::appId() const
136 {
137 return m_name+"appId";
138 }
139 Mir::Type MirSurface::type() const
140 {
141 return m_type;
142 }
143
144 Mir::State MirSurface::state() const
145 {
146 return m_state;
147 }
148
149 void MirSurface::requestState(Mir::State state)
150 {
151 if (state == m_state) {
152 return;
153 }
154 DEBUG_MSG(stateToStr(state));
155 Q_EMIT stateRequested(state);
156 }
157
158 void MirSurface::setState(Mir::State state)
159 {
160 if (state == m_state) {
161 return;
162 }
163 DEBUG_MSG(stateToStr(state));
164
165 bool oldVisible = visible();
166
167 m_state = state;
168 Q_EMIT stateChanged(state);
169
170 if (visible() != oldVisible) {
171 XDEBUG_MSG("visibleChanged("<<visible()<<")");
172 Q_EMIT visibleChanged(visible());
173 }
174 }
175
176 bool MirSurface::live() const
177 {
178 return m_live;
179 }
180
181 bool MirSurface::isReady() const
182 {
183 return m_ready;
184 }
185
186 bool MirSurface::visible() const
187 {
188 return m_state != Mir::MinimizedState && m_state != Mir::HiddenState;
189 }
190
191 void MirSurface::setLive(bool live)
192 {
193 if (live == m_live)
194 return;
195
196 DEBUG_MSG(live);
197 m_live = live;
198 Q_EMIT liveChanged(live);
199
200 // A window's surface might take some time to draw the first frame
201 if (live && !m_readyTimer.isActive()) {
202 m_readyTimer.start();
203 }
204
205 if (!m_live && m_views.count() == 0) {
206 deleteLater();
207 }
208 }
209
210 void MirSurface::setReady()
211 {
212 m_ready = true;
213 Q_EMIT ready();
214 }
215
216 QUrl MirSurface::qmlFilePath() const
217 {
218 return m_qmlFilePath;
219 }
220
221 QUrl MirSurface::screenshotUrl() const
222 {
223 return m_screenshotUrl;
224 }
225
226 void MirSurface::setScreenshotUrl(QUrl screenshotUrl)
227 {
228 if (screenshotUrl == m_screenshotUrl)
229 return;
230
231 m_screenshotUrl = screenshotUrl;
232 Q_EMIT screenshotUrlChanged(screenshotUrl);
233 }
234
235 Mir::OrientationAngle MirSurface::orientationAngle() const
236 {
237 return m_orientationAngle;
238 }
239
240 void MirSurface::setOrientationAngle(Mir::OrientationAngle angle)
241 {
242 if (angle == m_orientationAngle)
243 return;
244
245 m_orientationAngle = angle;
246 Q_EMIT orientationAngleChanged(angle);
247 }
248
249 void MirSurface::setKeymap(const QString &value)
250 {
251 if (value != m_keymap) {
252 DEBUG_MSG(value);
253 m_keymap = value;
254 Q_EMIT keymapChanged(m_keymap);
255 }
256 }
257
258 QString MirSurface::keymap() const
259 {
260 return m_keymap;
261 }
262
263 Mir::ShellChrome MirSurface::shellChrome() const
264 {
265 return m_shellChrome;
266 }
267
268 void MirSurface::setShellChrome(Mir::ShellChrome shellChrome)
269 {
270 if (shellChrome == m_shellChrome)
271 return;
272
273 DEBUG_MSG(shellChrome);
274 m_shellChrome = shellChrome;
275 Q_EMIT shellChromeChanged(shellChrome);
276 }
277
278 void MirSurface::registerView(qintptr viewId)
279 {
280 m_views.insert(viewId, MirSurface::View{false});
281 DEBUG_MSG(viewId << " after=" << m_views.count());
282 }
283
284 void MirSurface::unregisterView(qintptr viewId)
285 {
286 m_views.remove(viewId);
287 DEBUG_MSG(viewId << " after=" << m_views.count() << " live=" << m_live);
288 if (!m_live && m_views.count() == 0) {
289 deleteLater();
290 }
291 updateExposure();
292 }
293
294 void MirSurface::setViewExposure(qintptr viewId, bool visible)
295 {
296 if (!m_views.contains(viewId)) return;
297
298 m_views[viewId].visible = visible;
299 updateExposure();
300 }
301
302 void MirSurface::updateExposure()
303 {
304 bool newExposure = false;
305 QHashIterator<qintptr, View> i(m_views);
306 while (i.hasNext()) {
307 i.next();
308 newExposure |= i.value().visible;
309 }
310
311 if (newExposure != m_exposed) {
312 m_exposed = newExposure;
313 DEBUG_MSG(m_exposed);
314 Q_EMIT exposedChanged(m_exposed);
315 updateInputBoundsAfterResize();
316 }
317 }
318
319 bool MirSurface::activeFocus() const
320 {
321 return m_activeFocus;
322 }
323
324 void MirSurface::setActiveFocus(bool value)
325 {
326 if (m_activeFocus == value)
327 return;
328
329 m_activeFocus = value;
330
331 Q_EMIT activeFocusChanged(value);
332
333 if (m_activeFocus && !m_focused) {
334 requestFocus();
335 }
336 }
337
338 int MirSurface::width() const
339 {
340 return m_width;
341 }
342
343 int MirSurface::height() const
344 {
345 return m_height;
346 }
347
348 void MirSurface::resize(int width, int height)
349 {
350 if (m_slowToResize) {
351 if (!m_delayedResizeTimer.isActive()) {
352 m_delayedResize.setWidth(width);
353 m_delayedResize.setHeight(height);
354 m_delayedResizeTimer.start();
355 } else {
356 m_pendingResize.setWidth(width);
357 m_pendingResize.setHeight(height);
358 }
359 } else {
360 doResize(width, height);
361 }
362 }
363
364 void MirSurface::applyDelayedResize()
365 {
366 doResize(m_delayedResize.width(), m_delayedResize.height());
367 m_delayedResize.setWidth(-1);
368 m_delayedResize.setHeight(-1);
369
370 if (m_pendingResize.isValid()) {
371 QSize size = m_pendingResize;
372 m_pendingResize.setWidth(-1);
373 m_pendingResize.setHeight(-1);
374 resize(size.width(), size.height());
375 }
376 }
377
378 void MirSurface::doResize(int width, int height)
379 {
380 bool changed = false;
381
382 if (width != m_width) {
383 m_width = width;
384 Q_EMIT widthChanged();
385 changed = true;
386 }
387
388 if (m_height != height) {
389 m_height = height;
390 Q_EMIT heightChanged();
391 changed = true;
392 }
393
394 if (changed) {
395 XDEBUG_MSG("sizeChanged(width="<<width<<", height="<<height<<")");
396 Q_EMIT sizeChanged(QSize(width, height));
397 }
398
399 updateInputBoundsAfterResize();
400 }
401
402 void MirSurface::updateInputBoundsAfterResize()
403 {
404 setInputBounds(QRect(0, 0, m_width, m_height));
405 }
406
407 bool MirSurface::isSlowToResize() const
408 {
409 return m_slowToResize;
410 }
411
412 void MirSurface::setSlowToResize(bool value)
413 {
414 if (m_slowToResize != value) {
415 DEBUG_MSG(value);
416 m_slowToResize = value;
417 Q_EMIT slowToResizeChanged();
418 if (!m_slowToResize && m_delayedResizeTimer.isActive()) {
419 m_delayedResizeTimer.stop();
420 applyDelayedResize();
421 }
422 }
423 }
424
425 void MirSurface::setMinimumWidth(int value)
426 {
427 if (value != m_minimumWidth) {
428 m_minimumWidth = value;
429 Q_EMIT minimumWidthChanged(m_minimumWidth);
430 }
431 }
432
433 void MirSurface::setMaximumWidth(int value)
434 {
435 if (value != m_maximumWidth) {
436 m_maximumWidth = value;
437 Q_EMIT maximumWidthChanged(m_maximumWidth);
438 }
439 }
440
441 void MirSurface::setMinimumHeight(int value)
442 {
443 if (value != m_minimumHeight) {
444 m_minimumHeight = value;
445 Q_EMIT minimumHeightChanged(m_minimumHeight);
446 }
447 }
448
449 void MirSurface::setMaximumHeight(int value)
450 {
451 if (value != m_maximumHeight) {
452 m_maximumHeight = value;
453 Q_EMIT maximumHeightChanged(m_maximumHeight);
454 }
455 }
456
457 void MirSurface::setWidthIncrement(int value)
458 {
459 if (value != m_widthIncrement) {
460 m_widthIncrement = value;
461 Q_EMIT widthIncrementChanged(m_widthIncrement);
462 }
463 }
464
465 void MirSurface::setHeightIncrement(int value)
466 {
467 if (value != m_heightIncrement) {
468 m_heightIncrement = value;
469 Q_EMIT heightIncrementChanged(m_heightIncrement);
470 }
471 }
472
473 void MirSurface::close()
474 {
475 DEBUG_MSG("");
476
477 for (int i = 0; i < m_childSurfaceList->count(); ++i) {
478 m_childSurfaceList->get(i)->close();
479 }
480
481 if (!m_zombieTimer.isActive()) {
482 m_zombieTimer.start();
483 Q_EMIT closeRequested();
484 }
485 }
486
487 void MirSurface::activate()
488 {
489 DEBUG_MSG("");
490 SurfaceManager::instance()->activate(this);
491 }
492
493 void MirSurface::requestFocus()
494 {
495 DEBUG_MSG("");
496 Q_EMIT focusRequested();
497 }
498
499 void MirSurface::setFocused(bool value)
500 {
501 if (m_focused == value)
502 return;
503
504 DEBUG_MSG("(" << value << ")");
505
506 m_focused = value;
507 Q_EMIT focusedChanged(value);
508 }
509
510 bool MirSurface::focused() const
511 {
512 return m_focused;
513 }
514
515 QRect MirSurface::inputBounds() const
516 {
517 return m_inputBounds;
518 }
519
520 void MirSurface::setInputBounds(const QRect &boundsRect)
521 {
522 if (boundsRect != m_inputBounds) {
523 m_inputBounds = boundsRect;
524 DEBUG_MSG("(" << m_inputBounds << ")");
525 Q_EMIT inputBoundsChanged(m_inputBounds);
526 }
527 }
528
529 void MirSurface::openMenu(qreal x, qreal y, qreal width, qreal height)
530 {
531 auto *menu = SurfaceManager::instance()->createSurface("menu", Mir::MenuType, Mir::HiddenState,
532 this /* parentSurface */,
533 QUrl() /* screenshot */,
534 QUrl("qrc:///QtMir/Application/KateMenu.qml"));
535
536 menu->setRequestedPosition(QPoint(x,y));
537 menu->resize(width, height);
538 menu->requestState(Mir::RestoredState);
539
540 SurfaceManager::instance()->notifySurfaceCreated(menu);
541 }
542
543 void MirSurface::openDialog(qreal x, qreal y, qreal width, qreal height)
544 {
545 auto *dialog = SurfaceManager::instance()->createSurface("dialog", Mir::DialogType, Mir::HiddenState,
546 this /* parentSurface */,
547 QUrl() /* screenshot */,
548 QUrl("qrc:///QtMir/Application/KateDialog.qml"));
549
550 dialog->setRequestedPosition(QPoint(x,y));
551 dialog->resize(width, height);
552 dialog->requestState(Mir::RestoredState);
553
554 SurfaceManager::instance()->notifySurfaceCreated(dialog);
555
556 dialog->requestFocus();
557 }
558
559 void MirSurface::setRequestedPosition(const QPoint &value)
560 {
561 if (value != m_requestedPosition) {
562 m_requestedPosition = value;
563 Q_EMIT requestedPositionChanged(value);
564
565 // fake-miral: always comply
566 m_position = m_requestedPosition;
567 XDEBUG_MSG("positionChanged("<<m_position<<")");
568 Q_EMIT positionChanged(m_position);
569 }
570 }
571
572 MirSurfaceInterface* MirSurface::parentSurface() const
573 {
574 return m_parentSurface;
575 }
576
577 MirSurfaceListInterface* MirSurface::childSurfaceList() const
578 {
579 return m_childSurfaceList;
580 }