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 Mar 24, 2024
1 parent 00119a8 commit 3428605
Show file tree
Hide file tree
Showing 19 changed files with 430 additions and 291 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
- Bugfix: Fixed split header tooltips appearing too tall. (#5232)
- Bugfix: Fixed past messages not showing in the search popup after adding a channel. (#5248)
- Bugfix: Detect when OBS is running on MacOS. (#5260)
- Bugfix: Fixed broken scaling in context menus. (#4868)
- Dev: Run miniaudio in a separate thread, and simplify it to not manage the device ourselves. There's a chance the simplification is a bad idea. (#4978)
- Dev: Change clang-format from v14 to v16. (#4929)
- Dev: Fixed UTF16 encoding of `modes` file for the installer. (#4791)
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
14 changes: 1 addition & 13 deletions src/RunGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,7 @@ namespace {
void initQt()
{
// set up the QApplication flags
QApplication::setAttribute(Qt::AA_Use96Dpi, true);

#ifdef Q_OS_WIN32
// Avoid promoting child widgets to child windows
// This causes bugs with frameless windows as not all child events
// get sent to the parent - effectively making the window immovable.
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::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 @@ -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 @@ -797,7 +797,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
Loading

0 comments on commit 3428605

Please sign in to comment.