highlightwindow.cpp 10.7 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 27
/********************************************************************
 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"

#include <kdebug.h>

namespace KWin
{

28
KWIN_EFFECT(highlightwindow, HighlightWindowEffect)
29 30

HighlightWindowEffect::HighlightWindowEffect()
31 32 33 34 35 36
    : m_finishing(false)
    , m_fadeDuration(double(animationTime(150)))
    , m_monitorWindow(NULL)
{
    m_atom = XInternAtom(display(), "_KDE_WINDOW_HIGHLIGHT", False);
    effects->registerPropertyType(m_atom, true);
37 38 39

    // Announce support by creating a dummy version on the root window
    unsigned char dummy = 0;
40
    XChangeProperty(display(), rootWindow(), m_atom, m_atom, 8, PropModeReplace, &dummy, 1);
41
    connect(effects, SIGNAL(windowAdded(EffectWindow*)), this, SLOT(slotWindowAdded(EffectWindow*)));
42
    connect(effects, SIGNAL(windowClosed(EffectWindow*)), this, SLOT(slotWindowClosed(EffectWindow*)));
43
    connect(effects, SIGNAL(windowDeleted(EffectWindow*)), this, SLOT(slotWindowDeleted(EffectWindow*)));
44
}
45 46

HighlightWindowEffect::~HighlightWindowEffect()
47 48 49 50
{
    XDeleteProperty(display(), rootWindow(), m_atom);
    effects->registerPropertyType(m_atom, false);
}
51

52 53 54
static bool isInitiallyHidden(EffectWindow* w)
{
    // Is the window initially hidden until it is highlighted?
55
    return !w->visibleInClientGroup() || !w->isOnCurrentDesktop();
56
}
57

58 59
void HighlightWindowEffect::prePaintWindow(EffectWindow* w, WindowPrePaintData& data, int time)
{
60
    // Calculate window opacities
61 62
    if (!m_highlightedWindows.isEmpty()) {
        // Initial fade out and changing highlight animation
63
        double oldOpacity = m_windowOpacity[w];
64 65 66 67 68
        if (m_highlightedWindows.contains(w))
            m_windowOpacity[w] = qMin(1.0, m_windowOpacity[w] + time / m_fadeDuration);
        else if (w->isNormalWindow() || w->isDialog())   // Only fade out windows
            m_windowOpacity[w] = qMax(isInitiallyHidden(w) ? 0.0 : 0.15,
                                      m_windowOpacity[w] - time / m_fadeDuration);
69

70
        if (m_windowOpacity[w] != 1.0)
71
            data.setTranslucent();
72
        if (oldOpacity != m_windowOpacity[w])
73
            w->addRepaintFull();
74 75
    } else if (m_finishing && m_windowOpacity.contains(w)) {
        // Final fading back in animation
76
        double oldOpacity = m_windowOpacity[w];
77 78
        if (isInitiallyHidden(w))
            m_windowOpacity[w] = qMax(0.0, m_windowOpacity[w] - time / m_fadeDuration);
79
        else
80
            m_windowOpacity[w] = qMin(1.0, m_windowOpacity[w] + time / m_fadeDuration);
81

82
        if (m_windowOpacity[w] != 1.0)
83
            data.setTranslucent();
84
        if (oldOpacity != m_windowOpacity[w])
85 86
            w->addRepaintFull();

87 88 89
        if (m_windowOpacity[w] == 1.0 || m_windowOpacity[w] == 0.0)
            m_windowOpacity.remove(w);   // We default to 1.0
    }
90

91
    // Show tabbed windows and windows on other desktops if highlighted
92 93 94 95 96
    if (m_windowOpacity.contains(w) && m_windowOpacity[w] != 0.0) {
        if (!w->visibleInClientGroup())
            w->enablePainting(EffectWindow::PAINT_DISABLED_BY_CLIENT_GROUP);
        if (!w->isOnCurrentDesktop())
            w->enablePainting(EffectWindow::PAINT_DISABLED_BY_DESKTOP);
97 98
    }

99 100 101 102 103 104
    effects->prePaintWindow(w, data, time);
}

void HighlightWindowEffect::paintWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data)
{
    if (m_windowOpacity.contains(w))
105
        data.opacity *= m_windowOpacity[w];
106 107
    effects->paintWindow(w, mask, region, data);
}
108

109
void HighlightWindowEffect::slotWindowAdded(EffectWindow* w)
110 111 112 113 114
{
    if (!m_highlightedWindows.isEmpty()) {
        // The effect is activated thus we need to add it to the opacity hash
        if (w->isNormalWindow() || w->isDialog())   // Only fade out windows
            m_windowOpacity[w] = isInitiallyHidden(w) ? 0.0 : 0.15;
115 116
        else
            m_windowOpacity[w] = 1.0;
117
    }
118 119
    propertyNotify(w, m_atom);   // Check initial value
}
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 133 134
void HighlightWindowEffect::propertyNotify(EffectWindow* w, long a)
{
    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
        finishHighlighting();
        return;
144 145
    }
    long* data = reinterpret_cast<long*>(byteData.data());
146

147 148
    if (!data[0]) {
        // Purposely clearing highlight by issuing a NULL target
149
        finishHighlighting();
150
        return;
151
    }
152
    m_monitorWindow = w;
153
    bool found = false;
154 155
    int length = byteData.length() / sizeof(data[0]);
    //foreach ( EffectWindow* e, m_highlightedWindows )
156
    //    effects->setElevatedWindow( e, false );
157
    m_highlightedWindows.clear();
158 159 160
    for (int i = 0; i < length; i++) {
        EffectWindow* foundWin = effects->findWindow(data[i]);
        if (!foundWin) {
161 162
            kDebug(1212) << "Invalid window targetted for highlight. Requested:" << data[i];
            continue;
163 164 165
        }
        if (!foundWin->isMinimized()) {
            m_highlightedWindows.append(foundWin);
166 167 168
            // 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 );
169
        }
170 171 172
        found = true;
    }
    if (!found) {
173
        finishHighlighting();
174
        return;
175
    }
176
    prepareHighlighting();
177
    if (w)
178
        m_windowOpacity[w] = 1.0; // Because it's not in stackingOrder() yet
179 180

    /* TODO: Finish thumbnails of offscreen windows, not sure if it's worth it though
181
    if ( !m_highlightedWindow->isOnCurrentDesktop() )
182 183 184 185 186 187 188 189 190 191 192 193
        { // 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
194
        if ( windowAspect > areaAspect )
195 196 197 198
            // 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() ));
199
        if ( thumbRect.width() >= m_highlightedWindow->width() )
200 201 202 203
            // 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
204 205
        QPoint direction = effects->desktopGridCoords( m_highlightedWindow->desktop() ) -
            effects->desktopGridCoords( effects->currentDesktop() );
206 207 208 209 210 211 212 213 214 215

        // 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() );
216
        if ( desktopLine.intersect( testLine, &arrowTip ) != QLineF::BoundedIntersection )
217 218 219 220
            {
            testLine = QLineF( // Right
                outerArea.x() + outerArea.width(), outerArea.y(),
                outerArea.x() + outerArea.width(), outerArea.y() + outerArea.height() );
221
            if ( desktopLine.intersect( testLine, &arrowTip ) != QLineF::BoundedIntersection )
222 223 224 225
                {
                testLine = QLineF( // Bottom
                    outerArea.x() + outerArea.width(), outerArea.y() + outerArea.height(),
                    outerArea.x(), outerArea.y() + outerArea.height() );
226
                if ( desktopLine.intersect( testLine, &arrowTip ) != QLineF::BoundedIntersection )
227 228 229 230 231 232 233 234 235 236
                    {
                    testLine = QLineF( // Left
                        outerArea.x(), outerArea.y() + outerArea.height(),
                        outerArea.x(), outerArea.y() );
                    desktopLine.intersect( testLine, &arrowTip ); // Should never fail
                    }
                }
            }
        m_arrowTip = arrowTip.toPoint();
        } */
237
}
238 239

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

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

} // namespace