Verified Commit 013ad033 authored by Fushan Wen's avatar Fushan Wen 💬
Browse files

wallpapers/image: add `MaximizedWindowMonitor` to monitor maximized window

This class monitors if there is any maximized window. It is used by
the animated image component.
parent 189ff0c1
......@@ -16,6 +16,7 @@ set(image_SRCS
model/imageproxymodel.cpp
provider/packageimageprovider.cpp
provider/providertype.h
utils/maximizedwindowmonitor.cpp
utils/mediaproxy.cpp
)
......@@ -38,6 +39,7 @@ target_link_libraries(plasma_wallpaper_imageplugin_static
KF5::KIOGui
KF5::NewStuff
KF5::Notifications
PW::LibTaskManager
)
add_library(plasma_wallpaper_imageplugin SHARED imageplugin.cpp)
......
......@@ -6,12 +6,14 @@
#include "imageplugin.h"
#include <QQmlContext>
#include <QScreen>
#include <KFileItem>
#include "imagebackend.h"
#include "provider/packageimageprovider.h"
#include "sortingmode.h"
#include "utils/maximizedwindowmonitor.h"
#include "utils/mediaproxy.h"
const auto pluginName = QByteArrayLiteral("org.kde.plasma.wallpapers.image");
......@@ -32,6 +34,8 @@ void ImagePlugin::registerTypes(const char *uri)
qmlRegisterType<ImageBackend>(uri, 2, 0, "ImageBackend");
qmlRegisterType<MediaProxy>(uri, 2, 0, "MediaProxy");
qmlRegisterType<MaximizedWindowMonitor>(uri, 2, 0, "MaximizedWindowMonitor");
qmlRegisterAnonymousType<QAbstractItemModel>("QAbstractItemModel", 1);
qmlRegisterUncreatableType<SortingMode>(uri, 2, 0, "SortingMode", QStringLiteral("error: only enums"));
}
/*
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "maximizedwindowmonitor.h"
#include <QGuiApplication>
#include <QScreen>
#include "abstracttasksmodel.h" // For enums
#include "activityinfo.h"
#include "virtualdesktopinfo.h"
#include "debug.h"
class MaximizedWindowMonitor::Private
{
public:
explicit Private(MaximizedWindowMonitor *q);
void init();
private:
Q_DISABLE_COPY(Private);
MaximizedWindowMonitor *q = nullptr;
bool m_ready = false;
TaskManager::ActivityInfo *m_activityInfo = nullptr;
TaskManager::VirtualDesktopInfo *m_virtualDesktopInfo = nullptr;
QRect m_geometry;
friend class MaximizedWindowMonitor;
};
MaximizedWindowMonitor::Private::Private(MaximizedWindowMonitor *q)
: q(q)
, m_activityInfo(new TaskManager::ActivityInfo(q))
, m_virtualDesktopInfo(new TaskManager::VirtualDesktopInfo(q))
{
}
void MaximizedWindowMonitor::Private::init()
{
q->setSortMode(SortMode::SortActivity);
q->setGroupMode(GroupMode::GroupDisabled);
q->setActivity(m_activityInfo->currentActivity());
q->connect(m_activityInfo,
&TaskManager::ActivityInfo::currentActivityChanged,
q,
std::bind(&MaximizedWindowMonitor::setActivity, q, m_activityInfo->currentActivity()));
q->setVirtualDesktop(m_virtualDesktopInfo->currentDesktop());
q->connect(m_virtualDesktopInfo,
&TaskManager::VirtualDesktopInfo::currentDesktopChanged,
q,
std::bind(&MaximizedWindowMonitor::setVirtualDesktop, q, m_virtualDesktopInfo->currentDesktop()));
q->setFilterByActivity(true);
q->setFilterByVirtualDesktop(true);
}
MaximizedWindowMonitor::MaximizedWindowMonitor(QObject *parent)
: TaskManager::TasksModel(parent)
, d(std::make_unique<Private>(this))
{
d->init();
}
MaximizedWindowMonitor::~MaximizedWindowMonitor()
{
}
QRect MaximizedWindowMonitor::targetRect() const
{
return d->m_geometry;
}
void MaximizedWindowMonitor::setTargetRect(const QRect &rect)
{
if (d->m_geometry == rect) {
return;
}
d->m_geometry = rect;
Q_EMIT targetRectChanged();
invalidateFilter();
}
bool MaximizedWindowMonitor::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
const QModelIndex sourceIndex = sourceModel()->index(sourceRow, 0);
// Filter out minimized "maximized" windows
if (sourceIndex.data(TaskManager::AbstractTasksModel::IsMinimized).toBool()) {
return false;
}
if (!TaskManager::TasksModel::filterAcceptsRow(sourceRow, sourceParent)) {
return false;
}
// Filter windows on the screen where the wallpaper plugin is on
const QRect windowGeometry = sourceIndex.data(TaskManager::AbstractTasksModel::Geometry).toRect();
// Use intersects so the geometry doesn't need be multiplied by devicePixelRatio
if (!d->m_geometry.intersects(windowGeometry)) {
return false;
}
if (sourceIndex.data(TaskManager::AbstractTasksModel::IsMaximized).toBool() || sourceIndex.data(TaskManager::AbstractTasksModel::IsFullScreen).toBool()) {
return true;
}
return false;
}
/*
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef MAXIMIZEDWINDOWMONITOR_H
#define MAXIMIZEDWINDOWMONITOR_H
#include <memory>
#include <QObject>
#include "tasksmodel.h"
class QRect;
/**
* This class monitors if there is any maximized or fullscreen window.
* It is used by the animated image component.
*/
class MaximizedWindowMonitor : public TaskManager::TasksModel
{
Q_OBJECT
Q_PROPERTY(QRect targetRect READ targetRect WRITE setTargetRect NOTIFY targetRectChanged)
public:
explicit MaximizedWindowMonitor(QObject *parent = nullptr);
~MaximizedWindowMonitor();
QRect targetRect() const;
void setTargetRect(const QRect &rect);
Q_SIGNALS:
void targetRectChanged();
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
private:
class Private;
std::unique_ptr<Private> d;
};
#endif // MAXIMIZEDWINDOWMONITOR_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment