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/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

View file

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

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>
#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<BaseWidget *>()) {
// 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 -

View file

@ -28,6 +28,10 @@ BaseWidget::~BaseWidget()
float BaseWidget::getScale() const
{
if (this->overrideScale) {
return this->overrideScale.get();
}
BaseWidget *baseWidget = dynamic_cast<BaseWidget *>(this->window());
if (baseWidget == nullptr) {
@ -37,6 +41,17 @@ float BaseWidget::getScale() const
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
{
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());
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <QWidget>
#include <boost/optional.hpp>
#include <pajlada/signals/signal.hpp>
namespace chatterino {
@ -23,6 +24,9 @@ public:
virtual float getScale() const;
pajlada::Signals::Signal<float> scaleChanged;
void setOverrideScale(boost::optional<float>);
boost::optional<float> 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<float> overrideScale = boost::none;
QSize scaleIndependantSize;
std::vector<BaseWidget *> widgets;

View file

@ -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 <ShellScalingApi.h>
#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 <QVBoxLayout>
@ -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;