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 Oct 5, 2023
1 parent 7c8caba commit 2636eeb
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 190 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- Bugfix: Fixed the Quick Switcher (CTRL+K) from sometimes showing up on the wrong window. (#4819)
- Bugfix: Fixed too much text being copied when copying chat messages. (#4812, #4830, #4839)
- Bugfix: Fixed empty page being added when showing out of bounds dialog. (#4849)
- Bugfix: Fixed broken scaling in context menus. (#4868)
- Dev: Fixed UTF16 encoding of `modes` file for the installer. (#4791)
- Dev: Temporarily disable High DPI scaling on Qt6 builds on Windows. (#4767)
- Dev: Tests now run on Ubuntu 22.04 instead of 20.04 to loosen C++ restrictions in tests. (#4774)
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 @@ -75,11 +75,7 @@ namespace {
void initQt()
{
// set up the QApplication flags
QApplication::setAttribute(Qt::AA_Use96Dpi, true);
#ifdef Q_OS_WIN32
QApplication::setAttribute(Qt::AA_DisableHighDpiScaling, true);
#endif

QApplication::setAttribute(Qt::AA_Use96Dpi);
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 @@ -24,11 +24,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
12 changes: 6 additions & 6 deletions src/messages/MessageElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,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 @@ -262,7 +262,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 @@ -417,7 +417,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 @@ -691,7 +691,7 @@ void SingleLineTextElement::addToContainer(MessageLayoutContainer &container,
{
auto emote = boost::get<EmotePtr>(parsedWord);
auto image =
emote->images.getImageOrLoaded(container.getScale());
emote->images.getImageOrLoaded(container.getImageScale());
if (!image->isEmpty())
{
auto emoteScale = getSettings()->emoteScale.getValue();
Expand Down Expand Up @@ -828,7 +828,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)
{
// 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 @@ -143,7 +146,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 @@ -280,16 +284,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
4 changes: 3 additions & 1 deletion src/messages/layouts/MessageLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class MessageLayout

MessageLayoutFlags flags;

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

// Painting
void paint(const MessagePaintContext &ctx);
Expand Down Expand Up @@ -105,6 +106,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 =
getApp()->fonts->getFontMetrics(FontStyle::ChatMedium, scale);
Expand Down Expand Up @@ -477,6 +478,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 @@ -131,6 +132,11 @@ struct MessageLayoutContainer {
*/
float getScale() const;

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

/**
* Returns true if this message is collapsed
*/
Expand Down Expand Up @@ -250,6 +256,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 @@ -271,20 +271,22 @@ void AttachedWindow::updateWindowRect(void *_attachedPtr)
}

float scale = 1.f;
float ourScale = 1.F;
if (auto dpi = getWindowDpi(attached))
{
scale = dpi.get() / 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
Loading

0 comments on commit 2636eeb

Please sign in to comment.