Skip to content

Commit

Permalink
fix: Use Qt's High-DPI scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz committed May 11, 2024
1 parent 321d881 commit bdcd51c
Show file tree
Hide file tree
Showing 21 changed files with 454 additions and 321 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Major: Release plugins alpha. (#5288)
- Bugfix: If a network request errors with 200 OK, Qt's error code is now reported instead of the HTTP status. (#5378)
- Bugfix: Fixed broken scaling in context menus. (#4868)
- Dev: Use Qt's high DPI scaling. (#4868)
- Dev: Add doxygen build target. (#5377)
- Dev: Make printing of strings in tests easier. (#5379)

Expand Down
6 changes: 3 additions & 3 deletions resources/qss/settings.qss
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
* {
font-size: <font-size>px;
font-size: 14px;
font-family: "Segoe UI";
}

QCheckBox::indicator {
width: <checkbox-size>px;
height: <checkbox-size>px;
width: 14px;
height: 14px;
}

chatterino--ComboBox {
Expand Down
6 changes: 1 addition & 5 deletions src/RunGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace {
void initQt()
{
// set up the QApplication flags
QApplication::setAttribute(Qt::AA_Use96Dpi, true);
QApplication::setAttribute(Qt::AA_Use96Dpi);

#ifdef Q_OS_WIN32
// Avoid promoting child widgets to child windows
Expand All @@ -86,10 +86,6 @@ namespace {
QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
#endif

#if defined(Q_OS_WIN32) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QApplication::setAttribute(Qt::AA_DisableHighDpiScaling, true);
#endif

QApplication::setStyle(QStyleFactory::create("Fusion"));

#ifndef Q_OS_MAC
Expand Down
5 changes: 0 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ using namespace chatterino;

int main(int argc, char **argv)
{
// TODO: This is a temporary fix (see #4552).
#if defined(Q_OS_WINDOWS) && QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
qputenv("QT_ENABLE_HIGHDPI_SCALING", "0");
#endif

QApplication a(argc, argv);

QCoreApplication::setApplicationName("chatterino");
Expand Down
10 changes: 5 additions & 5 deletions src/messages/MessageElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ void EmoteElement::addToContainer(MessageLayoutContainer &container,
{
if (flags.has(MessageElementFlag::EmoteImages))
{
auto image =
this->emote_->images.getImageOrLoaded(container.getScale());
auto image = this->emote_->images.getImageOrLoaded(
container.getImageScale());
if (image->isEmpty())
{
return;
Expand Down Expand Up @@ -210,7 +210,7 @@ void LayeredEmoteElement::addToContainer(MessageLayoutContainer &container,
{
if (flags.has(MessageElementFlag::EmoteImages))
{
auto images = this->getLoadedImages(container.getScale());
auto images = this->getLoadedImages(container.getImageScale());
if (images.empty())
{
return;
Expand Down Expand Up @@ -364,7 +364,7 @@ void BadgeElement::addToContainer(MessageLayoutContainer &container,
if (flags.hasAny(this->getFlags()))
{
auto image =
this->emote_->images.getImageOrLoaded(container.getScale());
this->emote_->images.getImageOrLoaded(container.getImageScale());
if (image->isEmpty())
{
return;
Expand Down Expand Up @@ -798,7 +798,7 @@ void ScalingImageElement::addToContainer(MessageLayoutContainer &container,
if (flags.hasAny(this->getFlags()))
{
const auto &image =
this->images_.getImageOrLoaded(container.getScale());
this->images_.getImageOrLoaded(container.getImageScale());
if (image->isEmpty())
{
return;
Expand Down
13 changes: 6 additions & 7 deletions src/messages/layouts/MessageLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ int MessageLayout::getWidth() const

// Layout
// return true if redraw is required
bool MessageLayout::layout(int width, float scale, MessageElementFlags flags,
bool MessageLayout::layout(int width, float scale, float imageScale,
MessageElementFlags flags,
bool shouldInvalidateBuffer)
{
// BenchmarkGuard benchmark("MessageLayout::layout()");
Expand Down Expand Up @@ -106,6 +107,8 @@ bool MessageLayout::layout(int width, float scale, MessageElementFlags flags,
// check if dpi changed
layoutRequired |= this->scale_ != scale;
this->scale_ = scale;
layoutRequired |= this->imageScale_ != imageScale;
this->imageScale_ = imageScale;

if (!layoutRequired)
{
Expand Down Expand Up @@ -148,7 +151,8 @@ void MessageLayout::actuallyLayout(int width, MessageElementFlags flags)
bool hideSimilar = getSettings()->hideSimilar;
bool hideReplies = !flags.has(MessageElementFlag::RepliedMessage);

this->container_.beginLayout(width, this->scale_, messageFlags);
this->container_.beginLayout(width, this->scale_, this->imageScale_,
messageFlags);

for (const auto &element : this->message_->elements)
{
Expand Down Expand Up @@ -288,16 +292,11 @@ QPixmap *MessageLayout::ensureBuffer(QPainter &painter, int width)
}

// Create new buffer
#if defined(Q_OS_MACOS) || defined(Q_OS_LINUX)
this->buffer_ = std::make_unique<QPixmap>(
int(width * painter.device()->devicePixelRatioF()),
int(this->container_.getHeight() *
painter.device()->devicePixelRatioF()));
this->buffer_->setDevicePixelRatio(painter.device()->devicePixelRatioF());
#else
this->buffer_ = std::make_unique<QPixmap>(
width, std::max(16, this->container_.getHeight()));
#endif

this->bufferValid_ = false;
DebugCount::increase("message drawing buffers");
Expand Down
5 changes: 3 additions & 2 deletions src/messages/layouts/MessageLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class MessageLayout

MessageLayoutFlags flags;

bool layout(int width, float scale_, MessageElementFlags flags,
bool shouldInvalidateBuffer);
bool layout(int width, float scale_, float imageScale,
MessageElementFlags flags, bool shouldInvalidateBuffer);

// Painting
MessagePaintResult paint(const MessagePaintContext &ctx);
Expand Down Expand Up @@ -128,6 +128,7 @@ class MessageLayout
int currentLayoutWidth_ = -1;
int layoutState_ = -1;
float scale_ = -1;
float imageScale_ = -1.F;
MessageElementFlags currentWordFlags_;

#ifdef FOURTF
Expand Down
8 changes: 7 additions & 1 deletion src/messages/layouts/MessageLayoutContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ constexpr const QMargins MARGIN{8, 4, 8, 4};
namespace chatterino {

void MessageLayoutContainer::beginLayout(int width, float scale,
MessageFlags flags)
float imageScale, MessageFlags flags)
{
this->elements_.clear();
this->lines_.clear();
Expand All @@ -45,6 +45,7 @@ void MessageLayoutContainer::beginLayout(int width, float scale,
this->width_ = width;
this->height_ = 0;
this->scale_ = scale;
this->imageScale_ = imageScale;
this->flags_ = flags;
auto mediumFontMetrics =
getIApp()->getFonts()->getFontMetrics(FontStyle::ChatMedium, scale);
Expand Down Expand Up @@ -526,6 +527,11 @@ float MessageLayoutContainer::getScale() const
return this->scale_;
}

float MessageLayoutContainer::getImageScale() const
{
return this->imageScale_;
}

bool MessageLayoutContainer::isCollapsed() const
{
return this->isCollapsed_;
Expand Down
12 changes: 11 additions & 1 deletion src/messages/layouts/MessageLayoutContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ struct MessageLayoutContainer {
* This will reset all line calculations, and will be considered incomplete
* until the accompanying end function has been called
*/
void beginLayout(int width_, float scale_, MessageFlags flags_);
void beginLayout(int width, float scale, float imageScale,
MessageFlags flags);

/**
* Finish the layout process of this message
Expand Down Expand Up @@ -146,6 +147,11 @@ struct MessageLayoutContainer {
*/
float getScale() const;

/**
* Returns the image scale
*/
float getImageScale() const;

/**
* Returns true if this message is collapsed
*/
Expand Down Expand Up @@ -270,6 +276,10 @@ struct MessageLayoutContainer {

// variables
float scale_ = 1.F;
/**
* Scale factor for images
*/
float imageScale_ = 1.F;
int width_ = 0;
MessageFlags flags_{};
/**
Expand Down
8 changes: 5 additions & 3 deletions src/widgets/AttachedWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,20 +270,22 @@ void AttachedWindow::updateWindowRect(void *_attachedPtr)
}

float scale = 1.f;
float ourScale = 1.F;
if (auto dpi = getWindowDpi(attached))
{
scale = *dpi / 96.f;
ourScale = scale / this->devicePixelRatio();

for (auto w : this->ui_.split->findChildren<BaseWidget *>())
{
w->setOverrideScale(scale);
w->setOverrideScale(ourScale);
}
this->ui_.split->setOverrideScale(scale);
this->ui_.split->setOverrideScale(ourScale);
}

if (this->height_ != -1)
{
this->ui_.split->setFixedWidth(int(this->width_ * scale));
this->ui_.split->setFixedWidth(int(this->width_ * ourScale));

// offset
int o = this->fullscreen_ ? 0 : 8;
Expand Down
13 changes: 0 additions & 13 deletions src/widgets/BaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,6 @@ void BaseWidget::setScaleIndependantHeight(int value)
QSize(this->scaleIndependantSize_.width(), value));
}

float BaseWidget::qtFontScale() const
{
if (auto *window = dynamic_cast<BaseWindow *>(this->window()))
{
// ensure no div by 0
return this->scale() / std::max<float>(0.01f, window->nativeScale_);
}
else
{
return this->scale();
}
}

void BaseWidget::childEvent(QChildEvent *event)
{
if (event->added())
Expand Down
2 changes: 0 additions & 2 deletions src/widgets/BaseWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class BaseWidget : public QWidget
void setScaleIndependantWidth(int value);
void setScaleIndependantHeight(int value);

float qtFontScale() const;

protected:
void childEvent(QChildEvent *) override;
void showEvent(QShowEvent *) override;
Expand Down
Loading

0 comments on commit bdcd51c

Please sign in to comment.