highlightwindow.cpp 12.1 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)))
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
29
    , m_monitorWindow(nullptr)
30
{
31
    m_atom = effects->announceSupportProperty("_KDE_WINDOW_HIGHLIGHT", this);
32 33 34 35 36 37 38 39
    connect(effects, &EffectsHandler::windowAdded, this, &HighlightWindowEffect::slotWindowAdded);
    connect(effects, &EffectsHandler::windowClosed, this, &HighlightWindowEffect::slotWindowClosed);
    connect(effects, &EffectsHandler::windowDeleted, this, &HighlightWindowEffect::slotWindowDeleted);
    connect(effects, &EffectsHandler::propertyNotify, this,
        [this](EffectWindow *w, long atom) {
            slotPropertyNotify(w, atom, nullptr);
        }
    );
40 41 42 43 44
    connect(effects, &EffectsHandler::xcbConnectionChanged, this,
        [this] {
            m_atom = effects->announceSupportProperty("_KDE_WINDOW_HIGHLIGHT", this);
        }
    );
45
}
46 47

HighlightWindowEffect::~HighlightWindowEffect()
48 49
{
}
50

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

57 58
void HighlightWindowEffect::prePaintWindow(EffectWindow* w, WindowPrePaintData& data, int time)
{
59
    // Calculate window opacities
60
    QHash<EffectWindow*, float>::iterator opacity = m_windowOpacity.find(w);
61 62
    if (!m_highlightedWindows.isEmpty()) {
        // Initial fade out and changing highlight animation
63
        if (opacity == m_windowOpacity.end())
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
64
            opacity = m_windowOpacity.insert(w, 0.0f);
65
        float oldOpacity = *opacity;
66
        if (m_highlightedWindows.contains(w))
67
            *opacity = qMin(1.0f, oldOpacity + time / m_fadeDuration);
68
        else if (w->isNormalWindow() || w->isDialog())   // Only fade out windows
69
            *opacity = qMax(isInitiallyHidden(w) ? 0.0f : 0.15f, oldOpacity - time / m_fadeDuration);
70

71
        if (*opacity < 0.98f)
72
            data.setTranslucent();
73
        if (oldOpacity != *opacity)
74
            effects->addRepaint(w->expandedGeometry());
75 76
    } else if (m_finishing && m_windowOpacity.contains(w)) {
        // Final fading back in animation
77 78 79
        if (opacity == m_windowOpacity.end())
            opacity = m_windowOpacity.insert(w, 0.0f);
        float oldOpacity = *opacity;
80
        if (isInitiallyHidden(w))
81
            *opacity = qMax(0.0f, oldOpacity - time / m_fadeDuration);
82
        else
83
            *opacity = qMin(1.0f, oldOpacity + time / m_fadeDuration);
84

85
        if (*opacity < 0.98f)
86
            data.setTranslucent();
87
        if (oldOpacity != *opacity)
88
            effects->addRepaint(w->expandedGeometry());
89

90
        if (*opacity > 0.98f || *opacity < 0.02f) {
91
            m_windowOpacity.remove(w);   // We default to 1.0
92 93
            opacity = m_windowOpacity.end();
        }
94
    }
95

96
    // Show tabbed windows and windows on other desktops if highlighted
97 98 99
    if (opacity != m_windowOpacity.end() && *opacity > 0.01) {
        if (w->isMinimized())
            w->enablePainting(EffectWindow::PAINT_DISABLED_BY_MINIMIZE);
100 101
        if (!w->isOnCurrentDesktop())
            w->enablePainting(EffectWindow::PAINT_DISABLED_BY_DESKTOP);
102 103
    }

104 105 106 107 108
    effects->prePaintWindow(w, data, time);
}

void HighlightWindowEffect::paintWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data)
{
109
    data.multiplyOpacity(m_windowOpacity.value(w, 1.0f));
110 111
    effects->paintWindow(w, mask, region, data);
}
112

113
void HighlightWindowEffect::slotWindowAdded(EffectWindow* w)
114 115 116
{
    if (!m_highlightedWindows.isEmpty()) {
        // The effect is activated thus we need to add it to the opacity hash
117 118 119 120 121 122 123
        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
124
    }
125
    slotPropertyNotify(w, m_atom, w);   // Check initial value
126
}
127

128
void HighlightWindowEffect::slotWindowClosed(EffectWindow* w)
129 130
{
    if (m_monitorWindow == w)   // The monitoring window was destroyed
131
        finishHighlighting();
132
}
133

134
void HighlightWindowEffect::slotWindowDeleted(EffectWindow* w)
135 136 137
{
    m_windowOpacity.remove(w);
}
138

139
void HighlightWindowEffect::slotPropertyNotify(EffectWindow* w, long a, EffectWindow *addedWindow)
140
{
141
    if (a != m_atom || m_atom == XCB_ATOM_NONE)
142 143
        return; // Not our atom

144
    // if the window is null, the property was set on the root window - see events.cpp
145 146 147 148
    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
149 150
        if (!addedWindow || w != addedWindow)
            finishHighlighting();
151
        return;
152
    }
153
    auto* data = reinterpret_cast<uint32_t*>(byteData.data());
154

155 156
    if (!data[0]) {
        // Purposely clearing highlight by issuing a NULL target
157
        finishHighlighting();
158
        return;
159
    }
160
    m_monitorWindow = w;
161
    bool found = false;
162 163
    int length = byteData.length() / sizeof(data[0]);
    //foreach ( EffectWindow* e, m_highlightedWindows )
164
    //    effects->setElevatedWindow( e, false );
165
    m_highlightedWindows.clear();
166
    m_highlightedIds.clear();
167
    for (int i = 0; i < length; i++) {
168
        m_highlightedIds << data[i];
169 170
        EffectWindow* foundWin = effects->findWindow(data[i]);
        if (!foundWin) {
171
            qCDebug(KWINEFFECTS) << "Invalid window targetted for highlight. Requested:" << data[i];
172
            continue; // might come in later.
173
        }
174 175 176 177
        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 );
178 179 180
        found = true;
    }
    if (!found) {
181
        finishHighlighting();
182
        return;
183
    }
184
    prepareHighlighting();
185
    if (w)
186
        m_windowOpacity[w] = 1.0; // Because it's not in stackingOrder() yet
187 188

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

        // 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() );
224
        if ( desktopLine.intersect( testLine, &arrowTip ) != QLineF::BoundedIntersection )
225 226 227 228
            {
            testLine = QLineF( // Right
                outerArea.x() + outerArea.width(), outerArea.y(),
                outerArea.x() + outerArea.width(), outerArea.y() + outerArea.height() );
229
            if ( desktopLine.intersect( testLine, &arrowTip ) != QLineF::BoundedIntersection )
230 231 232 233
                {
                testLine = QLineF( // Bottom
                    outerArea.x() + outerArea.width(), outerArea.y() + outerArea.height(),
                    outerArea.x(), outerArea.y() + outerArea.height() );
234
                if ( desktopLine.intersect( testLine, &arrowTip ) != QLineF::BoundedIntersection )
235 236 237 238 239 240 241 242 243 244
                    {
                    testLine = QLineF( // Left
                        outerArea.x(), outerArea.y() + outerArea.height(),
                        outerArea.x(), outerArea.y() );
                    desktopLine.intersect( testLine, &arrowTip ); // Should never fail
                    }
                }
            }
        m_arrowTip = arrowTip.toPoint();
        } */
245
}
246 247

void HighlightWindowEffect::prepareHighlighting()
248
{
249 250
    // Create window data for every window. Just calling [w] creates it.
    m_finishing = false;
251 252
    foreach (EffectWindow * w, effects->stackingOrder()) {
        if (!m_windowOpacity.contains(w))    // Just in case we are still finishing from last time
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
253
            m_windowOpacity.insert(w, isInitiallyHidden(w) ? 0.0 : 1.0);
254 255 256
        if (!m_highlightedWindows.isEmpty())
            m_highlightedWindows.at(0)->addRepaintFull();
    }
257
}
258 259

void HighlightWindowEffect::finishHighlighting()
260
{
261
    m_finishing = true;
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
262
    m_monitorWindow = nullptr;
263
    m_highlightedWindows.clear();
264 265
    if (!m_windowOpacity.isEmpty())
        m_windowOpacity.constBegin().key()->addRepaintFull();
266
}
267

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
void HighlightWindowEffect::highlightWindows(const QVector<KWin::EffectWindow *> &windows)
{
    if (windows.isEmpty()) {
        finishHighlighting();
        return;
    }

    m_monitorWindow = nullptr;
    m_highlightedWindows.clear();
    m_highlightedIds.clear();
    for (auto w : windows) {
        m_highlightedWindows << w;
    }
    prepareHighlighting();
}

284 285
bool HighlightWindowEffect::isActive() const
{
286
    return !(m_windowOpacity.isEmpty() || effects->isScreenLocked());
287 288
}

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
bool HighlightWindowEffect::provides(Feature feature)
{
    switch (feature) {
    case HighlightWindows:
        return true;
    default:
        return false;
    }
}

bool HighlightWindowEffect::perform(Feature feature, const QVariantList &arguments)
{
    if (feature != HighlightWindows) {
        return false;
    }
    if (arguments.size() != 1) {
        return false;
    }
    highlightWindows(arguments.first().value<QVector<EffectWindow*>>());
    return true;
}

311
} // namespace