From 206a3518b0684af127e42d94c0b4ea62e615e45f Mon Sep 17 00:00:00 2001 From: Daniel _ Date: Sat, 23 Sep 2017 18:37:51 +0200 Subject: [PATCH] fixed dpi not initializing when the window shows --- src/main.cpp | 12 +++++++----- src/util/nativeeventhelper.hpp | 23 +++++++++++++++++++++-- src/widgets/basewidget.cpp | 11 +++++++++++ src/widgets/basewidget.hpp | 1 + src/widgets/emotepopup.cpp | 6 ++++-- src/widgets/mainwindow.cpp | 2 ++ 6 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d5dd70da4..ab0ca1bcf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,14 +22,16 @@ public: MSG *msg = reinterpret_cast(message); if (msg->message == WM_NCCREATE) { - typedef BOOL(WINAPI * EnableNonClientDpiScaling)(HWND); QLibrary user32("user32.dll", NULL); + { + typedef BOOL(WINAPI * EnableNonClientDpiScaling)(HWND); - EnableNonClientDpiScaling enableNonClientDpiScaling = - (EnableNonClientDpiScaling)user32.resolve("EnableNonClientDpiScaling"); + EnableNonClientDpiScaling enableNonClientDpiScaling = + (EnableNonClientDpiScaling)user32.resolve("EnableNonClientDpiScaling"); - if (enableNonClientDpiScaling) - enableNonClientDpiScaling(msg->hwnd); + if (enableNonClientDpiScaling) + enableNonClientDpiScaling(msg->hwnd); + } } return false; } diff --git a/src/util/nativeeventhelper.hpp b/src/util/nativeeventhelper.hpp index ee9731954..b6df01e4d 100644 --- a/src/util/nativeeventhelper.hpp +++ b/src/util/nativeeventhelper.hpp @@ -1,7 +1,10 @@ #pragma once #ifdef USEWINSDK -#include "windows.h" +#include +#include + +#include namespace chatterino { namespace util { @@ -17,7 +20,23 @@ static bool tryHandleDpiChangedMessage(void *message, int &dpi) } return false; } + +static boost::optional getWindowDpi(quintptr ptr) +{ + typedef UINT(WINAPI * GetDpiForWindow)(HWND); + QLibrary user32("user32.dll", NULL); + + GetDpiForWindow getDpiForWindow = (GetDpiForWindow)user32.resolve("GetDpiForWindow"); + + if (getDpiForWindow) { + UINT value = getDpiForWindow((HWND)ptr); + + return value == 0 ? boost::none : boost::optional(value); + } + + return boost::none; } -} +} // namespace util +} // namespace chatterino #endif diff --git a/src/widgets/basewidget.cpp b/src/widgets/basewidget.cpp index dab5ebf97..9790b5687 100644 --- a/src/widgets/basewidget.cpp +++ b/src/widgets/basewidget.cpp @@ -51,6 +51,17 @@ void BaseWidget::init() }); } +void BaseWidget::initAsWindow() +{ +#ifdef USEWINSDK + auto dpi = util::getWindowDpi(this->winId()); + + if (dpi) { + this->dpiMultiplier = dpi.value() / 96.f; + } +#endif +} + void BaseWidget::refreshTheme() { // Do any color scheme updates here diff --git a/src/widgets/basewidget.hpp b/src/widgets/basewidget.hpp index 3ebcd61c3..af6bed0cb 100644 --- a/src/widgets/basewidget.hpp +++ b/src/widgets/basewidget.hpp @@ -32,6 +32,7 @@ protected: #ifdef USEWINSDK virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result) override; #endif + void initAsWindow(); private: float dpiMultiplier = 1.f; diff --git a/src/widgets/emotepopup.cpp b/src/widgets/emotepopup.cpp index ec30e1b64..c137e5e55 100644 --- a/src/widgets/emotepopup.cpp +++ b/src/widgets/emotepopup.cpp @@ -16,6 +16,8 @@ EmotePopup::EmotePopup(ColorScheme &colorScheme, EmoteManager &emoteManager, : BaseWidget(colorScheme, 0) , emoteManager(emoteManager) { + this->initAsWindow(); + QHBoxLayout *layout = new QHBoxLayout(this); this->setLayout(layout); layout->setMargin(0); @@ -69,5 +71,5 @@ void EmotePopup::loadChannel(std::shared_ptr _channel) this->view->setChannel(emoteChannel); } -} -} +} // namespace widgets +} // namespace chatterino diff --git a/src/widgets/mainwindow.cpp b/src/widgets/mainwindow.cpp index 97f3625a3..d6d140b62 100644 --- a/src/widgets/mainwindow.cpp +++ b/src/widgets/mainwindow.cpp @@ -26,6 +26,8 @@ MainWindow::MainWindow(ChannelManager &_channelManager, ColorScheme &_colorSchem , dpi(this->getDpiMultiplier()) // , windowGeometry("/windows/0/geometry") { + this->initAsWindow(); + QVBoxLayout *layout = new QVBoxLayout(this); // add titlebar