2022-04-06 21:18:20 +00:00
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: 2014 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org>
|
|
|
|
|
* SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "windowutil.h"
|
|
|
|
|
|
2023-03-19 01:48:49 +00:00
|
|
|
#include <KApplicationTrader>
|
|
|
|
|
|
2022-04-06 21:18:20 +00:00
|
|
|
#include <QGuiApplication>
|
|
|
|
|
|
2022-04-11 15:55:56 +00:00
|
|
|
constexpr int ACTIVE_WINDOW_UPDATE_INVERVAL = 0;
|
2022-04-06 21:18:20 +00:00
|
|
|
|
|
|
|
|
WindowUtil::WindowUtil(QObject *parent)
|
|
|
|
|
: QObject{parent}
|
|
|
|
|
, m_activeWindowTimer{new QTimer{this}}
|
|
|
|
|
{
|
2022-04-11 15:55:56 +00:00
|
|
|
// use 0 tick timer to update active window to ensure window state has finished changing
|
2022-04-06 21:18:20 +00:00
|
|
|
m_activeWindowTimer->setSingleShot(true);
|
|
|
|
|
m_activeWindowTimer->setInterval(ACTIVE_WINDOW_UPDATE_INVERVAL);
|
|
|
|
|
connect(m_activeWindowTimer, &QTimer::timeout, this, &WindowUtil::updateActiveWindow);
|
|
|
|
|
|
2022-04-06 21:59:36 +00:00
|
|
|
connect(this, &WindowUtil::activeWindowChanged, this, &WindowUtil::updateActiveWindowIsShell);
|
|
|
|
|
|
2022-04-06 21:18:20 +00:00
|
|
|
initWayland();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WindowUtil *WindowUtil::instance()
|
|
|
|
|
{
|
|
|
|
|
static WindowUtil *inst = new WindowUtil();
|
|
|
|
|
return inst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WindowUtil::isShowingDesktop() const
|
|
|
|
|
{
|
|
|
|
|
return m_showingDesktop;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 21:59:36 +00:00
|
|
|
bool WindowUtil::activeWindowIsShell() const
|
|
|
|
|
{
|
|
|
|
|
return m_activeWindowIsShell;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 21:18:20 +00:00
|
|
|
void WindowUtil::initWayland()
|
|
|
|
|
{
|
|
|
|
|
if (!QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) {
|
2022-04-11 20:19:16 +00:00
|
|
|
qWarning() << "Plasma Mobile must use wayland! The current platform detected is:" << QGuiApplication::platformName();
|
2022-04-06 21:18:20 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using namespace KWayland::Client;
|
|
|
|
|
ConnectionThread *connection = ConnectionThread::fromApplication(this);
|
|
|
|
|
|
|
|
|
|
if (!connection) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto *registry = new Registry(this);
|
|
|
|
|
registry->create(connection);
|
|
|
|
|
|
|
|
|
|
connect(registry, &Registry::plasmaWindowManagementAnnounced, this, [this, registry](quint32 name, quint32 version) {
|
|
|
|
|
m_windowManagement = registry->createPlasmaWindowManagement(name, version, this);
|
|
|
|
|
qRegisterMetaType<QVector<int>>("QVector<int>");
|
|
|
|
|
|
|
|
|
|
connect(m_windowManagement, &KWayland::Client::PlasmaWindowManagement::windowCreated, this, [this](KWayland::Client::PlasmaWindow *window) {
|
|
|
|
|
Q_EMIT windowCreated(window);
|
|
|
|
|
});
|
2022-10-12 21:00:21 +00:00
|
|
|
connect(m_windowManagement, &KWayland::Client::PlasmaWindowManagement::windowCreated, this, &WindowUtil::windowCreatedSlot);
|
2022-04-06 21:18:20 +00:00
|
|
|
|
|
|
|
|
connect(m_windowManagement, &PlasmaWindowManagement::showingDesktopChanged, this, &WindowUtil::updateShowingDesktop);
|
|
|
|
|
connect(m_windowManagement, &PlasmaWindowManagement::activeWindowChanged, m_activeWindowTimer, qOverload<>(&QTimer::start));
|
|
|
|
|
|
|
|
|
|
m_activeWindowTimer->start();
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-19 01:48:49 +00:00
|
|
|
connect(registry, &Registry::plasmaActivationFeedbackAnnounced, this, [this, registry](quint32 name, quint32 version) {
|
|
|
|
|
auto iface = registry->createPlasmaActivationFeedback(name, version, this);
|
|
|
|
|
|
|
|
|
|
connect(iface, &PlasmaActivationFeedback::activation, this, [this](PlasmaActivation *activation) {
|
|
|
|
|
connect(activation, &PlasmaActivation::applicationId, this, [this](const QString &appId) {
|
2023-03-19 02:01:48 +00:00
|
|
|
// do not show activation screen for the plasmashell process
|
|
|
|
|
if (appId == "org.kde.plasmashell") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-19 01:48:49 +00:00
|
|
|
const auto servicesFound = KApplicationTrader::query([&appId](const KService::Ptr &service) {
|
|
|
|
|
if (service->exec().isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (service->desktopEntryName().compare(appId, Qt::CaseInsensitive) == 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
const auto idWithoutDesktop = QString(appId).remove(QStringLiteral(".desktop"));
|
|
|
|
|
if (service->desktopEntryName().compare(idWithoutDesktop, Qt::CaseInsensitive) == 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
const auto renamedFrom = service->property(QStringLiteral("X-Flatpak-RenamedFrom")).toStringList();
|
|
|
|
|
if (renamedFrom.contains(appId, Qt::CaseInsensitive) || renamedFrom.contains(idWithoutDesktop, Qt::CaseInsensitive))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!servicesFound.isEmpty()) {
|
|
|
|
|
Q_EMIT appActivationStarted(appId, servicesFound.constFirst()->icon());
|
|
|
|
|
} else {
|
|
|
|
|
qDebug() << "WindowUtil: Could not find service" << appId;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connect(activation, &PlasmaActivation::finished, this, &WindowUtil::appActivationFinished);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-06 21:18:20 +00:00
|
|
|
registry->setup();
|
|
|
|
|
connection->roundtrip();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowUtil::updateActiveWindow()
|
|
|
|
|
{
|
|
|
|
|
if (!m_windowManagement || m_activeWindow == m_windowManagement->activeWindow()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using namespace KWayland::Client;
|
|
|
|
|
if (m_activeWindow) {
|
|
|
|
|
disconnect(m_activeWindow.data(), &PlasmaWindow::closeableChanged, this, &WindowUtil::hasCloseableActiveWindowChanged);
|
|
|
|
|
disconnect(m_activeWindow.data(), &PlasmaWindow::unmapped, this, &WindowUtil::forgetActiveWindow);
|
|
|
|
|
}
|
2023-03-19 01:48:49 +00:00
|
|
|
|
2022-04-06 21:18:20 +00:00
|
|
|
m_activeWindow = m_windowManagement->activeWindow();
|
2022-04-06 21:59:36 +00:00
|
|
|
Q_EMIT activeWindowChanged();
|
2022-04-06 21:18:20 +00:00
|
|
|
|
|
|
|
|
if (m_activeWindow) {
|
|
|
|
|
connect(m_activeWindow.data(), &PlasmaWindow::closeableChanged, this, &WindowUtil::hasCloseableActiveWindowChanged);
|
|
|
|
|
connect(m_activeWindow.data(), &PlasmaWindow::unmapped, this, &WindowUtil::forgetActiveWindow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Q_EMIT hasCloseableActiveWindowChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WindowUtil::hasCloseableActiveWindow() const
|
|
|
|
|
{
|
|
|
|
|
return m_activeWindow && m_activeWindow->isCloseable() /*&& !m_activeWindow->isMinimized()*/;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 06:29:46 +00:00
|
|
|
bool WindowUtil::activateWindowByStorageId(const QString &storageId)
|
|
|
|
|
{
|
|
|
|
|
auto windows = windowsFromStorageId(storageId);
|
|
|
|
|
|
|
|
|
|
if (!windows.empty()) {
|
|
|
|
|
windows[0]->requestActivate();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 21:18:20 +00:00
|
|
|
void WindowUtil::closeActiveWindow()
|
|
|
|
|
{
|
|
|
|
|
if (m_activeWindow) {
|
|
|
|
|
m_activeWindow->requestClose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowUtil::requestShowingDesktop(bool showingDesktop)
|
|
|
|
|
{
|
|
|
|
|
if (!m_windowManagement) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_windowManagement->setShowingDesktop(showingDesktop);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 20:47:20 +00:00
|
|
|
void WindowUtil::minimizeAll()
|
2022-04-11 20:19:16 +00:00
|
|
|
{
|
|
|
|
|
if (!m_windowManagement) {
|
|
|
|
|
qWarning() << "Ignoring request for minimizing all windows since window management hasn't been announced yet!";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto *w : m_windowManagement->windows()) {
|
|
|
|
|
if (!w->isMinimized()) {
|
|
|
|
|
w->requestToggleMinimized();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 20:47:20 +00:00
|
|
|
void WindowUtil::unsetAllMinimizedGeometries(QQuickItem *parent)
|
|
|
|
|
{
|
|
|
|
|
if (!m_windowManagement) {
|
|
|
|
|
qWarning() << "Ignoring request for minimizing all windows since window management hasn't been announced yet!";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!parent) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWindow *window = parent->window();
|
|
|
|
|
if (!window) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KWayland::Client::Surface *surface = KWayland::Client::Surface::fromWindow(window);
|
|
|
|
|
if (!surface) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto *w : m_windowManagement->windows()) {
|
|
|
|
|
w->unsetMinimizedGeometry(surface);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 21:18:20 +00:00
|
|
|
void WindowUtil::updateShowingDesktop(bool showing)
|
|
|
|
|
{
|
|
|
|
|
if (showing != m_showingDesktop) {
|
|
|
|
|
m_showingDesktop = showing;
|
|
|
|
|
Q_EMIT showingDesktopChanged(m_showingDesktop);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 21:59:36 +00:00
|
|
|
void WindowUtil::updateActiveWindowIsShell()
|
|
|
|
|
{
|
2023-03-19 01:48:49 +00:00
|
|
|
auto activeWindow = m_windowManagement->activeWindow();
|
|
|
|
|
if (activeWindow) {
|
|
|
|
|
if (activeWindow->appId() == QStringLiteral("org.kde.plasmashell") && !m_activeWindowIsShell) {
|
2022-04-06 21:59:36 +00:00
|
|
|
m_activeWindowIsShell = true;
|
|
|
|
|
Q_EMIT activeWindowIsShellChanged();
|
2023-03-19 01:48:49 +00:00
|
|
|
} else if (activeWindow->appId() != QStringLiteral("org.kde.plasmashell") && m_activeWindowIsShell) {
|
2022-04-06 21:59:36 +00:00
|
|
|
m_activeWindowIsShell = false;
|
|
|
|
|
Q_EMIT activeWindowIsShellChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 21:18:20 +00:00
|
|
|
void WindowUtil::forgetActiveWindow()
|
|
|
|
|
{
|
|
|
|
|
using namespace KWayland::Client;
|
|
|
|
|
if (m_activeWindow) {
|
|
|
|
|
disconnect(m_activeWindow.data(), &PlasmaWindow::closeableChanged, this, &WindowUtil::hasCloseableActiveWindowChanged);
|
|
|
|
|
disconnect(m_activeWindow.data(), &PlasmaWindow::unmapped, this, &WindowUtil::forgetActiveWindow);
|
|
|
|
|
}
|
|
|
|
|
m_activeWindow.clear();
|
|
|
|
|
Q_EMIT hasCloseableActiveWindowChanged();
|
|
|
|
|
}
|
2022-10-12 21:00:21 +00:00
|
|
|
|
|
|
|
|
QList<KWayland::Client::PlasmaWindow *> WindowUtil::windowsFromStorageId(const QString &storageId) const
|
|
|
|
|
{
|
|
|
|
|
if (!m_windows.contains(storageId)) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
return m_windows[storageId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowUtil::windowCreatedSlot(KWayland::Client::PlasmaWindow *window)
|
|
|
|
|
{
|
|
|
|
|
QString storageId = window->appId() + QStringLiteral(".desktop");
|
|
|
|
|
|
|
|
|
|
// ignore empty windows
|
|
|
|
|
if (storageId == ".desktop" || storageId == "org.kde.plasmashell.desktop") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_windows.contains(storageId)) {
|
|
|
|
|
m_windows[storageId] = {};
|
|
|
|
|
}
|
|
|
|
|
m_windows[storageId].push_back(window);
|
|
|
|
|
|
|
|
|
|
// listen for window close
|
|
|
|
|
connect(window, &KWayland::Client::PlasmaWindow::unmapped, this, [this, storageId]() {
|
|
|
|
|
m_windows.remove(storageId);
|
|
|
|
|
Q_EMIT windowChanged(storageId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Q_EMIT windowChanged(storageId);
|
|
|
|
|
}
|