From e2beadb67122750430b8dc3bc82a491d482a3ed6 Mon Sep 17 00:00:00 2001 From: Elias Steurer Date: Sat, 3 Dec 2022 17:01:50 +0100 Subject: [PATCH] Fix windows wallpaper fade in calculation again --- ScreenPlayWallpaper/qml/Wallpaper.qml | 50 ++++++++++++++----- .../src/windowsdesktopproperties.cpp | 9 ++++ .../src/windowsdesktopproperties.h | 15 ++++++ ScreenPlayWallpaper/src/winwindow.cpp | 12 +++-- 4 files changed, 68 insertions(+), 18 deletions(-) diff --git a/ScreenPlayWallpaper/qml/Wallpaper.qml b/ScreenPlayWallpaper/qml/Wallpaper.qml index ce9e4449..c05f372a 100644 --- a/ScreenPlayWallpaper/qml/Wallpaper.qml +++ b/ScreenPlayWallpaper/qml/Wallpaper.qml @@ -7,7 +7,6 @@ import ScreenPlay.Enums.VideoCodec Rectangle { id: root - onStateChanged: print(state) property bool canFadeByWallpaperFillMode: true @@ -173,6 +172,43 @@ Rectangle { switch (Wallpaper.windowsDesktopProperties.wallpaperStyle) { case 10: imgCover.fillMode = Image.PreserveAspectCrop + // We only support fade in for one screen + if (Wallpaper.activeScreensList.length !== 1) + return + if (Wallpaper.width === 0) + return + + if (Wallpaper.width > Wallpaper.windowsDesktopProperties.defaultWallpaperSize.width) + return + + // Windows does some weird top margin if the Wallpaper + // is bigger than the monitor. So instead of centering + // it vertically it moves the wallpaper down by 1/3 of + // the remaining height. + // 1. Scale down the wallpaper based on the height. + // The default Windows 11 wallpaper C:\Windows\Web\Wallpaper\Windows\img19.jpg + // has a resoltion of 3841x2400 scaled down to 3440x2150,3 with a given monitor + // resolution of 3440x1440 resulting in a 2150,3 - 1440 = 710,3 + // 710,3 / (1/3) = 236,767 + const monitorWidth = Wallpaper.width + const monitorHeight = Wallpaper.height + const windowsWallpaperWidth = Wallpaper.windowsDesktopProperties.defaultWallpaperSize.width + const windowsWallpapeHeight = Wallpaper.windowsDesktopProperties.defaultWallpaperSize.height + + // 1. Get scale factor: + // -> 3440 / 3840 = 0.8956 + const scaleFactor = monitorWidth / windowsWallpaperWidth + // 2. Scale down the default Windows wallpaper height (width stays the same for correct aspect ratio): + // -> 2400 * 0.8956 = 2149.4 + const scaledDownDefaultWallpaperHeight = windowsWallpapeHeight * scaleFactor + // 3. Calc offste + // -> 2150,3 - 1440 = 710,3 + const offset = scaledDownDefaultWallpaperHeight - monitorHeight + // 4. Calc the one third offset (topMargin) + // -> 710,3 * (1/3) = 236,767 + const topMargin = Math.floor(offset * 0.3333333) + imgCover.anchors.topMargin = -topMargin + break case 6: imgCover.fillMode = Image.PreserveAspectFit @@ -199,18 +235,6 @@ Rectangle { anchors { top: parent.top - topMargin: { - // FHD 16:9 - if (root.width === 1920 && root.height === 1080) - return 0 - // FHD 21:9 - if (root.width === 2560 && root.height === 1080) - return -66 - // WQHD 21:9 - if (root.width === 3440 && root.height === 1440) - return -93 - return 0 - } left: parent.left right: parent.right } diff --git a/ScreenPlayWallpaper/src/windowsdesktopproperties.cpp b/ScreenPlayWallpaper/src/windowsdesktopproperties.cpp index e36d102c..d25ccb87 100644 --- a/ScreenPlayWallpaper/src/windowsdesktopproperties.cpp +++ b/ScreenPlayWallpaper/src/windowsdesktopproperties.cpp @@ -1,5 +1,8 @@ #include "windowsdesktopproperties.h" +#include +#include + /*! \class WindowsDesktopProperties \inmodule ScreenPlayWallpaper @@ -15,6 +18,12 @@ WindowsDesktopProperties::WindowsDesktopProperties(QObject* parent) setWallpaperPath(settings.value("WallPaper").toString()); setIsTiled(settings.value("TileWallpaper").toBool()); + QFileInfo defaultBackgroundImageInfo(m_wallpaperPath); + if(defaultBackgroundImageInfo.exists()){ + QImage backgroundImage(defaultBackgroundImageInfo.absoluteFilePath()); + setDefaultWallpaperSize(backgroundImage.size()); + } + QPoint pos; pos.setX(settings.value("WallpaperOriginX").toInt()); pos.setY(settings.value("WallpaperOriginY").toInt()); diff --git a/ScreenPlayWallpaper/src/windowsdesktopproperties.h b/ScreenPlayWallpaper/src/windowsdesktopproperties.h index 4615ad38..0916582e 100644 --- a/ScreenPlayWallpaper/src/windowsdesktopproperties.h +++ b/ScreenPlayWallpaper/src/windowsdesktopproperties.h @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -51,6 +52,8 @@ public: explicit WindowsDesktopProperties(QObject* parent = nullptr); Q_PROPERTY(int windowsVersion READ windowsVersion WRITE setWindowsVersion NOTIFY windowsVersionChanged) + Q_PROPERTY(QSize defaultWallpaperSize READ defaultWallpaperSize WRITE setDefaultWallpaperSize NOTIFY defaultWallpaperSizeChanged) + Q_PROPERTY(QString wallpaperPath READ wallpaperPath WRITE setWallpaperPath NOTIFY wallpaperPathChanged) Q_PROPERTY(QPoint position READ position WRITE setPosition NOTIFY positionChanged) Q_PROPERTY(bool isTiled READ isTiled WRITE setIsTiled NOTIFY isTiledChanged) @@ -72,6 +75,7 @@ public: int wallpaperStyle() const { return m_wallpaperStyle; } QColor color() const { return m_color; } int windowsVersion() const { return m_windowsVersion; } + QSize defaultWallpaperSize() const { return m_defaultWallpaperSize; } signals: void wallpaperPathChanged(QString wallpaperPath); @@ -81,7 +85,17 @@ signals: void colorChanged(QColor color); void windowsVersionChanged(int windowsVersion); + void defaultWallpaperSizeChanged(); + public slots: + void setDefaultWallpaperSize(const QSize& newDefaultWallpaperSize) + { + if (m_defaultWallpaperSize == newDefaultWallpaperSize) + return; + m_defaultWallpaperSize = newDefaultWallpaperSize; + emit defaultWallpaperSizeChanged(); + } + void setWallpaperPath(QString wallpaperPath) { if (m_wallpaperPath == wallpaperPath) @@ -139,4 +153,5 @@ private: int m_wallpaperStyle; QColor m_color; int m_windowsVersion; + QSize m_defaultWallpaperSize; }; diff --git a/ScreenPlayWallpaper/src/winwindow.cpp b/ScreenPlayWallpaper/src/winwindow.cpp index e7b62de3..729aea85 100644 --- a/ScreenPlayWallpaper/src/winwindow.cpp +++ b/ScreenPlayWallpaper/src/winwindow.cpp @@ -264,7 +264,9 @@ void WinWindow::setupWallpaperForOneScreen(int activeScreen) const int x = monitors.rcMonitors[activeScreen].left + m_zeroPoint.x() + borderOffset; const int y = monitors.rcMonitors[activeScreen].top + m_zeroPoint.y() + borderOffset; qInfo() << QString("Setup window activeScreen: %1 scaling: %2 x: %3 y: %4 width: %5 height: %6").arg(activeScreen).arg(scaling).arg(x).arg(y).arg(width).arg(height); - + // Also set it in BaseWindow. This is needed for Windows fade in. + setWidth(width - boderWidth); + setHeight(height - boderWidth); { // Must be called twice for some reason when window has scaling... if (!SetWindowPos(m_windowHandle, nullptr, x, y, width, height, SWP_HIDEWINDOW)) { @@ -387,7 +389,6 @@ float WinWindow::getScaling(const int monitorIndex) return 1; case 107: return 1.5; - case 109: return 1; case 161: @@ -417,6 +418,7 @@ bool WinWindow::hasWindowScaling() */ void WinWindow::configureWindowGeometry() { + qInfo() << "configureWindowGeometry"; setVisible(false); if (!searchWorkerWindowToParentTo()) { @@ -448,12 +450,12 @@ void WinWindow::configureWindowGeometry() setupWallpaperForMultipleScreens(activeScreensList()); } - setWidth(m_window.width()); - setHeight(m_window.height()); - // Instead of setting "renderType: Text.NativeRendering" every time we can set it here once m_window.setTextRenderType(QQuickWindow::TextRenderType::NativeTextRendering); m_window.setResizeMode(QQuickView::ResizeMode::SizeRootObjectToView); + m_window.setWidth(width()); + m_window.setHeight(height()); + qInfo() << "Setup " << width() << height(); m_window.setSource(QUrl("qrc:/qml/ScreenPlayWallpaper/qml/Wallpaper.qml")); m_window.hide(); }