From 8ebf9cfc583d2030e5d29ed4d8d9db8cd937d408 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Sun, 21 Mar 2021 11:13:09 +0800 Subject: [PATCH] Disable acrylic blur by default If the user want to use it, the user should enable it. Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- examples/quick/qml/main.qml | 1 + examples/widget/widget.cpp | 11 +++++++++++ examples/widget/widget.h | 1 + qtacryliceffecthelper.cpp | 12 ++++++++++-- qtacrylicitem.cpp | 2 -- qtacrylicitem.h | 2 +- qtacrylicwidget.cpp | 4 ---- utilities.cpp | 19 ++++++++++++++++++- utilities.h | 3 ++- 9 files changed, 44 insertions(+), 11 deletions(-) diff --git a/examples/quick/qml/main.qml b/examples/quick/qml/main.qml index e45912f..1b91619 100644 --- a/examples/quick/qml/main.qml +++ b/examples/quick/qml/main.qml @@ -50,6 +50,7 @@ Window { AcrylicItem { id: acrylicItem anchors.fill: parent + acrylicEnabled: true frameVisible: true } diff --git a/examples/widget/widget.cpp b/examples/widget/widget.cpp index 3b7814e..0c84c60 100644 --- a/examples/widget/widget.cpp +++ b/examples/widget/widget.cpp @@ -49,6 +49,17 @@ void Widget::moveToDesktopCenter() move(newX, newY); } +void Widget::showEvent(QShowEvent *event) +{ + QtAcrylicWidget::showEvent(event); + static bool inited = false; + if (!inited) { + FramelessWindowsManager::addWindow(windowHandle()); + setAcrylicEnabled(true); + inited = true; + } +} + void Widget::timerEvent(QTimerEvent *event) { QtAcrylicWidget::timerEvent(event); diff --git a/examples/widget/widget.h b/examples/widget/widget.h index 913d210..575caee 100644 --- a/examples/widget/widget.h +++ b/examples/widget/widget.h @@ -43,6 +43,7 @@ public: Q_INVOKABLE void moveToDesktopCenter(); protected: + void showEvent(QShowEvent *event) override; void timerEvent(QTimerEvent *event) override; private: diff --git a/qtacryliceffecthelper.cpp b/qtacryliceffecthelper.cpp index 756a8e1..a31735e 100644 --- a/qtacryliceffecthelper.cpp +++ b/qtacryliceffecthelper.cpp @@ -227,7 +227,12 @@ void QtAcrylicEffectHelper::paintBackground(QPainter *painter, const QRect &rect } else { // Emulate blur behind window by blurring the desktop wallpaper. updateBehindWindowBackground(); - painter->drawPixmap(QPoint{0, 0}, m_bluredWallpaper, QRect{m_window->mapToGlobal(QPoint{0, 0}), rect.size()}); + QPoint point = m_window->mapToGlobal(QPoint{0, 0}); + const QRect geometry = Utilities::getScreenAvailableGeometry(m_window); + if (geometry.isValid()) { + point -= geometry.topLeft(); + } + painter->drawPixmap(QPoint{0, 0}, m_bluredWallpaper, QRect{point, rect.size()}); } painter->setCompositionMode(QPainter::CompositionMode_SourceOver); painter->setOpacity(1); @@ -299,10 +304,13 @@ void QtAcrylicEffectHelper::updateAcrylicBrush(const QColor &alternativeTintColo void QtAcrylicEffectHelper::updateBehindWindowBackground() { + if (!checkWindow()) { + return; + } if (!m_bluredWallpaper.isNull()) { return; } - const QSize size = Utilities::getScreenAvailableGeometry().size(); + const QSize size = Utilities::getScreenAvailableGeometry(m_window).size(); m_bluredWallpaper = QPixmap(size); m_bluredWallpaper.fill(Qt::transparent); QImage image = Utilities::getDesktopWallpaperImage(); diff --git a/qtacrylicitem.cpp b/qtacrylicitem.cpp index 632bf45..e1783f6 100644 --- a/qtacrylicitem.cpp +++ b/qtacrylicitem.cpp @@ -30,8 +30,6 @@ QtAcrylicItem::QtAcrylicItem(QQuickItem *parent) : QQuickPaintedItem(parent) { - m_acrylicHelper.showWarning(); - connect(this, &QtAcrylicItem::windowChanged, this, [this](QQuickWindow *win){ if (m_repaintConnection) { disconnect(m_repaintConnection); diff --git a/qtacrylicitem.h b/qtacrylicitem.h index 69e8297..e5e99b9 100644 --- a/qtacrylicitem.h +++ b/qtacrylicitem.h @@ -83,5 +83,5 @@ private: QtAcrylicEffectHelper m_acrylicHelper; bool m_frameVisible = true; QMetaObject::Connection m_repaintConnection = {}; - bool m_acrylicEnabled = true; + bool m_acrylicEnabled = false; }; diff --git a/qtacrylicwidget.cpp b/qtacrylicwidget.cpp index 8b98de6..d9d962c 100644 --- a/qtacrylicwidget.cpp +++ b/qtacrylicwidget.cpp @@ -24,14 +24,12 @@ #include "qtacrylicwidget.h" #include "utilities.h" -#include "framelesswindowsmanager.h" #include #include #include QtAcrylicWidget::QtAcrylicWidget(QWidget *parent) : QWidget(parent) { - m_acrylicHelper.showWarning(); } QtAcrylicWidget::~QtAcrylicWidget() = default; @@ -163,11 +161,9 @@ void QtAcrylicWidget::showEvent(QShowEvent *event) { QWidget::showEvent(event); if (!m_inited) { - FramelessWindowsManager::addWindow(windowHandle()); m_acrylicHelper.install(windowHandle()); m_acrylicHelper.updateAcrylicBrush(tintColor()); connect(&m_acrylicHelper, &QtAcrylicEffectHelper::needsRepaint, this, qOverload<>(&QtAcrylicWidget::update)); - setAcrylicEnabled(true); m_inited = true; } } diff --git a/utilities.cpp b/utilities.cpp index b2b0ba1..736c265 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -374,8 +374,14 @@ QWindow *Utilities::findWindow(const WId winId) return nullptr; } -QRect Utilities::getScreenAvailableGeometry() +QRect Utilities::getScreenAvailableGeometry(const QWindow *window) { + if (window) { + const QScreen *screen = window->screen(); + if (screen) { + return screen->availableGeometry(); + } + } return QGuiApplication::primaryScreen()->availableGeometry(); } @@ -470,3 +476,14 @@ bool Utilities::isMouseInSpecificObjects(const QPointF &mousePos, const QObjectL } return false; } + +QRect Utilities::getScreenAvailableGeometry(const QPoint &pos) +{ + if (!pos.isNull()) { + const QScreen *screen = QGuiApplication::screenAt(pos); + if (screen) { + return screen->availableGeometry(); + } + } + return QGuiApplication::primaryScreen()->availableGeometry(); +} diff --git a/utilities.h b/utilities.h index ae33251..d9ab627 100644 --- a/utilities.h +++ b/utilities.h @@ -61,7 +61,8 @@ FRAMELESSHELPER_EXPORT QWindow *findWindow(const WId winId); FRAMELESSHELPER_EXPORT QImage getDesktopWallpaperImage(const int screen = -1); FRAMELESSHELPER_EXPORT DesktopWallpaperAspectStyle getDesktopWallpaperAspectStyle(const int screen = -1); -FRAMELESSHELPER_EXPORT QRect getScreenAvailableGeometry(); +FRAMELESSHELPER_EXPORT QRect getScreenAvailableGeometry(const QWindow *window); +FRAMELESSHELPER_EXPORT QRect getScreenAvailableGeometry(const QPoint &pos); FRAMELESSHELPER_EXPORT QRect alignedRect(const Qt::LayoutDirection direction, const Qt::Alignment alignment, const QSize &size, const QRect &rectangle);