From 2803dc069802c54a5cdefdf1c41f701c4e438485 Mon Sep 17 00:00:00 2001 From: Ilya Erokhin Date: Fri, 5 Jul 2024 15:35:17 +0300 Subject: [PATCH] [HOS-2828] Create System Theme Tokens cache in native side --- .../bridge/declarative_frontend/BUILD.gn | 2 + .../ark_theme/theme_apply/js_with_theme.cpp | 12 +++++ .../src/utility/ArkResourcesHelper.ts | 2 +- .../ark_system_theme_tokens_cache.cpp | 48 +++++++++++++++++++ .../ark_system_theme_tokens_cache.h | 40 ++++++++++++++++ .../declarative_frontend/engine/arkTheme.js | 2 +- 6 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 frameworks/bridge/declarative_frontend/ark_theme/theme_native/ark_system_theme_tokens_cache.cpp create mode 100644 frameworks/bridge/declarative_frontend/ark_theme/theme_native/ark_system_theme_tokens_cache.h diff --git a/frameworks/bridge/declarative_frontend/BUILD.gn b/frameworks/bridge/declarative_frontend/BUILD.gn index 65a9cbaca3e..c5f116205fc 100644 --- a/frameworks/bridge/declarative_frontend/BUILD.gn +++ b/frameworks/bridge/declarative_frontend/BUILD.gn @@ -357,6 +357,7 @@ template("declarative_js_engine") { # ark_theme "ark_theme/theme_apply/js_with_theme.cpp", "ark_theme/theme_native/ark_theme_storage.cpp", + "ark_theme/theme_native/ark_system_theme_tokens_cache.cpp", # Models implemenations for classic fw "jsview/models/action_sheet_model_impl.cpp", @@ -869,6 +870,7 @@ template("declarative_js_engine_ng") { # ark_theme "ark_theme/theme_apply/js_with_theme.cpp", "ark_theme/theme_native/ark_theme_storage.cpp", + "ark_theme/theme_native/ark_system_theme_tokens_cache.cpp", ] sources += [ diff --git a/frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_with_theme.cpp b/frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_with_theme.cpp index 5c0508cb12a..41e5cf7613b 100644 --- a/frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_with_theme.cpp +++ b/frameworks/bridge/declarative_frontend/ark_theme/theme_apply/js_with_theme.cpp @@ -17,6 +17,7 @@ #include "base/memory/referenced.h" #include "bridge/declarative_frontend/ark_theme/theme_apply/js_theme.h" +#include "bridge/declarative_frontend/ark_theme/theme_native/ark_system_theme_tokens_cache.h" #include "bridge/declarative_frontend/ark_theme/theme_native/ark_theme_storage.h" #include "core/components/common/properties/color.h" #include "core/components_ng/base/view_abstract_model.h" @@ -154,7 +155,18 @@ void JSWithTheme::Create(const JSCallbackInfo& info) std::vector colors; for (int i = 0; i < COLORS_NUMBER; i++) { + auto jsColor = jsColorsArray->GetValueAt(i); Color color; + if (jsColor->IsObject()) { + JSRef jsObj = JSRef::Cast(jsColor); + if (jsObj->HasProperty("sysTheme")) { + NG::ArkSystemThemeTokensCache::GetInstance()->GetOrCreateColor(color, i, [&](Color& col){ + JSViewAbstract::ParseJsColor(jsColor, col); + }); + colors.push_back(color); + continue; + } + } JSViewAbstract::ParseJsColor(jsColorsArray->GetValueAt(i), color); colors.push_back(color); } diff --git a/frameworks/bridge/declarative_frontend/ark_theme/theme_manager/src/utility/ArkResourcesHelper.ts b/frameworks/bridge/declarative_frontend/ark_theme/theme_manager/src/utility/ArkResourcesHelper.ts index df0dd3047f5..a7293d7513f 100644 --- a/frameworks/bridge/declarative_frontend/ark_theme/theme_manager/src/utility/ArkResourcesHelper.ts +++ b/frameworks/bridge/declarative_frontend/ark_theme/theme_manager/src/utility/ArkResourcesHelper.ts @@ -30,6 +30,6 @@ class ArkResourcesHelper { type = ArkResourcesHelper.COLOR; break; } - return { "id": id ?? -1, "type": type, 'params': [name], 'bundleName': '', 'moduleName': '' }; + return { "id": id ?? -1, "type": type, 'params': [name], 'bundleName': '', 'moduleName': '', 'sysTheme': true }; } } \ No newline at end of file diff --git a/frameworks/bridge/declarative_frontend/ark_theme/theme_native/ark_system_theme_tokens_cache.cpp b/frameworks/bridge/declarative_frontend/ark_theme/theme_native/ark_system_theme_tokens_cache.cpp new file mode 100644 index 00000000000..121f4eac5f5 --- /dev/null +++ b/frameworks/bridge/declarative_frontend/ark_theme/theme_native/ark_system_theme_tokens_cache.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "bridge/declarative_frontend/ark_theme/theme_native/ark_system_theme_tokens_cache.h" + +namespace OHOS::Ace::NG { + +thread_local std::unique_ptr ArkSystemThemeTokensCache::instance_ = nullptr; +std::vector> ArkSystemThemeTokensCache::colorsCache_(51, std::nullopt); + +ArkSystemThemeTokensCache * ArkSystemThemeTokensCache::GetInstance() +{ + if (!instance_) { + instance_.reset(new ArkSystemThemeTokensCache); + } + return instance_.get(); +} + +void ArkSystemThemeTokensCache::GetOrCreateColor(Color& color, int32_t index, std::function&& populateColorFunc) +{ + if (index < 0 || index >= 51) { + return; + } + + auto colorOptional = colorsCache_[index]; + if (colorOptional.has_value()) { + color = colorOptional.value(); + return; + } + + populateColorFunc(color); + colorsCache_[index] = std::make_optional(color); + return; +} + +} // namespace OHOS::Ace::NG \ No newline at end of file diff --git a/frameworks/bridge/declarative_frontend/ark_theme/theme_native/ark_system_theme_tokens_cache.h b/frameworks/bridge/declarative_frontend/ark_theme/theme_native/ark_system_theme_tokens_cache.h new file mode 100644 index 00000000000..4af62944d5e --- /dev/null +++ b/frameworks/bridge/declarative_frontend/ark_theme/theme_native/ark_system_theme_tokens_cache.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ARK_THEME_THEME_NATIVE_ARK_SYSTEM_THEME_TOKENS_CACHE_H +#define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ARK_THEME_THEME_NATIVE_ARK_SYSTEM_THEME_TOKENS_CACHE_H + +#include "base/memory/referenced.h" +#include "core/components/common/properties/color.h" + +namespace OHOS::Ace::NG { + +class ACE_EXPORT ArkSystemThemeTokensCache final { +public: + ACE_FORCE_EXPORT static ArkSystemThemeTokensCache* GetInstance(); + ~ArkSystemThemeTokensCache() = default; + + void GetOrCreateColor(Color& color, int32_t index, std::function&& populateColorFunc); +private: + ArkSystemThemeTokensCache() = default; + + // Singleton instance + static thread_local std::unique_ptr instance_; + + static std::vector> colorsCache_; +}; + +} // namespace OHOS::Ace::NG +#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ARK_THEME_THEME_NATIVE_ARK_SYSTEM_THEME_TOKENS_CACHE_H \ No newline at end of file diff --git a/frameworks/bridge/declarative_frontend/engine/arkTheme.js b/frameworks/bridge/declarative_frontend/engine/arkTheme.js index 2bdd9b417c5..5195af82021 100644 --- a/frameworks/bridge/declarative_frontend/engine/arkTheme.js +++ b/frameworks/bridge/declarative_frontend/engine/arkTheme.js @@ -27,7 +27,7 @@ class ArkResourcesHelper { type = ArkResourcesHelper.COLOR; break; } - return { "id": id !== null && id !== void 0 ? id : -1, "type": type, 'params': [name], 'bundleName': '', 'moduleName': '' }; + return { "id": id !== null && id !== void 0 ? id : -1, "type": type, 'params': [name], 'bundleName': '', 'moduleName': '', 'sysTheme': true }; } } ArkResourcesHelper.COLOR = 10001; -- Gitee