fmoved windows message handlers into their own functions

This commit is contained in:
fourtf 2018-07-03 13:04:27 +02:00
parent eb809d1572
commit 9f6d09db7c
2 changed files with 251 additions and 209 deletions

View file

@ -410,13 +410,101 @@ void BaseWindow::moveIntoDesktopRect(QWidget *parent)
this->move(p); this->move(p);
} }
#ifdef USEWINSDK
bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{ {
#ifdef USEWINSDK
MSG *msg = reinterpret_cast<MSG *>(message); MSG *msg = reinterpret_cast<MSG *>(message);
switch (msg->message) { switch (msg->message) {
case WM_DPICHANGED: { case WM_DPICHANGED:
return handleDPICHANGED(msg);
case WM_SHOWWINDOW:
return this->handleSHOWWINDOW(msg);
case WM_NCCALCSIZE:
return this->handleNCCALCSIZE(msg, result);
case WM_SIZE:
return this->handleSIZE(msg);
case WM_NCHITTEST:
return this->handleNCHITTEST(msg, result);
default:
return QWidget::nativeEvent(eventType, message, result);
}
#endif
}
void BaseWindow::scaleChangedEvent(float)
{
#ifdef USEWINSDK
this->calcButtonsSizes();
#endif
}
void BaseWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
if (this->frameless_) {
painter.setPen(QColor("#999"));
painter.drawRect(0, 0, this->width() - 1, this->height() - 1);
}
this->drawCustomWindowFrame(painter);
}
void BaseWindow::updateScale()
{
auto scale = this->nativeScale_ *
(this->flags_ & DisableCustomScaling ? 1 : getApp()->windows->getUiScaleValue());
this->setScale(scale);
for (auto child : this->findChildren<BaseWidget *>()) {
child->setScale(scale);
}
}
void BaseWindow::calcButtonsSizes()
{
if (!this->shown_) {
return;
}
if ((this->width() / this->getScale()) < 300) {
if (this->ui_.minButton)
this->ui_.minButton->setScaleIndependantSize(30, 30);
if (this->ui_.maxButton)
this->ui_.maxButton->setScaleIndependantSize(30, 30);
if (this->ui_.exitButton)
this->ui_.exitButton->setScaleIndependantSize(30, 30);
} else {
if (this->ui_.minButton)
this->ui_.minButton->setScaleIndependantSize(46, 30);
if (this->ui_.maxButton)
this->ui_.maxButton->setScaleIndependantSize(46, 30);
if (this->ui_.exitButton)
this->ui_.exitButton->setScaleIndependantSize(46, 30);
}
}
void BaseWindow::drawCustomWindowFrame(QPainter &painter)
{
#ifdef USEWINSDK
if (this->hasCustomWindowFrame()) {
QPainter painter(this);
painter.fillRect(QRect(0, 1, this->width() - 0, this->height() - 0),
this->themeManager->window.background);
}
#endif
}
bool BaseWindow::handleDPICHANGED(MSG *msg)
{
#ifdef USEWINSDK
int dpi = HIWORD(msg->wParam); int dpi = HIWORD(msg->wParam);
float _scale = dpi / 96.f; float _scale = dpi / 96.f;
@ -427,8 +515,7 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
auto *prcNewWindow = reinterpret_cast<RECT *>(msg->lParam); auto *prcNewWindow = reinterpret_cast<RECT *>(msg->lParam);
SetWindowPos(msg->hwnd, nullptr, prcNewWindow->left, prcNewWindow->top, SetWindowPos(msg->hwnd, nullptr, prcNewWindow->left, prcNewWindow->top,
prcNewWindow->right - prcNewWindow->left, prcNewWindow->right - prcNewWindow->left,
prcNewWindow->bottom - prcNewWindow->top, prcNewWindow->bottom - prcNewWindow->top, SWP_NOZORDER | SWP_NOACTIVATE);
SWP_NOZORDER | SWP_NOACTIVATE);
} }
firstResize = false; firstResize = false;
@ -436,8 +523,14 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
this->updateScale(); this->updateScale();
return true; return true;
#endif
return false;
} }
case WM_SHOWWINDOW: {
bool BaseWindow::handleSHOWWINDOW(MSG *msg)
{
#ifdef USEWINSDK
if (auto dpi = getWindowDpi(msg->hwnd)) { if (auto dpi = getWindowDpi(msg->hwnd)) {
this->nativeScale_ = dpi.get() / 96.f; this->nativeScale_ = dpi.get() / 96.f;
this->updateScale(); this->updateScale();
@ -445,17 +538,20 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
if (!this->shown_ && this->isVisible() && this->hasCustomWindowFrame()) { if (!this->shown_ && this->isVisible() && this->hasCustomWindowFrame()) {
this->shown_ = true; this->shown_ = true;
// SetWindowLongPtr((HWND)this->winId(), GWL_STYLE,
// WS_POPUP | WS_CAPTION | WS_THICKFRAME | WS_MAXIMIZEBOX |
// WS_MINIMIZEBOX);
const MARGINS shadow = {8, 8, 8, 8}; const MARGINS shadow = {8, 8, 8, 8};
DwmExtendFrameIntoClientArea(HWND(this->winId()), &shadow); DwmExtendFrameIntoClientArea(HWND(this->winId()), &shadow);
} }
return true; return true;
#endif
return false;
} }
case WM_NCCALCSIZE: {
bool BaseWindow::handleNCCALCSIZE(MSG *msg, long *result)
{
#ifdef USEWINSDK
if (this->hasCustomWindowFrame()) { if (this->hasCustomWindowFrame()) {
// int cx = GetSystemMetrics(SM_CXSIZEFRAME); // int cx = GetSystemMetrics(SM_CXSIZEFRAME);
// int cy = GetSystemMetrics(SM_CYSIZEFRAME); // int cy = GetSystemMetrics(SM_CYSIZEFRAME);
@ -465,26 +561,23 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
ncp->lppos->flags |= SWP_NOREDRAW; ncp->lppos->flags |= SWP_NOREDRAW;
RECT *clientRect = &ncp->rgrc[0]; RECT *clientRect = &ncp->rgrc[0];
// if (IsWindows10OrGreater()) {
// clientRect->left += cx;
// clientRect->top += 0;
// clientRect->right -= cx;
// clientRect->bottom -= cy;
// } else {
clientRect->left += 1; clientRect->left += 1;
clientRect->top += 0; clientRect->top += 0;
clientRect->right -= 1; clientRect->right -= 1;
clientRect->bottom -= 1; clientRect->bottom -= 1;
// }
} }
*result = 0; *result = 0;
return true; return true;
} else {
return QWidget::nativeEvent(eventType, message, result);
} }
} break; #endif
case WM_SIZE: {
return false;
}
bool BaseWindow::handleSIZE(MSG *msg)
{
#ifdef USEWINSDK
if (this->ui_.windowLayout) { if (this->ui_.windowLayout) {
if (this->hasCustomWindowFrame() && !this->frameless_) { if (this->hasCustomWindowFrame() && !this->frameless_) {
if (msg->wParam == SIZE_MAXIMIZED) { if (msg->wParam == SIZE_MAXIMIZED) {
@ -497,9 +590,14 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
} }
} }
return QWidget::nativeEvent(eventType, message, result); #endif
return false;
} }
case WM_NCHITTEST: {
bool BaseWindow::handleNCHITTEST(MSG *msg, long *result)
{
#ifdef USEWINSDK
const LONG border_width = 8; // in pixels const LONG border_width = 8; // in pixels
RECT winrect; RECT winrect;
GetWindowRect(HWND(winId()), &winrect); GetWindowRect(HWND(winId()), &winrect);
@ -537,13 +635,13 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
} }
if (resizeWidth && resizeHeight) { if (resizeWidth && resizeHeight) {
// bottom left corner // bottom left corner
if (x >= winrect.left && x < winrect.left + border_width && if (x >= winrect.left && x < winrect.left + border_width && y < winrect.bottom &&
y < winrect.bottom && y >= winrect.bottom - border_width) { y >= winrect.bottom - border_width) {
*result = HTBOTTOMLEFT; *result = HTBOTTOMLEFT;
} }
// bottom right corner // bottom right corner
if (x < winrect.right && x >= winrect.right - border_width && if (x < winrect.right && x >= winrect.right - border_width && y < winrect.bottom &&
y < winrect.bottom && y >= winrect.bottom - border_width) { y >= winrect.bottom - border_width) {
*result = HTBOTTOMRIGHT; *result = HTBOTTOMRIGHT;
} }
// top left corner // top left corner
@ -552,8 +650,8 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
*result = HTTOPLEFT; *result = HTTOPLEFT;
} }
// top right corner // top right corner
if (x < winrect.right && x >= winrect.right - border_width && if (x < winrect.right && x >= winrect.right - border_width && y >= winrect.top &&
y >= winrect.top && y < winrect.top + border_width) { y < winrect.top + border_width) {
*result = HTTOPRIGHT; *result = HTTOPRIGHT;
} }
} }
@ -609,74 +707,10 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
} }
return true; return true;
} else {
return QWidget::nativeEvent(eventType, message, result);
}
break;
}
default:
return QWidget::nativeEvent(eventType, message, result);
}
}
void BaseWindow::scaleChangedEvent(float)
{
this->calcButtonsSizes();
} }
#endif #endif
void BaseWindow::paintEvent(QPaintEvent *) return false;
{
if (this->frameless_) {
QPainter painter(this);
painter.setPen(QColor("#999"));
painter.drawRect(0, 0, this->width() - 1, this->height() - 1);
}
#ifdef USEWINSDK
if (this->hasCustomWindowFrame()) {
QPainter painter(this);
// bool windowFocused = this->window() == QApplication::activeWindow();
painter.fillRect(QRect(0, 1, this->width() - 0, this->height() - 0),
this->themeManager->window.background);
}
#endif
}
void BaseWindow::updateScale()
{
auto scale = this->nativeScale_ *
(this->flags_ & DisableCustomScaling ? 1 : getApp()->windows->getUiScaleValue());
this->setScale(scale);
for (auto child : this->findChildren<BaseWidget *>()) {
child->setScale(scale);
}
}
void BaseWindow::calcButtonsSizes()
{
if (!this->shown_) {
return;
}
if ((this->width() / this->getScale()) < 300) {
if (this->ui_.minButton)
this->ui_.minButton->setScaleIndependantSize(30, 30);
if (this->ui_.maxButton)
this->ui_.maxButton->setScaleIndependantSize(30, 30);
if (this->ui_.exitButton)
this->ui_.exitButton->setScaleIndependantSize(30, 30);
} else {
if (this->ui_.minButton)
this->ui_.minButton->setScaleIndependantSize(46, 30);
if (this->ui_.maxButton)
this->ui_.maxButton->setScaleIndependantSize(46, 30);
if (this->ui_.exitButton)
this->ui_.exitButton->setScaleIndependantSize(46, 30);
}
} }
} // namespace chatterino } // namespace chatterino

View file

@ -7,6 +7,8 @@
#include <pajlada/signals/signalholder.hpp> #include <pajlada/signals/signalholder.hpp>
class QHBoxLayout; class QHBoxLayout;
struct tagMSG;
typedef struct tagMSG MSG;
namespace chatterino { namespace chatterino {
@ -30,6 +32,7 @@ public:
}; };
explicit BaseWindow(QWidget *parent = nullptr, Flags flags_ = None); explicit BaseWindow(QWidget *parent = nullptr, Flags flags_ = None);
virtual ~BaseWindow() = default;
QWidget *getLayoutContainer(); QWidget *getLayoutContainer();
bool hasCustomWindowFrame(); bool hasCustomWindowFrame();
@ -46,10 +49,8 @@ public:
Flags getFlags(); Flags getFlags();
protected: protected:
#ifdef USEWINSDK
virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result) override; virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result) override;
virtual void scaleChangedEvent(float) override; virtual void scaleChangedEvent(float) override;
#endif
virtual void paintEvent(QPaintEvent *) override; virtual void paintEvent(QPaintEvent *) override;
@ -73,6 +74,13 @@ private:
void init(); void init();
void moveIntoDesktopRect(QWidget *parent); void moveIntoDesktopRect(QWidget *parent);
void calcButtonsSizes(); void calcButtonsSizes();
void drawCustomWindowFrame(QPainter &painter);
bool handleDPICHANGED(MSG *msg);
bool handleSHOWWINDOW(MSG *msg);
bool handleNCCALCSIZE(MSG *msg, long *result);
bool handleSIZE(MSG *msg);
bool handleNCHITTEST(MSG *msg, long *result);
bool enableCustomFrame_; bool enableCustomFrame_;
bool frameless_; bool frameless_;