kwin/effects/thumbnailaside/thumbnailaside.cpp
Martin Gräßlin 76efe517a7 Turn built-in effects into a library kwin links against
As all effects have always been compiled into the same .so file it's
questionable whether resolving the effects through a library is useful
at all. By linking against the built-in effects we gain the following
advantages:
* don't have to load/unload the KLibrary
* don't have to resolve the create, supported and enabled functions
* no version check required
* no dependency resolving (effects don't use it)
* remove the KWIN_EFFECT macros from the effects

All the effects are now registered in an effects_builtins file which
maps the name to a factory method and supported or enabled by default
methods.

During loading the effects we first check whether there is a built-in
effect by the given name and make a shortcut to create it through that.
If that's not possible the normal plugin loading is used.

Completely unscientific testing [1] showed an improvement of almost 10
msec during loading all the effects I use.

[1] QElapsedTimer around the loading code, start kwin five times, take
average.

REVIEW: 115073
2014-01-24 14:13:59 +01:00

191 lines
6.1 KiB
C++

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2007 Lubos Lunak <l.lunak@kde.org>
Copyright (C) 2007 Christian Nitschkowski <christian.nitschkowski@kdemail.net>
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 "thumbnailaside.h"
// KConfigSkeleton
#include "thumbnailasideconfig.h"
#include <QAction>
#include <KDE/KGlobalAccel>
#include <KDE/KLocalizedString>
namespace KWin
{
ThumbnailAsideEffect::ThumbnailAsideEffect()
{
QAction* a = new QAction(this);
a->setObjectName(QStringLiteral("ToggleCurrentThumbnail"));
a->setText(i18n("Toggle Thumbnail for Current Window"));
KGlobalAccel::self()->setDefaultShortcut(a, QList<QKeySequence>() << Qt::META + Qt::CTRL + Qt::Key_T);
KGlobalAccel::self()->setShortcut(a, QList<QKeySequence>() << Qt::META + Qt::CTRL + Qt::Key_T);
connect(a, SIGNAL(triggered(bool)), this, SLOT(toggleCurrentThumbnail()));
connect(effects, SIGNAL(windowClosed(KWin::EffectWindow*)), this, SLOT(slotWindowClosed(KWin::EffectWindow*)));
connect(effects, SIGNAL(windowGeometryShapeChanged(KWin::EffectWindow*,QRect)), this, SLOT(slotWindowGeometryShapeChanged(KWin::EffectWindow*,QRect)));
connect(effects, SIGNAL(windowDamaged(KWin::EffectWindow*,QRect)), this, SLOT(slotWindowDamaged(KWin::EffectWindow*,QRect)));
connect(effects, SIGNAL(screenLockingChanged(bool)), SLOT(repaintAll()));
reconfigure(ReconfigureAll);
}
void ThumbnailAsideEffect::reconfigure(ReconfigureFlags)
{
ThumbnailAsideConfig::self()->readConfig();
maxwidth = ThumbnailAsideConfig::maxWidth();
spacing = ThumbnailAsideConfig::spacing();
opacity = ThumbnailAsideConfig::opacity()/100.0;
screen = ThumbnailAsideConfig::screen(); // Xinerama screen TODO add gui option
arrange();
}
void ThumbnailAsideEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data)
{
painted = QRegion();
effects->paintScreen(mask, region, data);
foreach (const Data & d, windows) {
if (painted.intersects(d.rect)) {
WindowPaintData data(d.window);
data.multiplyOpacity(opacity);
QRect region;
setPositionTransformations(data, region, d.window, d.rect, Qt::KeepAspectRatio);
effects->drawWindow(d.window, PAINT_WINDOW_OPAQUE | PAINT_WINDOW_TRANSLUCENT | PAINT_WINDOW_TRANSFORMED | PAINT_WINDOW_LANCZOS,
region, data);
}
}
}
void ThumbnailAsideEffect::paintWindow(EffectWindow *w, int mask, QRegion region, WindowPaintData &data)
{
effects->paintWindow(w, mask, region, data);
painted |= region;
}
void ThumbnailAsideEffect::slotWindowDamaged(EffectWindow* w, const QRect&)
{
foreach (const Data & d, windows) {
if (d.window == w)
effects->addRepaint(d.rect);
}
}
void ThumbnailAsideEffect::slotWindowGeometryShapeChanged(EffectWindow* w, const QRect& old)
{
foreach (const Data & d, windows) {
if (d.window == w) {
if (w->size() == old.size())
effects->addRepaint(d.rect);
else
arrange();
return;
}
}
}
void ThumbnailAsideEffect::slotWindowClosed(EffectWindow* w)
{
removeThumbnail(w);
}
void ThumbnailAsideEffect::toggleCurrentThumbnail()
{
EffectWindow* active = effects->activeWindow();
if (active == NULL)
return;
if (windows.contains(active))
removeThumbnail(active);
else
addThumbnail(active);
}
void ThumbnailAsideEffect::addThumbnail(EffectWindow* w)
{
repaintAll(); // repaint old areas
Data d;
d.window = w;
d.index = windows.count();
windows[ w ] = d;
arrange();
}
void ThumbnailAsideEffect::removeThumbnail(EffectWindow* w)
{
if (!windows.contains(w))
return;
repaintAll(); // repaint old areas
int index = windows[ w ].index;
windows.remove(w);
for (QHash< EffectWindow*, Data >::Iterator it = windows.begin();
it != windows.end();
++it) {
Data& d = *it;
if (d.index > index)
--d.index;
}
arrange();
}
void ThumbnailAsideEffect::arrange()
{
if (windows.size() == 0)
return;
int height = 0;
QVector< int > pos(windows.size());
int mwidth = 0;
foreach (const Data & d, windows) {
height += d.window->height();
mwidth = qMax(mwidth, d.window->width());
pos[ d.index ] = d.window->height();
}
QRect area = effects->clientArea(MaximizeArea, screen, effects->currentDesktop());
double scale = area.height() / double(height);
scale = qMin(scale, maxwidth / double(mwidth)); // don't be wider than maxwidth pixels
int add = 0;
for (int i = 0;
i < windows.size();
++i) {
pos[ i ] = int(pos[ i ] * scale);
pos[ i ] += spacing + add; // compute offset of each item
add = pos[ i ];
}
for (QHash< EffectWindow*, Data >::Iterator it = windows.begin();
it != windows.end();
++it) {
Data& d = *it;
int width = int(d.window->width() * scale);
d.rect = QRect(area.right() - width, area.bottom() - pos[ d.index ], width, int(d.window->height() * scale));
}
repaintAll();
}
void ThumbnailAsideEffect::repaintAll()
{
foreach (const Data & d, windows)
effects->addRepaint(d.rect);
}
bool ThumbnailAsideEffect::isActive() const
{
return !windows.isEmpty() && !effects->isScreenLocked();
}
} // namespace
#include "thumbnailaside.moc"