kwin/onscreennotification.cpp

231 lines
5.5 KiB
C++
Raw Normal View History

Introduce a KWin internal on-screen-notification service Summary: Recently we noticed that there are multiple areas where KWin needs to inform the user about how to operate. Examples are: * Screenshot * ColorPicker * Pointer constraint enabled * Pointer constraint about to be removed * Kill Window For Screenshot and ColorPicker we used an EffectFrame to render it. But this is not an optimal solution as it's lacking many features we would need. We cannot properly use it from within KWin core, we cannot implement features like hide on mouse over, etc. etc. This change introduces an OnScreenNotification which supports: * showing an icon * showing a message * timeout It is Qml styled, so that it can be easily adjusted. This is a big improvement over the EffectFrame solution. The Qml file creates a Plasma Dialog of type OSD. Thus KWin places it like the normal OSD windows and also looks kind of similar. In the case of KWin the focus is more on the message, than an icon, so the icon is placed left of the text. While the OnScreenNotification is supposed to be used like a singleton, it doesn't use the KWin singleton pattern. Instead a small wrapper namespace OSD is introduced which provides a convenient API for KWin internal areas to show/hide the notification. By not using the KWin singleton pattern, the OnScreenNotification does not depend on any other parts of KWin and can be easily unit-tested. A few features are still missing and will be added in further commits: * hide-out on mouse over * optional skip close animation (needed for screenshot) * X11 support (not that important as it's mostly for Wayland features) Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D3723
2016-12-18 10:53:50 +00:00
/*
2020-08-02 22:22:19 +00:00
SPDX-FileCopyrightText: 2016 Martin Graesslin <mgraesslin@kde.org>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
Introduce a KWin internal on-screen-notification service Summary: Recently we noticed that there are multiple areas where KWin needs to inform the user about how to operate. Examples are: * Screenshot * ColorPicker * Pointer constraint enabled * Pointer constraint about to be removed * Kill Window For Screenshot and ColorPicker we used an EffectFrame to render it. But this is not an optimal solution as it's lacking many features we would need. We cannot properly use it from within KWin core, we cannot implement features like hide on mouse over, etc. etc. This change introduces an OnScreenNotification which supports: * showing an icon * showing a message * timeout It is Qml styled, so that it can be easily adjusted. This is a big improvement over the EffectFrame solution. The Qml file creates a Plasma Dialog of type OSD. Thus KWin places it like the normal OSD windows and also looks kind of similar. In the case of KWin the focus is more on the message, than an icon, so the icon is placed left of the text. While the OnScreenNotification is supposed to be used like a singleton, it doesn't use the KWin singleton pattern. Instead a small wrapper namespace OSD is introduced which provides a convenient API for KWin internal areas to show/hide the notification. By not using the KWin singleton pattern, the OnScreenNotification does not depend on any other parts of KWin and can be easily unit-tested. A few features are still missing and will be added in further commits: * hide-out on mouse over * optional skip close animation (needed for screenshot) * X11 support (not that important as it's mostly for Wayland features) Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D3723
2016-12-18 10:53:50 +00:00
#include "onscreennotification.h"
#include "input.h"
#include "input_event.h"
#include "input_event_spy.h"
Introduce a KWin internal on-screen-notification service Summary: Recently we noticed that there are multiple areas where KWin needs to inform the user about how to operate. Examples are: * Screenshot * ColorPicker * Pointer constraint enabled * Pointer constraint about to be removed * Kill Window For Screenshot and ColorPicker we used an EffectFrame to render it. But this is not an optimal solution as it's lacking many features we would need. We cannot properly use it from within KWin core, we cannot implement features like hide on mouse over, etc. etc. This change introduces an OnScreenNotification which supports: * showing an icon * showing a message * timeout It is Qml styled, so that it can be easily adjusted. This is a big improvement over the EffectFrame solution. The Qml file creates a Plasma Dialog of type OSD. Thus KWin places it like the normal OSD windows and also looks kind of similar. In the case of KWin the focus is more on the message, than an icon, so the icon is placed left of the text. While the OnScreenNotification is supposed to be used like a singleton, it doesn't use the KWin singleton pattern. Instead a small wrapper namespace OSD is introduced which provides a convenient API for KWin internal areas to show/hide the notification. By not using the KWin singleton pattern, the OnScreenNotification does not depend on any other parts of KWin and can be easily unit-tested. A few features are still missing and will be added in further commits: * hide-out on mouse over * optional skip close animation (needed for screenshot) * X11 support (not that important as it's mostly for Wayland features) Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D3723
2016-12-18 10:53:50 +00:00
#include <config-kwin.h>
#include <QPropertyAnimation>
Introduce a KWin internal on-screen-notification service Summary: Recently we noticed that there are multiple areas where KWin needs to inform the user about how to operate. Examples are: * Screenshot * ColorPicker * Pointer constraint enabled * Pointer constraint about to be removed * Kill Window For Screenshot and ColorPicker we used an EffectFrame to render it. But this is not an optimal solution as it's lacking many features we would need. We cannot properly use it from within KWin core, we cannot implement features like hide on mouse over, etc. etc. This change introduces an OnScreenNotification which supports: * showing an icon * showing a message * timeout It is Qml styled, so that it can be easily adjusted. This is a big improvement over the EffectFrame solution. The Qml file creates a Plasma Dialog of type OSD. Thus KWin places it like the normal OSD windows and also looks kind of similar. In the case of KWin the focus is more on the message, than an icon, so the icon is placed left of the text. While the OnScreenNotification is supposed to be used like a singleton, it doesn't use the KWin singleton pattern. Instead a small wrapper namespace OSD is introduced which provides a convenient API for KWin internal areas to show/hide the notification. By not using the KWin singleton pattern, the OnScreenNotification does not depend on any other parts of KWin and can be easily unit-tested. A few features are still missing and will be added in further commits: * hide-out on mouse over * optional skip close animation (needed for screenshot) * X11 support (not that important as it's mostly for Wayland features) Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D3723
2016-12-18 10:53:50 +00:00
#include <QStandardPaths>
#include <QTimer>
#include <QQmlComponent>
#include <QQmlContext>
#include <QQmlEngine>
#include <QQuickWindow>
#include <KConfigGroup>
#include <functional>
using namespace KWin;
class KWin::OnScreenNotificationInputEventSpy : public InputEventSpy
{
public:
explicit OnScreenNotificationInputEventSpy(OnScreenNotification *parent);
void pointerEvent(MouseEvent *event) override;
private:
OnScreenNotification *m_parent;
};
OnScreenNotificationInputEventSpy::OnScreenNotificationInputEventSpy(OnScreenNotification *parent)
: m_parent(parent)
{
}
void OnScreenNotificationInputEventSpy::pointerEvent(MouseEvent *event)
{
if (event->type() != QEvent::MouseMove) {
return;
}
m_parent->setContainsPointer(m_parent->geometry().contains(event->globalPos()));
}
Introduce a KWin internal on-screen-notification service Summary: Recently we noticed that there are multiple areas where KWin needs to inform the user about how to operate. Examples are: * Screenshot * ColorPicker * Pointer constraint enabled * Pointer constraint about to be removed * Kill Window For Screenshot and ColorPicker we used an EffectFrame to render it. But this is not an optimal solution as it's lacking many features we would need. We cannot properly use it from within KWin core, we cannot implement features like hide on mouse over, etc. etc. This change introduces an OnScreenNotification which supports: * showing an icon * showing a message * timeout It is Qml styled, so that it can be easily adjusted. This is a big improvement over the EffectFrame solution. The Qml file creates a Plasma Dialog of type OSD. Thus KWin places it like the normal OSD windows and also looks kind of similar. In the case of KWin the focus is more on the message, than an icon, so the icon is placed left of the text. While the OnScreenNotification is supposed to be used like a singleton, it doesn't use the KWin singleton pattern. Instead a small wrapper namespace OSD is introduced which provides a convenient API for KWin internal areas to show/hide the notification. By not using the KWin singleton pattern, the OnScreenNotification does not depend on any other parts of KWin and can be easily unit-tested. A few features are still missing and will be added in further commits: * hide-out on mouse over * optional skip close animation (needed for screenshot) * X11 support (not that important as it's mostly for Wayland features) Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D3723
2016-12-18 10:53:50 +00:00
OnScreenNotification::OnScreenNotification(QObject *parent)
: QObject(parent)
, m_timer(new QTimer(this))
{
m_timer->setSingleShot(true);
connect(m_timer, &QTimer::timeout, this, std::bind(&OnScreenNotification::setVisible, this, false));
connect(this, &OnScreenNotification::visibleChanged, this,
[this] {
if (m_visible) {
show();
} else {
m_timer->stop();
m_spy.reset();
m_containsPointer = false;
Introduce a KWin internal on-screen-notification service Summary: Recently we noticed that there are multiple areas where KWin needs to inform the user about how to operate. Examples are: * Screenshot * ColorPicker * Pointer constraint enabled * Pointer constraint about to be removed * Kill Window For Screenshot and ColorPicker we used an EffectFrame to render it. But this is not an optimal solution as it's lacking many features we would need. We cannot properly use it from within KWin core, we cannot implement features like hide on mouse over, etc. etc. This change introduces an OnScreenNotification which supports: * showing an icon * showing a message * timeout It is Qml styled, so that it can be easily adjusted. This is a big improvement over the EffectFrame solution. The Qml file creates a Plasma Dialog of type OSD. Thus KWin places it like the normal OSD windows and also looks kind of similar. In the case of KWin the focus is more on the message, than an icon, so the icon is placed left of the text. While the OnScreenNotification is supposed to be used like a singleton, it doesn't use the KWin singleton pattern. Instead a small wrapper namespace OSD is introduced which provides a convenient API for KWin internal areas to show/hide the notification. By not using the KWin singleton pattern, the OnScreenNotification does not depend on any other parts of KWin and can be easily unit-tested. A few features are still missing and will be added in further commits: * hide-out on mouse over * optional skip close animation (needed for screenshot) * X11 support (not that important as it's mostly for Wayland features) Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D3723
2016-12-18 10:53:50 +00:00
}
}
);
}
OnScreenNotification::~OnScreenNotification()
{
if (QQuickWindow *w = qobject_cast<QQuickWindow*>(m_mainItem.data())) {
w->hide();
w->destroy();
}
}
void OnScreenNotification::setConfig(KSharedConfigPtr config)
{
m_config = config;
}
void OnScreenNotification::setEngine(QQmlEngine *engine)
{
m_qmlEngine = engine;
}
bool OnScreenNotification::isVisible() const
{
return m_visible;
}
void OnScreenNotification::setVisible(bool visible)
{
if (m_visible == visible) {
return;
}
m_visible = visible;
emit visibleChanged();
}
QString OnScreenNotification::message() const
{
return m_message;
}
void OnScreenNotification::setMessage(const QString &message)
{
if (m_message == message) {
return;
}
m_message = message;
emit messageChanged();
}
QString OnScreenNotification::iconName() const
{
return m_iconName;
}
void OnScreenNotification::setIconName(const QString &iconName)
{
if (m_iconName == iconName) {
return;
}
m_iconName = iconName;
emit iconNameChanged();
}
int OnScreenNotification::timeout() const
{
return m_timer->interval();
}
void OnScreenNotification::setTimeout(int timeout)
{
if (m_timer->interval() == timeout) {
return;
}
m_timer->setInterval(timeout);
emit timeoutChanged();
}
void OnScreenNotification::show()
{
Q_ASSERT(m_visible);
ensureQmlContext();
ensureQmlComponent();
createInputSpy();
Introduce a KWin internal on-screen-notification service Summary: Recently we noticed that there are multiple areas where KWin needs to inform the user about how to operate. Examples are: * Screenshot * ColorPicker * Pointer constraint enabled * Pointer constraint about to be removed * Kill Window For Screenshot and ColorPicker we used an EffectFrame to render it. But this is not an optimal solution as it's lacking many features we would need. We cannot properly use it from within KWin core, we cannot implement features like hide on mouse over, etc. etc. This change introduces an OnScreenNotification which supports: * showing an icon * showing a message * timeout It is Qml styled, so that it can be easily adjusted. This is a big improvement over the EffectFrame solution. The Qml file creates a Plasma Dialog of type OSD. Thus KWin places it like the normal OSD windows and also looks kind of similar. In the case of KWin the focus is more on the message, than an icon, so the icon is placed left of the text. While the OnScreenNotification is supposed to be used like a singleton, it doesn't use the KWin singleton pattern. Instead a small wrapper namespace OSD is introduced which provides a convenient API for KWin internal areas to show/hide the notification. By not using the KWin singleton pattern, the OnScreenNotification does not depend on any other parts of KWin and can be easily unit-tested. A few features are still missing and will be added in further commits: * hide-out on mouse over * optional skip close animation (needed for screenshot) * X11 support (not that important as it's mostly for Wayland features) Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D3723
2016-12-18 10:53:50 +00:00
if (m_timer->interval() != 0) {
m_timer->start();
}
}
void OnScreenNotification::ensureQmlContext()
{
Q_ASSERT(m_qmlEngine);
if (!m_qmlContext.isNull()) {
return;
}
m_qmlContext.reset(new QQmlContext(m_qmlEngine));
m_qmlContext->setContextProperty(QStringLiteral("osd"), this);
}
void OnScreenNotification::ensureQmlComponent()
{
Q_ASSERT(m_config);
Q_ASSERT(m_qmlEngine);
if (!m_qmlComponent.isNull()) {
return;
}
m_qmlComponent.reset(new QQmlComponent(m_qmlEngine));
const QString fileName = QStandardPaths::locate(QStandardPaths::GenericDataLocation,
m_config->group(QStringLiteral("OnScreenNotification")).readEntry("QmlPath", QStringLiteral(KWIN_NAME "/onscreennotification/plasma/main.qml")));
if (fileName.isEmpty()) {
return;
}
m_qmlComponent->loadUrl(QUrl::fromLocalFile(fileName));
if (!m_qmlComponent->isError()) {
m_mainItem.reset(m_qmlComponent->create(m_qmlContext.data()));
} else {
m_qmlComponent.reset();
}
}
void OnScreenNotification::createInputSpy()
{
Q_ASSERT(m_spy.isNull());
if (auto w = qobject_cast<QQuickWindow*>(m_mainItem.data())) {
m_spy.reset(new OnScreenNotificationInputEventSpy(this));
input()->installInputEventSpy(m_spy.data());
if (!m_animation) {
m_animation = new QPropertyAnimation(w, "opacity", this);
m_animation->setStartValue(1.0);
m_animation->setEndValue(0.0);
m_animation->setDuration(250);
m_animation->setEasingCurve(QEasingCurve::InOutCubic);
}
}
}
QRect OnScreenNotification::geometry() const
{
if (QQuickWindow *w = qobject_cast<QQuickWindow*>(m_mainItem.data())) {
return w->geometry();
}
return QRect();
}
void OnScreenNotification::setContainsPointer(bool contains)
{
if (m_containsPointer == contains) {
return;
}
m_containsPointer = contains;
if (!m_animation) {
return;
}
m_animation->setDirection(m_containsPointer ? QAbstractAnimation::Forward : QAbstractAnimation::Backward);
m_animation->start();
}
void OnScreenNotification::setSkipCloseAnimation(bool skip)
{
if (QQuickWindow *w = qobject_cast<QQuickWindow*>(m_mainItem.data())) {
w->setProperty("KWIN_SKIP_CLOSE_ANIMATION", skip);
}
}