on bionic help sign in
559 lines 13 kB history 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 */
17#include "MirSurface.h"
19#include <QDebug>
20#include <QQmlEngine>
22// local
23#include "SurfaceManager.h"
25#define MIRSURFACE_DEBUG 0
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
31const char *stateToStr(Mir::State state)
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 }
67#else
68#define DEBUG_MSG(params) ((void)0)
69#define XDEBUG_MSG(params) ((void)0)
70#endif
72using namespace unity::shell::application;
74MirSurface::MirSurface(const QString& name,
75 Mir::Type type,
76 Mir::State state,
77 MirSurface *parentSurface,
78 const QUrl& screenshot,
79 const QUrl &qmlFilePath)
80 : unity::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_focused(false)
89 , m_activeFocus(false)
90 , m_width(-1)
91 , m_height(-1)
92 , m_slowToResize(false)
93 , m_shellChrome(Mir::NormalChrome)
94 , m_parentSurface(parentSurface)
95 , m_childSurfaceList(new MirSurfaceListModel(this))
97 DEBUG_MSG("state=" << stateToStr(state));
99 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
101 m_delayedResizeTimer.setInterval(600);
102 m_delayedResizeTimer.setSingleShot(true);
103 connect(&m_delayedResizeTimer, &QTimer::timeout, this, &MirSurface::applyDelayedResize);
105 m_zombieTimer.setInterval(100);
106 m_zombieTimer.setSingleShot(true);
107 connect(&m_zombieTimer, &QTimer::timeout, this, [this](){ this->setLive(false); });
109 updateInputBoundsAfterResize();
112MirSurface::~MirSurface()
114 DEBUG_MSG("");
116 // Early warning, while MirSurface methods can still be accessed.
117 Q_EMIT destroyed(this);
120QString MirSurface::name() const
122 return m_name;
125QString MirSurface::persistentId() const
127 return m_name+"Id";
130QString MirSurface::appId() const
132 return m_name+"appId";
134Mir::Type MirSurface::type() const
136 return m_type;
139Mir::State MirSurface::state() const
141 return m_state;
144void MirSurface::requestState(Mir::State state)
146 if (state == m_state) {
147 return;
148 }
149 DEBUG_MSG(stateToStr(state));
150 Q_EMIT stateRequested(state);
153void MirSurface::setState(Mir::State state)
155 if (state == m_state) {
156 return;
157 }
158 DEBUG_MSG(stateToStr(state));
160 bool oldVisible = visible();
162 m_state = state;
163 Q_EMIT stateChanged(state);
165 if (visible() != oldVisible) {
166 XDEBUG_MSG("visibleChanged("<<visible()<<")");
167 Q_EMIT visibleChanged(visible());
168 }
171bool MirSurface::live() const
173 return m_live;
176bool MirSurface::visible() const
178 return m_state != Mir::MinimizedState && m_state != Mir::HiddenState;
181void MirSurface::setLive(bool live)
183 if (live == m_live)
184 return;
186 DEBUG_MSG(live);
187 m_live = live;
188 Q_EMIT liveChanged(live);
190 if (!m_live && m_views.count() == 0) {
191 deleteLater();
192 }
195QUrl MirSurface::qmlFilePath() const
197 return m_qmlFilePath;
200QUrl MirSurface::screenshotUrl() const
202 return m_screenshotUrl;
205void MirSurface::setScreenshotUrl(QUrl screenshotUrl)
207 if (screenshotUrl == m_screenshotUrl)
208 return;
210 m_screenshotUrl = screenshotUrl;
211 Q_EMIT screenshotUrlChanged(screenshotUrl);
214Mir::OrientationAngle MirSurface::orientationAngle() const
216 return m_orientationAngle;
219void MirSurface::setOrientationAngle(Mir::OrientationAngle angle)
221 if (angle == m_orientationAngle)
222 return;
224 m_orientationAngle = angle;
225 Q_EMIT orientationAngleChanged(angle);
228void MirSurface::setKeymap(const QString &value)
230 if (value != m_keymap) {
231 DEBUG_MSG(value);
232 m_keymap = value;
233 Q_EMIT keymapChanged(m_keymap);
234 }
237QString MirSurface::keymap() const
239 return m_keymap;
242Mir::ShellChrome MirSurface::shellChrome() const
244 return m_shellChrome;
247void MirSurface::setShellChrome(Mir::ShellChrome shellChrome)
249 if (shellChrome == m_shellChrome)
250 return;
252 DEBUG_MSG(shellChrome);
253 m_shellChrome = shellChrome;
254 Q_EMIT shellChromeChanged(shellChrome);
257void MirSurface::registerView(qintptr viewId)
259 m_views.insert(viewId, MirSurface::View{false});
260 DEBUG_MSG(viewId << " after=" << m_views.count());
263void MirSurface::unregisterView(qintptr viewId)
265 m_views.remove(viewId);
266 DEBUG_MSG(viewId << " after=" << m_views.count() << " live=" << m_live);
267 if (!m_live && m_views.count() == 0) {
268 deleteLater();
269 }
270 updateExposure();
273void MirSurface::setViewExposure(qintptr viewId, bool visible)
275 if (!m_views.contains(viewId)) return;
277 m_views[viewId].visible = visible;
278 updateExposure();
281void MirSurface::updateExposure()
283 bool newExposure = false;
284 QHashIterator<qintptr, View> i(m_views);
285 while (i.hasNext()) {
286 i.next();
287 newExposure |= i.value().visible;
288 }
290 if (newExposure != m_exposed) {
291 m_exposed = newExposure;
292 DEBUG_MSG(m_exposed);
293 Q_EMIT exposedChanged(m_exposed);
294 updateInputBoundsAfterResize();
295 }
298bool MirSurface::activeFocus() const
300 return m_activeFocus;
303void MirSurface::setActiveFocus(bool value)
305 if (m_activeFocus == value)
306 return;
308 m_activeFocus = value;
310 Q_EMIT activeFocusChanged(value);
312 if (m_activeFocus && !m_focused) {
313 requestFocus();
314 }
317int MirSurface::width() const
319 return m_width;
322int MirSurface::height() const
324 return m_height;
327void MirSurface::resize(int width, int height)
329 if (m_slowToResize) {
330 if (!m_delayedResizeTimer.isActive()) {
331 m_delayedResize.setWidth(width);
332 m_delayedResize.setHeight(height);
333 m_delayedResizeTimer.start();
334 } else {
335 m_pendingResize.setWidth(width);
336 m_pendingResize.setHeight(height);
337 }
338 } else {
339 doResize(width, height);
340 }
343void MirSurface::applyDelayedResize()
345 doResize(m_delayedResize.width(), m_delayedResize.height());
346 m_delayedResize.setWidth(-1);
347 m_delayedResize.setHeight(-1);
349 if (m_pendingResize.isValid()) {
350 QSize size = m_pendingResize;
351 m_pendingResize.setWidth(-1);
352 m_pendingResize.setHeight(-1);
353 resize(size.width(), size.height());
354 }
357void MirSurface::doResize(int width, int height)
359 bool changed = false;
361 if (width != m_width) {
362 m_width = width;
363 Q_EMIT widthChanged();
364 changed = true;
365 }
367 if (m_height != height) {
368 m_height = height;
369 Q_EMIT heightChanged();
370 changed = true;
371 }
373 if (changed) {
374 XDEBUG_MSG("sizeChanged(width="<<width<<", height="<<height<<")");
375 Q_EMIT sizeChanged(QSize(width, height));
376 }
378 updateInputBoundsAfterResize();
381void MirSurface::updateInputBoundsAfterResize()
383 setInputBounds(QRect(0, 0, m_width, m_height));
386bool MirSurface::isSlowToResize() const
388 return m_slowToResize;
391void MirSurface::setSlowToResize(bool value)
393 if (m_slowToResize != value) {
394 DEBUG_MSG(value);
395 m_slowToResize = value;
396 Q_EMIT slowToResizeChanged();
397 if (!m_slowToResize && m_delayedResizeTimer.isActive()) {
398 m_delayedResizeTimer.stop();
399 applyDelayedResize();
400 }
401 }
404void MirSurface::setMinimumWidth(int value)
406 if (value != m_minimumWidth) {
407 m_minimumWidth = value;
408 Q_EMIT minimumWidthChanged(m_minimumWidth);
409 }
412void MirSurface::setMaximumWidth(int value)
414 if (value != m_maximumWidth) {
415 m_maximumWidth = value;
416 Q_EMIT maximumWidthChanged(m_maximumWidth);
417 }
420void MirSurface::setMinimumHeight(int value)
422 if (value != m_minimumHeight) {
423 m_minimumHeight = value;
424 Q_EMIT minimumHeightChanged(m_minimumHeight);
425 }
428void MirSurface::setMaximumHeight(int value)
430 if (value != m_maximumHeight) {
431 m_maximumHeight = value;
432 Q_EMIT maximumHeightChanged(m_maximumHeight);
433 }
436void MirSurface::setWidthIncrement(int value)
438 if (value != m_widthIncrement) {
439 m_widthIncrement = value;
440 Q_EMIT widthIncrementChanged(m_widthIncrement);
441 }
444void MirSurface::setHeightIncrement(int value)
446 if (value != m_heightIncrement) {
447 m_heightIncrement = value;
448 Q_EMIT heightIncrementChanged(m_heightIncrement);
449 }
452void MirSurface::close()
454 DEBUG_MSG("");
456 for (int i = 0; i < m_childSurfaceList->count(); ++i) {
457 m_childSurfaceList->get(i)->close();
458 }
460 if (!m_zombieTimer.isActive()) {
461 m_zombieTimer.start();
462 Q_EMIT closeRequested();
463 }
466void MirSurface::activate()
468 DEBUG_MSG("");
469 SurfaceManager::instance()->activate(this);
472void MirSurface::requestFocus()
474 DEBUG_MSG("");
475 Q_EMIT focusRequested();
478void MirSurface::setFocused(bool value)
480 if (m_focused == value)
481 return;
483 DEBUG_MSG("(" << value << ")");
485 m_focused = value;
486 Q_EMIT focusedChanged(value);
489bool MirSurface::focused() const
491 return m_focused;
494QRect MirSurface::inputBounds() const
496 return m_inputBounds;
499void MirSurface::setInputBounds(const QRect &boundsRect)
501 if (boundsRect != m_inputBounds) {
502 m_inputBounds = boundsRect;
503 DEBUG_MSG("(" << m_inputBounds << ")");
504 Q_EMIT inputBoundsChanged(m_inputBounds);
505 }
508void MirSurface::openMenu(qreal x, qreal y, qreal width, qreal height)
510 auto *menu = SurfaceManager::instance()->createSurface("menu", Mir::MenuType, Mir::HiddenState,
511 this /* parentSurface */,
512 QUrl() /* screenshot */,
513 QUrl("qrc:///Unity/Application/KateMenu.qml"));
515 menu->setRequestedPosition(QPoint(x,y));
516 menu->resize(width, height);
517 menu->requestState(Mir::RestoredState);
519 SurfaceManager::instance()->notifySurfaceCreated(menu);
522void MirSurface::openDialog(qreal x, qreal y, qreal width, qreal height)
524 auto *dialog = SurfaceManager::instance()->createSurface("dialog", Mir::DialogType, Mir::HiddenState,
525 this /* parentSurface */,
526 QUrl() /* screenshot */,
527 QUrl("qrc:///Unity/Application/KateDialog.qml"));
529 dialog->setRequestedPosition(QPoint(x,y));
530 dialog->resize(width, height);
531 dialog->requestState(Mir::RestoredState);
533 SurfaceManager::instance()->notifySurfaceCreated(dialog);
535 dialog->requestFocus();
538void MirSurface::setRequestedPosition(const QPoint &value)
540 if (value != m_requestedPosition) {
541 m_requestedPosition = value;
542 Q_EMIT requestedPositionChanged(value);
544 // fake-miral: always comply
545 m_position = m_requestedPosition;
546 XDEBUG_MSG("positionChanged("<<m_position<<")");
547 Q_EMIT positionChanged(m_position);
548 }
551MirSurfaceInterface* MirSurface::parentSurface() const
553 return m_parentSurface;
556MirSurfaceListInterface* MirSurface::childSurfaceList() const
558 return m_childSurfaceList;