Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font/UI scaling improvements #3690

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Minor: The account switcher is now styled to match your theme. (#4817)
- Minor: Add an invisible resize handle to the bottom of frameless user info popups and reply thread popups. (#4795)
- Minor: The installer now checks for the VC Runtime version and shows more info when it's outdated. (#4847)
- Minor: Improved font and UI scaling on high DPI screens. (#3690)
- Bugfix: Fixed capitalized channel names in log inclusion list not being logged. (#4848)
- Bugfix: Trimmed custom streamlink paths on all platforms making sure you don't accidentally add spaces at the beginning or end of its path. (#4834)
- Bugfix: Fixed a performance issue when displaying replies to certain messages. (#4807)
Expand Down
3 changes: 1 addition & 2 deletions src/controllers/accounts/AccountModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ void AccountModel::getRowFromItem(const std::shared_ptr<Account> &item,
std::vector<QStandardItem *> &row)
{
setStringItem(row[0], item->toString(), false);
row[0]->setData(QFont("Segoe UI", 10), Qt::FontRole);
}

int AccountModel::beforeInsert(const std::shared_ptr<Account> &item,
Expand All @@ -34,7 +33,7 @@ int AccountModel::beforeInsert(const std::shared_ptr<Account> &item,
auto newRow = this->createRow();

setStringItem(newRow[0], item->getCategory(), false, false);
newRow[0]->setData(QFont("Segoe UI Light", 16), Qt::FontRole);
newRow[0]->setBackground(QBrush(Qt::GlobalColor::darkGray));

this->insertCustomRow(std::move(newRow), proposedIndex);

Expand Down
18 changes: 8 additions & 10 deletions src/controllers/hotkeys/HotkeyModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@ std::shared_ptr<Hotkey> HotkeyModel::getItemFromRow(
void HotkeyModel::getRowFromItem(const std::shared_ptr<Hotkey> &item,
std::vector<QStandardItem *> &row)
{
QFont font("Segoe UI", 10);

setStringItem(row[0], item->name(), false);
setStringItem(row[1], item->toString(), false);
if (!item->validAction())
{
font.setStrikeOut(true);
QBrush bgColor(Qt::GlobalColor::darkRed);
pajlada marked this conversation as resolved.
Show resolved Hide resolved
row[0]->setBackground(bgColor);
row[1]->setBackground(bgColor);
}

setStringItem(row[0], item->name(), false);
row[0]->setData(font, Qt::FontRole);

setStringItem(row[1], item->toString(), false);
row[1]->setData(font, Qt::FontRole);
}

int HotkeyModel::beforeInsert(const std::shared_ptr<Hotkey> &item,
Expand All @@ -46,12 +42,14 @@ int HotkeyModel::beforeInsert(const std::shared_ptr<Hotkey> &item,
auto newRow = this->createRow();

setStringItem(newRow[0], category, false, false);
newRow[0]->setData(QFont("Segoe UI Light", 16), Qt::FontRole);
QBrush bgColor(Qt::GlobalColor::darkGray);
pajlada marked this conversation as resolved.
Show resolved Hide resolved
newRow[0]->setBackground(bgColor);

// make sure category headers aren't editable
for (unsigned long i = 1; i < newRow.size(); i++)
{
setStringItem(newRow[i], "", false, false);
newRow[i]->setBackground(bgColor);
}

this->insertCustomRow(std::move(newRow), proposedIndex);
Expand Down
61 changes: 30 additions & 31 deletions src/singletons/Fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,33 @@ void Fonts::initialize(Settings &, Paths &)
#endif
}

QFont Fonts::getFont(FontStyle type, float scale)
QFont Fonts::getFont(FontStyle type, float scale, QWidget *widget)
{
return this->getOrCreateFontData(type, scale).font;
return this->getOrCreateFontData(type, scale, widget).font;
}

QFontMetrics Fonts::getFontMetrics(FontStyle type, float scale)
QFontMetrics Fonts::getFontMetrics(FontStyle type, float scale, QWidget *widget)
{
return this->getOrCreateFontData(type, scale).metrics;
return this->getOrCreateFontData(type, scale, widget).metrics;
}

Fonts::FontData &Fonts::getOrCreateFontData(FontStyle type, float scale)
Fonts::FontData &Fonts::getOrCreateFontData(FontStyle type, float scale,
QWidget *widget)
pajlada marked this conversation as resolved.
Show resolved Hide resolved
{
assertInGuiThread();

assert(type < FontStyle::EndType);

// use DPI scaling if widget was passed
#if defined(Q_OS_WIN) || defined(Q_OS_MACOS)
if (widget != nullptr)
{
scale = scale * 96.f /
std::max<float>(
0.01f, widget->logicalDpiX() * widget->devicePixelRatioF());
}
#endif

auto &map = this->fontsByType_[size_t(type)];

// find element
Expand All @@ -159,7 +170,7 @@ Fonts::FontData &Fonts::getOrCreateFontData(FontStyle type, float scale)

Fonts::FontData Fonts::createFontData(FontStyle type, float scale)
{
// check if it's a chat (scale the setting)
// check if it's a chat
if (type >= FontStyle::ChatStart && type <= FontStyle::ChatEnd)
{
static std::unordered_map<FontStyle, ChatFontData> sizeScale{
Expand All @@ -172,37 +183,25 @@ Fonts::FontData Fonts::createFontData(FontStyle type, float scale)
{FontStyle::ChatLarge, {1.2f, false, QFont::Normal}},
{FontStyle::ChatVeryLarge, {1.4f, false, QFont::Normal}},
};
sizeScale[FontStyle::ChatMediumBold] = {1, false,
QFont::Weight(getBoldness())};
auto data = sizeScale[type];

ChatFontData data = sizeScale[type];
pajlada marked this conversation as resolved.
Show resolved Hide resolved
return FontData(
QFont(this->chatFontFamily.getValue(),
int(this->chatFontSize.getValue() * data.scale * scale),
data.weight, data.italic));
}

// normal Ui font (use pt size)
{
#ifdef Q_OS_MAC
constexpr float multiplier = 0.8f;
#else
constexpr float multiplier = 1.f;
#endif

static std::unordered_map<FontStyle, UiFontData> defaultSize{
{FontStyle::Tiny, {8, "Monospace", false, QFont::Normal}},
{FontStyle::UiMedium,
{int(9 * multiplier), DEFAULT_FONT_FAMILY, false, QFont::Normal}},
{FontStyle::UiMediumBold,
{int(9 * multiplier), DEFAULT_FONT_FAMILY, false, QFont::Bold}},
{FontStyle::UiTabs,
{int(9 * multiplier), DEFAULT_FONT_FAMILY, false, QFont::Normal}},
};

UiFontData &data = defaultSize[type];
QFont font(data.name, int(data.size * scale), data.weight, data.italic);
return FontData(font);
}
// normal UI font
static std::unordered_map<FontStyle, UiFontData> defaultSize{
{FontStyle::Tiny, {8, "Monospace", false, QFont::Normal}},
{FontStyle::UiMedium, {9, DEFAULT_FONT_FAMILY, false, QFont::Normal}},
{FontStyle::UiMediumBold, {9, DEFAULT_FONT_FAMILY, false, QFont::Bold}},
{FontStyle::UiTabs, {9, DEFAULT_FONT_FAMILY, false, QFont::Normal}},
};

UiFontData &data = defaultSize[type];
pajlada marked this conversation as resolved.
Show resolved Hide resolved
return FontData(
pajlada marked this conversation as resolved.
Show resolved Hide resolved
QFont(data.name, int(data.size * scale), data.weight, data.italic));
}

Fonts *getFonts()
Expand Down
7 changes: 4 additions & 3 deletions src/singletons/Fonts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ class Fonts final : public Singleton

// font data gets set in createFontData(...)

QFont getFont(FontStyle type, float scale);
QFontMetrics getFontMetrics(FontStyle type, float scale);
QFont getFont(FontStyle type, float scale, QWidget *widget = nullptr);
QFontMetrics getFontMetrics(FontStyle type, float scale,
QWidget *widget = nullptr);

QStringSetting chatFontFamily;
IntSetting chatFontSize;
Expand Down Expand Up @@ -81,7 +82,7 @@ class Fonts final : public Singleton
QFont::Weight weight;
};

FontData &getOrCreateFontData(FontStyle type, float scale);
FontData &getOrCreateFontData(FontStyle type, float scale, QWidget *widget);
FontData createFontData(FontStyle type, float scale);

std::vector<std::unordered_map<float, FontData>> fontsByType_;
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/BaseWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ void BaseWindow::scaleChangedEvent(float scale)
this->calcButtonsSizes();
#endif

this->setFont(getFonts()->getFont(FontStyle::UiTabs, this->qtFontScale()));
this->setFont(getFonts()->getFont(FontStyle::UiTabs, this->scale(), this));
}

void BaseWindow::paintEvent(QPaintEvent *)
Expand Down
20 changes: 5 additions & 15 deletions src/widgets/Label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,11 @@ void Label::paintEvent(QPaintEvent *)
{
QPainter painter(this);

qreal deviceDpi =
#ifdef Q_OS_WIN
this->devicePixelRatioF();
#else
1.0;
#endif
QFontMetrics metrics =
pajlada marked this conversation as resolved.
Show resolved Hide resolved
getFonts()->getFontMetrics(this->getFontStyle(), this->scale(), this);

QFontMetrics metrics = getFonts()->getFontMetrics(
this->getFontStyle(),
this->scale() * 96.f /
std::max<float>(0.01, this->logicalDpiX() * deviceDpi));
painter.setFont(getFonts()->getFont(
this->getFontStyle(),
this->scale() * 96.f /
std::max<float>(0.02, this->logicalDpiX() * deviceDpi)));
painter.setFont(
getFonts()->getFont(this->getFontStyle(), this->scale(), this));

int offset = this->getOffset();

Expand All @@ -126,7 +116,7 @@ void Label::paintEvent(QPaintEvent *)
void Label::updateSize()
{
QFontMetrics metrics =
getFonts()->getFontMetrics(this->fontStyle_, this->scale());
getFonts()->getFontMetrics(this->fontStyle_, this->scale(), this);

int width =
metrics.horizontalAdvance(this->text_) + (2 * this->getOffset());
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/TooltipWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ void TooltipWidget::scaleChangedEvent(float)
void TooltipWidget::updateFont()
{
this->setFont(
getFonts()->getFont(FontStyle::ChatMediumSmall, this->scale()));
getFonts()->getFont(FontStyle::ChatMediumSmall, this->scale(), this));
}

void TooltipWidget::setWordWrap(bool wrap)
Expand Down
3 changes: 2 additions & 1 deletion src/widgets/dialogs/UserInfoPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ void UserInfoPopup::themeChangedEvent()

for (auto &&child : this->findChildren<QCheckBox *>())
{
child->setFont(getFonts()->getFont(FontStyle::UiMedium, this->scale()));
child->setFont(
getFonts()->getFont(FontStyle::UiMedium, this->scale(), this));
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/widgets/helper/ChannelView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,8 @@ void ChannelView::scaleChangedEvent(float scale)

if (this->goToBottom_)
{
auto factor = this->qtFontScale();
#ifdef Q_OS_MACOS
factor = scale * 80.f /
std::max<float>(
0.01, this->logicalDpiX() * this->devicePixelRatioF());
#endif
this->goToBottom_->getLabel().setFont(
getFonts()->getFont(FontStyle::UiMedium, factor));
getFonts()->getFont(FontStyle::UiMedium, this->scale(), this));
}
}

Expand Down
18 changes: 4 additions & 14 deletions src/widgets/helper/NotebookTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@

namespace chatterino {
namespace {
qreal deviceDpi(QWidget *widget)
{
#ifdef Q_OS_WIN
return widget->devicePixelRatioF();
#else
return 1.0;
#endif
}

// Translates the given rectangle by an amount in the direction to appear like the tab is selected.
// For example, if location is Top, the rectangle will be translated in the negative Y direction,
Expand Down Expand Up @@ -196,8 +188,8 @@ int NotebookTab::normalTabWidth()
float scale = this->scale();
int width;

QFontMetrics metrics = getApp()->fonts->getFontMetrics(
FontStyle::UiTabs, float(qreal(this->scale()) * deviceDpi(this)));
QFontMetrics metrics =
pajlada marked this conversation as resolved.
Show resolved Hide resolved
getApp()->fonts->getFontMetrics(FontStyle::UiTabs, scale, this);

if (this->hasXButton())
{
Expand Down Expand Up @@ -414,11 +406,9 @@ void NotebookTab::paintEvent(QPaintEvent *)
QPainter painter(this);
float scale = this->scale();

auto div = std::max<float>(0.01f, this->logicalDpiX() * deviceDpi(this));
painter.setFont(
getApp()->fonts->getFont(FontStyle::UiTabs, scale * 96.f / div));
painter.setFont(getApp()->fonts->getFont(FontStyle::UiTabs, scale, this));
QFontMetrics metrics =
app->fonts->getFontMetrics(FontStyle::UiTabs, scale * 96.f / div);
app->fonts->getFontMetrics(FontStyle::UiTabs, scale, this);

int height = int(scale * NOTEBOOK_TAB_HEIGHT);

Expand Down
5 changes: 2 additions & 3 deletions src/widgets/settingspages/GeneralPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1167,10 +1167,9 @@ QString GeneralPage::getFont(const DropdownArgs &args) const
{
args.combobox->setCurrentIndex(0);
args.combobox->setEditText("Choosing...");
QFontDialog dialog(getApp()->fonts->getFont(FontStyle::ChatMedium, 1.));

auto ok = bool();
auto font = dialog.getFont(&ok, this->window());
bool ok;
auto font = QFontDialog::getFont(&ok, this->window());
pajlada marked this conversation as resolved.
Show resolved Hide resolved

if (ok)
return font.family();
Expand Down
10 changes: 8 additions & 2 deletions src/widgets/splits/Split.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,11 @@ void Split::showChangeChannelPopup(const char *dialogTitle, bool empty,
this->selectChannelDialog_ = dialog;
}

void Split::scaleChangedEvent(float scale)
{
this->setFont(getApp()->fonts->getFont(FontStyle::UiMedium, scale, this));
}

void Split::updateGifEmotes()
{
this->view_->queueUpdate();
Expand Down Expand Up @@ -1161,10 +1166,11 @@ void Split::showViewerList()
auto *loadingLabel = new QLabel("Loading...");
searchBar->setPlaceholderText("Search User...");

auto formatListItemText = [](QString text) {
auto formatListItemText = [this](QString text) {
pajlada marked this conversation as resolved.
Show resolved Hide resolved
auto item = new QListWidgetItem();
item->setText(text);
item->setFont(getApp()->fonts->getFont(FontStyle::ChatMedium, 1.0));
item->setFont(
getApp()->fonts->getFont(FontStyle::UiMedium, this->scale(), this));
return item;
};

Expand Down
2 changes: 2 additions & 0 deletions src/widgets/splits/Split.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class Split : public BaseWidget
pajlada::Signals::Signal<int, Split *> insertSplitRequested;

protected:
void scaleChangedEvent(float scale_) override;

void paintEvent(QPaintEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/splits/SplitContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,8 @@ void SplitContainer::paintEvent(QPaintEvent * /*event*/)

painter.setPen(this->theme->splits.header.text);

const auto font =
getApp()->fonts->getFont(FontStyle::ChatMedium, this->scale());
const auto font = getApp()->fonts->getFont(FontStyle::ChatMedium,
this->scale(), this);
painter.setFont(font);

QString text = "Click to add a split";
Expand Down
Loading
Loading