highlightwindow.cpp 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/********************************************************************
 KWin - the KDE window manager
 This file is part of the KDE project.

Copyright (C) 2009 Lucas Murray <lmurray@undefinedfire.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
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/

#include "highlightwindow.h"

namespace KWin
{

HighlightWindowEffect::HighlightWindowEffect()
27
    : m_finishing(false)
28
    , m_fadeDuration(float(animationTime(150)))
29 30
    , m_monitorWindow(NULL)
{
31
    m_atom = effects->announceSupportProperty("_KDE_WINDOW_HIGHLIGHT", this);
32 33 34 35
    connect(effects, SIGNAL(windowAdded(KWin::EffectWindow*)), this, SLOT(slotWindowAdded(KWin::EffectWindow*)));
    connect(effects, SIGNAL(windowClosed(KWin::EffectWindow*)), this, SLOT(slotWindowClosed(KWin::EffectWindow*)));
    connect(effects, SIGNAL(windowDeleted(KWin::EffectWindow*)), this, SLOT(slotWindowDeleted(KWin::EffectWindow*)));
    connect(effects, SIGNAL(propertyNotify(KWin::EffectWindow*,long)), this, SLOT(slotPropertyNotify(KWin::EffectWindow*,long)));
36
}
37 38

HighlightWindowEffect::~HighlightWindowEffect()
39 40
{
}
41

42 43 44
static bool isInitiallyHidden(EffectWindow* w)
{
    // Is the window initially hidden until it is highlighted?
Thomas Lübking's avatar
Thomas Lübking committed
45
    return w->isMinimized() || !w->isCurrentTab() || !w->isOnCurrentDesktop();
46
}
47

48 49
void HighlightWindowEffect::prePaintWindow(EffectWindow* w, WindowPrePaintData& data, int time)
{
50
    // Calculate window opacities
51
    QHash<EffectWindow*, float>::iterator opacity = m_windowOpacity.find(w);
52 53
    if (!m_highlightedWindows.isEmpty()) {
        // Initial fade out and changing highlight animation
54 55 56
        if (opacity == m_windowOpacity.end())
            opacity = m_windowOpacity.insertMulti(w, 0.0f);
        float oldOpacity = *opacity;
57
        if (m_highlightedWindows.contains(w))
58
            *opacity = qMin(1.0f, oldOpacity + time / m_fadeDuration);
59
        else if (w->isNormalWindow() || w->isDialog())   // Only fade out windows
60
            *opacity = qMax(isInitiallyHidden(w) ? 0.0f : 0.15f, oldOpacity - time / m_fadeDuration);
61

62
        if (*opacity < 0.98f)
63
            data.setTranslucent();
64 65
        if (oldOpacity != *opacity)
            effects->addRepaint(w->geometry().adjusted(-16,-16,16,32)); // add some padding. w->addRepaintFull() is wrong for at least isInitiallyHidden ...
66 67
    } else if (m_finishing && m_windowOpacity.contains(w)) {
        // Final fading back in animation
68 69 70
        if (opacity == m_windowOpacity.end())
            opacity = m_windowOpacity.insert(w, 0.0f);
        float oldOpacity = *opacity;
71
        if (isInitiallyHidden(w))
72
            *opacity = qMax(0.0f, oldOpacity - time / m_fadeDuration);
73
        else
74
            *opacity = qMin(1.0f, oldOpacity + time / m_fadeDuration);
75

76
        if (*opacity < 0.98f)
77
            data.setTranslucent();
78 79
        if (oldOpacity != *opacity)
            effects->addRepaint(w->geometry().adjusted(-16,-16,16,32)); // ... see above ... because the window is pot. gone in the last pass
80

81
        if (*opacity > 0.98f || *opacity < 0.02f) {
82
            m_windowOpacity.remove(w);   // We default to 1.0
83 84
            opacity = m_windowOpacity.end();
        }
85
    }
86

87
    // Show tabbed windows and windows on other desktops if highlighted
88 89 90
    if (opacity != m_windowOpacity.end() && *opacity > 0.01) {
        if (w->isMinimized())
            w->enablePainting(EffectWindow::PAINT_DISABLED_BY_MINIMIZE);
Thomas Lübking's avatar
Thomas Lübking committed
91 92
        if (!w->isCurrentTab())
            w->enablePainting(EffectWindow::PAINT_DISABLED_BY_TAB_GROUP);
93 94
        if (!w->isOnCurrentDesktop())
            w->enablePainting(EffectWindow::PAINT_DISABLED_BY_DESKTOP);
95 96
    }

97 98 99 100 101
    effects->prePaintWindow(w, data, time);
}

void HighlightWindowEffect::paintWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data)
{
102
    data.multiplyOpacity(m_windowOpacity.value(w, 1.0f));
103 104
    effects->paintWindow(w, mask, region, data);
}
105

106
void HighlightWindowEffect::slotWindowAdded(EffectWindow* w)
107 108 109
{
    if (!m_highlightedWindows.isEmpty()) {
        // The effect is activated thus we need to add it to the opacity hash
110 111 112 113 114 115 116
        foreach (const WId id, m_highlightedIds) {
            if (w == effects->findWindow(id)) {
                m_windowOpacity[w] = 1.0; // this window was demanded to be highlighted before it appeared
                return;
            }
        }
        m_windowOpacity[w] = 0.15; // this window is not currently highlighted
117
    }
118
    slotPropertyNotify(w, m_atom, w);   // Check initial value
119
}
120

121
void HighlightWindowEffect::slotWindowClosed(EffectWindow* w)
122 123
{
    if (m_monitorWindow == w)   // The monitoring window was destroyed
124
        finishHighlighting();
125
}
126

127
void HighlightWindowEffect::slotWindowDeleted(EffectWindow* w)
128 129 130
{
    m_windowOpacity.remove(w);
}
131

132
void HighlightWindowEffect::slotPropertyNotify(EffectWindow* w, long a, EffectWindow *addedWindow)
133 134
{
    if (a != m_atom)
135 136
        return; // Not our atom

137
    // if the window is null, the property was set on the root window - see events.cpp
138 139 140 141
    QByteArray byteData = w ? w->readProperty(m_atom, m_atom, 32) :
                          effects->readRootProperty(m_atom, m_atom, 32);
    if (byteData.length() < 1) {
        // Property was removed, clearing highlight
142 143
        if (!addedWindow || w != addedWindow)
            finishHighlighting();
144
        return;
145
    }
146
    auto* data = reinterpret_cast<uint32_t*>(byteData.data());
147

148 149
    if (!data[0]) {
        // Purposely clearing highlight by issuing a NULL target
150
        finishHighlighting();
151
        return;
152
    }
153
    m_monitorWindow = w;
154
    bool found = false;
155 156
    int length = byteData.length() / sizeof(data[0]);
    //foreach ( EffectWindow* e, m_highlightedWindows )
157
    //    effects->setElevatedWindow( e, false );
158
    m_highlightedWindows.clear();
159
    m_highlightedIds.clear();
160
    for (int i = 0; i < length; i++) {
161
        m_highlightedIds << data[i];
162 163
        EffectWindow* foundWin = effects->findWindow(data[i]);
        if (!foundWin) {
164
            qCDebug(KWINEFFECTS) << "Invalid window targetted for highlight. Requested:" << data[i];
165
            continue; // might come in later.
166
        }
167 168 169 170
        m_highlightedWindows.append(foundWin);
        // TODO: We cannot just simply elevate the window as this will elevate it over
        // Plasma tooltips and other such windows as well
        //effects->setElevatedWindow( foundWin, true );
171 172 173
        found = true;
    }
    if (!found) {
174
        finishHighlighting();
175
        return;
176
    }
177
    prepareHighlighting();
178
    if (w)
179
        m_windowOpacity[w] = 1.0; // Because it's not in stackingOrder() yet
180 181

    /* TODO: Finish thumbnails of offscreen windows, not sure if it's worth it though
182
    if ( !m_highlightedWindow->isOnCurrentDesktop() )
183 184 185 186 187 188 189 190 191 192 193 194
        { // Window is offscreen, determine thumbnail position
        QRect screenArea = effects->clientArea( MaximizeArea ); // Workable area of the active screen
        QRect outerArea = outerArea.adjusted( outerArea.width() / 10, outerArea.height() / 10,
            -outerArea.width() / 10, -outerArea.height() / 10 ); // Add 10% margin around the edge
        QRect innerArea = outerArea.adjusted( outerArea.width() / 40, outerArea.height() / 40,
            -outerArea.width() / 40, -outerArea.height() / 40 ); // Outer edge of the thumbnail border (2.5%)
        QRect thumbArea = outerArea.adjusted( 20, 20, -20, -20 ); // Outer edge of the thumbnail (20px)

        // Determine the maximum size that we can make the thumbnail within the innerArea
        double areaAspect = double( thumbArea.width() ) / double( thumbArea.height() );
        double windowAspect = aspectRatio( m_highlightedWindow );
        QRect thumbRect; // Position doesn't matter right now, but it will later
195
        if ( windowAspect > areaAspect )
196 197 198 199
            // Top/bottom will touch first
            thumbRect = QRect( 0, 0, widthForHeight( thumbArea.height() ), thumbArea.height() );
        else // Left/right will touch first
            thumbRect = QRect( 0, 0, thumbArea.width(), heightForWidth( thumbArea.width() ));
200
        if ( thumbRect.width() >= m_highlightedWindow->width() )
201 202 203 204
            // Area is larger than the window, just use the window's size
            thumbRect = m_highlightedWindow->geometry();

        // Determine position of desktop relative to the current one
205 206
        QPoint direction = effects->desktopGridCoords( m_highlightedWindow->desktop() ) -
            effects->desktopGridCoords( effects->currentDesktop() );
207 208 209 210 211 212 213 214 215 216

        // Draw a line from the center of the current desktop to the center of the target desktop.
        QPointF desktopLine( 0, 0, direction.x() * screenArea.width(), direction.y() * screenArea.height() );
        desktopLeft.translate( screenArea.width() / 2, screenArea.height() / 2 ); // Move to the screen center

        // Take the point where the line crosses the outerArea, this will be the tip of our arrow
        QPointF arrowTip;
        QLineF testLine( // Top
            outerArea.x(), outerArea.y(),
            outerArea.x() + outerArea.width(), outerArea.y() );
217
        if ( desktopLine.intersect( testLine, &arrowTip ) != QLineF::BoundedIntersection )
218 219 220 221
            {
            testLine = QLineF( // Right
                outerArea.x() + outerArea.width(), outerArea.y(),
                outerArea.x() + outerArea.width(), outerArea.y() + outerArea.height() );
222
            if ( desktopLine.intersect( testLine, &arrowTip ) != QLineF::BoundedIntersection )
223 224 225 226
                {
                testLine = QLineF( // Bottom
                    outerArea.x() + outerArea.width(), outerArea.y() + outerArea.height(),
                    outerArea.x(), outerArea.y() + outerArea.height() );
227
                if ( desktopLine.intersect( testLine, &arrowTip ) != QLineF::BoundedIntersection )
228 229 230 231 232 233 234 235 236 237
                    {
                    testLine = QLineF( // Left
                        outerArea.x(), outerArea.y() + outerArea.height(),
                        outerArea.x(), outerArea.y() );
                    desktopLine.intersect( testLine, &arrowTip ); // Should never fail
                    }
                }
            }
        m_arrowTip = arrowTip.toPoint();
        } */
238
}
239 240

void HighlightWindowEffect::prepareHighlighting()
241
{
242 243
    // Create window data for every window. Just calling [w] creates it.
    m_finishing = false;
244 245 246 247 248 249
    foreach (EffectWindow * w, effects->stackingOrder()) {
        if (!m_windowOpacity.contains(w))    // Just in case we are still finishing from last time
            m_windowOpacity.insertMulti(w, isInitiallyHidden(w) ? 0.0 : 1.0);
        if (!m_highlightedWindows.isEmpty())
            m_highlightedWindows.at(0)->addRepaintFull();
    }
250
}
251 252

void HighlightWindowEffect::finishHighlighting()
253
{
254 255
    m_finishing = true;
    m_monitorWindow = NULL;
256
    m_highlightedWindows.clear();
257 258
    if (!m_windowOpacity.isEmpty())
        m_windowOpacity.constBegin().key()->addRepaintFull();
259
}
260

261 262
bool HighlightWindowEffect::isActive() const
{
263
    return !(m_windowOpacity.isEmpty() || effects->isScreenLocked());
264 265
}

266
} // namespace