HiDPI commit

This commit is contained in:
fourtf 2017-09-22 00:50:43 +02:00
parent 222eedcd21
commit 14511e10ef
13 changed files with 138 additions and 55 deletions

View file

@ -163,7 +163,8 @@ HEADERS += \
src/widgets/rippleeffectlabel.hpp \ src/widgets/rippleeffectlabel.hpp \
src/widgets/qualitypopup.hpp \ src/widgets/qualitypopup.hpp \
src/widgets/emotepopup.hpp \ src/widgets/emotepopup.hpp \
src/messages/messagecolor.hpp src/messages/messagecolor.hpp \
src/util/nativeeventhelper.hpp
PRECOMPILED_HEADER = PRECOMPILED_HEADER =

View file

@ -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<MSG *>(message);
// WM_DPICHANGED
if (msg->message == 0x02E0) {
dpi = HIWORD(msg->wParam);
return true;
}
return false;
}
}
}
#endif

View file

@ -1,15 +1,21 @@
#include "widgets/basewidget.hpp" #include "widgets/basewidget.hpp"
#include "colorscheme.hpp" #include "colorscheme.hpp"
//#include <QApplication>
#include <QDebug> #include <QDebug>
//#include <QDesktopWidget>
//#include <QScreen>
#include <boost/signals2.hpp> #include <boost/signals2.hpp>
#include "util/nativeeventhelper.hpp"
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
// BaseWidget::BaseWidget(ColorScheme &_colorScheme, WindowManager &_windowManager, QWidget *parent)
BaseWidget::BaseWidget(ColorScheme &_colorScheme, QWidget *parent) BaseWidget::BaseWidget(ColorScheme &_colorScheme, QWidget *parent)
: QWidget(parent) : QWidget(parent)
, colorScheme(_colorScheme) , colorScheme(_colorScheme)
// , windowManager(_windowManager)
{ {
this->init(); this->init();
} }
@ -17,10 +23,25 @@ BaseWidget::BaseWidget(ColorScheme &_colorScheme, QWidget *parent)
BaseWidget::BaseWidget(BaseWidget *parent) BaseWidget::BaseWidget(BaseWidget *parent)
: QWidget(parent) : QWidget(parent)
, colorScheme(parent->colorScheme) , colorScheme(parent->colorScheme)
// , windowManager(parent->windowManager)
{ {
this->init(); this->init();
} }
float BaseWidget::getDpiMultiplier()
{
BaseWidget *baseWidget = dynamic_cast<BaseWidget *>(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() void BaseWidget::init()
{ {
this->colorScheme.updated.connect([this]() { this->colorScheme.updated.connect([this]() {
@ -35,5 +56,28 @@ void BaseWidget::refreshTheme()
// Do any color scheme updates here // 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<int>(this->width() * scale),
static_cast<int>(this->height() * scale));
}
return QWidget::nativeEvent(eventType, message, result);
}
#endif
} // namespace widgets } // namespace widgets
} // namespace chatterino } // namespace chatterino

View file

@ -2,6 +2,8 @@
#include <QWidget> #include <QWidget>
//#include "windowmanager.hpp"
namespace chatterino { namespace chatterino {
class ColorScheme; class ColorScheme;
@ -13,13 +15,27 @@ class BaseWidget : public QWidget
Q_OBJECT Q_OBJECT
public: public:
// explicit BaseWidget(ColorScheme &_colorScheme, WindowManager &windowManager, QWidget
// *parent);
explicit BaseWidget(ColorScheme &_colorScheme, QWidget *parent); explicit BaseWidget(ColorScheme &_colorScheme, QWidget *parent);
explicit BaseWidget(BaseWidget *parent); explicit BaseWidget(BaseWidget *parent);
ColorScheme &colorScheme; ColorScheme &colorScheme;
float getDpiMultiplier();
// protected:
// WindowManager &windowManager;
protected:
#ifdef USEWINSDK
virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result) override;
#endif
private: private:
float dpiMultiplier = 1.f;
void init(); void init();
virtual void refreshTheme(); virtual void refreshTheme();

View file

@ -21,8 +21,6 @@ ChatWidgetHeader::ChatWidgetHeader(ChatWidget *_chatWidget)
, rightLabel(this) , rightLabel(this)
, rightMenu(this) , rightMenu(this)
{ {
this->setFixedHeight(32);
this->refreshTheme(); this->refreshTheme();
this->updateChannelText(); this->updateChannelText();
@ -76,6 +74,11 @@ ChatWidgetHeader::ChatWidgetHeader(ChatWidget *_chatWidget)
timer->start(60000); timer->start(60000);
} }
void ChatWidgetHeader::resizeEvent(QResizeEvent *event)
{
this->setFixedHeight(static_cast<float>(32 * getDpiMultiplier()));
}
void ChatWidgetHeader::updateChannelText() void ChatWidgetHeader::updateChannelText()
{ {
const std::string channelName = this->chatWidget->channelName; const std::string channelName = this->chatWidget->channelName;
@ -85,14 +88,14 @@ void ChatWidgetHeader::updateChannelText()
auto channel = this->chatWidget->getChannel(); auto channel = this->chatWidget->getChannel();
twitch::TwitchChannel *twitchChannel = dynamic_cast<twitch::TwitchChannel *>(channel.get()); twitch::TwitchChannel *twitchChannel = dynamic_cast<twitch::TwitchChannel *>(channel.get());
if (twitchChannel != nullptr && twitchChannel->isLive) { if (twitchChannel != nullptr && twitchChannel->isLive) {
this->channelNameLabel.setText(QString::fromStdString(channelName) + " (live)"); this->channelNameLabel.setText(QString::fromStdString(channelName) + " (live)");
this->setToolTip("<style>.center { text-align: center; }</style>" this->setToolTip("<style>.center { text-align: center; }</style>"
"<p class = \"center\">" + "<p class = \"center\">" +
twitchChannel->streamStatus + "<br><br>" + twitchChannel->streamStatus + "<br><br>" + twitchChannel->streamGame +
twitchChannel->streamGame + "<br>" "<br>"
"Live for " + "Live for " +
twitchChannel->streamUptime + " with " + twitchChannel->streamUptime + " with " +
twitchChannel->streamViewerCount + " viewers" twitchChannel->streamViewerCount + " viewers"
"</p>"); "</p>");

View file

@ -36,6 +36,7 @@ protected:
virtual void mousePressEvent(QMouseEvent *event) override; virtual void mousePressEvent(QMouseEvent *event) override;
virtual void mouseMoveEvent(QMouseEvent *event) override; virtual void mouseMoveEvent(QMouseEvent *event) override;
virtual void mouseDoubleClickEvent(QMouseEvent *event) override; virtual void mouseDoubleClickEvent(QMouseEvent *event) override;
virtual void resizeEvent(QResizeEvent *event) override;
private: private:
ChatWidget *const chatWidget; ChatWidget *const chatWidget;
@ -66,7 +67,6 @@ public slots:
void menuReloadChannelEmotes(); void menuReloadChannelEmotes();
void menuManualReconnect(); void menuManualReconnect();
void menuShowChangelog(); void menuShowChangelog();
}; };
} // namespace widgets } // namespace widgets

View file

@ -13,10 +13,6 @@
#include <QVBoxLayout> #include <QVBoxLayout>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#ifdef USEWINSDK
#include "windows.h"
#endif
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
@ -27,6 +23,7 @@ MainWindow::MainWindow(ChannelManager &_channelManager, ColorScheme &_colorSchem
, colorScheme(_colorScheme) , colorScheme(_colorScheme)
, completionManager(_completionManager) , completionManager(_completionManager)
, notebook(this->channelManager, this) , notebook(this->channelManager, this)
, dpi(this->getDpiMultiplier())
// , windowGeometry("/windows/0/geometry") // , windowGeometry("/windows/0/geometry")
{ {
QVBoxLayout *layout = new QVBoxLayout(this); 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<MSG *>(message);
if (msg->message == 0x02E0) {
qDebug() << "dpi changed";
}
return QWidget::nativeEvent(eventType, message, result);
}
#endif
void MainWindow::repaintVisibleChatWidgets(Channel *channel) void MainWindow::repaintVisibleChatWidgets(Channel *channel)
{ {
auto *page = this->notebook.getSelectedPage(); auto *page = this->notebook.getSelectedPage();

View file

@ -42,11 +42,10 @@ public:
protected: protected:
virtual void closeEvent(QCloseEvent *event) override; virtual void closeEvent(QCloseEvent *event) override;
#ifdef USEWINSDK
virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result) override;
#endif
private: private:
float dpi;
virtual void refreshTheme() override; virtual void refreshTheme() override;
ChannelManager &channelManager; ChannelManager &channelManager;

View file

@ -31,15 +31,11 @@ Notebook::Notebook(ChannelManager &_channelManager, MainWindow *parent)
this->connect(&this->userButton, SIGNAL(clicked()), this, SLOT(usersButtonClicked())); this->connect(&this->userButton, SIGNAL(clicked()), this, SLOT(usersButtonClicked()));
this->connect(&this->addButton, SIGNAL(clicked()), this, SLOT(addPageButtonClicked())); this->connect(&this->addButton, SIGNAL(clicked()), this, SLOT(addPageButtonClicked()));
this->settingsButton.resize(24, 24);
this->settingsButton.icon = NotebookButton::IconSettings; this->settingsButton.icon = NotebookButton::IconSettings;
this->userButton.resize(24, 24);
this->userButton.move(24, 0); this->userButton.move(24, 0);
this->userButton.icon = NotebookButton::IconUser; this->userButton.icon = NotebookButton::IconUser;
this->addButton.resize(24, 24);
SettingsManager::getInstance().hidePreferencesButton.valueChanged.connect( SettingsManager::getInstance().hidePreferencesButton.valueChanged.connect(
[this](const bool &) { this->performLayout(); }); [this](const bool &) { this->performLayout(); });
SettingsManager::getInstance().hideUserButton.valueChanged.connect( SettingsManager::getInstance().hideUserButton.valueChanged.connect(
@ -163,27 +159,26 @@ void Notebook::previousTab()
void Notebook::performLayout(bool animated) void Notebook::performLayout(bool animated)
{ {
int x = 0, y = 0; int x = 0, y = 0;
float scale = this->getDpiMultiplier();
if (SettingsManager::getInstance().hidePreferencesButton.get()) { if (SettingsManager::getInstance().hidePreferencesButton.get()) {
this->settingsButton.hide(); this->settingsButton.hide();
} else { } else {
this->settingsButton.show(); this->settingsButton.show();
x += 24; x += settingsButton.width();
} }
if (SettingsManager::getInstance().hideUserButton.get()) { if (SettingsManager::getInstance().hideUserButton.get()) {
this->userButton.hide(); this->userButton.hide();
} else { } else {
this->userButton.move(x, 0); this->userButton.move(x, 0);
this->userButton.show(); this->userButton.show();
x += 24; x += userButton.width();
} }
int tabHeight = 16; int tabHeight = static_cast<int>(24 * scale);
bool first = true; bool first = true;
for (auto &i : this->pages) { for (auto &i : this->pages) {
tabHeight = i->getTab()->height();
if (!first && if (!first &&
(i == this->pages.last() ? tabHeight : 0) + x + i->getTab()->width() > width()) { (i == this->pages.last() ? tabHeight : 0) + x + i->getTab()->width() > width()) {
y += i->getTab()->height(); y += i->getTab()->height();
@ -207,6 +202,16 @@ void Notebook::performLayout(bool animated)
void Notebook::resizeEvent(QResizeEvent *) void Notebook::resizeEvent(QResizeEvent *)
{ {
float scale = this->getDpiMultiplier();
this->settingsButton.resize(static_cast<int>(24 * scale), static_cast<int>(24 * scale));
this->userButton.resize(static_cast<int>(24 * scale), static_cast<int>(24 * scale));
this->addButton.resize(static_cast<int>(24 * scale), static_cast<int>(24 * scale));
for (auto &i : this->pages) {
i->getTab()->calcSize();
}
this->performLayout(false); this->performLayout(false);
} }

View file

@ -67,10 +67,14 @@ NotebookTab::~NotebookTab()
void NotebookTab::calcSize() void NotebookTab::calcSize()
{ {
float scale = getDpiMultiplier();
if (SettingsManager::getInstance().hideTabX.get()) { if (SettingsManager::getInstance().hideTabX.get()) {
this->resize(fontMetrics().width(title) + 8, 24); this->resize(static_cast<int>((fontMetrics().width(title) + 16) * scale),
static_cast<int>(24 * scale));
} else { } else {
this->resize(fontMetrics().width(title) + 8 + 24, 24); this->resize(static_cast<int>((fontMetrics().width(title) + 8 + 24) * scale),
static_cast<int>(24 * scale));
} }
if (this->parent() != nullptr) { if (this->parent() != nullptr) {
@ -179,8 +183,9 @@ void NotebookTab::paintEvent(QPaintEvent *)
painter.setPen(fg); painter.setPen(fg);
QRect rect(0, 0, this->width() - (SettingsManager::getInstance().hideTabX.get() ? 0 : 16), float scale = this->getDpiMultiplier();
this->height()); int rectW = (SettingsManager::getInstance().hideTabX.get() ? 0 : static_cast<int>(16) * scale);
QRect rect(0, 0, this->width() - rectW, this->height());
painter.drawText(rect, title, QTextOption(Qt::AlignCenter)); 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)); int a = static_cast<int>(scale * 4);
painter.drawLine(xRect.topRight() + QPoint(-4, 4), xRect.bottomLeft() + QPoint(4, -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));
} }
} }

View file

@ -83,7 +83,9 @@ private:
QRect getXRect() QRect getXRect()
{ {
return QRect(this->width() - 20, 4, 16, 16); float scale = this->getDpiMultiplier();
return QRect(this->width() - static_cast<int>(20 * scale), static_cast<int>(4 * scale),
static_cast<int>(16 * scale), static_cast<int>(16 * scale));
} }
public: public:

View file

@ -10,15 +10,16 @@ namespace widgets {
RippleEffectLabel::RippleEffectLabel(BaseWidget *parent, int spacing) RippleEffectLabel::RippleEffectLabel(BaseWidget *parent, int spacing)
: RippleEffectButton(parent) : 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->hbox.setMargin(0);
this->ui.hbox.addSpacing(spacing); this->hbox.addSpacing(spacing);
this->ui.hbox.addWidget(&this->ui.label); this->hbox.addWidget(&this->label);
this->ui.hbox.addSpacing(spacing); this->hbox.addSpacing(spacing);
this->setMouseEffectColor(QColor(255, 255, 255, 63)); this->setMouseEffectColor(QColor(255, 255, 255, 63));
} }

View file

@ -22,14 +22,12 @@ public:
SignalLabel &getLabel() SignalLabel &getLabel()
{ {
return this->ui.label; return this->label;
} }
private: private:
struct { QHBoxLayout hbox;
QHBoxLayout hbox; SignalLabel label;
SignalLabel label;
} ui;
}; };
} // namespace widgets } // namespace widgets