Select the default theme font when clicking custom font check boxes

This commit is contained in:
David Capello 2025-02-15 12:54:42 -03:00
parent 698c581c39
commit b7967a078e
3 changed files with 38 additions and 17 deletions

View File

@ -34,6 +34,7 @@
#include "app/ui/rgbmap_algorithm_selector.h"
#include "app/ui/sampling_selector.h"
#include "app/ui/separator_in_view.h"
#include "app/ui/skin/font_data.h"
#include "app/ui/skin/skin_theme.h"
#include "app/util/render_text.h"
#include "base/convert_to.h"
@ -445,16 +446,12 @@ public:
// Theme Custom Font
customThemeFont()->Click.connect([this] {
const bool state = customThemeFont()->isSelected();
themeFont()->setEnabled(state);
if (!state)
themeFont()->setInfo(FontInfo(), FontEntry::From::Init);
auto* theme = skin::SkinTheme::get(this);
onSwitchCustomFontCheckBox(customThemeFont(), themeFont(), theme->getOriginalDefaultFont());
});
customMiniFont()->Click.connect([this] {
const bool state = customMiniFont()->isSelected();
themeMiniFont()->setEnabled(state);
if (!state)
themeMiniFont()->setInfo(FontInfo(), FontEntry::From::Init);
auto* theme = skin::SkinTheme::get(this);
onSwitchCustomFontCheckBox(customMiniFont(), themeMiniFont(), theme->getOriginalMiniFont());
});
themeFont()->FontChange.connect([this] { updateFontPreviews(); });
themeMiniFont()->FontChange.connect([this] { updateFontPreviews(); });
@ -1090,6 +1087,29 @@ private:
themeMiniFont()->setInfo(miniInfo, FontEntry::From::Init);
}
void onSwitchCustomFontCheckBox(CheckBox* fontCheckBox,
FontEntry* fontEntry,
const text::FontRef& themeFont)
{
const bool state = fontCheckBox->isSelected();
fontEntry->setEnabled(state);
FontInfo fi;
auto* theme = skin::SkinTheme::get(this);
text::FontMgrRef fontMgr = theme->fontMgr();
for (auto kv : theme->getWellKnownFonts()) {
if (kv.second->getFont(fontMgr, themeFont->height(), guiscale()) == themeFont) {
fi = FontInfo(FontInfo::Type::Name,
kv.first,
themeFont->height(),
text::FontStyle(),
themeFont->antialias() ? FontInfo::Flags::Antialias : FontInfo::Flags::None);
break;
}
}
fontEntry->setInfo(fi, FontEntry::From::Init);
}
void updateFontPreviews()
{
m_font = get_font_from_info(themeFont()->info());

View File

@ -275,12 +275,7 @@ SkinTheme* SkinTheme::get(const ui::Widget* widget)
return static_cast<SkinTheme*>(widget->theme());
}
SkinTheme::SkinTheme()
: m_sheet(nullptr)
, m_defaultFont(nullptr)
, m_miniFont(nullptr)
, m_preferredScreenScaling(-1)
, m_preferredUIScaling(-1)
SkinTheme::SkinTheme() : m_sheet(nullptr), m_preferredScreenScaling(-1), m_preferredUIScaling(-1)
{
m_standardCursors.fill(nullptr);
}
@ -484,6 +479,9 @@ void SkinTheme::loadXml(BackwardCompatibility* backward)
if (!m_miniFont)
m_miniFont = m_defaultFont;
m_originalDefaultFont = m_defaultFont;
m_originalMiniFont = m_miniFont;
// Overwrite theme fonts by user defined fonts.
Preferences& pref = Preferences::instance();
if (!pref.theme.font().empty()) {

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2020-2024 Igara Studio S.A.
// Copyright (C) 2020-2025 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
@ -63,6 +63,9 @@ public:
int preferredScreenScaling() const { return m_preferredScreenScaling; }
int preferredUIScaling() const { return m_preferredUIScaling; }
text::FontRef getOriginalDefaultFont() const { return m_originalDefaultFont; }
text::FontRef getOriginalMiniFont() { return m_originalMiniFont; }
text::FontRef getDefaultFont() const override { return m_defaultFont; }
text::FontRef getWidgetFont(const ui::Widget* widget) const override;
text::FontRef getMiniFont() const { return m_miniFont; }
@ -218,8 +221,8 @@ private:
std::map<std::string, ThemeFont> m_themeFonts;
// Stores the unscaled font version of the Font pointer used as a key.
std::map<text::Font*, text::FontRef> m_unscaledFonts;
text::FontRef m_defaultFont;
text::FontRef m_miniFont;
text::FontRef m_originalDefaultFont, m_defaultFont;
text::FontRef m_originalMiniFont, m_miniFont;
int m_preferredScreenScaling;
int m_preferredUIScaling;
};