on 24b9982 help sign in
332 lines 7.4 kB history raw
1/*
2 * Copyright (C) 2015 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>
21MirSurface::MirSurface(const QString& name,
22 Mir::Type type,
23 Mir::State state,
24 const QUrl& screenshot,
25 const QUrl &qmlFilePath)
26 : unity::shell::application::MirSurfaceInterface(nullptr)
27 , m_name(name)
28 , m_type(type)
29 , m_state(state)
30 , m_orientationAngle(Mir::Angle0)
31 , m_screenshotUrl(screenshot)
32 , m_qmlFilePath(qmlFilePath)
33 , m_live(true)
34 , m_visible(true)
35 , m_activeFocus(false)
36 , m_width(-1)
37 , m_height(-1)
38 , m_slowToResize(false)
39 , m_shellChrome(Mir::NormalChrome)
41// qDebug() << "MirSurface::MirSurface() " << name;
42 m_delayedResizeTimer.setInterval(600);
43 m_delayedResizeTimer.setSingleShot(true);
44 connect(&m_delayedResizeTimer, &QTimer::timeout, this, &MirSurface::applyDelayedResize);
47MirSurface::~MirSurface()
49// qDebug() << "MirSurface::~MirSurface() " << name();
52QString MirSurface::name() const
54 return m_name;
57Mir::Type MirSurface::type() const
59 return m_type;
62Mir::State MirSurface::state() const
64 return m_state;
67void MirSurface::setState(Mir::State state)
69 if (state == m_state)
70 return;
72 m_state = state;
73 Q_EMIT stateChanged(state);
76bool MirSurface::live() const
78 return m_live;
81bool MirSurface::visible() const
83 return m_visible;
86void MirSurface::setLive(bool live)
88// qDebug().nospace() << "MirSurface::setLive("<<live<<") " << name();
89 if (live == m_live)
90 return;
92 m_live = live;
93 Q_EMIT liveChanged(live);
95 if (!m_live && m_views.count() == 0) {
96 deleteLater();
97 }
100QUrl MirSurface::qmlFilePath() const
102 return m_qmlFilePath;
105QUrl MirSurface::screenshotUrl() const
107 return m_screenshotUrl;
110void MirSurface::setScreenshotUrl(QUrl screenshotUrl)
112 if (screenshotUrl == m_screenshotUrl)
113 return;
115 m_screenshotUrl = screenshotUrl;
116 Q_EMIT screenshotUrlChanged(screenshotUrl);
119Mir::OrientationAngle MirSurface::orientationAngle() const
121 return m_orientationAngle;
124void MirSurface::setOrientationAngle(Mir::OrientationAngle angle)
126 if (angle == m_orientationAngle)
127 return;
129 m_orientationAngle = angle;
130 Q_EMIT orientationAngleChanged(angle);
134Mir::ShellChrome MirSurface::shellChrome() const
136 return m_shellChrome;
139void MirSurface::setShellChrome(Mir::ShellChrome shellChrome)
141 if (shellChrome == m_shellChrome)
142 return;
144 m_shellChrome = shellChrome;
145 Q_EMIT shellChromeChanged(shellChrome);
150void MirSurface::registerView(qintptr viewId)
152 m_views.insert(viewId, MirSurface::View{false});
153// qDebug().nospace() << "MirSurface[" << name() << "]::registerView(" << viewId << ")"
154// << " after=" << m_views.count();
157void MirSurface::unregisterView(qintptr viewId)
159// qDebug().nospace() << "MirSurface[" << name() << "]::unregisterView(" << viewId << ")"
160// << " after=" << m_views.count() << " live=" << m_live;
161 m_views.remove(viewId);
162 if (!m_live && m_views.count() == 0) {
163 deleteLater();
164 }
165 updateVisibility();
168void MirSurface::setViewVisibility(qintptr viewId, bool visible)
170 if (!m_views.contains(viewId)) return;
172 m_views[viewId].visible = visible;
173 updateVisibility();
176void MirSurface::updateVisibility()
178 bool newVisible = false;
179 QHashIterator<qintptr, View> i(m_views);
180 while (i.hasNext()) {
181 i.next();
182 newVisible |= i.value().visible;
183 }
185 if (newVisible != visible()) {
186// qDebug().nospace() << "MirSurface[" << name() << "]::updateVisibility(" << newVisible << ")";
188 m_visible = newVisible;
189 Q_EMIT visibleChanged(m_visible);
190 }
193bool MirSurface::activeFocus() const
195 return m_activeFocus;
198void MirSurface::setActiveFocus(bool value)
200 if (m_activeFocus == value)
201 return;
203 m_activeFocus = value;
205 Q_EMIT activeFocusChanged(value);
208int MirSurface::width() const
210 return m_width;
213int MirSurface::height() const
215 return m_height;
218void MirSurface::resize(int width, int height)
220 if (m_slowToResize) {
221 if (!m_delayedResizeTimer.isActive()) {
222 m_delayedResize.setWidth(width);
223 m_delayedResize.setHeight(height);
224 m_delayedResizeTimer.start();
225 } else {
226 m_pendingResize.setWidth(width);
227 m_pendingResize.setHeight(height);
228 }
229 } else {
230 doResize(width, height);
231 }
234void MirSurface::applyDelayedResize()
236 doResize(m_delayedResize.width(), m_delayedResize.height());
237 m_delayedResize.setWidth(-1);
238 m_delayedResize.setHeight(-1);
240 if (m_pendingResize.isValid()) {
241 QSize size = m_pendingResize;
242 m_pendingResize.setWidth(-1);
243 m_pendingResize.setHeight(-1);
244 resize(size.width(), size.height());
245 }
248void MirSurface::doResize(int width, int height)
250 bool changed = false;
252 if (width != m_width) {
253 m_width = width;
254 Q_EMIT widthChanged();
255 changed = true;
256 }
258 if (m_height != height) {
259 m_height = height;
260 Q_EMIT heightChanged();
261 changed = true;
262 }
264 if (changed) {
265 Q_EMIT sizeChanged(QSize(width, height));
266 }
269bool MirSurface::isSlowToResize() const
271 return m_slowToResize;
274void MirSurface::setSlowToResize(bool value)
276 if (m_slowToResize != value) {
277 m_slowToResize = value;
278 Q_EMIT slowToResizeChanged();
279 if (!m_slowToResize && m_delayedResizeTimer.isActive()) {
280 m_delayedResizeTimer.stop();
281 applyDelayedResize();
282 }
283 }
286void MirSurface::setMinimumWidth(int value)
288 if (value != m_minimumWidth) {
289 m_minimumWidth = value;
290 Q_EMIT minimumWidthChanged(m_minimumWidth);
291 }
294void MirSurface::setMaximumWidth(int value)
296 if (value != m_maximumWidth) {
297 m_maximumWidth = value;
298 Q_EMIT maximumWidthChanged(m_maximumWidth);
299 }
302void MirSurface::setMinimumHeight(int value)
304 if (value != m_minimumHeight) {
305 m_minimumHeight = value;
306 Q_EMIT minimumHeightChanged(m_minimumHeight);
307 }
310void MirSurface::setMaximumHeight(int value)
312 if (value != m_maximumHeight) {
313 m_maximumHeight = value;
314 Q_EMIT maximumHeightChanged(m_maximumHeight);
315 }
318void MirSurface::setWidthIncrement(int value)
320 if (value != m_widthIncrement) {
321 m_widthIncrement = value;
322 Q_EMIT widthIncrementChanged(m_widthIncrement);
323 }
326void MirSurface::setHeightIncrement(int value)
328 if (value != m_heightIncrement) {
329 m_heightIncrement = value;
330 Q_EMIT heightIncrementChanged(m_heightIncrement);
331 }