diff --git a/chatterino.pro b/chatterino.pro index eabd8d396..5afd6277c 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -163,7 +163,8 @@ HEADERS += \ src/widgets/rippleeffectlabel.hpp \ src/widgets/qualitypopup.hpp \ src/widgets/emotepopup.hpp \ - src/messages/messagecolor.hpp + src/messages/messagecolor.hpp \ + src/util/nativeeventhelper.hpp PRECOMPILED_HEADER = diff --git a/src/util/nativeeventhelper.hpp b/src/util/nativeeventhelper.hpp new file mode 100644 index 000000000..ee9731954 --- /dev/null +++ b/src/util/nativeeventhelper.hpp @@ -0,0 +1,23 @@ +#pragma once + +#ifdef USEWINSDK +#include "windows.h" + +namespace chatterino { +namespace util { +static bool tryHandleDpiChangedMessage(void *message, int &dpi) +{ + MSG *msg = reinterpret_cast(message); + + // WM_DPICHANGED + if (msg->message == 0x02E0) { + dpi = HIWORD(msg->wParam); + + return true; + } + return false; +} +} +} + +#endif diff --git a/src/widgets/basewidget.cpp b/src/widgets/basewidget.cpp index ea2b46356..dab5ebf97 100644 --- a/src/widgets/basewidget.cpp +++ b/src/widgets/basewidget.cpp @@ -1,15 +1,21 @@ #include "widgets/basewidget.hpp" #include "colorscheme.hpp" +//#include #include +//#include +//#include #include +#include "util/nativeeventhelper.hpp" namespace chatterino { namespace widgets { +// BaseWidget::BaseWidget(ColorScheme &_colorScheme, WindowManager &_windowManager, QWidget *parent) BaseWidget::BaseWidget(ColorScheme &_colorScheme, QWidget *parent) : QWidget(parent) , colorScheme(_colorScheme) +// , windowManager(_windowManager) { this->init(); } @@ -17,10 +23,25 @@ BaseWidget::BaseWidget(ColorScheme &_colorScheme, QWidget *parent) BaseWidget::BaseWidget(BaseWidget *parent) : QWidget(parent) , colorScheme(parent->colorScheme) +// , windowManager(parent->windowManager) { this->init(); } +float BaseWidget::getDpiMultiplier() +{ + BaseWidget *baseWidget = dynamic_cast(this->window()); + + if (baseWidget == nullptr) { + return 1.f; + } else { + return baseWidget->dpiMultiplier; + // int screenNr = QApplication::desktop()->screenNumber(this); + // QScreen *screen = QApplication::screens().at(screenNr); + // return screen->logicalDotsPerInch() / 96.f; + } +} + void BaseWidget::init() { this->colorScheme.updated.connect([this]() { @@ -35,5 +56,28 @@ void BaseWidget::refreshTheme() // Do any color scheme updates here } +#ifdef USEWINSDK +bool BaseWidget::nativeEvent(const QByteArray &eventType, void *message, long *result) +{ + int dpi; + + if (util::tryHandleDpiChangedMessage(message, dpi)) { + qDebug() << "dpi changed"; + + float oldDpiMultiplier = this->dpiMultiplier; + this->dpiMultiplier = dpi / 96.f; + float scale = this->dpiMultiplier / oldDpiMultiplier; + + qDebug() << scale; + qDebug() << dpi; + + this->resize(static_cast(this->width() * scale), + static_cast(this->height() * scale)); + } + + return QWidget::nativeEvent(eventType, message, result); +} +#endif + } // namespace widgets } // namespace chatterino diff --git a/src/widgets/basewidget.hpp b/src/widgets/basewidget.hpp index e2322e40a..3ebcd61c3 100644 --- a/src/widgets/basewidget.hpp +++ b/src/widgets/basewidget.hpp @@ -2,6 +2,8 @@ #include +//#include "windowmanager.hpp" + namespace chatterino { class ColorScheme; @@ -13,13 +15,27 @@ class BaseWidget : public QWidget Q_OBJECT public: + // explicit BaseWidget(ColorScheme &_colorScheme, WindowManager &windowManager, QWidget + // *parent); explicit BaseWidget(ColorScheme &_colorScheme, QWidget *parent); explicit BaseWidget(BaseWidget *parent); ColorScheme &colorScheme; + float getDpiMultiplier(); + + // protected: + // WindowManager &windowManager; + +protected: +#ifdef USEWINSDK + virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result) override; +#endif + private: + float dpiMultiplier = 1.f; + void init(); virtual void refreshTheme(); diff --git a/src/widgets/chatwidgetheader.cpp b/src/widgets/chatwidgetheader.cpp index fe510e4b2..1c6bc7f17 100644 --- a/src/widgets/chatwidgetheader.cpp +++ b/src/widgets/chatwidgetheader.cpp @@ -21,8 +21,6 @@ ChatWidgetHeader::ChatWidgetHeader(ChatWidget *_chatWidget) , rightLabel(this) , rightMenu(this) { - this->setFixedHeight(32); - this->refreshTheme(); this->updateChannelText(); @@ -76,6 +74,11 @@ ChatWidgetHeader::ChatWidgetHeader(ChatWidget *_chatWidget) timer->start(60000); } +void ChatWidgetHeader::resizeEvent(QResizeEvent *event) +{ + this->setFixedHeight(static_cast(32 * getDpiMultiplier())); +} + void ChatWidgetHeader::updateChannelText() { const std::string channelName = this->chatWidget->channelName; @@ -85,14 +88,14 @@ void ChatWidgetHeader::updateChannelText() auto channel = this->chatWidget->getChannel(); twitch::TwitchChannel *twitchChannel = dynamic_cast(channel.get()); - + if (twitchChannel != nullptr && twitchChannel->isLive) { this->channelNameLabel.setText(QString::fromStdString(channelName) + " (live)"); this->setToolTip("" "

" + - twitchChannel->streamStatus + "

" + - twitchChannel->streamGame + "
" - "Live for " + + twitchChannel->streamStatus + "

" + twitchChannel->streamGame + + "
" + "Live for " + twitchChannel->streamUptime + " with " + twitchChannel->streamViewerCount + " viewers" "

"); diff --git a/src/widgets/chatwidgetheader.hpp b/src/widgets/chatwidgetheader.hpp index f03c26a88..3e4188342 100644 --- a/src/widgets/chatwidgetheader.hpp +++ b/src/widgets/chatwidgetheader.hpp @@ -36,6 +36,7 @@ protected: virtual void mousePressEvent(QMouseEvent *event) override; virtual void mouseMoveEvent(QMouseEvent *event) override; virtual void mouseDoubleClickEvent(QMouseEvent *event) override; + virtual void resizeEvent(QResizeEvent *event) override; private: ChatWidget *const chatWidget; @@ -66,7 +67,6 @@ public slots: void menuReloadChannelEmotes(); void menuManualReconnect(); void menuShowChangelog(); - }; } // namespace widgets diff --git a/src/widgets/mainwindow.cpp b/src/widgets/mainwindow.cpp index 9107b0c44..97f3625a3 100644 --- a/src/widgets/mainwindow.cpp +++ b/src/widgets/mainwindow.cpp @@ -13,10 +13,6 @@ #include #include -#ifdef USEWINSDK -#include "windows.h" -#endif - namespace chatterino { namespace widgets { @@ -27,6 +23,7 @@ MainWindow::MainWindow(ChannelManager &_channelManager, ColorScheme &_colorSchem , colorScheme(_colorScheme) , completionManager(_completionManager) , notebook(this->channelManager, this) + , dpi(this->getDpiMultiplier()) // , windowGeometry("/windows/0/geometry") { QVBoxLayout *layout = new QVBoxLayout(this); @@ -71,19 +68,6 @@ MainWindow::~MainWindow() { } -#ifdef USEWINSDK -bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) -{ - MSG *msg = reinterpret_cast(message); - - if (msg->message == 0x02E0) { - qDebug() << "dpi changed"; - } - - return QWidget::nativeEvent(eventType, message, result); -} -#endif - void MainWindow::repaintVisibleChatWidgets(Channel *channel) { auto *page = this->notebook.getSelectedPage(); diff --git a/src/widgets/mainwindow.hpp b/src/widgets/mainwindow.hpp index 6e96a0ea4..2b9d68ad7 100644 --- a/src/widgets/mainwindow.hpp +++ b/src/widgets/mainwindow.hpp @@ -42,11 +42,10 @@ public: protected: virtual void closeEvent(QCloseEvent *event) override; -#ifdef USEWINSDK - virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result) override; -#endif private: + float dpi; + virtual void refreshTheme() override; ChannelManager &channelManager; diff --git a/src/widgets/notebook.cpp b/src/widgets/notebook.cpp index b8c19b42c..6ca9ea250 100644 --- a/src/widgets/notebook.cpp +++ b/src/widgets/notebook.cpp @@ -31,15 +31,11 @@ Notebook::Notebook(ChannelManager &_channelManager, MainWindow *parent) this->connect(&this->userButton, SIGNAL(clicked()), this, SLOT(usersButtonClicked())); this->connect(&this->addButton, SIGNAL(clicked()), this, SLOT(addPageButtonClicked())); - this->settingsButton.resize(24, 24); this->settingsButton.icon = NotebookButton::IconSettings; - this->userButton.resize(24, 24); this->userButton.move(24, 0); this->userButton.icon = NotebookButton::IconUser; - this->addButton.resize(24, 24); - SettingsManager::getInstance().hidePreferencesButton.valueChanged.connect( [this](const bool &) { this->performLayout(); }); SettingsManager::getInstance().hideUserButton.valueChanged.connect( @@ -163,27 +159,26 @@ void Notebook::previousTab() void Notebook::performLayout(bool animated) { int x = 0, y = 0; + float scale = this->getDpiMultiplier(); if (SettingsManager::getInstance().hidePreferencesButton.get()) { this->settingsButton.hide(); } else { this->settingsButton.show(); - x += 24; + x += settingsButton.width(); } if (SettingsManager::getInstance().hideUserButton.get()) { this->userButton.hide(); } else { this->userButton.move(x, 0); this->userButton.show(); - x += 24; + x += userButton.width(); } - int tabHeight = 16; + int tabHeight = static_cast(24 * scale); bool first = true; for (auto &i : this->pages) { - tabHeight = i->getTab()->height(); - if (!first && (i == this->pages.last() ? tabHeight : 0) + x + i->getTab()->width() > width()) { y += i->getTab()->height(); @@ -207,6 +202,16 @@ void Notebook::performLayout(bool animated) void Notebook::resizeEvent(QResizeEvent *) { + float scale = this->getDpiMultiplier(); + + this->settingsButton.resize(static_cast(24 * scale), static_cast(24 * scale)); + this->userButton.resize(static_cast(24 * scale), static_cast(24 * scale)); + this->addButton.resize(static_cast(24 * scale), static_cast(24 * scale)); + + for (auto &i : this->pages) { + i->getTab()->calcSize(); + } + this->performLayout(false); } diff --git a/src/widgets/notebooktab.cpp b/src/widgets/notebooktab.cpp index cf85bc78a..e674cfe42 100644 --- a/src/widgets/notebooktab.cpp +++ b/src/widgets/notebooktab.cpp @@ -67,10 +67,14 @@ NotebookTab::~NotebookTab() void NotebookTab::calcSize() { + float scale = getDpiMultiplier(); + if (SettingsManager::getInstance().hideTabX.get()) { - this->resize(fontMetrics().width(title) + 8, 24); + this->resize(static_cast((fontMetrics().width(title) + 16) * scale), + static_cast(24 * scale)); } else { - this->resize(fontMetrics().width(title) + 8 + 24, 24); + this->resize(static_cast((fontMetrics().width(title) + 8 + 24) * scale), + static_cast(24 * scale)); } if (this->parent() != nullptr) { @@ -179,8 +183,9 @@ void NotebookTab::paintEvent(QPaintEvent *) painter.setPen(fg); - QRect rect(0, 0, this->width() - (SettingsManager::getInstance().hideTabX.get() ? 0 : 16), - this->height()); + float scale = this->getDpiMultiplier(); + int rectW = (SettingsManager::getInstance().hideTabX.get() ? 0 : static_cast(16) * scale); + QRect rect(0, 0, this->width() - rectW, this->height()); painter.drawText(rect, title, QTextOption(Qt::AlignCenter)); @@ -194,8 +199,10 @@ void NotebookTab::paintEvent(QPaintEvent *) } } - painter.drawLine(xRect.topLeft() + QPoint(4, 4), xRect.bottomRight() + QPoint(-4, -4)); - painter.drawLine(xRect.topRight() + QPoint(-4, 4), xRect.bottomLeft() + QPoint(4, -4)); + int a = static_cast(scale * 4); + + painter.drawLine(xRect.topLeft() + QPoint(a, a), xRect.bottomRight() + QPoint(-a, -a)); + painter.drawLine(xRect.topRight() + QPoint(-a, a), xRect.bottomLeft() + QPoint(a, -a)); } } diff --git a/src/widgets/notebooktab.hpp b/src/widgets/notebooktab.hpp index 3ba17c636..bf3caae7c 100644 --- a/src/widgets/notebooktab.hpp +++ b/src/widgets/notebooktab.hpp @@ -83,7 +83,9 @@ private: QRect getXRect() { - return QRect(this->width() - 20, 4, 16, 16); + float scale = this->getDpiMultiplier(); + return QRect(this->width() - static_cast(20 * scale), static_cast(4 * scale), + static_cast(16 * scale), static_cast(16 * scale)); } public: diff --git a/src/widgets/rippleeffectlabel.cpp b/src/widgets/rippleeffectlabel.cpp index 5e64f03e0..a5aa80279 100644 --- a/src/widgets/rippleeffectlabel.cpp +++ b/src/widgets/rippleeffectlabel.cpp @@ -10,15 +10,16 @@ namespace widgets { RippleEffectLabel::RippleEffectLabel(BaseWidget *parent, int spacing) : RippleEffectButton(parent) + , label(this) { - setLayout(&this->ui.hbox); + setLayout(&this->hbox); - this->ui.label.setAlignment(Qt::AlignCenter); + this->label.setAlignment(Qt::AlignCenter); - this->ui.hbox.setMargin(0); - this->ui.hbox.addSpacing(spacing); - this->ui.hbox.addWidget(&this->ui.label); - this->ui.hbox.addSpacing(spacing); + this->hbox.setMargin(0); + this->hbox.addSpacing(spacing); + this->hbox.addWidget(&this->label); + this->hbox.addSpacing(spacing); this->setMouseEffectColor(QColor(255, 255, 255, 63)); } diff --git a/src/widgets/rippleeffectlabel.hpp b/src/widgets/rippleeffectlabel.hpp index bafd21ac1..bb0fc4ca2 100644 --- a/src/widgets/rippleeffectlabel.hpp +++ b/src/widgets/rippleeffectlabel.hpp @@ -22,14 +22,12 @@ public: SignalLabel &getLabel() { - return this->ui.label; + return this->label; } private: - struct { - QHBoxLayout hbox; - SignalLabel label; - } ui; + QHBoxLayout hbox; + SignalLabel label; }; } // namespace widgets