fixed size of the attachedwindow for the browser extension for scaling

This commit is contained in:
fourtf 2018-06-25 22:06:17 +02:00
parent 13d28d0d8b
commit 6375a902a0
8 changed files with 135 additions and 69 deletions

View file

@ -218,7 +218,8 @@ SOURCES += \
src/widgets/userinfopopup.cpp \ src/widgets/userinfopopup.cpp \
src/widgets/welcomedialog.cpp \ src/widgets/welcomedialog.cpp \
src/widgets/label.cpp \ src/widgets/label.cpp \
src/widgets/settingspages/browserextensionpage.cpp src/widgets/settingspages/browserextensionpage.cpp \
src/util/windows_helper.cpp
HEADERS += \ HEADERS += \
src/precompiled_header.hpp \ src/precompiled_header.hpp \
@ -382,7 +383,8 @@ HEADERS += \
src/widgets/label.hpp \ src/widgets/label.hpp \
src/util/combine_path.hpp \ src/util/combine_path.hpp \
src/widgets/settingspages/browserextensionpage.hpp \ src/widgets/settingspages/browserextensionpage.hpp \
src/nullableptr.hpp src/nullableptr.hpp \
src/util/windows_helper.hpp
RESOURCES += \ RESOURCES += \
resources/resources.qrc resources/resources.qrc

View file

@ -10,46 +10,46 @@
namespace chatterino { namespace chatterino {
namespace util { namespace util {
static boost::optional<UINT> getWindowDpi(quintptr ptr) // static boost::optional<UINT> getWindowDpi(quintptr ptr)
{ //{
typedef UINT(WINAPI * GetDpiForWindow)(HWND); // typedef UINT(WINAPI * GetDpiForWindow)(HWND);
QLibrary user32("user32.dll", 0); // QLibrary user32("user32.dll", 0);
GetDpiForWindow getDpiForWindow = (GetDpiForWindow)user32.resolve("GetDpiForWindow"); // GetDpiForWindow getDpiForWindow = (GetDpiForWindow)user32.resolve("GetDpiForWindow");
if (getDpiForWindow) { // if (getDpiForWindow) {
UINT value = getDpiForWindow((HWND)ptr); // UINT value = getDpiForWindow((HWND)ptr);
return value == 0 ? boost::none : boost::optional<UINT>(value); // return value == 0 ? boost::none : boost::optional<UINT>(value);
} // }
return boost::none; // return boost::none;
} //}
#ifdef USEWINSDK //#ifdef USEWINSDK
class DpiNativeEventFilter : public QAbstractNativeEventFilter // class DpiNativeEventFilter : public QAbstractNativeEventFilter
{ //{
public: // public:
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override // bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override
{ // {
// MSG *msg = reinterpret_cast<MSG *>(message); // // MSG *msg = reinterpret_cast<MSG *>(message);
// if (msg->message == WM_NCCREATE) { // // if (msg->message == WM_NCCREATE) {
// QLibrary user32("user32.dll", 0); // // QLibrary user32("user32.dll", 0);
// { // // {
// typedef BOOL(WINAPI * EnableNonClientDpiScaling)(HWND); // // typedef BOOL(WINAPI * EnableNonClientDpiScaling)(HWND);
// EnableNonClientDpiScaling enableNonClientDpiScaling = // // EnableNonClientDpiScaling enableNonClientDpiScaling =
// (EnableNonClientDpiScaling)user32.resolve("EnableNonClientDpiScaling"); // // (EnableNonClientDpiScaling)user32.resolve("EnableNonClientDpiScaling");
// if (enableNonClientDpiScaling) // // if (enableNonClientDpiScaling)
// enableNonClientDpiScaling(msg->hwnd); // // enableNonClientDpiScaling(msg->hwnd);
// } // // }
// } // // }
return false; // return false;
} // }
}; //};
#endif //#endif
} // namespace util } // namespace util
} // namespace chatterino } // namespace chatterino

View file

@ -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<UINT> 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

View file

@ -0,0 +1,16 @@
#pragma once
#ifdef USEWINSDK
#include <Windows.h>
#include <boost/optional.hpp>
namespace chatterino {
namespace util {
boost::optional<UINT> getWindowDpi(HWND hwnd);
} // namespace util
} // namespace chatterino
#endif

View file

@ -8,6 +8,8 @@
#include <QVBoxLayout> #include <QVBoxLayout>
#ifdef USEWINSDK #ifdef USEWINSDK
#include "util/windows_helper.hpp"
#include "Windows.h" #include "Windows.h"
// don't even think about reordering these // don't even think about reordering these
#include "Psapi.h" #include "Psapi.h"
@ -177,12 +179,24 @@ void AttachedWindow::updateWindowRect_(void *_attachedPtr)
::SetWindowPos(hwnd, next ? next : HWND_TOPMOST, 0, 0, 0, 0, ::SetWindowPos(hwnd, next ? next : HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); 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<BaseWidget *>()) {
// w->setOverrideScale(scale);
// }
// this->ui_.split->setOverrideScale(scale);
}
if (this->height_ == -1) { if (this->height_ == -1) {
::MoveWindow(hwnd, rect.right - this->width_ - 8, rect.top + this->yOffset_ - 8, // ::MoveWindow(hwnd, rect.right - this->width_ - 8, rect.top + this->yOffset_ - 8,
this->width_, rect.bottom - rect.top - this->yOffset_, false); // this->width_, rect.bottom - rect.top - this->yOffset_, false);
} else { } else {
::MoveWindow(hwnd, rect.right - this->width_ - 8, rect.bottom - this->height_ - 8, ::MoveWindow(hwnd, //
this->width_, this->height_, false); 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 - // ::MoveWindow(hwnd, rect.right - 360, rect.top + 82, 360 - 8, rect.bottom -

View file

@ -28,6 +28,10 @@ BaseWidget::~BaseWidget()
float BaseWidget::getScale() const float BaseWidget::getScale() const
{ {
if (this->overrideScale) {
return this->overrideScale.get();
}
BaseWidget *baseWidget = dynamic_cast<BaseWidget *>(this->window()); BaseWidget *baseWidget = dynamic_cast<BaseWidget *>(this->window());
if (baseWidget == nullptr) { if (baseWidget == nullptr) {
@ -37,6 +41,17 @@ float BaseWidget::getScale() const
return baseWidget->scale; return baseWidget->scale;
} }
void BaseWidget::setOverrideScale(boost::optional<float> value)
{
this->overrideScale = value;
this->setScale(this->getScale());
}
boost::optional<float> BaseWidget::getOverrideScale() const
{
return this->overrideScale;
}
QSize BaseWidget::getScaleIndependantSize() const QSize BaseWidget::getScaleIndependantSize() const
{ {
return this->scaleIndependantSize; return this->scaleIndependantSize;
@ -120,8 +135,8 @@ void BaseWidget::setScale(float value)
// update scale value // update scale value
this->scale = value; this->scale = value;
this->scaleChangedEvent(value); this->scaleChangedEvent(this->getScale());
this->scaleChanged.invoke(value); this->scaleChanged.invoke(this->getScale());
this->setScaleIndependantSize(this->getScaleIndependantSize()); this->setScaleIndependantSize(this->getScaleIndependantSize());
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <QWidget> #include <QWidget>
#include <boost/optional.hpp>
#include <pajlada/signals/signal.hpp> #include <pajlada/signals/signal.hpp>
namespace chatterino { namespace chatterino {
@ -23,6 +24,9 @@ public:
virtual float getScale() const; virtual float getScale() const;
pajlada::Signals::Signal<float> scaleChanged; pajlada::Signals::Signal<float> scaleChanged;
void setOverrideScale(boost::optional<float>);
boost::optional<float> getOverrideScale() const;
QSize getScaleIndependantSize() const; QSize getScaleIndependantSize() const;
int getScaleIndependantWidth() const; int getScaleIndependantWidth() const;
int getScaleIndependantHeight() const; int getScaleIndependantHeight() const;
@ -46,6 +50,7 @@ protected:
private: private:
void init(); void init();
float scale = 1.f; float scale = 1.f;
boost::optional<float> overrideScale = boost::none;
QSize scaleIndependantSize; QSize scaleIndependantSize;
std::vector<BaseWidget *> widgets; std::vector<BaseWidget *> widgets;

View file

@ -7,6 +7,7 @@
#include "singletons/windowmanager.hpp" #include "singletons/windowmanager.hpp"
#include "util/nativeeventhelper.hpp" #include "util/nativeeventhelper.hpp"
#include "util/posttothread.hpp" #include "util/posttothread.hpp"
#include "util/windows_helper.hpp"
#include "widgets/helper/rippleeffectlabel.hpp" #include "widgets/helper/rippleeffectlabel.hpp"
#include "widgets/helper/shortcut.hpp" #include "widgets/helper/shortcut.hpp"
#include "widgets/label.hpp" #include "widgets/label.hpp"
@ -29,15 +30,6 @@
//#include <ShellScalingApi.h> //#include <ShellScalingApi.h>
#pragma comment(lib, "Dwmapi.lib") #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 <QHBoxLayout> #include <QHBoxLayout>
#include <QVBoxLayout> #include <QVBoxLayout>
@ -78,7 +70,7 @@ BaseWindow::BaseWindow(QWidget *parent, Flags _flags)
float BaseWindow::getScale() const float BaseWindow::getScale() const
{ {
return this->scale; return this->getOverrideScale().value_or(this->scale);
} }
BaseWindow::Flags BaseWindow::getFlags() BaseWindow::Flags BaseWindow::getFlags()
@ -448,25 +440,9 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
return true; return true;
} }
case WM_SHOWWINDOW: { case WM_SHOWWINDOW: {
// if (IsWindows8Point1OrGreater()) { if (auto dpi = util::getWindowDpi(msg->hwnd)) {
this->nativeScale_ = dpi.get() / 96.f;
static HINSTANCE shcore = LoadLibrary(L"Shcore.dll"); this->updateScale();
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();
}
} }
return true; return true;