Skip to content

Commit

Permalink
Check inclusion and exclusion at configChange event only
Browse files Browse the repository at this point in the history
  • Loading branch information
matinlotfali committed Jul 29, 2024
1 parent 90ee051 commit 51b08d9
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 36 deletions.
43 changes: 27 additions & 16 deletions src/Effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void
ShapeCorners::Effect::windowAdded(KWin::EffectWindow *w)
{
#ifdef QT_DEBUG
qInfo() << "ShapeCorners: window added" << w->windowClass() << "type" << w->windowType() << "role" << w->windowRole();
qInfo() << "ShapeCorners: window added." << Window::debugName(w);
#endif

if (w->windowClass().trimmed().isEmpty()) {
Expand All @@ -90,7 +90,9 @@ ShapeCorners::Effect::windowAdded(KWin::EffectWindow *w)
return;
}

if (const auto& [w2, r] = m_managed.insert({w, ShapeCorners::Window(w, name)}); !r) {
auto window = new Window(w);
auto pair = std::make_pair(w, window);
if (const auto& [iter, r] = m_managed.insert(pair); !r) {
#ifdef QT_DEBUG
qWarning() << "ShapeCorners: ignoring duplicate window.";
#endif
Expand All @@ -108,6 +110,7 @@ ShapeCorners::Effect::windowAdded(KWin::EffectWindow *w)
#else
connect(KWin::effects, &KWin::EffectsHandler::windowFrameGeometryChanged, this, &Effect::windowResized);
#endif
connect(Config::self(), &Config::configChanged, window, &Window::configChanged);
redirect(w);
setShader(w, m_shaderManager.GetShader().get());
checkTiled();
Expand All @@ -118,7 +121,8 @@ void ShapeCorners::Effect::windowRemoved(KWin::EffectWindow *w)
{
auto window_iterator = m_managed.find(w);
if (window_iterator != m_managed.end()) {
qDebug() << "ShapeCorners: window removed" << window_iterator->second.name;
qDebug() << "ShapeCorners: window removed" << window_iterator->first->windowClass();
window_iterator->second->deleteLater();
m_managed.erase(window_iterator);
} else {
qDebug() << "ShapeCorners: window removed";
Expand All @@ -138,21 +142,21 @@ void ShapeCorners::Effect::prePaintWindow(KWin::EffectWindow *w, KWin::WindowPre
auto window_iterator = m_managed.find(w);
if (!m_shaderManager.IsValid()
|| window_iterator == m_managed.end()
|| !window_iterator->second.hasEffect())
|| !window_iterator->second->hasEffect())
{
OffscreenEffect::prePaintWindow(w, data, time);
return;
}

window_iterator->second.animateProperties(time);
window_iterator->second->animateProperties(time);

if(window_iterator->second.hasRoundCorners()) {
if(window_iterator->second->hasRoundCorners()) {
#if QT_VERSION_MAJOR >= 6
const auto geo = w->frameGeometry() * w->screen()->scale();
const auto size = window_iterator->second.cornerRadius * w->screen()->scale();
const auto size = window_iterator->second->cornerRadius * w->screen()->scale();
#else
const auto geo = w->frameGeometry() * KWin::effects->renderTargetScale();
const auto size = window_iterator->second.cornerRadius * KWin::effects->renderTargetScale();
const auto size = window_iterator->second->cornerRadius * KWin::effects->renderTargetScale();
#endif

QRegion reg{};
Expand Down Expand Up @@ -184,7 +188,7 @@ void ShapeCorners::Effect::drawWindow(KWin::EffectWindow *w, int mask, const QRe
auto window_iterator = m_managed.find(w);
if (!m_shaderManager.IsValid()
|| window_iterator == m_managed.end()
|| !window_iterator->second.hasEffect())
|| !window_iterator->second->hasEffect())
{
unredirect(w);
#if QT_VERSION_MAJOR >= 6
Expand All @@ -203,7 +207,7 @@ void ShapeCorners::Effect::drawWindow(KWin::EffectWindow *w, int mask, const QRe

redirect(w);
setShader(w, m_shaderManager.GetShader().get());
m_shaderManager.Bind(window_iterator->second, scale);
m_shaderManager.Bind(*window_iterator->second, scale);
glActiveTexture(GL_TEXTURE0);

#if QT_VERSION_MAJOR >= 6
Expand All @@ -217,8 +221,8 @@ void ShapeCorners::Effect::drawWindow(KWin::EffectWindow *w, int mask, const QRe
QString ShapeCorners::Effect::get_window_titles() const {
QStringList response;
for (const auto& [w, window]: m_managed) {
if (!response.contains(window.name))
response.push_back(window.name);
if (!response.contains(w->windowClass()))
response.push_back(w->windowClass());
}
return response.join("\n");
}
Expand All @@ -242,26 +246,33 @@ void ShapeCorners::Effect::checkMaximized(KWin::EffectWindow *w) {
if (window_iterator == m_managed.end())
return;

window_iterator->second.isMaximized = false;
window_iterator->second->isMaximized = false;

auto screen_region = QRegion(w->screen()->geometry());
#ifdef DEBUG_MAXIMIZED
qDebug() << "ShapeCorners: screen region" << screen_region;
#endif

// subtract all menus
for (auto& [ptr, window]: m_managed)
if (ptr->isDock()) {
#ifdef DEBUG_MAXIMIZED
qDebug() << "ShapeCorners: menu is" << ptr->frameGeometry();
#endif
screen_region -= ptr->frameGeometry().toRect();
}

#ifdef DEBUG_MAXIMIZED
qDebug() << "ShapeCorners: screen region without menus" << screen_region;
#endif

// check if window and screen match
auto remaining = screen_region - w->frameGeometry().toRect();
qDebug() << "ShapeCorners: active window remaining region" << remaining;
if (remaining.isEmpty()) {
window_iterator->second.isMaximized = true;
#ifdef QT_DEBUG
qInfo() << "ShapeCorners: window maximized" << window_iterator->second.name;
window_iterator->second->isMaximized = true;
#ifdef DEBUG_MAXIMIZED
qInfo() << "ShapeCorners: window maximized" << window_iterator->first->windowClass();
#endif
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Effect.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ namespace ShapeCorners {
void windowResized(KWin::EffectWindow *window, const QRectF &);

private:
// Pair of Window pointers and their maximized/tiled state.
std::unordered_map<const KWin::EffectWindow *, Window> m_managed;
WindowList m_managed;
Shader m_shaderManager;

void checkTiled();
Expand Down
6 changes: 3 additions & 3 deletions src/TileChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bool ShapeCorners::TileChecker::checkTiled_Recursive(double window_start, const
bool r = false;
for (auto& [w, window]: m_managed) {

if (!window.hasEffect())
if (!window->hasEffect())
continue;

const auto x = std::get<vertical>(std::make_pair(w->x(), w->y()));
Expand All @@ -39,7 +39,7 @@ bool ShapeCorners::TileChecker::checkTiled_Recursive(double window_start, const

if (x == window_start && width > 0) {
if (checkTiled_Recursive<vertical>(window_start + width + gap, depth+1)) {
window.isTiled = true; // Mark every tile as you go back to the first.
window->isTiled = true; // Mark every tile as you go back to the first.
r = true;
}
}
Expand All @@ -53,7 +53,7 @@ bool ShapeCorners::TileChecker::checkTiled_Recursive(double window_start, const

void ShapeCorners::TileChecker::clearTiles() {
for (auto& [ptr, window]: m_managed) { // Delete tile memory.
window.isTiled = false;
window->isTiled = false;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/TileChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <unordered_map>
#include <cstdint>
#include <memory>

class QRect;
namespace KWin {
Expand All @@ -15,7 +16,7 @@ namespace KWin {

namespace ShapeCorners {
class Window;
using WindowList = std::unordered_map<const KWin::EffectWindow *, Window>;
using WindowList = std::unordered_map<KWin::EffectWindow*, Window*>;

class TileChecker {

Expand Down
45 changes: 36 additions & 9 deletions src/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
#include <qconfig.h>
#if QT_VERSION_MAJOR >= 6
#include <effect/effecthandler.h>
#include <utility>
#else
#include <kwineffects.h>
#endif

QWidget ShapeCorners::Window::m_widget {};

ShapeCorners::Window::Window(KWin::EffectWindow *w, QString name)
: w(w), name(std::move(name))
{ }
ShapeCorners::Window::Window(KWin::EffectWindow *w)
: w(w), isIncluded(false), isExcluded(false)
{
configChanged();
}

bool ShapeCorners::Window::isActive() const {
return KWin::effects->activeWindow() == w;
Expand All @@ -28,9 +29,9 @@ bool ShapeCorners::Window::hasEffect() const {
(
(w->isNormalWindow() && Config::includeNormalWindows())
|| (w->isDialog() && Config::includeDialogs())
|| Config::inclusions().contains(name)
|| isIncluded
)
&& !Config::exclusions().contains(name)
&& !isExcluded
&& (hasRoundCorners() || hasOutline())
);
}
Expand Down Expand Up @@ -154,9 +155,9 @@ void ShapeCorners::Window::animateProperties(const std::chrono::milliseconds& ti
&& deltaOutlineColor.isZero()
&& deltaSecondOutlineColor.isZero()
) {
#ifdef QT_DEBUG
#ifdef DEBUG_ANIMATION
if (repaintCount > 0)
qDebug() << "ShapeCorners: repainted" << name << repaintCount << "times for animation.";
qDebug() << "ShapeCorners: repainted" << w->windowClass() << repaintCount << "times for animation.";
repaintCount = 0;
#endif
return;
Expand All @@ -181,8 +182,34 @@ void ShapeCorners::Window::animateProperties(const std::chrono::milliseconds& ti
secondOutlineColor.clamp();

// the animation is still in progress
#ifdef QT_DEBUG
#ifdef DEBUG_ANIMATION
repaintCount++;
#endif
w->addRepaintFull();
}

QString ShapeCorners::Window::debugName(const KWin::EffectWindow* w) {
return QStringLiteral("\tclass: ") + w->windowClass() +
QStringLiteral("\ttype: ") + QVariant::fromValue(w->windowType()).toString() +
QStringLiteral("\trole: ") + w->windowRole() +
QStringLiteral("\tcaption: ") + w->caption();
}

void ShapeCorners::Window::configChanged() {
for (auto& inclusion: Config::inclusions()) {
if (w->windowClass().contains(inclusion, Qt::CaseInsensitive)
|| w->caption().contains(inclusion, Qt::CaseInsensitive)
) {
isIncluded = true;
break;
}
}
for (auto& exclusion: Config::exclusions()) {
if (w->windowClass().contains(exclusion, Qt::CaseInsensitive)
|| w->caption().contains(exclusion, Qt::CaseInsensitive)
) {
isExcluded = true;
break;
}
}
}
17 changes: 13 additions & 4 deletions src/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ namespace KWin
}

namespace ShapeCorners {
struct Window {
class Window: public QObject {
Q_OBJECT

public:
KWin::EffectWindow *w;
QString name;
bool isTiled = false;
bool isMaximized = false;

Expand All @@ -34,11 +36,11 @@ namespace ShapeCorners {
Color outlineColor = {};
Color secondOutlineColor = {};

#ifdef QT_DEBUG
#ifdef DEBUG_ANIMATION
uint32_t repaintCount = 0;
#endif

explicit Window(KWin::EffectWindow *w, QString name);
explicit Window(KWin::EffectWindow *w);

void animateProperties(const std::chrono::milliseconds &time);

Expand All @@ -50,8 +52,15 @@ namespace ShapeCorners {

[[nodiscard]] bool hasEffect() const;

[[nodiscard]] static QString debugName(const KWin::EffectWindow* w);

public slots:
void configChanged();

private:
std::chrono::milliseconds m_last_time = {};
bool isIncluded;
bool isExcluded;

/**
* \brief Used only for its `palette()` function which holds the currently active highlight colors.
Expand Down
2 changes: 1 addition & 1 deletion tools/test-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if ctest > /dev/null; then
echo "KDE-Rounded-Corners is supported by KWin and doesn't need re-installation."
else
kdialog --msgbox "KDE-Rounded-Corners is not supported by KWin anymore.\n\nThis can probably be for an update.\nWe will now rebuild and reinstall the effect."
make clean
rm -rf ./*
cmake .. --install-prefix /usr
cmake --build . -j &
pid=$!
Expand Down

0 comments on commit 51b08d9

Please sign in to comment.