From 6375a902a0936e52a93233e28a21d42852d92f51 Mon Sep 17 00:00:00 2001 From: fourtf Date: Mon, 25 Jun 2018 22:06:17 +0200 Subject: [PATCH] fixed size of the attachedwindow for the browser extension for scaling --- chatterino.pro | 6 ++-- src/util/nativeeventhelper.hpp | 64 +++++++++++++++++----------------- src/util/windows_helper.cpp | 38 ++++++++++++++++++++ src/util/windows_helper.hpp | 16 +++++++++ src/widgets/attachedwindow.cpp | 22 +++++++++--- src/widgets/basewidget.cpp | 19 ++++++++-- src/widgets/basewidget.hpp | 5 +++ src/widgets/basewindow.cpp | 34 +++--------------- 8 files changed, 135 insertions(+), 69 deletions(-) create mode 100644 src/util/windows_helper.cpp create mode 100644 src/util/windows_helper.hpp diff --git a/chatterino.pro b/chatterino.pro index 38168d72e..c027839ff 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -218,7 +218,8 @@ SOURCES += \ src/widgets/userinfopopup.cpp \ src/widgets/welcomedialog.cpp \ src/widgets/label.cpp \ - src/widgets/settingspages/browserextensionpage.cpp + src/widgets/settingspages/browserextensionpage.cpp \ + src/util/windows_helper.cpp HEADERS += \ src/precompiled_header.hpp \ @@ -382,7 +383,8 @@ HEADERS += \ src/widgets/label.hpp \ src/util/combine_path.hpp \ src/widgets/settingspages/browserextensionpage.hpp \ - src/nullableptr.hpp + src/nullableptr.hpp \ + src/util/windows_helper.hpp RESOURCES += \ resources/resources.qrc diff --git a/src/util/nativeeventhelper.hpp b/src/util/nativeeventhelper.hpp index 982e6bf1e..b78f8dae6 100644 --- a/src/util/nativeeventhelper.hpp +++ b/src/util/nativeeventhelper.hpp @@ -10,46 +10,46 @@ namespace chatterino { namespace util { -static boost::optional getWindowDpi(quintptr ptr) -{ - typedef UINT(WINAPI * GetDpiForWindow)(HWND); - QLibrary user32("user32.dll", 0); +// static boost::optional getWindowDpi(quintptr ptr) +//{ +// typedef UINT(WINAPI * GetDpiForWindow)(HWND); +// QLibrary user32("user32.dll", 0); - GetDpiForWindow getDpiForWindow = (GetDpiForWindow)user32.resolve("GetDpiForWindow"); +// GetDpiForWindow getDpiForWindow = (GetDpiForWindow)user32.resolve("GetDpiForWindow"); - if (getDpiForWindow) { - UINT value = getDpiForWindow((HWND)ptr); +// if (getDpiForWindow) { +// UINT value = getDpiForWindow((HWND)ptr); - return value == 0 ? boost::none : boost::optional(value); - } +// return value == 0 ? boost::none : boost::optional(value); +// } - return boost::none; -} +// return boost::none; +//} -#ifdef USEWINSDK -class DpiNativeEventFilter : public QAbstractNativeEventFilter -{ -public: - bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override - { - // MSG *msg = reinterpret_cast(message); +//#ifdef USEWINSDK +// class DpiNativeEventFilter : public QAbstractNativeEventFilter +//{ +// public: +// bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override +// { +// // MSG *msg = reinterpret_cast(message); - // if (msg->message == WM_NCCREATE) { - // QLibrary user32("user32.dll", 0); - // { - // typedef BOOL(WINAPI * EnableNonClientDpiScaling)(HWND); +// // if (msg->message == WM_NCCREATE) { +// // QLibrary user32("user32.dll", 0); +// // { +// // typedef BOOL(WINAPI * EnableNonClientDpiScaling)(HWND); - // EnableNonClientDpiScaling enableNonClientDpiScaling = - // (EnableNonClientDpiScaling)user32.resolve("EnableNonClientDpiScaling"); +// // EnableNonClientDpiScaling enableNonClientDpiScaling = +// // (EnableNonClientDpiScaling)user32.resolve("EnableNonClientDpiScaling"); - // if (enableNonClientDpiScaling) - // enableNonClientDpiScaling(msg->hwnd); - // } - // } - return false; - } -}; -#endif +// // if (enableNonClientDpiScaling) +// // enableNonClientDpiScaling(msg->hwnd); +// // } +// // } +// return false; +// } +//}; +//#endif } // namespace util } // namespace chatterino diff --git a/src/util/windows_helper.cpp b/src/util/windows_helper.cpp new file mode 100644 index 000000000..a215ed960 --- /dev/null +++ b/src/util/windows_helper.cpp @@ -0,0 +1,38 @@ +#include "windows_helper.hpp" + +#ifdef USEWINSDK + +namespace chatterino { +namespace util { + +typedef enum MONITOR_DPI_TYPE { + MDT_EFFECTIVE_DPI = 0, + MDT_ANGULAR_DPI = 1, + MDT_RAW_DPI = 2, + MDT_DEFAULT = MDT_EFFECTIVE_DPI +} MONITOR_DPI_TYPE; + +typedef HRESULT(CALLBACK *GetDpiForMonitor_)(HMONITOR, MONITOR_DPI_TYPE, UINT *, UINT *); + +boost::optional getWindowDpi(HWND hwnd) +{ + static HINSTANCE shcore = LoadLibrary(L"Shcore.dll"); + if (shcore != nullptr) { + if (auto getDpiForMonitor = GetDpiForMonitor_(GetProcAddress(shcore, "GetDpiForMonitor"))) { + HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); + + UINT xScale, yScale; + + getDpiForMonitor(monitor, MDT_DEFAULT, &xScale, &yScale); + + return xScale; + } + } + + return boost::none; +} + +} // namespace util +} // namespace chatterino + +#endif diff --git a/src/util/windows_helper.hpp b/src/util/windows_helper.hpp new file mode 100644 index 000000000..5ecc03424 --- /dev/null +++ b/src/util/windows_helper.hpp @@ -0,0 +1,16 @@ +#pragma once + +#ifdef USEWINSDK + +#include +#include + +namespace chatterino { +namespace util { + +boost::optional getWindowDpi(HWND hwnd); + +} // namespace util +} // namespace chatterino + +#endif diff --git a/src/widgets/attachedwindow.cpp b/src/widgets/attachedwindow.cpp index 12e376924..235a5cc7a 100644 --- a/src/widgets/attachedwindow.cpp +++ b/src/widgets/attachedwindow.cpp @@ -8,6 +8,8 @@ #include #ifdef USEWINSDK +#include "util/windows_helper.hpp" + #include "Windows.h" // don't even think about reordering these #include "Psapi.h" @@ -177,12 +179,24 @@ void AttachedWindow::updateWindowRect_(void *_attachedPtr) ::SetWindowPos(hwnd, next ? next : HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); + float scale = 1.f; + if (auto dpi = util::getWindowDpi(attached)) { + scale = dpi.get() / 96.f; + + // for (auto w : this->ui_.split->findChildren()) { + // w->setOverrideScale(scale); + // } + // this->ui_.split->setOverrideScale(scale); + } + if (this->height_ == -1) { - ::MoveWindow(hwnd, rect.right - this->width_ - 8, rect.top + this->yOffset_ - 8, - this->width_, rect.bottom - rect.top - this->yOffset_, false); + // ::MoveWindow(hwnd, rect.right - this->width_ - 8, rect.top + this->yOffset_ - 8, + // this->width_, rect.bottom - rect.top - this->yOffset_, false); } else { - ::MoveWindow(hwnd, rect.right - this->width_ - 8, rect.bottom - this->height_ - 8, - this->width_, this->height_, false); + ::MoveWindow(hwnd, // + int(rect.right - this->width_ * scale - 8), // + int(rect.bottom - this->height_ * scale - 8), // + int(this->width_ * scale), int(this->height_ * scale), true); } // ::MoveWindow(hwnd, rect.right - 360, rect.top + 82, 360 - 8, rect.bottom - diff --git a/src/widgets/basewidget.cpp b/src/widgets/basewidget.cpp index 493871e42..b94698551 100644 --- a/src/widgets/basewidget.cpp +++ b/src/widgets/basewidget.cpp @@ -28,6 +28,10 @@ BaseWidget::~BaseWidget() float BaseWidget::getScale() const { + if (this->overrideScale) { + return this->overrideScale.get(); + } + BaseWidget *baseWidget = dynamic_cast(this->window()); if (baseWidget == nullptr) { @@ -37,6 +41,17 @@ float BaseWidget::getScale() const return baseWidget->scale; } +void BaseWidget::setOverrideScale(boost::optional value) +{ + this->overrideScale = value; + this->setScale(this->getScale()); +} + +boost::optional BaseWidget::getOverrideScale() const +{ + return this->overrideScale; +} + QSize BaseWidget::getScaleIndependantSize() const { return this->scaleIndependantSize; @@ -120,8 +135,8 @@ void BaseWidget::setScale(float value) // update scale value this->scale = value; - this->scaleChangedEvent(value); - this->scaleChanged.invoke(value); + this->scaleChangedEvent(this->getScale()); + this->scaleChanged.invoke(this->getScale()); this->setScaleIndependantSize(this->getScaleIndependantSize()); } diff --git a/src/widgets/basewidget.hpp b/src/widgets/basewidget.hpp index 1348492bf..681c8e944 100644 --- a/src/widgets/basewidget.hpp +++ b/src/widgets/basewidget.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include namespace chatterino { @@ -23,6 +24,9 @@ public: virtual float getScale() const; pajlada::Signals::Signal scaleChanged; + void setOverrideScale(boost::optional); + boost::optional getOverrideScale() const; + QSize getScaleIndependantSize() const; int getScaleIndependantWidth() const; int getScaleIndependantHeight() const; @@ -46,6 +50,7 @@ protected: private: void init(); float scale = 1.f; + boost::optional overrideScale = boost::none; QSize scaleIndependantSize; std::vector widgets; diff --git a/src/widgets/basewindow.cpp b/src/widgets/basewindow.cpp index d6b32b9f9..81bdc31ae 100644 --- a/src/widgets/basewindow.cpp +++ b/src/widgets/basewindow.cpp @@ -7,6 +7,7 @@ #include "singletons/windowmanager.hpp" #include "util/nativeeventhelper.hpp" #include "util/posttothread.hpp" +#include "util/windows_helper.hpp" #include "widgets/helper/rippleeffectlabel.hpp" #include "widgets/helper/shortcut.hpp" #include "widgets/label.hpp" @@ -29,15 +30,6 @@ //#include #pragma comment(lib, "Dwmapi.lib") -typedef enum MONITOR_DPI_TYPE { - MDT_EFFECTIVE_DPI = 0, - MDT_ANGULAR_DPI = 1, - MDT_RAW_DPI = 2, - MDT_DEFAULT = MDT_EFFECTIVE_DPI -} MONITOR_DPI_TYPE; - -typedef HRESULT(CALLBACK *GetDpiForMonitor_)(HMONITOR, MONITOR_DPI_TYPE, UINT *, UINT *); - #include #include @@ -78,7 +70,7 @@ BaseWindow::BaseWindow(QWidget *parent, Flags _flags) float BaseWindow::getScale() const { - return this->scale; + return this->getOverrideScale().value_or(this->scale); } BaseWindow::Flags BaseWindow::getFlags() @@ -448,25 +440,9 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r return true; } case WM_SHOWWINDOW: { - // if (IsWindows8Point1OrGreater()) { - - static HINSTANCE shcore = LoadLibrary(L"Shcore.dll"); - if (shcore != nullptr) { - if (auto getDpiForMonitor = - GetDpiForMonitor_(GetProcAddress(shcore, "GetDpiForMonitor"))) { - HMONITOR monitor = MonitorFromWindow(msg->hwnd, MONITOR_DEFAULTTONEAREST); - - UINT xScale, yScale; - - getDpiForMonitor(monitor, MDT_DEFAULT, &xScale, &yScale); - - // GetDpiForMonitor(monitor, MDT_DEFAULT, &xScale, &yScale); - - float scale = xScale / 96.f; - - this->nativeScale_ = scale; - this->updateScale(); - } + if (auto dpi = util::getWindowDpi(msg->hwnd)) { + this->nativeScale_ = dpi.get() / 96.f; + this->updateScale(); } return true;