diff --git a/colorcorrection/manager.h b/colorcorrection/manager.h --- a/colorcorrection/manager.h +++ b/colorcorrection/manager.h @@ -32,7 +32,7 @@ namespace KWin { -class Platform; +class Workspace; namespace ColorCorrect { @@ -74,6 +74,24 @@ bool changeConfiguration(QHash data); void autoLocationUpdate(double latitude, double longitude); + /** + * Toggles the active state of the filter. + * + * A quick transition will be started if the difference between current screen + * color temperature and target screen color temperature is too large. Target + * temperature is defined in context of the new active state. + * + * If the filter becomes inactive after calling this method, the target color + * temperature is 6500 K. + * + * If the filter becomes active after calling this method, the target screen + * color temperature is defined by the current operation mode. + * + * Note that this method is a no-op if the underlying platform doesn't support + * adjusting gamma ramps. + **/ + void toggle(); + // for auto tests void reparseConfigAndReset(); @@ -85,6 +103,7 @@ void configChange(QHash data); private: + void initShortcuts(); void readConfig(); void hardReset(); void slowUpdate(int targetTemp); @@ -139,6 +158,9 @@ int m_nightTargetTemp = DEFAULT_NIGHT_TEMPERATURE; int m_failedCommitAttempts = 0; + + // The Workspace class needs to call initShortcuts during initialization. + friend class KWin::Workspace; }; } diff --git a/colorcorrection/manager.cpp b/colorcorrection/manager.cpp --- a/colorcorrection/manager.cpp +++ b/colorcorrection/manager.cpp @@ -31,9 +31,13 @@ #include -#include +#include +#include + +#include #include #include +#include #ifdef Q_OS_LINUX #include @@ -152,6 +156,53 @@ hardReset(); } +// FIXME: The internal OSD service doesn't work on X11 right now. Once the QPA +// is ported away from Wayland, drop this function in favor of the internal +// OSD service. +static void showStatusOsd(bool enabled) +{ + // TODO: Maybe use different icons? + const QString iconName = enabled + ? QStringLiteral("preferences-desktop-display-nightcolor-on") + : QStringLiteral("preferences-desktop-display-nightcolor-off"); + + const QString text = enabled + ? i18nc("Night Color was enabled", "Night Color On") + : i18nc("Night Color was disabled", "Night Color Off"); + + QDBusMessage message = QDBusMessage::createMethodCall( + QStringLiteral("org.kde.plasmashell"), + QStringLiteral("/org/kde/osdService"), + QStringLiteral("org.kde.osdService"), + QStringLiteral("showText")); + message.setArguments({ iconName, text }); + + QDBusConnection::sessionBus().asyncCall(message); +} + +void Manager::toggle() +{ + if (!kwinApp()->platform()->supportsGammaControl()) { + return; + } + + m_active = !m_active; + + showStatusOsd(m_active); + + resetAllTimers(); +} + +void Manager::initShortcuts() +{ + QAction *toggleAction = new QAction(this); + toggleAction->setProperty("componentName", QStringLiteral(KWIN_NAME)); + toggleAction->setObjectName(i18n("Toggle Night Color")); + toggleAction->setText(i18n("Toggle Night Color")); + KGlobalAccel::setGlobalShortcut(toggleAction, QList()); + input()->registerShortcut(QKeySequence(), toggleAction, this, &Manager::toggle); +} + void Manager::readConfig() { Settings *s = Settings::self(); diff --git a/useractions.cpp b/useractions.cpp --- a/useractions.cpp +++ b/useractions.cpp @@ -34,6 +34,7 @@ #include "useractions.h" #include "cursor.h" #include "client.h" +#include "colorcorrection/manager.h" #include "composite.h" #include "input.h" #include "workspace.h" @@ -1116,6 +1117,7 @@ TabBox::TabBox::self()->initShortcuts(); #endif VirtualDesktopManager::self()->initShortcuts(); + kwinApp()->platform()->colorCorrectManager()->initShortcuts(); m_userActionsMenu->discard(); // so that it's recreated next time }