diff --git a/source/MRCommonPlugins/ViewerButtons/MRAddCustomTheme.cpp b/source/MRCommonPlugins/ViewerButtons/MRAddCustomTheme.cpp index b1a5acbe4a81..6d1ea5171bf3 100644 --- a/source/MRCommonPlugins/ViewerButtons/MRAddCustomTheme.cpp +++ b/source/MRCommonPlugins/ViewerButtons/MRAddCustomTheme.cpp @@ -277,7 +277,6 @@ std::string AddCustomThemePlugin::save_() } ColorTheme::setupUserTheme( themeName_ ); - ColorTheme::apply(); if ( !applyToNewObjectsOnly_ ) { auto visualObjs = getAllObjectsInTree( &SceneRoot::get() ); diff --git a/source/MRViewer/MRColorTheme.cpp b/source/MRViewer/MRColorTheme.cpp index 7975dda0368e..6a263917b75b 100644 --- a/source/MRViewer/MRColorTheme.cpp +++ b/source/MRViewer/MRColorTheme.cpp @@ -36,7 +36,6 @@ EMSCRIPTEN_KEEPALIVE int emsChangeColorTheme( int theme ) ColorTheme::setupDefaultDark(); else ColorTheme::setupDefaultLight(); - ColorTheme::apply(); return 1; } @@ -83,23 +82,20 @@ void ColorTheme::setupFromJson( const Json::Value& root, Type type ) instance.themePreset_ = themePreset; instance.type_ = type; + std::vector sceneColors; if ( valid ) { - if ( instance.sceneColors_.size() < SceneColors::Count ) - instance.sceneColors_.resize( SceneColors::Count ); + sceneColors.resize( SceneColors::Count ); for ( int i = 0; i < SceneColors::Count; ++i ) { auto name = SceneColors::getName( SceneColors::Type( i ) ); if ( root[name].isObject() ) - deserializeFromJson( root[name], instance.sceneColors_[i] ); + deserializeFromJson( root[name], sceneColors[i] ); else defined = false; } - } - if ( valid ) - { if ( root["Ribbon Colors"].isObject() ) { const auto& ribColors = root["Ribbon Colors"]; @@ -137,7 +133,12 @@ void ColorTheme::setupFromJson( const Json::Value& root, Type type ) if ( !valid || ( type == Type::Default && !defined ) ) { spdlog::error( "Color theme deserialization failed: invalid json schema." ); - instance.sceneColors_.clear(); + instance.isInitialized_ = false; + } + else + { + instance.isInitialized_ = true; + apply_( sceneColors ); } } @@ -166,18 +167,16 @@ void ColorTheme::serializeCurrentToJson( Json::Value& root ) assert( ImGui::GetCurrentContext() ); auto& instance = ColorTheme::instance(); - if ( instance.sceneColors_.size() < SceneColors::Count ) - instance.sceneColors_.resize( SceneColors::Count ); - + std::vector sceneColors( SceneColors::Count ); for ( int i = 0; i < SceneColors::Count; ++i ) - instance.sceneColors_[i] = SceneColors::get( SceneColors::Type( i ) ); + sceneColors[i] = SceneColors::get( SceneColors::Type( i ) ); const auto& vpParams = Viewer::instanceRef().viewport().getParameters(); setViewportColor( vpParams.backgroundColor, ViewportColorsType::Background ); setViewportColor( vpParams.borderColor, ViewportColorsType::Borders ); for ( int i = 0; i < SceneColors::Count; ++i ) - serializeToJson( instance.sceneColors_[i], root[SceneColors::getName( SceneColors::Type( i ) )] ); + serializeToJson( sceneColors[i], root[SceneColors::getName( SceneColors::Type( i ) )] ); root["ImGuiPreset"] = getPresetName( instance.themePreset_ ); @@ -190,20 +189,14 @@ void ColorTheme::serializeCurrentToJson( Json::Value& root ) serializeToJson( instance.viewportColors_[i], vieportColors[getViewportColorTypeName( ViewportColorsType( i ) )] ); } -void ColorTheme::apply() +void ColorTheme::apply_( const std::vector& sceneColors ) { - if ( !ColorTheme::isInitialized() ) - { - spdlog::warn( "Color theme is not initialized" ); - return; - } - spdlog::info( "Apply color theme." ); const auto& instance = ColorTheme::instance(); for ( int i = 0; i < SceneColors::Count; ++i ) - SceneColors::set( SceneColors::Type( i ), instance.sceneColors_[i] ); + SceneColors::set( SceneColors::Type( i ), sceneColors[i] ); RibbonButtonDrawer::InitGradientTexture(); UI::init(); @@ -228,7 +221,7 @@ void ColorTheme::apply() bool ColorTheme::isInitialized() { - return !ColorTheme::instance().sceneColors_.empty(); + return ColorTheme::instance().isInitialized_; } void ColorTheme::setRibbonColor( const Color& color, RibbonColorsType type ) diff --git a/source/MRViewer/MRColorTheme.h b/source/MRViewer/MRColorTheme.h index 21c2579f5975..2016aab86e2e 100644 --- a/source/MRViewer/MRColorTheme.h +++ b/source/MRViewer/MRColorTheme.h @@ -57,11 +57,6 @@ class ColorTheme // gets active viewport background color MRVIEWER_API static void serializeCurrentToJson( Json::Value& root ); - // Applies colors stored in this struct to application - // really some colors of this theme are applied deferred on next frame because of ImGui::PushStyleColor problem - // note that struct should be initialized when apply is called - // initialized in this scope means that structure has it's own values for colors - MRVIEWER_API static void apply(); // True if this structure is filled with colors, false if empty MRVIEWER_API static bool isInitialized(); @@ -182,8 +177,13 @@ class ColorTheme ColorTheme() = default; ~ColorTheme() = default; + // Applies colors stored in this struct to application + // really some colors of this theme are applied deferred on next frame because of ImGui::PushStyleColor problem + // note that struct should be initialized when apply is called + // initialized in this scope means that structure has it's own values for colors + MRVIEWER_API static void apply_( const std::vector& sceneColors ); - std::vector sceneColors_; + bool isInitialized_ = false; Preset themePreset_ = Preset::Dark; std::array newUIColors_; std::array viewportColors_; diff --git a/source/MRViewer/MRRibbonLayoutConfig.cpp b/source/MRViewer/MRRibbonLayoutConfig.cpp index 010b31d9d8e1..d2873864bdcd 100644 --- a/source/MRViewer/MRRibbonLayoutConfig.cpp +++ b/source/MRViewer/MRRibbonLayoutConfig.cpp @@ -71,10 +71,7 @@ void applyRibbonConfig( const RibbonConfig& config ) ribbonMenu->getRibbonButtonDrawer().setMonochrome( config.monochromeRibbonIcons ); if ( config.colorTheme ) - { ColorTheme::setupFromJson( *config.colorTheme ); - ColorTheme::apply(); - } if ( config.ribbonStructure || config.ribbonItemsOverrides ) { diff --git a/source/MRViewer/MRViewerSettingsManager.cpp b/source/MRViewer/MRViewerSettingsManager.cpp index 8680423a21c5..7b1a57ee6294 100644 --- a/source/MRViewer/MRViewerSettingsManager.cpp +++ b/source/MRViewer/MRViewerSettingsManager.cpp @@ -192,7 +192,6 @@ void ViewerSettingsManager::resetSettings( Viewer& viewer ) #else ColorTheme::setupByTypeName( ColorTheme::Type::Default, ColorTheme::getPresetName( ColorTheme::getPreset() ) ); #endif - ColorTheme::apply(); // lastExtentions_.clear(); @@ -412,7 +411,6 @@ void ViewerSettingsManager::loadSettings( Viewer& viewer ) // setup default in this case ColorTheme::setupByTypeName( ColorTheme::Type::Default, ColorTheme::getPresetName( ColorTheme::Preset::Default ) ); } - ColorTheme::apply(); Json::Value lastExtentions = cfg.getJsonValue( lastExtensionsParamKey ); if ( lastExtentions.isArray() ) diff --git a/source/MRViewer/MRViewerSettingsPlugin.cpp b/source/MRViewer/MRViewerSettingsPlugin.cpp index 14b4409a03ad..3bc58f41ba72 100644 --- a/source/MRViewer/MRViewerSettingsPlugin.cpp +++ b/source/MRViewer/MRViewerSettingsPlugin.cpp @@ -888,7 +888,6 @@ void ViewerSettingsPlugin::drawThemeSelector_() showError( "This theme is not valid." ); } backgroundColor_ = Vector4f( ColorTheme::getViewportColor( ColorTheme::ViewportColorsType::Background ) ); - ColorTheme::apply(); } auto item = RibbonSchemaHolder::schema().items.find( "Add custom theme" ); if ( item != RibbonSchemaHolder::schema().items.end() )