on f20331b help sign in
550 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";
130Mir::Type MirSurface::type() const
132 return m_type;
135Mir::State MirSurface::state() const
137 return m_state;
140void MirSurface::requestState(Mir::State state)
142 if (state == m_state) {
143 return;
144 }
145 DEBUG_MSG(stateToStr(state));
146 Q_EMIT stateRequested(state);
149void MirSurface::setState(Mir::State state)
151 if (state == m_state) {
152 return;
153 }
154 DEBUG_MSG(stateToStr(state));
156 bool oldVisible = visible();
158 m_state = state;
159 Q_EMIT stateChanged(state);
161 if (visible() != oldVisible) {
162 XDEBUG_MSG("visibleChanged("<<visible()<<")");
163 Q_EMIT visibleChanged(visible());
164 }
167bool MirSurface::live() const
169 return m_live;
172bool MirSurface::visible() const
174 return m_state != Mir::MinimizedState && m_state != Mir::HiddenState;
177void MirSurface::setLive(bool live)
179 if (live == m_live)
180 return;
182 DEBUG_MSG(live);
183 m_live = live;
184 Q_EMIT liveChanged(live);
186 if (!m_live && m_views.count() == 0) {
187 deleteLater();
188 }
191QUrl MirSurface::qmlFilePath() const
193 return m_qmlFilePath;
196QUrl MirSurface::screenshotUrl() const
198 return m_screenshotUrl;
201void MirSurface::setScreenshotUrl(QUrl screenshotUrl)
203 if (screenshotUrl == m_screenshotUrl)
204 return;
206 m_screenshotUrl = screenshotUrl;
207 Q_EMIT screenshotUrlChanged(screenshotUrl);
210Mir::OrientationAngle MirSurface::orientationAngle() const
212 return m_orientationAngle;
215void MirSurface::setOrientationAngle(Mir::OrientationAngle angle)
217 if (angle == m_orientationAngle)
218 return;
220 m_orientationAngle = angle;
221 Q_EMIT orientationAngleChanged(angle);
224void MirSurface::setKeymap(const QString &value)
226 if (value != m_keymap) {
227 DEBUG_MSG(value);
228 m_keymap = value;
229 Q_EMIT keymapChanged(m_keymap);
230 }
233QString MirSurface::keymap() const
235 return m_keymap;
238Mir::ShellChrome MirSurface::shellChrome() const
240 return m_shellChrome;
243void MirSurface::setShellChrome(Mir::ShellChrome shellChrome)
245 if (shellChrome == m_shellChrome)
246 return;
248 DEBUG_MSG(shellChrome);
249 m_shellChrome = shellChrome;
250 Q_EMIT shellChromeChanged(shellChrome);
253void MirSurface::registerView(qintptr viewId)
255 m_views.insert(viewId, MirSurface::View{false});
256 DEBUG_MSG(viewId << " after=" << m_views.count());
259void MirSurface::unregisterView(qintptr viewId)
261 m_views.remove(viewId);
262 DEBUG_MSG(viewId << " after=" << m_views.count() << " live=" << m_live);
263 if (!m_live && m_views.count() == 0) {
264 deleteLater();
265 }
266 updateExposure();
269void MirSurface::setViewExposure(qintptr viewId, bool visible)
271 if (!m_views.contains(viewId)) return;
273 m_views[viewId].visible = visible;
274 updateExposure();
277void MirSurface::updateExposure()
279 bool newExposure = false;
280 QHashIterator<qintptr, View> i(m_views);
281 while (i.hasNext()) {
282 i.next();
283 newExposure |= i.value().visible;
284 }
286 if (newExposure != m_exposed) {
287 m_exposed = newExposure;
288 DEBUG_MSG(m_exposed);
289 Q_EMIT exposedChanged(m_exposed);
290 updateInputBoundsAfterResize();
291 }
294bool MirSurface::activeFocus() const
296 return m_activeFocus;
299void MirSurface::setActiveFocus(bool value)
301 if (m_activeFocus == value)
302 return;
304 m_activeFocus = value;
306 Q_EMIT activeFocusChanged(value);
308 if (m_activeFocus && !m_focused) {
309 requestFocus();
310 }
313int MirSurface::width() const
315 return m_width;
318int MirSurface::height() const
320 return m_height;
323void MirSurface::resize(int width, int height)
325 if (m_slowToResize) {
326 if (!m_delayedResizeTimer.isActive()) {
327 m_delayedResize.setWidth(width);
328 m_delayedResize.setHeight(height);
329 m_delayedResizeTimer.start();
330 } else {
331 m_pendingResize.setWidth(width);
332 m_pendingResize.setHeight(height);
333 }
334 } else {
335 doResize(width, height);
336 }
339void MirSurface::applyDelayedResize()
341 doResize(m_delayedResize.width(), m_delayedResize.height());
342 m_delayedResize.setWidth(-1);
343 m_delayedResize.setHeight(-1);
345 if (m_pendingResize.isValid()) {
346 QSize size = m_pendingResize;
347 m_pendingResize.setWidth(-1);
348 m_pendingResize.setHeight(-1);
349 resize(size.width(), size.height());
350 }
353void MirSurface::doResize(int width, int height)
355 bool changed = false;
357 if (width != m_width) {
358 m_width = width;
359 Q_EMIT widthChanged();
360 changed = true;
361 }
363 if (m_height != height) {
364 m_height = height;
365 Q_EMIT heightChanged();
366 changed = true;
367 }
369 if (changed) {
370 XDEBUG_MSG("sizeChanged(width="<<width<<", height="<<height<<")");
371 Q_EMIT sizeChanged(QSize(width, height));
372 }
374 updateInputBoundsAfterResize();
377void MirSurface::updateInputBoundsAfterResize()
379 setInputBounds(QRect(0, 0, m_width, m_height));
382bool MirSurface::isSlowToResize() const
384 return m_slowToResize;
387void MirSurface::setSlowToResize(bool value)
389 if (m_slowToResize != value) {
390 DEBUG_MSG(value);
391 m_slowToResize = value;
392 Q_EMIT slowToResizeChanged();
393 if (!m_slowToResize && m_delayedResizeTimer.isActive()) {
394 m_delayedResizeTimer.stop();
395 applyDelayedResize();
396 }
397 }
400void MirSurface::setMinimumWidth(int value)
402 if (value != m_minimumWidth) {
403 m_minimumWidth = value;
404 Q_EMIT minimumWidthChanged(m_minimumWidth);
405 }
408void MirSurface::setMaximumWidth(int value)
410 if (value != m_maximumWidth) {
411 m_maximumWidth = value;
412 Q_EMIT maximumWidthChanged(m_maximumWidth);
413 }
416void MirSurface::setMinimumHeight(int value)
418 if (value != m_minimumHeight) {
419 m_minimumHeight = value;
420 Q_EMIT minimumHeightChanged(m_minimumHeight);
421 }
424void MirSurface::setMaximumHeight(int value)
426 if (value != m_maximumHeight) {
427 m_maximumHeight = value;
428 Q_EMIT maximumHeightChanged(m_maximumHeight);
429 }
432void MirSurface::setWidthIncrement(int value)
434 if (value != m_widthIncrement) {
435 m_widthIncrement = value;
436 Q_EMIT widthIncrementChanged(m_widthIncrement);
437 }
440void MirSurface::setHeightIncrement(int value)
442 if (value != m_heightIncrement) {
443 m_heightIncrement = value;
444 Q_EMIT heightIncrementChanged(m_heightIncrement);
445 }
448void MirSurface::close()
450 DEBUG_MSG("");
451 if (!m_zombieTimer.isActive()) {
452 m_zombieTimer.start();
453 Q_EMIT closeRequested();
454 }
457void MirSurface::activate()
459 DEBUG_MSG("");
460 SurfaceManager::instance()->activate(this);
463void MirSurface::requestFocus()
465 DEBUG_MSG("");
466 Q_EMIT focusRequested();
469void MirSurface::setFocused(bool value)
471 if (m_focused == value)
472 return;
474 DEBUG_MSG("(" << value << ")");
476 m_focused = value;
477 Q_EMIT focusedChanged(value);
480bool MirSurface::focused() const
482 return m_focused;
485QRect MirSurface::inputBounds() const
487 return m_inputBounds;
490void MirSurface::setInputBounds(const QRect &boundsRect)
492 if (boundsRect != m_inputBounds) {
493 m_inputBounds = boundsRect;
494 DEBUG_MSG("(" << m_inputBounds << ")");
495 Q_EMIT inputBoundsChanged(m_inputBounds);
496 }
499void MirSurface::openMenu(qreal x, qreal y, qreal width, qreal height)
501 auto *menu = SurfaceManager::instance()->createSurface("menu", Mir::MenuType, Mir::HiddenState,
502 this /* parentSurface */,
503 QUrl() /* screenshot */,
504 QUrl("qrc:///Unity/Application/KateMenu.qml"));
506 menu->setRequestedPosition(QPoint(x,y));
507 menu->resize(width, height);
508 menu->requestState(Mir::RestoredState);
510 SurfaceManager::instance()->notifySurfaceCreated(menu);
513void MirSurface::openDialog(qreal x, qreal y, qreal width, qreal height)
515 auto *dialog = SurfaceManager::instance()->createSurface("dialog", Mir::DialogType, Mir::HiddenState,
516 this /* parentSurface */,
517 QUrl() /* screenshot */,
518 QUrl("qrc:///Unity/Application/KateDialog.qml"));
520 dialog->setRequestedPosition(QPoint(x,y));
521 dialog->resize(width, height);
522 dialog->requestState(Mir::RestoredState);
524 SurfaceManager::instance()->notifySurfaceCreated(dialog);
526 dialog->requestFocus();
529void MirSurface::setRequestedPosition(const QPoint &value)
531 if (value != m_requestedPosition) {
532 m_requestedPosition = value;
533 Q_EMIT requestedPositionChanged(value);
535 // fake-miral: always comply
536 m_position = m_requestedPosition;
537 XDEBUG_MSG("positionChanged("<<m_position<<")");
538 Q_EMIT positionChanged(m_position);
539 }
542MirSurfaceInterface* MirSurface::parentSurface() const
544 return m_parentSurface;
547MirSurfaceListInterface* MirSurface::childSurfaceList() const
549 return m_childSurfaceList;