diff --git a/scwx-qt/source/scwx/qt/manager/font_manager.cpp b/scwx-qt/source/scwx/qt/manager/font_manager.cpp index 295d0cd8..db35ec68 100644 --- a/scwx-qt/source/scwx/qt/manager/font_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/font_manager.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include @@ -9,6 +11,7 @@ #include #include #include +#include #include namespace scwx @@ -44,22 +47,27 @@ struct FontRecordHash class FontManager::Impl { public: - explicit Impl() + explicit Impl(FontManager* self) : self_ {self} { InitializeFontCache(); InitializeFontconfig(); + ConnectSignals(); } ~Impl() { FinalizeFontconfig(); } + void ConnectSignals(); void FinalizeFontconfig(); void InitializeFontCache(); void InitializeFontconfig(); + void UpdateImGuiFont(types::FontCategory fontCategory); const std::vector& GetRawFontData(const std::string& filename); static FontRecord MatchFontFile(const std::string& family, const std::vector& styles); + FontManager* self_; + std::string fontCachePath_ {}; std::shared_mutex imguiFontAtlasMutex_ {}; @@ -81,12 +89,82 @@ public: std::shared_ptr> fontCategoryMap_ {}; std::mutex fontCategoryMutex_ {}; + + boost::unordered_flat_set dirtyFonts_ {}; + std::mutex dirtyFontsMutex_ {}; }; -FontManager::FontManager() : p(std::make_unique()) {} +FontManager::FontManager() : p(std::make_unique(this)) {} FontManager::~FontManager() {}; +void FontManager::Impl::ConnectSignals() +{ + auto& textSettings = settings::TextSettings::Instance(); + + for (auto fontCategory : types::FontCategoryIterator()) + { + textSettings.font_family(fontCategory) + .RegisterValueChangedCallback( + [this, fontCategory](const auto&) + { + std::unique_lock lock {dirtyFontsMutex_}; + dirtyFonts_.insert(fontCategory); + }); + textSettings.font_style(fontCategory) + .RegisterValueChangedCallback( + [this, fontCategory](const auto&) + { + std::unique_lock lock {dirtyFontsMutex_}; + dirtyFonts_.insert(fontCategory); + }); + textSettings.font_point_size(fontCategory) + .RegisterValueChangedCallback( + [this, fontCategory](const auto&) + { + std::unique_lock lock {dirtyFontsMutex_}; + dirtyFonts_.insert(fontCategory); + }); + } + + QObject::connect( + &SettingsManager::Instance(), + &SettingsManager::SettingsSaved, + self_, + [this]() + { + std::scoped_lock lock {dirtyFontsMutex_, fontCategoryMutex_}; + + for (auto fontCategory : dirtyFonts_) + { + UpdateImGuiFont(fontCategory); + } + + dirtyFonts_.clear(); + }); +} + +void FontManager::InitializeFonts() +{ + for (auto fontCategory : types::FontCategoryIterator()) + { + p->UpdateImGuiFont(fontCategory); + } +} + +void FontManager::Impl::UpdateImGuiFont(types::FontCategory fontCategory) +{ + auto& textSettings = settings::TextSettings::Instance(); + + auto family = textSettings.font_family(fontCategory).GetValue(); + auto styles = textSettings.font_style(fontCategory).GetValue(); + units::font_size::points size { + textSettings.font_point_size(fontCategory).GetValue()}; + + fontCategoryMap_.insert_or_assign( + fontCategory, self_->LoadImGuiFont(family, {styles}, size)); +} + std::shared_mutex& FontManager::imgui_font_atlas_mutex() { return p->imguiFontAtlasMutex_; diff --git a/scwx-qt/source/scwx/qt/manager/font_manager.hpp b/scwx-qt/source/scwx/qt/manager/font_manager.hpp index 6cee1135..184cea18 100644 --- a/scwx-qt/source/scwx/qt/manager/font_manager.hpp +++ b/scwx-qt/source/scwx/qt/manager/font_manager.hpp @@ -17,6 +17,7 @@ namespace manager class FontManager : public QObject { Q_OBJECT + Q_DISABLE_COPY_MOVE(FontManager) public: explicit FontManager(); @@ -34,6 +35,7 @@ public: bool loadIfNotFound = true); void LoadApplicationFont(const std::string& filename); + void InitializeFonts(); static FontManager& Instance(); diff --git a/scwx-qt/source/scwx/qt/manager/settings_manager.cpp b/scwx-qt/source/scwx/qt/manager/settings_manager.cpp index 083453ac..a4c0f4a3 100644 --- a/scwx-qt/source/scwx/qt/manager/settings_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/settings_manager.cpp @@ -104,6 +104,8 @@ void SettingsManager::SaveSettings() boost::json::value settingsJson = Impl::ConvertSettingsToJson(); util::json::WriteJsonFile(p->settingsPath_, settingsJson); + + Q_EMIT SettingsSaved(); } } diff --git a/scwx-qt/source/scwx/qt/manager/settings_manager.hpp b/scwx-qt/source/scwx/qt/manager/settings_manager.hpp index e2115e93..254ea4c8 100644 --- a/scwx-qt/source/scwx/qt/manager/settings_manager.hpp +++ b/scwx-qt/source/scwx/qt/manager/settings_manager.hpp @@ -28,6 +28,9 @@ public: static SettingsManager& Instance(); +signals: + void SettingsSaved(); + private: class Impl; std::unique_ptr p;