1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*
* Copyright (C) 2016-2017 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Window.h"
// lomiri-api
#include <lomiri/shell/application/MirSurfaceInterface.h>
#include <QQmlEngine>
#include <QTextStream>
namespace lomiriapi = lomiri::shell::application;
Q_LOGGING_CATEGORY(LOMIRI_WINDOW, "lomiri.window", QtWarningMsg)
#define DEBUG_MSG qCDebug(LOMIRI_WINDOW).nospace() << qPrintable(toString()) << "::" << __func__
#define WARNING_MSG qCWarning(LOMIRI_WINDOW).nospace() << qPrintable(toString()) << "::" << __func__
Window::Window(int id, QObject *parent)
: QObject(parent)
, m_id(id)
{
DEBUG_MSG << "()";
QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
}
Window::~Window()
{
DEBUG_MSG << "()";
}
QPoint Window::position() const
{
return m_position;
}
QPoint Window::requestedPosition() const
{
return m_requestedPosition;
}
void Window::setRequestedPosition(const QPoint &value)
{
m_positionRequested = true;
if (value != m_requestedPosition) {
m_requestedPosition = value;
Q_EMIT requestedPositionChanged(m_requestedPosition);
if (m_surface) {
m_surface->setRequestedPosition(value);
} else {
// fake-miral: always comply
m_position = m_requestedPosition;
Q_EMIT positionChanged(m_position);
}
}
}
bool Window::allowClientResize() const
{
return m_allowClientResize;
}
void Window::setAllowClientResize(bool value)
{
if (value != m_allowClientResize) {
DEBUG_MSG << "("<<value<<")";
m_allowClientResize = value;
if (m_surface) {
m_surface->setAllowClientResize(value);
}
Q_EMIT allowClientResizeChanged(m_allowClientResize);
}
}
Mir::State Window::state() const
{
return m_state;
}
bool Window::focused() const
{
return m_focused;
}
bool Window::confinesMousePointer() const
{
if (m_surface) {
return m_surface->confinesMousePointer();
} else {
return false;
}
}
int Window::id() const
{
return m_id;
}
lomiriapi::MirSurfaceInterface* Window::surface() const
{
return m_surface;
}
void Window::requestState(Mir::State state)
{
m_stateRequested = true;
if (m_surface) {
m_surface->requestState(state);
} else if (m_state != state) {
m_state = state;
Q_EMIT stateChanged(m_state);
}
}
void Window::close()
{
if (m_surface) {
m_surface->close();
} else {
Q_EMIT closeRequested();
}
}
void Window::activate()
{
DEBUG_MSG << "()";
if (m_surface) {
m_surface->activate();
} else {
Q_EMIT emptyWindowActivated();
}
}
void Window::setSurface(lomiriapi::MirSurfaceInterface *surface)
{
DEBUG_MSG << "(" << surface << ")";
if (m_surface) {
disconnect(m_surface, 0, this, 0);
}
m_surface = surface;
if (m_surface) {
connect(surface, &lomiriapi::MirSurfaceInterface::focusRequested, this, [this]() {
Q_EMIT focusRequested();
});
connect(surface, &lomiriapi::MirSurfaceInterface::closeRequested, this, &Window::closeRequested);
connect(surface, &lomiriapi::MirSurfaceInterface::positionChanged, this, [this]() {
updatePosition();
});
connect(surface, &lomiriapi::MirSurfaceInterface::stateChanged, this, [this]() {
updateState();
});
connect(surface, &lomiriapi::MirSurfaceInterface::focusedChanged, this, [this]() {
updateFocused();
});
connect(surface, &lomiriapi::MirSurfaceInterface::allowClientResizeChanged, this, [this]() {
if (m_surface->allowClientResize() != m_allowClientResize) {
m_allowClientResize = m_surface->allowClientResize();
Q_EMIT allowClientResizeChanged(m_allowClientResize);
}
});
connect(surface, &lomiriapi::MirSurfaceInterface::liveChanged, this, &Window::liveChanged);
connect(surface, &QObject::destroyed, this, [this]() {
setSurface(nullptr);
});
// Surface should never be focused at this point!
if (m_surface->focused()) {
WARNING_MSG << "Initial surface is focused!";
}
// bring it up to speed
if (m_focused) {
m_surface->activate();
}
if (m_positionRequested) {
m_surface->setRequestedPosition(m_requestedPosition);
}
if (m_stateRequested && m_surface->state() == Mir::RestoredState) {
m_surface->requestState(m_state);
}
m_surface->setAllowClientResize(m_allowClientResize);
// and sync with surface
updatePosition();
updateState();
updateFocused();
}
Q_EMIT surfaceChanged(surface);
}
void Window::updatePosition()
{
if (m_surface->position() != m_position) {
m_position = m_surface->position();
Q_EMIT positionChanged(m_position);
}
}
void Window::updateState()
{
if (m_surface->state() != m_state) {
m_state = m_surface->state();
Q_EMIT stateChanged(m_state);
}
}
void Window::updateFocused()
{
if (m_surface->focused() != m_focused) {
m_focused = m_surface->focused();
Q_EMIT focusedChanged(m_focused);
}
}
void Window::setFocused(bool value)
{
if (value != m_focused) {
DEBUG_MSG << "(" << value << ")";
m_focused = value;
Q_EMIT focusedChanged(m_focused);
// when we have a surface we get focus changes from updateFocused() instead
Q_ASSERT(!m_surface);
}
}
QString Window::toString() const
{
QString result;
{
QTextStream stream(&result);
stream << "Window["<<(void*)this<<", id="<<id()<<", ";
if (surface()) {
stream << "MirSurface["<<(void*)surface()<<",\""<<surface()->name()<<"\"]";
} else {
stream << "null";
}
stream << "]";
}
return result;
}
QDebug operator<<(QDebug dbg, const Window *window)
{
QDebugStateSaver saver(dbg);
dbg.nospace();
if (window) {
dbg << qPrintable(window->toString());
} else {
dbg << (void*)(window);
}
return dbg;
}
|