mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
refactor: Fonts (#5228)
This commit is contained in:
parent
e56f7136a9
commit
e7508332ff
|
@ -151,6 +151,7 @@
|
|||
- Dev: Move `clang-tidy` checker to its own CI job. (#4996)
|
||||
- Dev: Refactored the Image Uploader feature. (#4971)
|
||||
- Dev: Refactored the SplitOverlay code. (#5082)
|
||||
- Dev: Refactored the Fonts code, making it less of a singleton. (#5228)
|
||||
- Dev: Refactored the TwitchBadges structure, making it less of a singleton. (#5096, #5144)
|
||||
- Dev: Refactored emotes out of TwitchIrcServer. (#5120, #5146)
|
||||
- Dev: Refactored the ChatterinoBadges structure, making it less of a singleton. (#5103)
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
#include "singletons/Paths.hpp"
|
||||
#include "singletons/Updates.hpp"
|
||||
|
||||
#include <QTemporaryDir>
|
||||
|
||||
namespace chatterino::mock {
|
||||
|
||||
class EmptyApplication : public IApplication
|
||||
|
@ -235,7 +237,8 @@ public:
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
protected:
|
||||
QTemporaryDir settingsDir;
|
||||
Paths paths_;
|
||||
Args args_;
|
||||
Updates updates_;
|
||||
|
|
|
@ -118,7 +118,7 @@ Application::Application(Settings &_settings, const Paths &paths,
|
|||
: paths_(paths)
|
||||
, args_(_args)
|
||||
, themes(&this->emplace<Theme>())
|
||||
, fonts(&this->emplace<Fonts>())
|
||||
, fonts(new Fonts(_settings))
|
||||
, emotes(&this->emplace<Emotes>())
|
||||
, accounts(&this->emplace<AccountController>())
|
||||
, hotkeys(&this->emplace<HotkeyController>())
|
||||
|
@ -170,6 +170,7 @@ void Application::fakeDtor()
|
|||
this->bttvEmotes.reset();
|
||||
this->ffzEmotes.reset();
|
||||
this->seventvEmotes.reset();
|
||||
this->fonts.reset();
|
||||
}
|
||||
|
||||
void Application::initialize(Settings &settings, const Paths &paths)
|
||||
|
@ -335,8 +336,9 @@ Theme *Application::getThemes()
|
|||
Fonts *Application::getFonts()
|
||||
{
|
||||
assertInGuiThread();
|
||||
assert(this->fonts);
|
||||
|
||||
return this->fonts;
|
||||
return this->fonts.get();
|
||||
}
|
||||
|
||||
IEmotes *Application::getEmotes()
|
||||
|
|
|
@ -135,7 +135,7 @@ public:
|
|||
|
||||
private:
|
||||
Theme *const themes{};
|
||||
Fonts *const fonts{};
|
||||
std::unique_ptr<Fonts> fonts{};
|
||||
Emotes *const emotes{};
|
||||
AccountController *const accounts{};
|
||||
HotkeyController *const hotkeys{};
|
||||
|
|
|
@ -8,23 +8,12 @@
|
|||
#include <QDebug>
|
||||
#include <QtGlobal>
|
||||
|
||||
#ifdef Q_OS_WIN32
|
||||
# define DEFAULT_FONT_FAMILY "Segoe UI"
|
||||
# define DEFAULT_FONT_SIZE 10
|
||||
#else
|
||||
# ifdef Q_OS_MACOS
|
||||
# define DEFAULT_FONT_FAMILY "Helvetica Neue"
|
||||
# define DEFAULT_FONT_SIZE 12
|
||||
# else
|
||||
# define DEFAULT_FONT_FAMILY "Arial"
|
||||
# define DEFAULT_FONT_SIZE 11
|
||||
# endif
|
||||
#endif
|
||||
|
||||
namespace chatterino {
|
||||
namespace {
|
||||
int getBoldness()
|
||||
{
|
||||
|
||||
using namespace chatterino;
|
||||
|
||||
int getBoldness()
|
||||
{
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
||||
// From qfont.cpp
|
||||
// https://github.com/qt/qtbase/blob/589c6d066f84833a7c3dda1638037f4b2e91b7aa/src/gui/text/qfont.cpp#L143-L169
|
||||
|
@ -65,24 +54,16 @@ namespace {
|
|||
#else
|
||||
return getSettings()->boldScale.getValue();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Fonts *Fonts::instance = nullptr;
|
||||
namespace chatterino {
|
||||
|
||||
Fonts::Fonts()
|
||||
: chatFontFamily("/appearance/currentFontFamily", DEFAULT_FONT_FAMILY)
|
||||
, chatFontSize("/appearance/currentFontSize", DEFAULT_FONT_SIZE)
|
||||
Fonts::Fonts(Settings &settings)
|
||||
{
|
||||
Fonts::instance = this;
|
||||
|
||||
this->fontsByType_.resize(size_t(FontStyle::EndType));
|
||||
}
|
||||
|
||||
void Fonts::initialize(Settings &, const Paths &)
|
||||
{
|
||||
this->chatFontFamily.connect(
|
||||
[this]() {
|
||||
this->fontChangedListener.setCB([this] {
|
||||
assertInGuiThread();
|
||||
|
||||
for (auto &map : this->fontsByType_)
|
||||
|
@ -90,37 +71,10 @@ void Fonts::initialize(Settings &, const Paths &)
|
|||
map.clear();
|
||||
}
|
||||
this->fontChanged.invoke();
|
||||
},
|
||||
false);
|
||||
|
||||
this->chatFontSize.connect(
|
||||
[this]() {
|
||||
assertInGuiThread();
|
||||
|
||||
for (auto &map : this->fontsByType_)
|
||||
{
|
||||
map.clear();
|
||||
}
|
||||
this->fontChanged.invoke();
|
||||
},
|
||||
false);
|
||||
|
||||
#ifdef CHATTERINO
|
||||
getSettings()->boldScale.connect(
|
||||
[this]() {
|
||||
assertInGuiThread();
|
||||
|
||||
// REMOVED
|
||||
getIApp()->getWindows()->incGeneration();
|
||||
|
||||
for (auto &map : this->fontsByType_)
|
||||
{
|
||||
map.clear();
|
||||
}
|
||||
this->fontChanged.invoke();
|
||||
},
|
||||
false);
|
||||
#endif
|
||||
});
|
||||
this->fontChangedListener.addSetting(settings.chatFontFamily);
|
||||
this->fontChangedListener.addSetting(settings.chatFontSize);
|
||||
this->fontChangedListener.addSetting(settings.boldScale);
|
||||
}
|
||||
|
||||
QFont Fonts::getFont(FontStyle type, float scale)
|
||||
|
@ -159,6 +113,8 @@ Fonts::FontData &Fonts::getOrCreateFontData(FontStyle type, float scale)
|
|||
|
||||
Fonts::FontData Fonts::createFontData(FontStyle type, float scale)
|
||||
{
|
||||
auto *settings = getSettings();
|
||||
|
||||
// check if it's a chat (scale the setting)
|
||||
if (type >= FontStyle::ChatStart && type <= FontStyle::ChatEnd)
|
||||
{
|
||||
|
@ -176,8 +132,8 @@ Fonts::FontData Fonts::createFontData(FontStyle type, float scale)
|
|||
QFont::Weight(getBoldness())};
|
||||
auto data = sizeScale[type];
|
||||
return FontData(
|
||||
QFont(this->chatFontFamily.getValue(),
|
||||
int(this->chatFontSize.getValue() * data.scale * scale),
|
||||
QFont(settings->chatFontFamily.getValue(),
|
||||
int(settings->chatFontSize.getValue() * data.scale * scale),
|
||||
data.weight, data.italic));
|
||||
}
|
||||
|
||||
|
@ -205,9 +161,4 @@ Fonts::FontData Fonts::createFontData(FontStyle type, float scale)
|
|||
}
|
||||
}
|
||||
|
||||
Fonts *getFonts()
|
||||
{
|
||||
return Fonts::instance;
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
|
|
@ -1,15 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include "common/ChatterinoSetting.hpp"
|
||||
#include "common/Singleton.hpp"
|
||||
#include "pajlada/settings/settinglistener.hpp"
|
||||
|
||||
#include <pajlada/signals/signal.hpp>
|
||||
#include <QFont>
|
||||
#include <QFontDatabase>
|
||||
#include <QFontMetrics>
|
||||
|
||||
#include <array>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
|
@ -38,23 +36,17 @@ enum class FontStyle : uint8_t {
|
|||
ChatEnd = ChatVeryLarge,
|
||||
};
|
||||
|
||||
class Fonts final : public Singleton
|
||||
class Fonts final
|
||||
{
|
||||
public:
|
||||
Fonts();
|
||||
|
||||
void initialize(Settings &settings, const Paths &paths) override;
|
||||
explicit Fonts(Settings &settings);
|
||||
|
||||
// font data gets set in createFontData(...)
|
||||
|
||||
QFont getFont(FontStyle type, float scale);
|
||||
QFontMetrics getFontMetrics(FontStyle type, float scale);
|
||||
|
||||
QStringSetting chatFontFamily;
|
||||
IntSetting chatFontSize;
|
||||
|
||||
pajlada::Signals::NoArgSignal fontChanged;
|
||||
static Fonts *instance;
|
||||
|
||||
private:
|
||||
struct FontData {
|
||||
|
@ -85,8 +77,8 @@ private:
|
|||
FontData createFontData(FontStyle type, float scale);
|
||||
|
||||
std::vector<std::unordered_map<float, FontData>> fontsByType_;
|
||||
|
||||
pajlada::SettingListener fontChangedListener;
|
||||
};
|
||||
|
||||
Fonts *getFonts();
|
||||
|
||||
} // namespace chatterino
|
||||
|
|
|
@ -25,6 +25,19 @@ using TimeoutButton = std::pair<QString, int>;
|
|||
|
||||
namespace chatterino {
|
||||
|
||||
#ifdef Q_OS_WIN32
|
||||
# define DEFAULT_FONT_FAMILY "Segoe UI"
|
||||
# define DEFAULT_FONT_SIZE 10
|
||||
#else
|
||||
# ifdef Q_OS_MACOS
|
||||
# define DEFAULT_FONT_FAMILY "Helvetica Neue"
|
||||
# define DEFAULT_FONT_SIZE 12
|
||||
# else
|
||||
# define DEFAULT_FONT_FAMILY "Arial"
|
||||
# define DEFAULT_FONT_SIZE 11
|
||||
# endif
|
||||
#endif
|
||||
|
||||
void _actuallyRegisterSetting(
|
||||
std::weak_ptr<pajlada::Settings::SettingData> setting);
|
||||
|
||||
|
@ -134,6 +147,14 @@ public:
|
|||
|
||||
// BoolSetting collapseLongMessages =
|
||||
// {"/appearance/messages/collapseLongMessages", false};
|
||||
QStringSetting chatFontFamily{
|
||||
"/appearance/currentFontFamily",
|
||||
DEFAULT_FONT_FAMILY,
|
||||
};
|
||||
IntSetting chatFontSize{
|
||||
"/appearance/currentFontSize",
|
||||
DEFAULT_FONT_SIZE,
|
||||
};
|
||||
BoolSetting hideReplyContext = {"/appearance/hideReplyContext", false};
|
||||
BoolSetting showReplyButton = {"/appearance/showReplyButton", false};
|
||||
BoolSetting stripReplyMention = {"/appearance/stripReplyMention", true};
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
#include "providers/irc/IrcChannel2.hpp"
|
||||
#include "providers/irc/IrcServer.hpp"
|
||||
#include "providers/twitch/TwitchIrcServer.hpp"
|
||||
#include "singletons/Fonts.hpp"
|
||||
#include "singletons/Paths.hpp"
|
||||
#include "singletons/Settings.hpp"
|
||||
#include "singletons/Theme.hpp"
|
||||
|
|
|
@ -775,7 +775,8 @@ void BaseWindow::scaleChangedEvent(float scale)
|
|||
this->calcButtonsSizes();
|
||||
#endif
|
||||
|
||||
this->setFont(getFonts()->getFont(FontStyle::UiTabs, this->qtFontScale()));
|
||||
this->setFont(
|
||||
getIApp()->getFonts()->getFont(FontStyle::UiTabs, this->qtFontScale()));
|
||||
}
|
||||
|
||||
void BaseWindow::paintEvent(QPaintEvent *)
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
#include "Label.hpp"
|
||||
#include "widgets/Label.hpp"
|
||||
|
||||
#include "Application.hpp"
|
||||
|
||||
#include <QPainter>
|
||||
|
||||
|
@ -14,7 +16,8 @@ Label::Label(BaseWidget *parent, QString text, FontStyle style)
|
|||
, text_(std::move(text))
|
||||
, fontStyle_(style)
|
||||
{
|
||||
this->connections_.managedConnect(getFonts()->fontChanged, [this] {
|
||||
this->connections_.managedConnect(getIApp()->getFonts()->fontChanged,
|
||||
[this] {
|
||||
this->updateSize();
|
||||
});
|
||||
}
|
||||
|
@ -92,12 +95,12 @@ void Label::paintEvent(QPaintEvent *)
|
|||
1.0;
|
||||
#endif
|
||||
|
||||
QFontMetrics metrics = getFonts()->getFontMetrics(
|
||||
QFontMetrics metrics = getIApp()->getFonts()->getFontMetrics(
|
||||
this->getFontStyle(),
|
||||
this->scale() * 96.f /
|
||||
std::max<float>(
|
||||
0.01F, static_cast<float>(this->logicalDpiX() * deviceDpi)));
|
||||
painter.setFont(getFonts()->getFont(
|
||||
painter.setFont(getIApp()->getFonts()->getFont(
|
||||
this->getFontStyle(),
|
||||
this->scale() * 96.f /
|
||||
std::max<float>(
|
||||
|
@ -128,7 +131,7 @@ void Label::paintEvent(QPaintEvent *)
|
|||
void Label::updateSize()
|
||||
{
|
||||
QFontMetrics metrics =
|
||||
getFonts()->getFontMetrics(this->fontStyle_, this->scale());
|
||||
getIApp()->getFonts()->getFontMetrics(this->fontStyle_, this->scale());
|
||||
|
||||
int width =
|
||||
metrics.horizontalAdvance(this->text_) + (2 * this->getOffset());
|
||||
|
|
|
@ -47,7 +47,8 @@ TooltipWidget::TooltipWidget(BaseWidget *parent)
|
|||
this->setLayout(this->vLayout_);
|
||||
this->currentStyle_ = TooltipStyle::Vertical;
|
||||
|
||||
this->connections_.managedConnect(getFonts()->fontChanged, [this] {
|
||||
this->connections_.managedConnect(getIApp()->getFonts()->fontChanged,
|
||||
[this] {
|
||||
this->updateFont();
|
||||
});
|
||||
this->updateFont();
|
||||
|
@ -299,8 +300,8 @@ void TooltipWidget::scaleChangedEvent(float)
|
|||
|
||||
void TooltipWidget::updateFont()
|
||||
{
|
||||
this->setFont(
|
||||
getFonts()->getFont(FontStyle::ChatMediumSmall, this->scale()));
|
||||
this->setFont(getIApp()->getFonts()->getFont(FontStyle::ChatMediumSmall,
|
||||
this->scale()));
|
||||
}
|
||||
|
||||
void TooltipWidget::setWordWrap(bool wrap)
|
||||
|
|
|
@ -572,7 +572,8 @@ void UserInfoPopup::themeChangedEvent()
|
|||
|
||||
for (auto &&child : this->findChildren<QCheckBox *>())
|
||||
{
|
||||
child->setFont(getFonts()->getFont(FontStyle::UiMedium, this->scale()));
|
||||
child->setFont(
|
||||
getIApp()->getFonts()->getFont(FontStyle::UiMedium, this->scale()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -620,7 +620,7 @@ void ChannelView::scaleChangedEvent(float scale)
|
|||
0.01, this->logicalDpiX() * this->devicePixelRatioF());
|
||||
#endif
|
||||
this->goToBottom_->getLabel().setFont(
|
||||
getFonts()->getFont(FontStyle::UiMedium, factor));
|
||||
getIApp()->getFonts()->getFont(FontStyle::UiMedium, factor));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -166,8 +166,7 @@ void GeneralPage::initLayout(GeneralPageView &layout)
|
|||
}
|
||||
|
||||
layout.addDropdown<QString>(
|
||||
"Font", {"Segoe UI", "Arial", "Choose..."},
|
||||
getIApp()->getFonts()->chatFontFamily,
|
||||
"Font", {"Segoe UI", "Arial", "Choose..."}, s.chatFontFamily,
|
||||
[](auto val) {
|
||||
return val;
|
||||
},
|
||||
|
@ -177,7 +176,7 @@ void GeneralPage::initLayout(GeneralPageView &layout)
|
|||
true, "", true);
|
||||
layout.addDropdown<int>(
|
||||
"Font size", {"9pt", "10pt", "12pt", "14pt", "16pt", "20pt"},
|
||||
getIApp()->getFonts()->chatFontSize,
|
||||
s.chatFontSize,
|
||||
[](auto val) {
|
||||
return QString::number(val) + "pt";
|
||||
},
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include "gmock/gmock.h"
|
||||
#include "mocks/EmptyApplication.hpp"
|
||||
#include "singletons/Fonts.hpp"
|
||||
#include "singletons/Settings.hpp"
|
||||
#include "singletons/Theme.hpp"
|
||||
#include "widgets/Notebook.hpp"
|
||||
|
||||
|
@ -21,6 +22,11 @@ namespace {
|
|||
class MockApplication : mock::EmptyApplication
|
||||
{
|
||||
public:
|
||||
MockApplication()
|
||||
: settings(this->settingsDir.filePath("settings.json"))
|
||||
, fonts(this->settings)
|
||||
{
|
||||
}
|
||||
Theme *getThemes() override
|
||||
{
|
||||
return &this->theme;
|
||||
|
@ -36,6 +42,7 @@ public:
|
|||
return &this->fonts;
|
||||
}
|
||||
|
||||
Settings settings;
|
||||
Theme theme;
|
||||
HotkeyController hotkeys;
|
||||
Fonts fonts;
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include "singletons/Emotes.hpp"
|
||||
#include "singletons/Fonts.hpp"
|
||||
#include "singletons/Paths.hpp"
|
||||
#include "singletons/Settings.hpp"
|
||||
#include "singletons/Theme.hpp"
|
||||
#include "singletons/WindowManager.hpp"
|
||||
#include "widgets/Notebook.hpp"
|
||||
|
@ -28,7 +29,9 @@ class MockApplication : mock::EmptyApplication
|
|||
{
|
||||
public:
|
||||
MockApplication()
|
||||
: windowManager(this->paths)
|
||||
: settings(this->settingsDir.filePath("settings.json"))
|
||||
, fonts(this->settings)
|
||||
, windowManager(this->paths)
|
||||
{
|
||||
}
|
||||
Theme *getThemes() override
|
||||
|
@ -66,6 +69,7 @@ public:
|
|||
return &this->emotes;
|
||||
}
|
||||
|
||||
Settings settings;
|
||||
Theme theme;
|
||||
HotkeyController hotkeys;
|
||||
Fonts fonts;
|
||||
|
|
Loading…
Reference in a new issue