Commit 5165ee45 authored by Vlad Zahorodnii's avatar Vlad Zahorodnii
Browse files

[effects] Rewrite the Dim Inactive effect

Summary:
The Dim Inactive effect was rewritten mostly to fix most of issues with
it, e.g. after leaving a full screen effect(e.g. Desktop Grid) windows
sometimes are not dimmed back, or when a window becomes inactive there
is no smooth transition, etc.

{F5956124}
//Before: the window is not smoothly dimmed.//

{F5956127}
//After: the window is smoothly dimmed.//

In combination with an effect that animates the disappearing of windows,
e.g. Glide, the rewritten Dim Inactive effect doesn't "flash" windows.
If an active window has been closed, it will stay bright. If an inactive
window has been closed, it will stay dimmed.

Among other changes, the KCM has been re-designed to follow common KCM
design in Plasma:

{F5956128, layout=center, size=full}

The way the rewritten Dim Inactive effect handles flashing/flickering problem can be
reused in the Dialog Parent effect.

### Demo

{F5959885}
//Before: dimming of a window group.//

{F5959886}
//After: Dimming of a window group.//

Depends on D13740

CCBUG: 359251

Test Plan:
Test plan #1
* Activated the Desktop Grid effect
* Dimmed windows smoothly brightened
* Left desktop grid
* Windows dimmed back

Test plan #2
* Opened Dolphin and its Preferences window
* Clicked on desktop, both Dolphin and the Preferences window dimmed
* Clicked on Dolphin, both windows smoothly brightened back

Reviewers: #kwin, #plasma, #vdg, davidedmundson

Reviewed By: #kwin, #plasma, #vdg, davidedmundson

Subscribers: davidedmundson, abetts, ngraham, kwin

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D13720
parent e3815aea
......@@ -4,6 +4,7 @@
Copyright (C) 2007 Lubos Lunak <l.lunak@kde.org>
Copyright (C) 2007 Christian Nitschkowski <christian.nitschkowski@kdemail.net>
Copyright (C) 2018 Vlad Zagorodniy <vladzzag@gmail.com>
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
......@@ -19,124 +20,371 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
// own
#include "diminactive.h"
// KConfigSkeleton
#include "diminactiveconfig.h"
#include <kconfiggroup.h>
namespace KWin
{
/**
* Checks if two windows belong to the same window group
*
* One possible example of a window group is an app window and app
* preferences window(e.g. Dolphin window and Dolphin Preferences window).
*
* @param w1 The first window
* @param w2 The second window
* @returns @c true if both windows belong to the same window group, @c false otherwise
**/
static inline bool belongToSameGroup(const EffectWindow *w1, const EffectWindow *w2)
{
return w1 && w2 && w1->group() && w1->group() == w2->group();
}
DimInactiveEffect::DimInactiveEffect()
{
m_activeWindow = nullptr;
initConfig<DimInactiveConfig>();
reconfigure(ReconfigureAll);
timeline.setDuration(animationTime(250));
previousActiveTimeline.setDuration(animationTime(250));
active = effects->activeWindow();
previousActive = NULL;
connect(effects, SIGNAL(windowActivated(KWin::EffectWindow*)), this, SLOT(slotWindowActivated(KWin::EffectWindow*)));
connect(effects, SIGNAL(windowDeleted(KWin::EffectWindow*)), this, SLOT(slotWindowDeleted(KWin::EffectWindow*)));
connect(effects, &EffectsHandler::windowActivated,
this, &DimInactiveEffect::windowActivated);
connect(effects, &EffectsHandler::windowClosed,
this, &DimInactiveEffect::windowClosed);
connect(effects, &EffectsHandler::windowDeleted,
this, &DimInactiveEffect::windowDeleted);
connect(effects, &EffectsHandler::activeFullScreenEffectChanged,
this, &DimInactiveEffect::activeFullScreenEffectChanged);
}
void DimInactiveEffect::reconfigure(ReconfigureFlags)
DimInactiveEffect::~DimInactiveEffect()
{
}
void DimInactiveEffect::reconfigure(ReconfigureFlags flags)
{
Q_UNUSED(flags)
DimInactiveConfig::self()->read();
dim_panels = DimInactiveConfig::dimPanels();
dim_desktop = DimInactiveConfig::dimDesktop();
dim_keepabove = DimInactiveConfig::dimKeepAbove();
dim_by_group = DimInactiveConfig::dimByGroup();
dim_strength = DimInactiveConfig::strength();
// TODO: Use normalized strength param.
m_dimStrength = DimInactiveConfig::strength() / 100.0;
m_dimPanels = DimInactiveConfig::dimPanels();
m_dimDesktop = DimInactiveConfig::dimDesktop();
m_dimKeepAbove = DimInactiveConfig::dimKeepAbove();
m_dimByGroup = DimInactiveConfig::dimByGroup();
EffectWindow *activeWindow = effects->activeWindow();
m_activeWindow = (activeWindow && canDimWindow(activeWindow))
? activeWindow
: nullptr;
m_activeWindowGroup = (m_dimByGroup && m_activeWindow)
? m_activeWindow->group()
: nullptr;
m_fullScreenTransition.timeLine.setDuration(
std::chrono::milliseconds(static_cast<int>(animationTime(250))));
effects->addRepaintFull();
}
void DimInactiveEffect::prePaintScreen(ScreenPrePaintData& data, int time)
void DimInactiveEffect::prePaintScreen(ScreenPrePaintData &data, int time)
{
double oldValue = timeline.currentValue();
if (effects->activeFullScreenEffect())
timeline.setCurrentTime(timeline.currentTime() - time);
else
timeline.setCurrentTime(timeline.currentTime() + time);
if (oldValue != timeline.currentValue())
effects->addRepaintFull();
if (previousActive) {
// We are fading out the previous window
previousActive->addRepaintFull();
previousActiveTimeline.setCurrentTime(previousActiveTimeline.currentTime() + time);
const std::chrono::milliseconds delta(time);
if (m_fullScreenTransition.active) {
m_fullScreenTransition.timeLine.update(delta);
}
auto transitionIt = m_transitions.begin();
while (transitionIt != m_transitions.end()) {
(*transitionIt).update(delta);
++transitionIt;
}
effects->prePaintScreen(data, time);
}
void DimInactiveEffect::paintWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data)
void DimInactiveEffect::paintWindow(EffectWindow *w, int mask, QRegion region, WindowPaintData &data)
{
if (dimWindow(w) || w == previousActive) {
double previous = 1.0;
if (w == previousActive)
previous = previousActiveTimeline.currentValue();
if (previousActiveTimeline.currentValue() == 1.0)
previousActive = NULL;
data.multiplyBrightness((1.0 - (dim_strength / 100.0) * timeline.currentValue() * previous));
data.multiplySaturation((1.0 - (dim_strength / 100.0) * timeline.currentValue() * previous));
auto transitionIt = m_transitions.constFind(w);
if (transitionIt != m_transitions.constEnd()) {
const qreal transitionProgress = (*transitionIt).value();
dimWindow(data, m_dimStrength * transitionProgress);
effects->paintWindow(w, mask, region, data);
return;
}
auto forceIt = m_forceDim.constFind(w);
if (forceIt != m_forceDim.constEnd()) {
const qreal forcedStrength = *forceIt;
dimWindow(data, forcedStrength);
effects->paintWindow(w, mask, region, data);
return;
}
if (canDimWindow(w)) {
dimWindow(data, m_dimStrength);
}
effects->paintWindow(w, mask, region, data);
}
bool DimInactiveEffect::dimWindow(const EffectWindow* w) const
{
if (effects->activeWindow() == w)
return false; // never dim active window
if (active && dim_by_group && active->group() == w->group())
return false; // don't dim in active group if configured so
if (w->isDock() && !dim_panels)
return false; // don't dim panels if configured so
if (w->isDesktop() && !dim_desktop)
return false; // don't dim the desktop if configured so
if (w->keepAbove() && !dim_keepabove)
return false; // don't dim keep-above windows if configured so
if (!w->isNormalWindow() && !w->isDialog() && !w->isDock() && !w->isDesktop())
return false; // don't dim more special window types
// don't dim unmanaged windows, grouping doesn't work for them and maybe dimming
// them doesn't make sense in general (they should be short-lived anyway)
if (!w->isManaged())
void DimInactiveEffect::postPaintScreen()
{
if (m_fullScreenTransition.active) {
if (m_fullScreenTransition.timeLine.done()) {
m_fullScreenTransition.active = false;
}
effects->addRepaintFull();
}
auto transitionIt = m_transitions.begin();
while (transitionIt != m_transitions.end()) {
EffectWindow *w = transitionIt.key();
if ((*transitionIt).done()) {
transitionIt = m_transitions.erase(transitionIt);
} else {
++transitionIt;
}
w->addRepaintFull();
}
effects->postPaintScreen();
}
void DimInactiveEffect::dimWindow(WindowPaintData &data, qreal strength)
{
qreal dimFactor;
if (m_fullScreenTransition.active) {
dimFactor = 1.0 - m_fullScreenTransition.timeLine.value();
} else if (effects->activeFullScreenEffect()) {
dimFactor = 0.0;
} else {
dimFactor = 1.0;
}
data.multiplyBrightness(1.0 - strength * dimFactor);
data.multiplySaturation(1.0 - strength * dimFactor);
}
bool DimInactiveEffect::canDimWindow(const EffectWindow *w) const
{
if (m_activeWindow == w) {
return false;
}
if (m_dimByGroup && belongToSameGroup(m_activeWindow, w)) {
return false;
return true; // dim the rest
}
if (w->isDock() && !m_dimPanels) {
return false;
}
if (w->isDesktop() && !m_dimDesktop) {
return false;
}
if (w->keepAbove() && !m_dimKeepAbove) {
return false;
}
if (!w->isManaged()) {
return false;
}
return w->isNormalWindow()
|| w->isDialog()
|| w->isUtility()
|| w->isDock()
|| w->isDesktop();
}
void DimInactiveEffect::slotWindowDeleted(EffectWindow* w)
void DimInactiveEffect::scheduleInTransition(EffectWindow *w)
{
if (w == previousActive)
previousActive = NULL;
TimeLine &timeLine = m_transitions[w];
timeLine.setDuration(
std::chrono::milliseconds(static_cast<int>(animationTime(160))));
if (timeLine.done()) {
// If the Out animation is still active, then we're trucating
// duration of the timeline(from 250ms to 160ms). If the timeline
// is about to be finished with the old duration, then after
// changing duration it will be in the "done" state. Thus, we
// have to reset the timeline, otherwise it won't update progress.
timeLine.reset();
}
timeLine.setDirection(TimeLine::Backward);
timeLine.setEasingCurve(QEasingCurve::InOutSine);
}
void DimInactiveEffect::slotWindowActivated(EffectWindow* w)
void DimInactiveEffect::scheduleGroupInTransition(EffectWindow *w)
{
if (active != NULL) {
previousActive = active;
previousActiveTimeline.setCurrentTime(0);
if (!dimWindow(previousActive))
previousActive = NULL;
if (!m_dimByGroup) {
scheduleInTransition(w);
return;
}
if (!w->group()) {
scheduleInTransition(w);
return;
}
if (dim_by_group) {
if ((w == NULL || w->group() != active->group()) && active->group() != NULL) {
// repaint windows that are no longer in the active group
foreach (EffectWindow * tmp, active->group()->members())
tmp->addRepaintFull();
}
} else
active->addRepaintFull();
const auto members = w->group()->members();
for (EffectWindow *member : members) {
scheduleInTransition(member);
}
}
void DimInactiveEffect::scheduleOutTransition(EffectWindow *w)
{
TimeLine &timeLine = m_transitions[w];
timeLine.setDuration(
std::chrono::milliseconds(static_cast<int>(animationTime(250))));
if (timeLine.done()) {
timeLine.reset();
}
active = w;
if (active != NULL) {
if (dim_by_group) {
if (active->group() != NULL) {
// repaint newly active windows
foreach (EffectWindow * tmp, active->group()->members())
tmp->addRepaintFull();
}
} else
active->addRepaintFull();
timeLine.setDirection(TimeLine::Forward);
timeLine.setEasingCurve(QEasingCurve::InOutSine);
}
void DimInactiveEffect::scheduleGroupOutTransition(EffectWindow *w)
{
if (!m_dimByGroup) {
scheduleOutTransition(w);
return;
}
if (!w->group()) {
scheduleOutTransition(w);
return;
}
const auto members = w->group()->members();
for (EffectWindow *member : members) {
scheduleOutTransition(member);
}
}
void DimInactiveEffect::scheduleRepaint(EffectWindow *w)
{
if (!m_dimByGroup) {
w->addRepaintFull();
return;
}
if (!w->group()) {
w->addRepaintFull();
return;
}
const auto members = w->group()->members();
for (EffectWindow *member : members) {
member->addRepaintFull();
}
}
void DimInactiveEffect::windowActivated(EffectWindow *w)
{
if (!w) {
return;
}
if (m_activeWindow == w) {
return;
}
if (m_dimByGroup && belongToSameGroup(m_activeWindow, w)) {
m_activeWindow = w;
return;
}
// WORKAROUND: Deleted windows do not belong to any of window groups.
// So, if one of windows in a window group is closed, the In transition
// will be false-triggered for the rest of the window group. In addition
// to the active window, keep track of active window group so we can
// tell whether "focus" moved from a closed window to some other window
// in a window group.
if (m_dimByGroup && w->group() && w->group() == m_activeWindowGroup) {
m_activeWindow = w;
return;
}
EffectWindow *previousActiveWindow = m_activeWindow;
m_activeWindow = canDimWindow(w) ? w : nullptr;
m_activeWindowGroup = (m_dimByGroup && m_activeWindow)
? m_activeWindow->group()
: nullptr;
if (previousActiveWindow) {
scheduleGroupOutTransition(previousActiveWindow);
scheduleRepaint(previousActiveWindow);
}
if (m_activeWindow) {
scheduleGroupInTransition(m_activeWindow);
scheduleRepaint(m_activeWindow);
}
}
void DimInactiveEffect::windowClosed(EffectWindow *w)
{
// When a window is closed, we should force current dim strength that
// is applied to it to avoid flickering when some effect animates
// the disappearing of the window. If there is no such effect then
// it won't be dimmed.
qreal forcedStrength = 0.0;
bool shouldForceDim = false;
auto transitionIt = m_transitions.find(w);
if (transitionIt != m_transitions.end()) {
forcedStrength = m_dimStrength * (*transitionIt).value();
shouldForceDim = true;
m_transitions.erase(transitionIt);
} else if (m_activeWindow == w) {
forcedStrength = 0.0;
shouldForceDim = true;
} else if (m_dimByGroup && belongToSameGroup(m_activeWindow, w)) {
forcedStrength = 0.0;
shouldForceDim = true;
} else if (canDimWindow(w)) {
forcedStrength = m_dimStrength;
shouldForceDim = true;
}
if (shouldForceDim) {
m_forceDim.insert(w, forcedStrength);
}
if (m_activeWindow == w) {
m_activeWindow = nullptr;
}
}
void DimInactiveEffect::windowDeleted(EffectWindow *w)
{
m_forceDim.remove(w);
}
void DimInactiveEffect::activeFullScreenEffectChanged()
{
if (m_fullScreenTransition.timeLine.done()) {
m_fullScreenTransition.timeLine.reset();
}
m_fullScreenTransition.timeLine.setDirection(
effects->activeFullScreenEffect()
? TimeLine::Forward
: TimeLine::Backward
);
m_fullScreenTransition.active = true;
effects->addRepaintFull();
}
} // namespace
} // namespace KWin
......@@ -4,6 +4,7 @@
Copyright (C) 2007 Lubos Lunak <l.lunak@kde.org>
Copyright (C) 2007 Christian Nitschkowski <christian.nitschkowski@kdemail.net>
Copyright (C) 2018 Vlad Zagorodniy <vladzzag@gmail.com>
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
......@@ -22,66 +23,108 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef KWIN_DIMINACTIVE_H
#define KWIN_DIMINACTIVE_H
// Include with base class for effects.
// kwineffects
#include <kwineffects.h>
#include <QTimeLine>
namespace KWin
{
class DimInactiveEffect
: public Effect
class DimInactiveEffect : public Effect
{
Q_OBJECT
Q_PROPERTY(bool dimPanels READ isDimPanels)
Q_PROPERTY(bool dimDesktop READ isDimDesktop)
Q_PROPERTY(bool dimKeepAbove READ isDimKeepAbove)
Q_PROPERTY(bool dimByGroup READ isDimByGroup)
Q_PROPERTY(int dimStrength READ configuredDimStrength)
Q_PROPERTY(int dimStrength READ dimStrength)
Q_PROPERTY(bool dimPanels READ dimPanels)
Q_PROPERTY(bool dimDesktop READ dimDesktop)
Q_PROPERTY(bool dimKeepAbove READ dimKeepAbove)
Q_PROPERTY(bool dimByGroup READ dimByGroup)
public:
DimInactiveEffect();
virtual void reconfigure(ReconfigureFlags);
virtual void prePaintScreen(ScreenPrePaintData& data, int time);
virtual void paintWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data);
int requestedEffectChainPosition() const override {
return 50;
}
// for properties
bool isDimPanels() const {
return dim_panels;
}
bool isDimDesktop() const {
return dim_desktop;
}
bool isDimKeepAbove() const {
return dim_keepabove;
}
bool isDimByGroup() const {
return dim_by_group;
}
int configuredDimStrength() const {
return dim_strength;
}
public Q_SLOTS:
void slotWindowActivated(KWin::EffectWindow* c);
void slotWindowDeleted(KWin::EffectWindow *w);
~DimInactiveEffect() override;
void reconfigure(ReconfigureFlags flags) override;
void prePaintScreen(ScreenPrePaintData &data, int time) override;
void paintWindow(EffectWindow *w, int mask, QRegion region, WindowPaintData &data) override;
void postPaintScreen() override;
int requestedEffectChainPosition() const override;
bool isActive() const override;
int dimStrength() const;
bool dimPanels() const;
bool dimDesktop() const;
bool dimKeepAbove() const;
bool dimByGroup() const;
private Q_SLOTS:
void windowActivated(EffectWindow *w);
void windowClosed(EffectWindow *w);
void windowDeleted(EffectWindow *w);
void activeFullScreenEffectChanged();
private:
bool dimWindow(const EffectWindow* w) const;
QTimeLine timeline;
EffectWindow* active;
EffectWindow* previousActive;
QTimeLine previousActiveTimeline;
int dim_strength; // reduce saturation and brightness by this percentage
bool dim_panels; // do/don't dim also all panels
bool dim_desktop; // do/don't dim the desktop
bool dim_keepabove; // do/don't dim keep-above windows
bool dim_by_group; // keep visible all windows from the active window's group or only the active window
void dimWindow(WindowPaintData &data, qreal strength);
bool canDimWindow(const EffectWindow *w) const;
void scheduleInTransition(EffectWindow *w);
void scheduleGroupInTransition(EffectWindow *w);
void scheduleOutTransition(EffectWindow *w);
void scheduleGroupOutTransition(EffectWindow *w);
void scheduleRepaint(EffectWindow *w);
private:
qreal m_dimStrength;
bool m_dimPanels;
bool m_dimDesktop;
bool m_dimKeepAbove;
bool m_dimByGroup;
EffectWindow *m_activeWindow;
const EffectWindowGroup *m_activeWindowGroup;
QHash<EffectWindow*, TimeLine> m_transitions;
QHash<EffectWindow*, qreal> m_forceDim;
struct {
bool active = false;
TimeLine timeLine;
} m_fullScreenTransition;
};
} // namespace
inline int DimInactiveEffect::requestedEffectChainPosition() const
{
return 50;
}
inline bool DimInactiveEffect::isActive() const
{
return true;
}
inline int DimInactiveEffect::dimStrength() const
{
return qRound(m_dimStrength * 100.0);
}
inline bool DimInactiveEffect::dimPanels() const
{
return m_dimPanels;
}