on 7339ef2 help sign in
268 lines 6.0 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)
40// qDebug() << "MirSurface::MirSurface() " << name;
41 m_delayedResizeTimer.setInterval(600);
42 m_delayedResizeTimer.setSingleShot(true);
43 connect(&m_delayedResizeTimer, &QTimer::timeout, this, &MirSurface::applyDelayedResize);
46MirSurface::~MirSurface()
48// qDebug() << "MirSurface::~MirSurface() " << name();
51QString MirSurface::name() const
53 return m_name;
56Mir::Type MirSurface::type() const
58 return m_type;
61Mir::State MirSurface::state() const
63 return m_state;
66void MirSurface::setState(Mir::State state)
68 if (state == m_state)
69 return;
71 m_state = state;
72 Q_EMIT stateChanged(state);
75bool MirSurface::live() const
77 return m_live;
80bool MirSurface::visible() const
82 return m_visible;
85void MirSurface::setLive(bool live)
87// qDebug().nospace() << "MirSurface::setLive("<<live<<") " << name();
88 if (live == m_live)
89 return;
91 m_live = live;
92 Q_EMIT liveChanged(live);
94 if (!m_live && m_views.count() == 0) {
95 deleteLater();
96 }
99QUrl MirSurface::qmlFilePath() const
101 return m_qmlFilePath;
104QUrl MirSurface::screenshotUrl() const
106 return m_screenshotUrl;
109void MirSurface::setScreenshotUrl(QUrl screenshotUrl)
111 if (screenshotUrl == m_screenshotUrl)
112 return;
114 m_screenshotUrl = screenshotUrl;
115 Q_EMIT screenshotUrlChanged(screenshotUrl);
118Mir::OrientationAngle MirSurface::orientationAngle() const
120 return m_orientationAngle;
123void MirSurface::setOrientationAngle(Mir::OrientationAngle angle)
125 if (angle == m_orientationAngle)
126 return;
128 m_orientationAngle = angle;
129 Q_EMIT orientationAngleChanged(angle);
134void MirSurface::registerView(qintptr viewId)
136 m_views.insert(viewId, MirSurface::View{false});
137// qDebug().nospace() << "MirSurface[" << name() << "]::registerView(" << viewId << ")"
138// << " after=" << m_views.count();
141void MirSurface::unregisterView(qintptr viewId)
143// qDebug().nospace() << "MirSurface[" << name() << "]::unregisterView(" << viewId << ")"
144// << " after=" << m_views.count() << " live=" << m_live;
145 m_views.remove(viewId);
146 if (!m_live && m_views.count() == 0) {
147 deleteLater();
148 }
149 updateVisibility();
152void MirSurface::setViewVisibility(qintptr viewId, bool visible)
154 if (!m_views.contains(viewId)) return;
156 m_views[viewId].visible = visible;
157 updateVisibility();
160void MirSurface::updateVisibility()
162 bool newVisible = false;
163 QHashIterator<qintptr, View> i(m_views);
164 while (i.hasNext()) {
165 i.next();
166 newVisible |= i.value().visible;
167 }
169 if (newVisible != visible()) {
170// qDebug().nospace() << "MirSurface[" << name() << "]::updateVisibility(" << newVisible << ")";
172 m_visible = newVisible;
173 Q_EMIT visibleChanged(m_visible);
174 }
177bool MirSurface::activeFocus() const
179 return m_activeFocus;
182void MirSurface::setActiveFocus(bool value)
184 if (m_activeFocus == value)
185 return;
187 m_activeFocus = value;
189 Q_EMIT activeFocusChanged(value);
192int MirSurface::width() const
194 return m_width;
197int MirSurface::height() const
199 return m_height;
202void MirSurface::resize(int width, int height)
204 if (m_slowToResize) {
205 if (!m_delayedResizeTimer.isActive()) {
206 m_delayedResize.setWidth(width);
207 m_delayedResize.setHeight(height);
208 m_delayedResizeTimer.start();
209 } else {
210 m_pendingResize.setWidth(width);
211 m_pendingResize.setHeight(height);
212 }
213 } else {
214 doResize(width, height);
215 }
218void MirSurface::applyDelayedResize()
220 doResize(m_delayedResize.width(), m_delayedResize.height());
221 m_delayedResize.setWidth(-1);
222 m_delayedResize.setHeight(-1);
224 if (m_pendingResize.isValid()) {
225 QSize size = m_pendingResize;
226 m_pendingResize.setWidth(-1);
227 m_pendingResize.setHeight(-1);
228 resize(size.width(), size.height());
229 }
232void MirSurface::doResize(int width, int height)
234 bool changed = false;
236 if (width != m_width) {
237 m_width = width;
238 Q_EMIT widthChanged();
239 changed = true;
240 }
242 if (m_height != height) {
243 m_height = height;
244 Q_EMIT heightChanged();
245 changed = true;
246 }
248 if (changed) {
249 Q_EMIT sizeChanged(QSize(width, height));
250 }
253bool MirSurface::isSlowToResize() const
255 return m_slowToResize;
258void MirSurface::setSlowToResize(bool value)
260 if (m_slowToResize != value) {
261 m_slowToResize = value;
262 Q_EMIT slowToResizeChanged();
263 if (!m_slowToResize && m_delayedResizeTimer.isActive()) {
264 m_delayedResizeTimer.stop();
265 applyDelayedResize();
266 }
267 }