From e957e3fb1c186fe12fa05444a257e1d589ec5026 Mon Sep 17 00:00:00 2001 From: yan-shuifeng Date: Sun, 19 Sep 2021 19:43:44 +0800 Subject: [PATCH] add setAppBgColor interface. Signed-off-by: yan-shuifeng Change-Id: Ifca81b95f7c56393ddab60114b0553dd7b9b9470 --- .../engine/jsi/jsi_view_register.cpp | 26 +++++++++++++++++ .../engine/quickjs/qjs_view_register.cpp | 26 +++++++++++++++++ .../engine/v8/v8_view_register.cpp | 29 +++++++++++++++++++ frameworks/core/components/theme/app_theme.h | 5 ++++ frameworks/core/pipeline/pipeline_context.cpp | 14 +++++++++ frameworks/core/pipeline/pipeline_context.h | 5 +--- 6 files changed, 101 insertions(+), 4 deletions(-) diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register.cpp index dd6648fe7db..9508c196392 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register.cpp +++ b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register.cpp @@ -420,6 +420,30 @@ panda::Local Px2Lpx(panda::EcmaVM* vm, panda::Local SetAppBackgroundColor(panda::EcmaVM* vm, panda::Local value, + const panda::Local args[], int32_t argc, void* data) +{ + if (argc != 1) { + LOGE("The arg is wrong, must have one argument"); + return panda::JSValueRef::Exception(vm); + } + if (!args[0]->IsString()) { + LOGE("The arg is wrong, value must be number"); + return panda::JSValueRef::Exception(vm); + } + std::string backgroundColorStr = args[0]->ToString(vm)->ToString(); + auto container = Container::Current(); + if (!container) { + LOGW("container is null"); + return panda::JSValueRef::Exception(vm); + } + auto pipelineContext = container->GetPipelineContext(); + if (pipelineContext) { + pipelineContext->SetRootBgColor(Color::FromString(backgroundColorStr)); + } + return panda::JSValueRef::Undefined(vm); +} + static const std::unordered_map> bindFuncs = { { "Flex", JSFlexImpl::JSBind }, { "Text", JSText::JSBind }, { "Animator", JSAnimator::JSBind }, { "SpringProp", JSAnimator::JSBind }, { "SpringMotion", JSAnimator::JSBind }, @@ -529,6 +553,8 @@ void JsRegisterViews(BindingTarget globalObj) panda::FunctionRef::New(const_cast(vm), Lpx2Px, nullptr)); globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "px2lpx"), panda::FunctionRef::New(const_cast(vm), Px2Lpx, nullptr)); + globalObj->Set(vm, panda::StringRef::NewFromUtf8(vm, "setAppBgColor"), + panda::FunctionRef::New(const_cast(vm), SetAppBackgroundColor, nullptr)); JSViewAbstract::JSBind(); JSContainerBase::JSBind(); diff --git a/frameworks/bridge/declarative_frontend/engine/quickjs/qjs_view_register.cpp b/frameworks/bridge/declarative_frontend/engine/quickjs/qjs_view_register.cpp index 8bd00bcfc28..09692c239b5 100644 --- a/frameworks/bridge/declarative_frontend/engine/quickjs/qjs_view_register.cpp +++ b/frameworks/bridge/declarative_frontend/engine/quickjs/qjs_view_register.cpp @@ -435,6 +435,31 @@ JSValue Px2Lpx(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst* return lpxQJSValue; } +JSValue SetAppBackgroundColor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst* argv) +{ + if (argc != 1) { + LOGE("The arg is wrong, must have one argument"); + return JS_ThrowSyntaxError(ctx, "input value must be number"); + } + QJSContext::Scope scp(ctx); + if (!JS_IsString(argv[0])) { + LOGE("The arg is wrong, value must be string"); + return JS_ThrowSyntaxError(ctx, "input value must be string"); + } + ScopedString valueString(ctx, argv[0]); + std::string backgroundColorStr = valueString.get(); + auto container = Container::Current(); + if (!container) { + LOGW("container is null"); + return JS_ThrowSyntaxError(ctx, "container is null"); + } + auto pipelineContext = container->GetPipelineContext(); + if (pipelineContext) { + pipelineContext->SetRootBgColor(Color::FromString(backgroundColorStr)); + } + return JS_UNDEFINED; +} + void JsRegisterViews(BindingTarget globalObj) { JSContext* ctx = QJSContext::Current(); @@ -449,6 +474,7 @@ void JsRegisterViews(BindingTarget globalObj) QJSUtils::DefineGlobalFunction(ctx, Px2Fp, "px2fp", 1); QJSUtils::DefineGlobalFunction(ctx, Lpx2Px, "lpx2px", 1); QJSUtils::DefineGlobalFunction(ctx, Px2Lpx, "px2lpx", 1); + QJSUtils::DefineGlobalFunction(ctx, SetAppBackgroundColor, "setAppBgColor", 1); JSViewAbstract::JSBind(); JSContainerBase::JSBind(); diff --git a/frameworks/bridge/declarative_frontend/engine/v8/v8_view_register.cpp b/frameworks/bridge/declarative_frontend/engine/v8/v8_view_register.cpp index f1ca5c0fc18..732920fd17e 100644 --- a/frameworks/bridge/declarative_frontend/engine/v8/v8_view_register.cpp +++ b/frameworks/bridge/declarative_frontend/engine/v8/v8_view_register.cpp @@ -495,6 +495,33 @@ void Px2Lpx(const v8::FunctionCallbackInfo& args) args.GetReturnValue().Set(lpxV8Value); } +void SetAppBackgroundColor(const v8::FunctionCallbackInfo& args) +{ + v8::Isolate* isolate = args.GetIsolate(); + ACE_DCHECK(isolate); + v8::Isolate::Scope isolateScope(isolate); + v8::HandleScope handleScope(isolate); + auto context = isolate->GetCurrentContext(); + if (args.Length() < 1 || !args[0]->IsString()) { + LOGE("The arg is wrong, must have one argument"); + return; + } + v8::String::Utf8Value backgroundColorUtf8Str(isolate, args[0]); + if (!(*backgroundColorUtf8Str)) { + return; + } + std::string backgroundColorStr(*backgroundColorUtf8Str); + auto container = Container::Current(); + if (!container) { + LOGW("container is null"); + return; + } + auto pipelineContext = container->GetPipelineContext(); + if (pipelineContext) { + pipelineContext->SetRootBgColor(Color::FromString(backgroundColorStr)); + } +} + static const std::unordered_map> bindFuncs = { {"Flex", JSFlexImpl::JSBind}, {"Text", JSText::JSBind}, @@ -646,6 +673,8 @@ void JsRegisterViews(BindingTarget globalObj) v8::FunctionTemplate::New(isolate, Lpx2Px)); globalObj->Set(v8::String::NewFromUtf8(isolate, "px2lpx").ToLocalChecked(), v8::FunctionTemplate::New(isolate, Px2Lpx)); + globalObj->Set(v8::String::NewFromUtf8(isolate, "setAppBgColor").ToLocalChecked(), + v8::FunctionTemplate::New(isolate, SetAppBackgroundColor)); JSViewAbstract::JSBind(); JSContainerBase::JSBind(); diff --git a/frameworks/core/components/theme/app_theme.h b/frameworks/core/components/theme/app_theme.h index 59c3c10a24e..8d1312abb60 100644 --- a/frameworks/core/components/theme/app_theme.h +++ b/frameworks/core/components/theme/app_theme.h @@ -47,6 +47,11 @@ public: return backgroundColor_; } + void SetBackgroundColor(const Color& color) + { + backgroundColor_ = color; + } + protected: AppTheme() = default; diff --git a/frameworks/core/pipeline/pipeline_context.cpp b/frameworks/core/pipeline/pipeline_context.cpp index 770de5eea1a..4c5a004b2cb 100644 --- a/frameworks/core/pipeline/pipeline_context.cpp +++ b/frameworks/core/pipeline/pipeline_context.cpp @@ -52,6 +52,7 @@ #include "core/components/scroll/scrollable.h" #include "core/components/semi_modal/semi_modal_component.h" #include "core/components/semi_modal/semi_modal_element.h" +#include "core/components/theme/app_theme.h" #include "core/components/stage/stage_component.h" #include "core/components/stage/stage_element.h" #include "core/image/image_provider.h" @@ -1684,6 +1685,19 @@ void PipelineContext::SetRootRect(double width, double height) const } } +void PipelineContext::SetRootBgColor(const Color& color) +{ + rootBgColor_ = color; + auto appTheme = themeManager_->GetTheme(); + appTheme->SetBackgroundColor(color); + if (rootElement_) { + auto renderRoot = DynamicCast(rootElement_->GetRenderNode()); + if (renderRoot) { + renderRoot->SetBgColor(color); + } + } +} + void PipelineContext::Finish(bool autoFinish) const { CHECK_RUN_ON(UI); diff --git a/frameworks/core/pipeline/pipeline_context.h b/frameworks/core/pipeline/pipeline_context.h index 3cac616ba2b..3d11a1a3d28 100644 --- a/frameworks/core/pipeline/pipeline_context.h +++ b/frameworks/core/pipeline/pipeline_context.h @@ -722,10 +722,7 @@ public: forbidePlatformQuit_ = forbidePlatformQuit; } - void SetRootBgColor(const Color& color) - { - rootBgColor_ = color; - } + void SetRootBgColor(const Color& color); const Color& GetRootBgColor() const { -- Gitee