123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#include "prompt_action.h" 1723b3eb3cSopenharmony_ci 1823b3eb3cSopenharmony_ci 1923b3eb3cSopenharmony_ci#include "interfaces/napi/kits/utils/napi_utils.h" 2023b3eb3cSopenharmony_ci#include "base/i18n/localization.h" 2123b3eb3cSopenharmony_ci#include "base/subwindow/subwindow_manager.h" 2223b3eb3cSopenharmony_ci#include "bridge/common/utils/engine_helper.h" 2323b3eb3cSopenharmony_ci#include "core/common/ace_engine.h" 2423b3eb3cSopenharmony_ci#include "core/components/theme/shadow_theme.h" 2523b3eb3cSopenharmony_ci 2623b3eb3cSopenharmony_cinamespace OHOS::Ace::Napi { 2723b3eb3cSopenharmony_cinamespace { 2823b3eb3cSopenharmony_ciconst int32_t SHOW_DIALOG_BUTTON_NUM_MAX = -1; 2923b3eb3cSopenharmony_ciconst int32_t SHOW_ACTION_MENU_BUTTON_NUM_MAX = 6; 3023b3eb3cSopenharmony_ciconst int32_t CUSTOM_DIALOG_PARAM_NUM = 2; 3123b3eb3cSopenharmony_ciconst int32_t BG_BLUR_STYLE_MAX_INDEX = 12; 3223b3eb3cSopenharmony_ciconst int32_t PROMPTACTION_VALID_PRIMARY_BUTTON_NUM = 1; 3323b3eb3cSopenharmony_ciconstexpr char DEFAULT_FONT_COLOR_STRING_VALUE[] = "#ff007dff"; 3423b3eb3cSopenharmony_ciconst std::vector<DialogAlignment> DIALOG_ALIGNMENT = { DialogAlignment::TOP, DialogAlignment::CENTER, 3523b3eb3cSopenharmony_ci DialogAlignment::BOTTOM, DialogAlignment::DEFAULT, DialogAlignment::TOP_START, DialogAlignment::TOP_END, 3623b3eb3cSopenharmony_ci DialogAlignment::CENTER_START, DialogAlignment::CENTER_END, DialogAlignment::BOTTOM_START, 3723b3eb3cSopenharmony_ci DialogAlignment::BOTTOM_END }; 3823b3eb3cSopenharmony_ciconst std::vector<KeyboardAvoidMode> KEYBOARD_AVOID_MODE = { KeyboardAvoidMode::DEFAULT, KeyboardAvoidMode::NONE }; 3923b3eb3cSopenharmony_ciconst std::vector<HoverModeAreaType> HOVER_MODE_AREA_TYPE = { HoverModeAreaType::TOP_SCREEN, 4023b3eb3cSopenharmony_ci HoverModeAreaType::BOTTOM_SCREEN }; 4123b3eb3cSopenharmony_ci 4223b3eb3cSopenharmony_ci#ifdef OHOS_STANDARD_SYSTEM 4323b3eb3cSopenharmony_cibool ContainerIsService() 4423b3eb3cSopenharmony_ci{ 4523b3eb3cSopenharmony_ci auto containerId = Container::CurrentIdSafely(); 4623b3eb3cSopenharmony_ci // Get active container when current instanceid is less than 0 4723b3eb3cSopenharmony_ci if (containerId < 0) { 4823b3eb3cSopenharmony_ci auto container = Container::GetActive(); 4923b3eb3cSopenharmony_ci if (container) { 5023b3eb3cSopenharmony_ci containerId = container->GetInstanceId(); 5123b3eb3cSopenharmony_ci } 5223b3eb3cSopenharmony_ci } 5323b3eb3cSopenharmony_ci // for pa service 5423b3eb3cSopenharmony_ci return containerId >= MIN_PA_SERVICE_ID || containerId < 0; 5523b3eb3cSopenharmony_ci} 5623b3eb3cSopenharmony_ci 5723b3eb3cSopenharmony_cibool ContainerIsScenceBoard() 5823b3eb3cSopenharmony_ci{ 5923b3eb3cSopenharmony_ci auto container = Container::CurrentSafely(); 6023b3eb3cSopenharmony_ci if (!container) { 6123b3eb3cSopenharmony_ci container = Container::GetActive(); 6223b3eb3cSopenharmony_ci } 6323b3eb3cSopenharmony_ci 6423b3eb3cSopenharmony_ci return container && container->IsScenceBoardWindow(); 6523b3eb3cSopenharmony_ci} 6623b3eb3cSopenharmony_ci#endif 6723b3eb3cSopenharmony_ci} // namespace 6823b3eb3cSopenharmony_ci 6923b3eb3cSopenharmony_cibool GetToastMessage(napi_env env, napi_value messageNApi, std::string& messageString) 7023b3eb3cSopenharmony_ci{ 7123b3eb3cSopenharmony_ci size_t ret = 0; 7223b3eb3cSopenharmony_ci ResourceInfo recv; 7323b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 7423b3eb3cSopenharmony_ci napi_typeof(env, messageNApi, &valueType); 7523b3eb3cSopenharmony_ci if (valueType == napi_string) { 7623b3eb3cSopenharmony_ci size_t messageLen = GetParamLen(env, messageNApi) + 1; 7723b3eb3cSopenharmony_ci std::unique_ptr<char[]> message = std::make_unique<char[]>(messageLen); 7823b3eb3cSopenharmony_ci napi_get_value_string_utf8(env, messageNApi, message.get(), messageLen, &ret); 7923b3eb3cSopenharmony_ci messageString = message.get(); 8023b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 8123b3eb3cSopenharmony_ci if (!ParseResourceParam(env, messageNApi, recv)) { 8223b3eb3cSopenharmony_ci NapiThrow(env, "Can not parse resource info from input params.", ERROR_CODE_INTERNAL_ERROR); 8323b3eb3cSopenharmony_ci return false; 8423b3eb3cSopenharmony_ci } 8523b3eb3cSopenharmony_ci if (!ParseString(recv, messageString)) { 8623b3eb3cSopenharmony_ci NapiThrow(env, "Can not get message from resource manager.", ERROR_CODE_INTERNAL_ERROR); 8723b3eb3cSopenharmony_ci return false; 8823b3eb3cSopenharmony_ci } 8923b3eb3cSopenharmony_ci if (messageString.size() == 0) { 9023b3eb3cSopenharmony_ci TAG_LOGE(AceLogTag::ACE_DIALOG, "Toast message is empty"); 9123b3eb3cSopenharmony_ci } 9223b3eb3cSopenharmony_ci } else { 9323b3eb3cSopenharmony_ci NapiThrow(env, "The type of message is incorrect.", ERROR_CODE_PARAM_INVALID); 9423b3eb3cSopenharmony_ci return false; 9523b3eb3cSopenharmony_ci } 9623b3eb3cSopenharmony_ci return true; 9723b3eb3cSopenharmony_ci} 9823b3eb3cSopenharmony_ci 9923b3eb3cSopenharmony_cibool GetToastDuration(napi_env env, napi_value durationNApi, int32_t& duration) 10023b3eb3cSopenharmony_ci{ 10123b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 10223b3eb3cSopenharmony_ci napi_typeof(env, durationNApi, &valueType); 10323b3eb3cSopenharmony_ci ResourceInfo recv; 10423b3eb3cSopenharmony_ci std::string durationStr; 10523b3eb3cSopenharmony_ci if (valueType == napi_number) { 10623b3eb3cSopenharmony_ci napi_get_value_int32(env, durationNApi, &duration); 10723b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 10823b3eb3cSopenharmony_ci recv = {}; 10923b3eb3cSopenharmony_ci if (!ParseResourceParam(env, durationNApi, recv)) { 11023b3eb3cSopenharmony_ci NapiThrow(env, "Can not parse resource info from input params.", ERROR_CODE_INTERNAL_ERROR); 11123b3eb3cSopenharmony_ci return false; 11223b3eb3cSopenharmony_ci } 11323b3eb3cSopenharmony_ci if (!ParseString(recv, durationStr)) { 11423b3eb3cSopenharmony_ci NapiThrow(env, "Can not get message from resource manager.", ERROR_CODE_INTERNAL_ERROR); 11523b3eb3cSopenharmony_ci return false; 11623b3eb3cSopenharmony_ci } 11723b3eb3cSopenharmony_ci duration = StringUtils::StringToInt(durationStr); 11823b3eb3cSopenharmony_ci } 11923b3eb3cSopenharmony_ci return true; 12023b3eb3cSopenharmony_ci} 12123b3eb3cSopenharmony_ci 12223b3eb3cSopenharmony_cibool GetToastBottom(napi_env env, napi_value bottomNApi, std::string& bottomString) 12323b3eb3cSopenharmony_ci{ 12423b3eb3cSopenharmony_ci size_t ret = 0; 12523b3eb3cSopenharmony_ci ResourceInfo recv; 12623b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 12723b3eb3cSopenharmony_ci napi_typeof(env, bottomNApi, &valueType); 12823b3eb3cSopenharmony_ci if (valueType == napi_string) { 12923b3eb3cSopenharmony_ci size_t bottomLen = GetParamLen(env, bottomNApi) + 1; 13023b3eb3cSopenharmony_ci std::unique_ptr<char[]> bottom = std::make_unique<char[]>(bottomLen); 13123b3eb3cSopenharmony_ci napi_get_value_string_utf8(env, bottomNApi, bottom.get(), bottomLen, &ret); 13223b3eb3cSopenharmony_ci bottomString = bottom.get(); 13323b3eb3cSopenharmony_ci } else if (valueType == napi_number) { 13423b3eb3cSopenharmony_ci double bottom = 0.0; 13523b3eb3cSopenharmony_ci napi_get_value_double(env, bottomNApi, &bottom); 13623b3eb3cSopenharmony_ci bottomString = std::to_string(bottom); 13723b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 13823b3eb3cSopenharmony_ci recv = {}; 13923b3eb3cSopenharmony_ci if (!ParseResourceParam(env, bottomNApi, recv)) { 14023b3eb3cSopenharmony_ci NapiThrow(env, "Can not parse resource info from input params.", ERROR_CODE_INTERNAL_ERROR); 14123b3eb3cSopenharmony_ci return false; 14223b3eb3cSopenharmony_ci } 14323b3eb3cSopenharmony_ci if (!ParseString(recv, bottomString)) { 14423b3eb3cSopenharmony_ci NapiThrow(env, "Can not get message from resource manager.", ERROR_CODE_INTERNAL_ERROR); 14523b3eb3cSopenharmony_ci return false; 14623b3eb3cSopenharmony_ci } 14723b3eb3cSopenharmony_ci } 14823b3eb3cSopenharmony_ci return true; 14923b3eb3cSopenharmony_ci} 15023b3eb3cSopenharmony_ci 15123b3eb3cSopenharmony_cibool GetToastShowMode(napi_env env, napi_value showModeNApi, NG::ToastShowMode& showMode) 15223b3eb3cSopenharmony_ci{ 15323b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 15423b3eb3cSopenharmony_ci napi_typeof(env, showModeNApi, &valueType); 15523b3eb3cSopenharmony_ci if (valueType == napi_number) { 15623b3eb3cSopenharmony_ci int32_t num = -1; 15723b3eb3cSopenharmony_ci napi_get_value_int32(env, showModeNApi, &num); 15823b3eb3cSopenharmony_ci if (num >= 0 && num <= static_cast<int32_t>(NG::ToastShowMode::SYSTEM_TOP_MOST)) { 15923b3eb3cSopenharmony_ci showMode = static_cast<NG::ToastShowMode>(num); 16023b3eb3cSopenharmony_ci } 16123b3eb3cSopenharmony_ci } 16223b3eb3cSopenharmony_ci return true; 16323b3eb3cSopenharmony_ci} 16423b3eb3cSopenharmony_ci 16523b3eb3cSopenharmony_cibool GetToastAlignment(napi_env env, napi_value alignmentApi, int32_t& alignment) 16623b3eb3cSopenharmony_ci{ 16723b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 16823b3eb3cSopenharmony_ci napi_typeof(env, alignmentApi, &valueType); 16923b3eb3cSopenharmony_ci if (valueType == napi_number) { 17023b3eb3cSopenharmony_ci napi_get_value_int32(env, alignmentApi, &alignment); 17123b3eb3cSopenharmony_ci } 17223b3eb3cSopenharmony_ci return true; 17323b3eb3cSopenharmony_ci} 17423b3eb3cSopenharmony_ci 17523b3eb3cSopenharmony_cibool GetToastOffset(napi_env env, napi_value offsetApi, std::optional<DimensionOffset>& offset) 17623b3eb3cSopenharmony_ci{ 17723b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 17823b3eb3cSopenharmony_ci napi_typeof(env, offsetApi, &valueType); 17923b3eb3cSopenharmony_ci if (valueType == napi_object) { 18023b3eb3cSopenharmony_ci napi_value dxApi = nullptr; 18123b3eb3cSopenharmony_ci napi_value dyApi = nullptr; 18223b3eb3cSopenharmony_ci napi_get_named_property(env, offsetApi, "dx", &dxApi); 18323b3eb3cSopenharmony_ci napi_get_named_property(env, offsetApi, "dy", &dyApi); 18423b3eb3cSopenharmony_ci CalcDimension dx; 18523b3eb3cSopenharmony_ci CalcDimension dy; 18623b3eb3cSopenharmony_ci ParseNapiDimension(env, dx, dxApi, DimensionUnit::VP); 18723b3eb3cSopenharmony_ci ParseNapiDimension(env, dy, dyApi, DimensionUnit::VP); 18823b3eb3cSopenharmony_ci offset = DimensionOffset { dx, dy }; 18923b3eb3cSopenharmony_ci } 19023b3eb3cSopenharmony_ci return true; 19123b3eb3cSopenharmony_ci} 19223b3eb3cSopenharmony_ci 19323b3eb3cSopenharmony_civoid GetToastBackgroundColor(napi_env env, napi_value backgroundColorNApi, std::optional<Color>& backgroundColor) 19423b3eb3cSopenharmony_ci{ 19523b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 19623b3eb3cSopenharmony_ci napi_typeof(env, backgroundColorNApi, &valueType); 19723b3eb3cSopenharmony_ci Color color; 19823b3eb3cSopenharmony_ci backgroundColor = std::nullopt; 19923b3eb3cSopenharmony_ci if (ParseNapiColor(env, backgroundColorNApi, color)) { 20023b3eb3cSopenharmony_ci backgroundColor = color; 20123b3eb3cSopenharmony_ci } 20223b3eb3cSopenharmony_ci} 20323b3eb3cSopenharmony_ci 20423b3eb3cSopenharmony_civoid GetToastTextColor(napi_env env, napi_value textColorNApi, std::optional<Color>& textColor) 20523b3eb3cSopenharmony_ci{ 20623b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 20723b3eb3cSopenharmony_ci napi_typeof(env, textColorNApi, &valueType); 20823b3eb3cSopenharmony_ci Color color; 20923b3eb3cSopenharmony_ci textColor = std::nullopt; 21023b3eb3cSopenharmony_ci if (ParseNapiColor(env, textColorNApi, color)) { 21123b3eb3cSopenharmony_ci textColor = color; 21223b3eb3cSopenharmony_ci } 21323b3eb3cSopenharmony_ci} 21423b3eb3cSopenharmony_ci 21523b3eb3cSopenharmony_civoid GetToastBackgroundBlurStyle(napi_env env, 21623b3eb3cSopenharmony_ci napi_value backgroundBlurStyleNApi, std::optional<int32_t>& backgroundBlurStyle) 21723b3eb3cSopenharmony_ci{ 21823b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 21923b3eb3cSopenharmony_ci napi_typeof(env, backgroundBlurStyleNApi, &valueType); 22023b3eb3cSopenharmony_ci if (valueType == napi_number) { 22123b3eb3cSopenharmony_ci int32_t num; 22223b3eb3cSopenharmony_ci napi_get_value_int32(env, backgroundBlurStyleNApi, &num); 22323b3eb3cSopenharmony_ci if (num >= 0 && num < BG_BLUR_STYLE_MAX_INDEX) { 22423b3eb3cSopenharmony_ci backgroundBlurStyle = num; 22523b3eb3cSopenharmony_ci } 22623b3eb3cSopenharmony_ci } 22723b3eb3cSopenharmony_ci} 22823b3eb3cSopenharmony_ci 22923b3eb3cSopenharmony_cibool GetShadowFromTheme(ShadowStyle shadowStyle, Shadow& shadow) 23023b3eb3cSopenharmony_ci{ 23123b3eb3cSopenharmony_ci auto colorMode = SystemProperties::GetColorMode(); 23223b3eb3cSopenharmony_ci if (shadowStyle == ShadowStyle::None) { 23323b3eb3cSopenharmony_ci return true; 23423b3eb3cSopenharmony_ci } 23523b3eb3cSopenharmony_ci auto container = Container::CurrentSafelyWithCheck(); 23623b3eb3cSopenharmony_ci CHECK_NULL_RETURN(container, false); 23723b3eb3cSopenharmony_ci auto pipelineContext = container->GetPipelineContext(); 23823b3eb3cSopenharmony_ci CHECK_NULL_RETURN(pipelineContext, false); 23923b3eb3cSopenharmony_ci auto shadowTheme = pipelineContext->GetTheme<ShadowTheme>(); 24023b3eb3cSopenharmony_ci if (!shadowTheme) { 24123b3eb3cSopenharmony_ci return false; 24223b3eb3cSopenharmony_ci } 24323b3eb3cSopenharmony_ci shadow = shadowTheme->GetShadow(shadowStyle, colorMode); 24423b3eb3cSopenharmony_ci return true; 24523b3eb3cSopenharmony_ci} 24623b3eb3cSopenharmony_ci 24723b3eb3cSopenharmony_cibool ParseResource(const ResourceInfo resource, CalcDimension& result) 24823b3eb3cSopenharmony_ci{ 24923b3eb3cSopenharmony_ci auto resourceWrapper = CreateResourceWrapper(resource); 25023b3eb3cSopenharmony_ci CHECK_NULL_RETURN(resourceWrapper, false); 25123b3eb3cSopenharmony_ci if (resource.type == static_cast<uint32_t>(ResourceType::STRING)) { 25223b3eb3cSopenharmony_ci auto value = resourceWrapper->GetString(resource.resId); 25323b3eb3cSopenharmony_ci return StringUtils::StringToCalcDimensionNG(value, result, false); 25423b3eb3cSopenharmony_ci } 25523b3eb3cSopenharmony_ci if (resource.type == static_cast<uint32_t>(ResourceType::INTEGER)) { 25623b3eb3cSopenharmony_ci auto value = std::to_string(resourceWrapper->GetInt(resource.resId)); 25723b3eb3cSopenharmony_ci StringUtils::StringToDimensionWithUnitNG(value, result); 25823b3eb3cSopenharmony_ci return true; 25923b3eb3cSopenharmony_ci } 26023b3eb3cSopenharmony_ci if (resource.type == static_cast<uint32_t>(ResourceType::FLOAT)) { 26123b3eb3cSopenharmony_ci result = resourceWrapper->GetDimension(resource.resId); 26223b3eb3cSopenharmony_ci return true; 26323b3eb3cSopenharmony_ci } 26423b3eb3cSopenharmony_ci return false; 26523b3eb3cSopenharmony_ci} 26623b3eb3cSopenharmony_ci 26723b3eb3cSopenharmony_civoid GetToastObjectShadow(napi_env env, napi_value shadowNApi, Shadow& shadowProps) 26823b3eb3cSopenharmony_ci{ 26923b3eb3cSopenharmony_ci napi_value radiusApi = nullptr; 27023b3eb3cSopenharmony_ci napi_value colorApi = nullptr; 27123b3eb3cSopenharmony_ci napi_value typeApi = nullptr; 27223b3eb3cSopenharmony_ci napi_value fillApi = nullptr; 27323b3eb3cSopenharmony_ci napi_get_named_property(env, shadowNApi, "radius", &radiusApi); 27423b3eb3cSopenharmony_ci napi_get_named_property(env, shadowNApi, "color", &colorApi); 27523b3eb3cSopenharmony_ci napi_get_named_property(env, shadowNApi, "type", &typeApi); 27623b3eb3cSopenharmony_ci napi_get_named_property(env, shadowNApi, "fill", &fillApi); 27723b3eb3cSopenharmony_ci ResourceInfo recv; 27823b3eb3cSopenharmony_ci double radiusValue = 0.0; 27923b3eb3cSopenharmony_ci if (ParseResourceParam(env, radiusApi, recv)) { 28023b3eb3cSopenharmony_ci CalcDimension radius; 28123b3eb3cSopenharmony_ci if (ParseResource(recv, radius)) { 28223b3eb3cSopenharmony_ci radiusValue = LessNotEqual(radius.Value(), 0.0) ? 0.0 : radius.Value(); 28323b3eb3cSopenharmony_ci } 28423b3eb3cSopenharmony_ci } else { 28523b3eb3cSopenharmony_ci napi_get_value_double(env, radiusApi, &radiusValue); 28623b3eb3cSopenharmony_ci if (LessNotEqual(radiusValue, 0.0)) { 28723b3eb3cSopenharmony_ci radiusValue = 0.0; 28823b3eb3cSopenharmony_ci } 28923b3eb3cSopenharmony_ci } 29023b3eb3cSopenharmony_ci shadowProps.SetBlurRadius(radiusValue); 29123b3eb3cSopenharmony_ci Color color; 29223b3eb3cSopenharmony_ci ShadowColorStrategy shadowColorStrategy; 29323b3eb3cSopenharmony_ci if (ParseShadowColorStrategy(env, colorApi, shadowColorStrategy)) { 29423b3eb3cSopenharmony_ci shadowProps.SetShadowColorStrategy(shadowColorStrategy); 29523b3eb3cSopenharmony_ci } else if (ParseNapiColor(env, colorApi, color)) { 29623b3eb3cSopenharmony_ci shadowProps.SetColor(color); 29723b3eb3cSopenharmony_ci } 29823b3eb3cSopenharmony_ci napi_valuetype valueType = GetValueType(env, typeApi); 29923b3eb3cSopenharmony_ci int32_t shadowType = static_cast<int32_t>(ShadowType::COLOR); 30023b3eb3cSopenharmony_ci if (valueType == napi_number) { 30123b3eb3cSopenharmony_ci napi_get_value_int32(env, typeApi, &shadowType); 30223b3eb3cSopenharmony_ci } 30323b3eb3cSopenharmony_ci if (shadowType != static_cast<int32_t>(ShadowType::BLUR)) { 30423b3eb3cSopenharmony_ci shadowType = static_cast<int32_t>(ShadowType::COLOR); 30523b3eb3cSopenharmony_ci } 30623b3eb3cSopenharmony_ci shadowType = 30723b3eb3cSopenharmony_ci std::clamp(shadowType, static_cast<int32_t>(ShadowType::COLOR), static_cast<int32_t>(ShadowType::BLUR)); 30823b3eb3cSopenharmony_ci shadowProps.SetShadowType(static_cast<ShadowType>(shadowType)); 30923b3eb3cSopenharmony_ci valueType = GetValueType(env, fillApi); 31023b3eb3cSopenharmony_ci bool isFilled = false; 31123b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 31223b3eb3cSopenharmony_ci napi_get_value_bool(env, fillApi, &isFilled); 31323b3eb3cSopenharmony_ci } 31423b3eb3cSopenharmony_ci shadowProps.SetIsFilled(isFilled); 31523b3eb3cSopenharmony_ci} 31623b3eb3cSopenharmony_ci 31723b3eb3cSopenharmony_civoid GetToastShadow(napi_env env, napi_value shadowNApi, std::optional<Shadow>& shadow, bool& isTypeStyleShadow) 31823b3eb3cSopenharmony_ci{ 31923b3eb3cSopenharmony_ci Shadow shadowProps; 32023b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 32123b3eb3cSopenharmony_ci napi_typeof(env, shadowNApi, &valueType); 32223b3eb3cSopenharmony_ci if (valueType == napi_number) { 32323b3eb3cSopenharmony_ci int32_t num = 0; 32423b3eb3cSopenharmony_ci napi_get_value_int32(env, shadowNApi, &num); 32523b3eb3cSopenharmony_ci auto style = static_cast<ShadowStyle>(num); 32623b3eb3cSopenharmony_ci GetShadowFromTheme(style, shadowProps); 32723b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 32823b3eb3cSopenharmony_ci napi_value offsetXApi = nullptr; 32923b3eb3cSopenharmony_ci napi_value offsetYApi = nullptr; 33023b3eb3cSopenharmony_ci napi_get_named_property(env, shadowNApi, "offsetX", &offsetXApi); 33123b3eb3cSopenharmony_ci napi_get_named_property(env, shadowNApi, "offsetY", &offsetYApi); 33223b3eb3cSopenharmony_ci ResourceInfo recv; 33323b3eb3cSopenharmony_ci bool isRtl = AceApplicationInfo::GetInstance().IsRightToLeft(); 33423b3eb3cSopenharmony_ci if (ParseResourceParam(env, offsetXApi, recv)) { 33523b3eb3cSopenharmony_ci CalcDimension offsetX; 33623b3eb3cSopenharmony_ci if (ParseResource(recv, offsetX)) { 33723b3eb3cSopenharmony_ci double xValue = isRtl ? offsetX.Value() * (-1) : offsetX.Value(); 33823b3eb3cSopenharmony_ci shadowProps.SetOffsetX(xValue); 33923b3eb3cSopenharmony_ci } 34023b3eb3cSopenharmony_ci } else { 34123b3eb3cSopenharmony_ci CalcDimension offsetX; 34223b3eb3cSopenharmony_ci if (ParseNapiDimension(env, offsetX, offsetXApi, DimensionUnit::VP)) { 34323b3eb3cSopenharmony_ci double xValue = isRtl ? offsetX.Value() * (-1) : offsetX.Value(); 34423b3eb3cSopenharmony_ci shadowProps.SetOffsetX(xValue); 34523b3eb3cSopenharmony_ci } 34623b3eb3cSopenharmony_ci } 34723b3eb3cSopenharmony_ci if (ParseResourceParam(env, offsetYApi, recv)) { 34823b3eb3cSopenharmony_ci CalcDimension offsetY; 34923b3eb3cSopenharmony_ci if (ParseResource(recv, offsetY)) { 35023b3eb3cSopenharmony_ci shadowProps.SetOffsetY(offsetY.Value()); 35123b3eb3cSopenharmony_ci } 35223b3eb3cSopenharmony_ci } else { 35323b3eb3cSopenharmony_ci CalcDimension offsetY; 35423b3eb3cSopenharmony_ci if (ParseNapiDimension(env, offsetY, offsetYApi, DimensionUnit::VP)) { 35523b3eb3cSopenharmony_ci shadowProps.SetOffsetY(offsetY.Value()); 35623b3eb3cSopenharmony_ci } 35723b3eb3cSopenharmony_ci } 35823b3eb3cSopenharmony_ci GetToastObjectShadow(env, shadowNApi, shadowProps); 35923b3eb3cSopenharmony_ci isTypeStyleShadow = false; 36023b3eb3cSopenharmony_ci } else { 36123b3eb3cSopenharmony_ci GetShadowFromTheme(ShadowStyle::OuterDefaultMD, shadowProps); 36223b3eb3cSopenharmony_ci } 36323b3eb3cSopenharmony_ci shadow = shadowProps; 36423b3eb3cSopenharmony_ci} 36523b3eb3cSopenharmony_ci 36623b3eb3cSopenharmony_civoid GetToastEnableHoverMode(napi_env env, napi_value enableHoverModeNApi, bool& enableHoverMode) 36723b3eb3cSopenharmony_ci{ 36823b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 36923b3eb3cSopenharmony_ci napi_typeof(env, enableHoverModeNApi, &valueType); 37023b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 37123b3eb3cSopenharmony_ci napi_get_value_bool(env, enableHoverModeNApi, &enableHoverMode); 37223b3eb3cSopenharmony_ci } 37323b3eb3cSopenharmony_ci} 37423b3eb3cSopenharmony_ci 37523b3eb3cSopenharmony_civoid GetToastHoverModeArea(napi_env env, napi_value hoverModeAreaNApi, HoverModeAreaType& hoverModeArea) 37623b3eb3cSopenharmony_ci{ 37723b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 37823b3eb3cSopenharmony_ci napi_typeof(env, hoverModeAreaNApi, &valueType); 37923b3eb3cSopenharmony_ci if (valueType == napi_number) { 38023b3eb3cSopenharmony_ci int32_t num = -1; 38123b3eb3cSopenharmony_ci napi_get_value_int32(env, hoverModeAreaNApi, &num); 38223b3eb3cSopenharmony_ci if (num >= 0 && num <= static_cast<int32_t>(HoverModeAreaType::BOTTOM_SCREEN)) { 38323b3eb3cSopenharmony_ci hoverModeArea = static_cast<HoverModeAreaType>(num); 38423b3eb3cSopenharmony_ci } 38523b3eb3cSopenharmony_ci } 38623b3eb3cSopenharmony_ci} 38723b3eb3cSopenharmony_ci 38823b3eb3cSopenharmony_civoid GetToastHoverModeParams(napi_env env, napi_value argv, NG::ToastInfo& toastInfo) 38923b3eb3cSopenharmony_ci{ 39023b3eb3cSopenharmony_ci napi_value enableHoverModeNApi = nullptr; 39123b3eb3cSopenharmony_ci napi_value hoverModeAreaNApi = nullptr; 39223b3eb3cSopenharmony_ci 39323b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "enableHoverMode", &enableHoverModeNApi); 39423b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "hoverModeArea", &hoverModeAreaNApi); 39523b3eb3cSopenharmony_ci 39623b3eb3cSopenharmony_ci GetToastEnableHoverMode(env, enableHoverModeNApi, toastInfo.enableHoverMode); 39723b3eb3cSopenharmony_ci GetToastHoverModeArea(env, hoverModeAreaNApi, toastInfo.hoverModeArea); 39823b3eb3cSopenharmony_ci} 39923b3eb3cSopenharmony_ci 40023b3eb3cSopenharmony_cibool GetToastParams(napi_env env, napi_value argv, NG::ToastInfo& toastInfo) 40123b3eb3cSopenharmony_ci{ 40223b3eb3cSopenharmony_ci napi_value messageNApi = nullptr; 40323b3eb3cSopenharmony_ci napi_value durationNApi = nullptr; 40423b3eb3cSopenharmony_ci napi_value bottomNApi = nullptr; 40523b3eb3cSopenharmony_ci napi_value showModeNApi = nullptr; 40623b3eb3cSopenharmony_ci napi_value alignmentApi = nullptr; 40723b3eb3cSopenharmony_ci napi_value offsetApi = nullptr; 40823b3eb3cSopenharmony_ci napi_value backgroundColorNApi = nullptr; 40923b3eb3cSopenharmony_ci napi_value textColorNApi = nullptr; 41023b3eb3cSopenharmony_ci napi_value backgroundBlurStyleNApi = nullptr; 41123b3eb3cSopenharmony_ci napi_value shadowNApi = nullptr; 41223b3eb3cSopenharmony_ci 41323b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 41423b3eb3cSopenharmony_ci napi_typeof(env, argv, &valueType); 41523b3eb3cSopenharmony_ci if (valueType == napi_object) { 41623b3eb3cSopenharmony_ci // message can not be null 41723b3eb3cSopenharmony_ci if (!HasProperty(env, argv, "message")) { 41823b3eb3cSopenharmony_ci NapiThrow(env, "Required input parameters are missing.", ERROR_CODE_PARAM_INVALID); 41923b3eb3cSopenharmony_ci return false; 42023b3eb3cSopenharmony_ci } 42123b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "message", &messageNApi); 42223b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "duration", &durationNApi); 42323b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "bottom", &bottomNApi); 42423b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "showMode", &showModeNApi); 42523b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "alignment", &alignmentApi); 42623b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "offset", &offsetApi); 42723b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "backgroundColor", &backgroundColorNApi); 42823b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "textColor", &textColorNApi); 42923b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "backgroundBlurStyle", &backgroundBlurStyleNApi); 43023b3eb3cSopenharmony_ci napi_get_named_property(env, argv, "shadow", &shadowNApi); 43123b3eb3cSopenharmony_ci } else { 43223b3eb3cSopenharmony_ci NapiThrow(env, "The type of parameters is incorrect.", ERROR_CODE_PARAM_INVALID); 43323b3eb3cSopenharmony_ci return false; 43423b3eb3cSopenharmony_ci } 43523b3eb3cSopenharmony_ci if (!GetToastMessage(env, messageNApi, toastInfo.message) || 43623b3eb3cSopenharmony_ci !GetToastDuration(env, durationNApi, toastInfo.duration) || 43723b3eb3cSopenharmony_ci !GetToastBottom(env, bottomNApi, toastInfo.bottom) || 43823b3eb3cSopenharmony_ci !GetToastShowMode(env, showModeNApi, toastInfo.showMode) || 43923b3eb3cSopenharmony_ci !GetToastAlignment(env, alignmentApi, toastInfo.alignment) || 44023b3eb3cSopenharmony_ci !GetToastOffset(env, offsetApi, toastInfo.offset)) { 44123b3eb3cSopenharmony_ci return false; 44223b3eb3cSopenharmony_ci } 44323b3eb3cSopenharmony_ci GetToastHoverModeParams(env, argv, toastInfo); 44423b3eb3cSopenharmony_ci GetToastBackgroundColor(env, backgroundColorNApi, toastInfo.backgroundColor); 44523b3eb3cSopenharmony_ci GetToastTextColor(env, textColorNApi, toastInfo.textColor); 44623b3eb3cSopenharmony_ci GetToastBackgroundBlurStyle(env, backgroundBlurStyleNApi, toastInfo.backgroundBlurStyle); 44723b3eb3cSopenharmony_ci GetToastShadow(env, shadowNApi, toastInfo.shadow, toastInfo.isTypeStyleShadow); 44823b3eb3cSopenharmony_ci return true; 44923b3eb3cSopenharmony_ci} 45023b3eb3cSopenharmony_ci 45123b3eb3cSopenharmony_cibool ShowToast(napi_env env, NG::ToastInfo& toastInfo, std::function<void(int32_t)>& toastCallback) 45223b3eb3cSopenharmony_ci{ 45323b3eb3cSopenharmony_ci#ifdef OHOS_STANDARD_SYSTEM 45423b3eb3cSopenharmony_ci if ((SystemProperties::GetExtSurfaceEnabled() || !ContainerIsService()) && !ContainerIsScenceBoard() && 45523b3eb3cSopenharmony_ci toastInfo.showMode == NG::ToastShowMode::DEFAULT) { 45623b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 45723b3eb3cSopenharmony_ci if (!delegate) { 45823b3eb3cSopenharmony_ci NapiThrow(env, "Can not get delegate.", ERROR_CODE_INTERNAL_ERROR); 45923b3eb3cSopenharmony_ci return false; 46023b3eb3cSopenharmony_ci } 46123b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_DIALOG, "before delegate show toast"); 46223b3eb3cSopenharmony_ci delegate->ShowToast(toastInfo, std::move(toastCallback)); 46323b3eb3cSopenharmony_ci } else if (SubwindowManager::GetInstance() != nullptr) { 46423b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_DIALOG, "before subwindow manager show toast"); 46523b3eb3cSopenharmony_ci SubwindowManager::GetInstance()->ShowToast(toastInfo, std::move(toastCallback)); 46623b3eb3cSopenharmony_ci } 46723b3eb3cSopenharmony_ci#else 46823b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 46923b3eb3cSopenharmony_ci if (!delegate) { 47023b3eb3cSopenharmony_ci NapiThrow(env, "UI execution context not found.", ERROR_CODE_INTERNAL_ERROR); 47123b3eb3cSopenharmony_ci return false; 47223b3eb3cSopenharmony_ci } 47323b3eb3cSopenharmony_ci if (toastInfo.showMode == NG::ToastShowMode::DEFAULT) { 47423b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_DIALOG, "before delegate show toast"); 47523b3eb3cSopenharmony_ci delegate->ShowToast(toastInfo, std::move(toastCallback)); 47623b3eb3cSopenharmony_ci } else if (SubwindowManager::GetInstance() != nullptr) { 47723b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_DIALOG, "before subwindow manager show toast"); 47823b3eb3cSopenharmony_ci SubwindowManager::GetInstance()->ShowToast(toastInfo, std::move(toastCallback)); 47923b3eb3cSopenharmony_ci } 48023b3eb3cSopenharmony_ci#endif 48123b3eb3cSopenharmony_ci return true; 48223b3eb3cSopenharmony_ci} 48323b3eb3cSopenharmony_ci 48423b3eb3cSopenharmony_cinapi_value JSPromptShowToast(napi_env env, napi_callback_info info) 48523b3eb3cSopenharmony_ci{ 48623b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_DIALOG, "show toast enter"); 48723b3eb3cSopenharmony_ci size_t requireArgc = 1; 48823b3eb3cSopenharmony_ci size_t argc = 1; 48923b3eb3cSopenharmony_ci napi_value argv = nullptr; 49023b3eb3cSopenharmony_ci napi_value thisVar = nullptr; 49123b3eb3cSopenharmony_ci void* data = nullptr; 49223b3eb3cSopenharmony_ci napi_get_cb_info(env, info, &argc, &argv, &thisVar, &data); 49323b3eb3cSopenharmony_ci if (argc != requireArgc) { 49423b3eb3cSopenharmony_ci NapiThrow(env, "The number of parameters must be equal to 1.", ERROR_CODE_PARAM_INVALID); 49523b3eb3cSopenharmony_ci return nullptr; 49623b3eb3cSopenharmony_ci } 49723b3eb3cSopenharmony_ci auto toastInfo = NG::ToastInfo { .duration = -1, .showMode = NG::ToastShowMode::DEFAULT, .alignment = -1 }; 49823b3eb3cSopenharmony_ci if (!GetToastParams(env, argv, toastInfo)) { 49923b3eb3cSopenharmony_ci return nullptr; 50023b3eb3cSopenharmony_ci } 50123b3eb3cSopenharmony_ci std::function<void(int32_t)> toastCallback = nullptr; 50223b3eb3cSopenharmony_ci ShowToast(env, toastInfo, toastCallback); 50323b3eb3cSopenharmony_ci return nullptr; 50423b3eb3cSopenharmony_ci} 50523b3eb3cSopenharmony_ci 50623b3eb3cSopenharmony_cinapi_value JSPromptOpenToast(napi_env env, napi_callback_info info) 50723b3eb3cSopenharmony_ci{ 50823b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_DIALOG, "open toast enter"); 50923b3eb3cSopenharmony_ci size_t requireArgc = 1; 51023b3eb3cSopenharmony_ci size_t argc = 1; 51123b3eb3cSopenharmony_ci napi_value argv = nullptr; 51223b3eb3cSopenharmony_ci napi_value thisVar = nullptr; 51323b3eb3cSopenharmony_ci void* data = nullptr; 51423b3eb3cSopenharmony_ci napi_get_cb_info(env, info, &argc, &argv, &thisVar, &data); 51523b3eb3cSopenharmony_ci if (argc != requireArgc) { 51623b3eb3cSopenharmony_ci NapiThrow(env, "The number of parameters must be equal to 1.", ERROR_CODE_PARAM_INVALID); 51723b3eb3cSopenharmony_ci return nullptr; 51823b3eb3cSopenharmony_ci } 51923b3eb3cSopenharmony_ci auto toastInfo = NG::ToastInfo { .duration = -1, .showMode = NG::ToastShowMode::DEFAULT, .alignment = -1 }; 52023b3eb3cSopenharmony_ci if (!GetToastParams(env, argv, toastInfo)) { 52123b3eb3cSopenharmony_ci return nullptr; 52223b3eb3cSopenharmony_ci } 52323b3eb3cSopenharmony_ci napi_deferred deferred; 52423b3eb3cSopenharmony_ci napi_value result; 52523b3eb3cSopenharmony_ci napi_create_promise(env, &deferred, &result); 52623b3eb3cSopenharmony_ci std::function<void(int32_t)> toastCallback = nullptr; 52723b3eb3cSopenharmony_ci toastCallback = [env, deferred](int32_t toastId) mutable { 52823b3eb3cSopenharmony_ci napi_value napiToastId = nullptr; 52923b3eb3cSopenharmony_ci napi_create_int32(env, toastId, &napiToastId); 53023b3eb3cSopenharmony_ci napi_resolve_deferred(env, deferred, napiToastId); 53123b3eb3cSopenharmony_ci }; 53223b3eb3cSopenharmony_ci if (ShowToast(env, toastInfo, toastCallback)) { 53323b3eb3cSopenharmony_ci return result; 53423b3eb3cSopenharmony_ci } 53523b3eb3cSopenharmony_ci return nullptr; 53623b3eb3cSopenharmony_ci} 53723b3eb3cSopenharmony_ci 53823b3eb3cSopenharmony_civoid CloseToast(napi_env env, int32_t toastId, NG::ToastShowMode showMode) 53923b3eb3cSopenharmony_ci{ 54023b3eb3cSopenharmony_ci std::function<void(int32_t)> toastCloseCallback = nullptr; 54123b3eb3cSopenharmony_ci toastCloseCallback = [env](int32_t errorCode) mutable { 54223b3eb3cSopenharmony_ci if (errorCode != ERROR_CODE_NO_ERROR) { 54323b3eb3cSopenharmony_ci NapiThrow(env, "", errorCode); 54423b3eb3cSopenharmony_ci } 54523b3eb3cSopenharmony_ci }; 54623b3eb3cSopenharmony_ci#ifdef OHOS_STANDARD_SYSTEM 54723b3eb3cSopenharmony_ci if ((SystemProperties::GetExtSurfaceEnabled() || !ContainerIsService()) && !ContainerIsScenceBoard() && 54823b3eb3cSopenharmony_ci showMode == NG::ToastShowMode::DEFAULT) { 54923b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 55023b3eb3cSopenharmony_ci if (delegate) { 55123b3eb3cSopenharmony_ci delegate->CloseToast(toastId, std::move(toastCloseCallback)); 55223b3eb3cSopenharmony_ci } else { 55323b3eb3cSopenharmony_ci NapiThrow(env, "Can not get delegate.", ERROR_CODE_INTERNAL_ERROR); 55423b3eb3cSopenharmony_ci } 55523b3eb3cSopenharmony_ci } else if (SubwindowManager::GetInstance() != nullptr) { 55623b3eb3cSopenharmony_ci SubwindowManager::GetInstance()->CloseToast(toastId, showMode, std::move(toastCloseCallback)); 55723b3eb3cSopenharmony_ci } 55823b3eb3cSopenharmony_ci#else 55923b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 56023b3eb3cSopenharmony_ci if (!delegate) { 56123b3eb3cSopenharmony_ci NapiThrow(env, "UI execution context not found.", ERROR_CODE_INTERNAL_ERROR); 56223b3eb3cSopenharmony_ci } 56323b3eb3cSopenharmony_ci if (showMode == NG::ToastShowMode::DEFAULT) { 56423b3eb3cSopenharmony_ci delegate->CloseToast(toastId, std::move(toastCloseCallback)); 56523b3eb3cSopenharmony_ci } else if (SubwindowManager::GetInstance() != nullptr) { 56623b3eb3cSopenharmony_ci SubwindowManager::GetInstance()->CloseToast(toastId, showMode, std::move(toastCloseCallback)); 56723b3eb3cSopenharmony_ci } 56823b3eb3cSopenharmony_ci#endif 56923b3eb3cSopenharmony_ci} 57023b3eb3cSopenharmony_ci 57123b3eb3cSopenharmony_cinapi_value JSPromptCloseToast(napi_env env, napi_callback_info info) 57223b3eb3cSopenharmony_ci{ 57323b3eb3cSopenharmony_ci size_t argc = 1; 57423b3eb3cSopenharmony_ci napi_value args[1]; 57523b3eb3cSopenharmony_ci napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); 57623b3eb3cSopenharmony_ci if (argc != 1) { 57723b3eb3cSopenharmony_ci NapiThrow(env, "The number of parameters is incorrect.", ERROR_CODE_PARAM_INVALID); 57823b3eb3cSopenharmony_ci return nullptr; 57923b3eb3cSopenharmony_ci } 58023b3eb3cSopenharmony_ci int32_t id = -1; 58123b3eb3cSopenharmony_ci napi_get_value_int32(env, args[0], &id); 58223b3eb3cSopenharmony_ci if (id < 0 || id > INT32_MAX) { 58323b3eb3cSopenharmony_ci NapiThrow(env, "The toastId is invalid.", ERROR_CODE_PARAM_INVALID); 58423b3eb3cSopenharmony_ci return nullptr; 58523b3eb3cSopenharmony_ci } 58623b3eb3cSopenharmony_ci int32_t showModeVal = static_cast<int32_t>(static_cast<uint32_t>(id) & 0b111); 58723b3eb3cSopenharmony_ci int32_t toastId = 58823b3eb3cSopenharmony_ci static_cast<int32_t>(static_cast<uint32_t>(id) >> 58923b3eb3cSopenharmony_ci 3); // 3 : Move 3 bits to the right to get toastId, and the last 3 bits are the showMode 59023b3eb3cSopenharmony_ci if (toastId < 0 || showModeVal < 0 || showModeVal > static_cast<int32_t>(NG::ToastShowMode::SYSTEM_TOP_MOST)) { 59123b3eb3cSopenharmony_ci NapiThrow(env, "", ERROR_CODE_TOAST_NOT_FOUND); 59223b3eb3cSopenharmony_ci return nullptr; 59323b3eb3cSopenharmony_ci } 59423b3eb3cSopenharmony_ci auto showMode = static_cast<NG::ToastShowMode>(showModeVal); 59523b3eb3cSopenharmony_ci CloseToast(env, toastId, showMode); 59623b3eb3cSopenharmony_ci return nullptr; 59723b3eb3cSopenharmony_ci} 59823b3eb3cSopenharmony_ci 59923b3eb3cSopenharmony_cistruct PromptAsyncContext { 60023b3eb3cSopenharmony_ci napi_env env = nullptr; 60123b3eb3cSopenharmony_ci napi_value titleNApi = nullptr; 60223b3eb3cSopenharmony_ci napi_value messageNApi = nullptr; 60323b3eb3cSopenharmony_ci napi_value buttonsNApi = nullptr; 60423b3eb3cSopenharmony_ci napi_value autoCancel = nullptr; 60523b3eb3cSopenharmony_ci napi_value showInSubWindow = nullptr; 60623b3eb3cSopenharmony_ci napi_value isModal = nullptr; 60723b3eb3cSopenharmony_ci napi_value alignmentApi = nullptr; 60823b3eb3cSopenharmony_ci napi_value offsetApi = nullptr; 60923b3eb3cSopenharmony_ci napi_value maskRectApi = nullptr; 61023b3eb3cSopenharmony_ci napi_value builder = nullptr; 61123b3eb3cSopenharmony_ci napi_value onWillDismiss = nullptr; 61223b3eb3cSopenharmony_ci napi_value backgroundColorApi = nullptr; 61323b3eb3cSopenharmony_ci napi_value backgroundBlurStyleApi = nullptr; 61423b3eb3cSopenharmony_ci napi_value enableHoverMode = nullptr; 61523b3eb3cSopenharmony_ci napi_value hoverModeAreaApi = nullptr; 61623b3eb3cSopenharmony_ci napi_value borderWidthApi = nullptr; 61723b3eb3cSopenharmony_ci napi_value borderColorApi = nullptr; 61823b3eb3cSopenharmony_ci napi_value borderStyleApi = nullptr; 61923b3eb3cSopenharmony_ci napi_value borderRadiusApi = nullptr; 62023b3eb3cSopenharmony_ci napi_value shadowApi = nullptr; 62123b3eb3cSopenharmony_ci napi_value widthApi = nullptr; 62223b3eb3cSopenharmony_ci napi_value heightApi = nullptr; 62323b3eb3cSopenharmony_ci napi_value frameNodePtr = nullptr; 62423b3eb3cSopenharmony_ci napi_value maskColorApi = nullptr; 62523b3eb3cSopenharmony_ci napi_value onDidAppear = nullptr; 62623b3eb3cSopenharmony_ci napi_value onDidDisappear = nullptr; 62723b3eb3cSopenharmony_ci napi_value onWillAppear = nullptr; 62823b3eb3cSopenharmony_ci napi_value onWillDisappear = nullptr; 62923b3eb3cSopenharmony_ci napi_value transitionApi = nullptr; 63023b3eb3cSopenharmony_ci napi_ref callbackSuccess = nullptr; 63123b3eb3cSopenharmony_ci napi_ref callbackCancel = nullptr; 63223b3eb3cSopenharmony_ci napi_ref callbackComplete = nullptr; 63323b3eb3cSopenharmony_ci std::string titleString; 63423b3eb3cSopenharmony_ci std::string messageString; 63523b3eb3cSopenharmony_ci std::vector<ButtonInfo> buttons; 63623b3eb3cSopenharmony_ci bool autoCancelBool = true; 63723b3eb3cSopenharmony_ci bool enableHoverModeBool = false; 63823b3eb3cSopenharmony_ci bool showInSubWindowBool = false; 63923b3eb3cSopenharmony_ci bool isModalBool = true; 64023b3eb3cSopenharmony_ci std::set<std::string> callbacks; 64123b3eb3cSopenharmony_ci std::string callbackSuccessString; 64223b3eb3cSopenharmony_ci std::string callbackCancelString; 64323b3eb3cSopenharmony_ci std::string callbackCompleteString; 64423b3eb3cSopenharmony_ci napi_deferred deferred = nullptr; 64523b3eb3cSopenharmony_ci napi_ref callbackRef = nullptr; 64623b3eb3cSopenharmony_ci napi_ref builderRef = nullptr; 64723b3eb3cSopenharmony_ci napi_ref onWillDismissRef = nullptr; 64823b3eb3cSopenharmony_ci int32_t callbackType = -1; 64923b3eb3cSopenharmony_ci int32_t successType = -1; 65023b3eb3cSopenharmony_ci bool valid = true; 65123b3eb3cSopenharmony_ci int32_t instanceId = -1; 65223b3eb3cSopenharmony_ci void* nativePtr = nullptr; 65323b3eb3cSopenharmony_ci napi_ref onDidAppearRef = nullptr; 65423b3eb3cSopenharmony_ci napi_ref onDidDisappearRef = nullptr; 65523b3eb3cSopenharmony_ci napi_ref onWillAppearRef = nullptr; 65623b3eb3cSopenharmony_ci napi_ref onWillDisappearRef = nullptr; 65723b3eb3cSopenharmony_ci napi_value keyboardAvoidModeApi = nullptr; 65823b3eb3cSopenharmony_ci}; 65923b3eb3cSopenharmony_ci 66023b3eb3cSopenharmony_civoid DeleteContextAndThrowError( 66123b3eb3cSopenharmony_ci napi_env env, std::shared_ptr<PromptAsyncContext>& context, const std::string& errorMessage) 66223b3eb3cSopenharmony_ci{ 66323b3eb3cSopenharmony_ci if (!context) { 66423b3eb3cSopenharmony_ci // context is null, no need to delete 66523b3eb3cSopenharmony_ci return; 66623b3eb3cSopenharmony_ci } 66723b3eb3cSopenharmony_ci NapiThrow(env, errorMessage, ERROR_CODE_PARAM_INVALID); 66823b3eb3cSopenharmony_ci} 66923b3eb3cSopenharmony_ci 67023b3eb3cSopenharmony_ciint32_t GetButtonArraryLen(napi_env env, std::shared_ptr<PromptAsyncContext>& context, 67123b3eb3cSopenharmony_ci int32_t maxButtonNum) 67223b3eb3cSopenharmony_ci{ 67323b3eb3cSopenharmony_ci uint32_t buttonsLen = 0; 67423b3eb3cSopenharmony_ci napi_get_array_length(env, context->buttonsNApi, &buttonsLen); 67523b3eb3cSopenharmony_ci int32_t buttonsLenInt = static_cast<int32_t>(buttonsLen); 67623b3eb3cSopenharmony_ci if (buttonsLenInt > maxButtonNum && maxButtonNum != -1) { 67723b3eb3cSopenharmony_ci buttonsLenInt = maxButtonNum; 67823b3eb3cSopenharmony_ci } 67923b3eb3cSopenharmony_ci return buttonsLenInt; 68023b3eb3cSopenharmony_ci} 68123b3eb3cSopenharmony_ci 68223b3eb3cSopenharmony_civoid GetPrimaryButtonNum(napi_env env, std::shared_ptr<PromptAsyncContext>& context, 68323b3eb3cSopenharmony_ci int32_t buttonsLenInt, int32_t& primaryButtonNum) 68423b3eb3cSopenharmony_ci{ 68523b3eb3cSopenharmony_ci napi_value buttonArray = nullptr; 68623b3eb3cSopenharmony_ci napi_value primaryButtonNApi = nullptr; 68723b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 68823b3eb3cSopenharmony_ci for (int32_t index = 0; index < buttonsLenInt; index++) { 68923b3eb3cSopenharmony_ci napi_get_element(env, context->buttonsNApi, index, &buttonArray); 69023b3eb3cSopenharmony_ci bool isPrimaryButtonSet = false; 69123b3eb3cSopenharmony_ci napi_get_named_property(env, buttonArray, "primary", &primaryButtonNApi); 69223b3eb3cSopenharmony_ci napi_typeof(env, primaryButtonNApi, &valueType); 69323b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 69423b3eb3cSopenharmony_ci napi_get_value_bool(env, primaryButtonNApi, &isPrimaryButtonSet); 69523b3eb3cSopenharmony_ci } 69623b3eb3cSopenharmony_ci if (isPrimaryButtonSet) { 69723b3eb3cSopenharmony_ci primaryButtonNum++; 69823b3eb3cSopenharmony_ci } 69923b3eb3cSopenharmony_ci } 70023b3eb3cSopenharmony_ci} 70123b3eb3cSopenharmony_ci 70223b3eb3cSopenharmony_cibool ParseButtons(napi_env env, std::shared_ptr<PromptAsyncContext>& context, 70323b3eb3cSopenharmony_ci int32_t maxButtonNum, int32_t& primaryButtonNum) 70423b3eb3cSopenharmony_ci{ 70523b3eb3cSopenharmony_ci napi_value buttonArray = nullptr; 70623b3eb3cSopenharmony_ci napi_value textNApi = nullptr; 70723b3eb3cSopenharmony_ci napi_value colorNApi = nullptr; 70823b3eb3cSopenharmony_ci napi_value primaryButtonNApi = nullptr; 70923b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 71023b3eb3cSopenharmony_ci int32_t buttonsLenInt = GetButtonArraryLen(env, context, maxButtonNum); 71123b3eb3cSopenharmony_ci GetPrimaryButtonNum(env, context, buttonsLenInt, primaryButtonNum); 71223b3eb3cSopenharmony_ci for (int32_t index = 0; index < buttonsLenInt; index++) { 71323b3eb3cSopenharmony_ci napi_get_element(env, context->buttonsNApi, index, &buttonArray); 71423b3eb3cSopenharmony_ci if (!HasProperty(env, buttonArray, "text")) { 71523b3eb3cSopenharmony_ci DeleteContextAndThrowError(env, context, "Required input parameters are missing."); 71623b3eb3cSopenharmony_ci return false; 71723b3eb3cSopenharmony_ci } 71823b3eb3cSopenharmony_ci std::string textString; 71923b3eb3cSopenharmony_ci napi_get_named_property(env, buttonArray, "text", &textNApi); 72023b3eb3cSopenharmony_ci if (!GetNapiString(env, textNApi, textString, valueType)) { 72123b3eb3cSopenharmony_ci DeleteContextAndThrowError(env, context, "The type of parameters is incorrect."); 72223b3eb3cSopenharmony_ci return false; 72323b3eb3cSopenharmony_ci } 72423b3eb3cSopenharmony_ci if (!HasProperty(env, buttonArray, "color")) { 72523b3eb3cSopenharmony_ci DeleteContextAndThrowError(env, context, "Required input parameters are missing."); 72623b3eb3cSopenharmony_ci return false; 72723b3eb3cSopenharmony_ci } 72823b3eb3cSopenharmony_ci std::string colorString; 72923b3eb3cSopenharmony_ci napi_get_named_property(env, buttonArray, "color", &colorNApi); 73023b3eb3cSopenharmony_ci if (!GetNapiString(env, colorNApi, colorString, valueType)) { 73123b3eb3cSopenharmony_ci if (valueType == napi_undefined) { 73223b3eb3cSopenharmony_ci colorString = DEFAULT_FONT_COLOR_STRING_VALUE; 73323b3eb3cSopenharmony_ci } else { 73423b3eb3cSopenharmony_ci DeleteContextAndThrowError(env, context, "The type of parameters is incorrect."); 73523b3eb3cSopenharmony_ci return false; 73623b3eb3cSopenharmony_ci } 73723b3eb3cSopenharmony_ci } 73823b3eb3cSopenharmony_ci ButtonInfo buttonInfo = { .text = textString, .textColor = colorString }; 73923b3eb3cSopenharmony_ci if (primaryButtonNum <= PROMPTACTION_VALID_PRIMARY_BUTTON_NUM) { 74023b3eb3cSopenharmony_ci napi_get_named_property(env, buttonArray, "primary", &primaryButtonNApi); 74123b3eb3cSopenharmony_ci napi_typeof(env, primaryButtonNApi, &valueType); 74223b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 74323b3eb3cSopenharmony_ci napi_get_value_bool(env, primaryButtonNApi, &buttonInfo.isPrimary); 74423b3eb3cSopenharmony_ci } 74523b3eb3cSopenharmony_ci } 74623b3eb3cSopenharmony_ci context->buttons.emplace_back(buttonInfo); 74723b3eb3cSopenharmony_ci } 74823b3eb3cSopenharmony_ci return true; 74923b3eb3cSopenharmony_ci} 75023b3eb3cSopenharmony_ci 75123b3eb3cSopenharmony_cibool ParseButtonsPara(napi_env env, std::shared_ptr<PromptAsyncContext>& context, 75223b3eb3cSopenharmony_ci int32_t maxButtonNum, bool isShowActionMenu) 75323b3eb3cSopenharmony_ci{ 75423b3eb3cSopenharmony_ci bool isBool = false; 75523b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 75623b3eb3cSopenharmony_ci int32_t primaryButtonNum = 0; 75723b3eb3cSopenharmony_ci napi_is_array(env, context->buttonsNApi, &isBool); 75823b3eb3cSopenharmony_ci napi_typeof(env, context->buttonsNApi, &valueType); 75923b3eb3cSopenharmony_ci if (valueType == napi_object && isBool) { 76023b3eb3cSopenharmony_ci if (!ParseButtons(env, context, SHOW_DIALOG_BUTTON_NUM_MAX, primaryButtonNum)) { 76123b3eb3cSopenharmony_ci return false; 76223b3eb3cSopenharmony_ci } 76323b3eb3cSopenharmony_ci } else if (isShowActionMenu) { 76423b3eb3cSopenharmony_ci DeleteContextAndThrowError(env, context, "The type of the button parameters is incorrect."); 76523b3eb3cSopenharmony_ci return false; 76623b3eb3cSopenharmony_ci } 76723b3eb3cSopenharmony_ci if (isShowActionMenu) { 76823b3eb3cSopenharmony_ci ButtonInfo buttonInfo = { .text = Localization::GetInstance()->GetEntryLetters("common.cancel"), 76923b3eb3cSopenharmony_ci .textColor = "", .isPrimary = primaryButtonNum == 0 ? true : false}; 77023b3eb3cSopenharmony_ci context->buttons.emplace_back(buttonInfo); 77123b3eb3cSopenharmony_ci } 77223b3eb3cSopenharmony_ci return true; 77323b3eb3cSopenharmony_ci} 77423b3eb3cSopenharmony_ci 77523b3eb3cSopenharmony_civoid GetNapiDialogProps(napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext, 77623b3eb3cSopenharmony_ci std::optional<DialogAlignment>& alignment, std::optional<DimensionOffset>& offset, 77723b3eb3cSopenharmony_ci std::optional<DimensionRect>& maskRect) 77823b3eb3cSopenharmony_ci{ 77923b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_DIALOG, "get napi dialog props enter"); 78023b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 78123b3eb3cSopenharmony_ci // parse alignment 78223b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->alignmentApi, &valueType); 78323b3eb3cSopenharmony_ci if (valueType == napi_number) { 78423b3eb3cSopenharmony_ci int32_t num; 78523b3eb3cSopenharmony_ci napi_get_value_int32(env, asyncContext->alignmentApi, &num); 78623b3eb3cSopenharmony_ci if (num >= 0 && num < static_cast<int32_t>(DIALOG_ALIGNMENT.size())) { 78723b3eb3cSopenharmony_ci alignment = DIALOG_ALIGNMENT[num]; 78823b3eb3cSopenharmony_ci } 78923b3eb3cSopenharmony_ci } 79023b3eb3cSopenharmony_ci 79123b3eb3cSopenharmony_ci // parse offset 79223b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->offsetApi, &valueType); 79323b3eb3cSopenharmony_ci if (valueType == napi_object) { 79423b3eb3cSopenharmony_ci napi_value dxApi = nullptr; 79523b3eb3cSopenharmony_ci napi_value dyApi = nullptr; 79623b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->offsetApi, "dx", &dxApi); 79723b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->offsetApi, "dy", &dyApi); 79823b3eb3cSopenharmony_ci CalcDimension dx; 79923b3eb3cSopenharmony_ci CalcDimension dy; 80023b3eb3cSopenharmony_ci ParseNapiDimension(env, dx, dxApi, DimensionUnit::VP); 80123b3eb3cSopenharmony_ci ParseNapiDimension(env, dy, dyApi, DimensionUnit::VP); 80223b3eb3cSopenharmony_ci offset = DimensionOffset { dx, dy }; 80323b3eb3cSopenharmony_ci } 80423b3eb3cSopenharmony_ci 80523b3eb3cSopenharmony_ci // parse maskRect 80623b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->maskRectApi, &valueType); 80723b3eb3cSopenharmony_ci if (valueType == napi_object) { 80823b3eb3cSopenharmony_ci napi_value xApi = nullptr; 80923b3eb3cSopenharmony_ci napi_value yApi = nullptr; 81023b3eb3cSopenharmony_ci napi_value widthApi = nullptr; 81123b3eb3cSopenharmony_ci napi_value heightApi = nullptr; 81223b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->maskRectApi, "x", &xApi); 81323b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->maskRectApi, "y", &yApi); 81423b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->maskRectApi, "width", &widthApi); 81523b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->maskRectApi, "height", &heightApi); 81623b3eb3cSopenharmony_ci CalcDimension x; 81723b3eb3cSopenharmony_ci CalcDimension y; 81823b3eb3cSopenharmony_ci CalcDimension width; 81923b3eb3cSopenharmony_ci CalcDimension height; 82023b3eb3cSopenharmony_ci ParseNapiDimension(env, x, xApi, DimensionUnit::VP); 82123b3eb3cSopenharmony_ci ParseNapiDimension(env, y, yApi, DimensionUnit::VP); 82223b3eb3cSopenharmony_ci ParseNapiDimension(env, width, widthApi, DimensionUnit::VP); 82323b3eb3cSopenharmony_ci ParseNapiDimension(env, height, heightApi, DimensionUnit::VP); 82423b3eb3cSopenharmony_ci DimensionOffset dimensionOffset = { x, y }; 82523b3eb3cSopenharmony_ci maskRect = DimensionRect { width, height, dimensionOffset }; 82623b3eb3cSopenharmony_ci } 82723b3eb3cSopenharmony_ci} 82823b3eb3cSopenharmony_ci 82923b3eb3cSopenharmony_civoid GetNapiBlurStyleAndHoverModeProps(napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext, 83023b3eb3cSopenharmony_ci std::optional<int32_t>& backgroundBlurStyle, std::optional<HoverModeAreaType>& hoverModeArea) 83123b3eb3cSopenharmony_ci{ 83223b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_DIALOG, "get napi dialog backgroundBlurStyle and hoverModeArea props enter"); 83323b3eb3cSopenharmony_ci napi_valuetype blurStyleValueType = napi_undefined; 83423b3eb3cSopenharmony_ci 83523b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->backgroundBlurStyleApi, &blurStyleValueType); 83623b3eb3cSopenharmony_ci if (blurStyleValueType == napi_number) { 83723b3eb3cSopenharmony_ci int32_t num = 0; 83823b3eb3cSopenharmony_ci napi_get_value_int32(env, asyncContext->backgroundBlurStyleApi, &num); 83923b3eb3cSopenharmony_ci if (num >= 0 && num < BG_BLUR_STYLE_MAX_INDEX) { 84023b3eb3cSopenharmony_ci backgroundBlurStyle = num; 84123b3eb3cSopenharmony_ci } 84223b3eb3cSopenharmony_ci } 84323b3eb3cSopenharmony_ci 84423b3eb3cSopenharmony_ci napi_valuetype hoverModeValueType = napi_undefined; 84523b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->hoverModeAreaApi, &hoverModeValueType); 84623b3eb3cSopenharmony_ci if (hoverModeValueType == napi_number) { 84723b3eb3cSopenharmony_ci int32_t num = 0; 84823b3eb3cSopenharmony_ci napi_get_value_int32(env, asyncContext->hoverModeAreaApi, &num); 84923b3eb3cSopenharmony_ci if (num >= 0 && num < static_cast<int32_t>(HOVER_MODE_AREA_TYPE.size())) { 85023b3eb3cSopenharmony_ci hoverModeArea = HOVER_MODE_AREA_TYPE[num]; 85123b3eb3cSopenharmony_ci } 85223b3eb3cSopenharmony_ci } 85323b3eb3cSopenharmony_ci} 85423b3eb3cSopenharmony_ci 85523b3eb3cSopenharmony_civoid CheckNapiDimension(CalcDimension value) 85623b3eb3cSopenharmony_ci{ 85723b3eb3cSopenharmony_ci if (value.IsNegative()) { 85823b3eb3cSopenharmony_ci value.Reset(); 85923b3eb3cSopenharmony_ci } 86023b3eb3cSopenharmony_ci} 86123b3eb3cSopenharmony_ci 86223b3eb3cSopenharmony_cistd::optional<NG::BorderColorProperty> GetBorderColorProps( 86323b3eb3cSopenharmony_ci napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext) 86423b3eb3cSopenharmony_ci{ 86523b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 86623b3eb3cSopenharmony_ci NG::BorderColorProperty colorProperty; 86723b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->borderColorApi, &valueType); 86823b3eb3cSopenharmony_ci if (valueType != napi_number && valueType != napi_string && valueType != napi_object) { 86923b3eb3cSopenharmony_ci return std::nullopt; 87023b3eb3cSopenharmony_ci } 87123b3eb3cSopenharmony_ci Color borderColor; 87223b3eb3cSopenharmony_ci if (ParseNapiColor(env, asyncContext->borderColorApi, borderColor)) { 87323b3eb3cSopenharmony_ci colorProperty.SetColor(borderColor); 87423b3eb3cSopenharmony_ci return colorProperty; 87523b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 87623b3eb3cSopenharmony_ci napi_value leftApi = nullptr; 87723b3eb3cSopenharmony_ci napi_value rightApi = nullptr; 87823b3eb3cSopenharmony_ci napi_value topApi = nullptr; 87923b3eb3cSopenharmony_ci napi_value bottomApi = nullptr; 88023b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderColorApi, "left", &leftApi); 88123b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderColorApi, "right", &rightApi); 88223b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderColorApi, "top", &topApi); 88323b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderColorApi, "bottom", &bottomApi); 88423b3eb3cSopenharmony_ci Color leftColor; 88523b3eb3cSopenharmony_ci Color rightColor; 88623b3eb3cSopenharmony_ci Color topColor; 88723b3eb3cSopenharmony_ci Color bottomColor; 88823b3eb3cSopenharmony_ci if (ParseNapiColor(env, leftApi, leftColor)) { 88923b3eb3cSopenharmony_ci colorProperty.leftColor = leftColor; 89023b3eb3cSopenharmony_ci } 89123b3eb3cSopenharmony_ci if (ParseNapiColor(env, rightApi, rightColor)) { 89223b3eb3cSopenharmony_ci colorProperty.rightColor = rightColor; 89323b3eb3cSopenharmony_ci } 89423b3eb3cSopenharmony_ci if (ParseNapiColor(env, topApi, topColor)) { 89523b3eb3cSopenharmony_ci colorProperty.topColor = topColor; 89623b3eb3cSopenharmony_ci } 89723b3eb3cSopenharmony_ci if (ParseNapiColor(env, bottomApi, bottomColor)) { 89823b3eb3cSopenharmony_ci colorProperty.bottomColor = bottomColor; 89923b3eb3cSopenharmony_ci } 90023b3eb3cSopenharmony_ci colorProperty.multiValued = true; 90123b3eb3cSopenharmony_ci return colorProperty; 90223b3eb3cSopenharmony_ci } 90323b3eb3cSopenharmony_ci return std::nullopt; 90423b3eb3cSopenharmony_ci} 90523b3eb3cSopenharmony_ci 90623b3eb3cSopenharmony_cistd::optional<NG::BorderWidthProperty> GetBorderWidthProps( 90723b3eb3cSopenharmony_ci napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext) 90823b3eb3cSopenharmony_ci{ 90923b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 91023b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->borderWidthApi, &valueType); 91123b3eb3cSopenharmony_ci if (valueType != napi_number && valueType != napi_string && valueType != napi_object) { 91223b3eb3cSopenharmony_ci return std::nullopt; 91323b3eb3cSopenharmony_ci } 91423b3eb3cSopenharmony_ci NG::BorderWidthProperty borderWidthProps; 91523b3eb3cSopenharmony_ci CalcDimension borderWidth; 91623b3eb3cSopenharmony_ci if (ParseNapiDimensionNG(env, borderWidth, asyncContext->borderWidthApi, DimensionUnit::VP, true)) { 91723b3eb3cSopenharmony_ci CheckNapiDimension(borderWidth); 91823b3eb3cSopenharmony_ci borderWidthProps = NG::BorderWidthProperty({ borderWidth, borderWidth, borderWidth, borderWidth }); 91923b3eb3cSopenharmony_ci return borderWidthProps; 92023b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 92123b3eb3cSopenharmony_ci napi_value leftApi = nullptr; 92223b3eb3cSopenharmony_ci napi_value rightApi = nullptr; 92323b3eb3cSopenharmony_ci napi_value topApi = nullptr; 92423b3eb3cSopenharmony_ci napi_value bottomApi = nullptr; 92523b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderWidthApi, "left", &leftApi); 92623b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderWidthApi, "right", &rightApi); 92723b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderWidthApi, "top", &topApi); 92823b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderWidthApi, "bottom", &bottomApi); 92923b3eb3cSopenharmony_ci CalcDimension leftDimen; 93023b3eb3cSopenharmony_ci CalcDimension rightDimen; 93123b3eb3cSopenharmony_ci CalcDimension topDimen; 93223b3eb3cSopenharmony_ci CalcDimension bottomDimen; 93323b3eb3cSopenharmony_ci if (ParseNapiDimensionNG(env, leftDimen, leftApi, DimensionUnit::VP, true)) { 93423b3eb3cSopenharmony_ci CheckNapiDimension(leftDimen); 93523b3eb3cSopenharmony_ci borderWidthProps.leftDimen = leftDimen; 93623b3eb3cSopenharmony_ci } 93723b3eb3cSopenharmony_ci if (ParseNapiDimensionNG(env, rightDimen, rightApi, DimensionUnit::VP, true)) { 93823b3eb3cSopenharmony_ci CheckNapiDimension(rightDimen); 93923b3eb3cSopenharmony_ci borderWidthProps.rightDimen = rightDimen; 94023b3eb3cSopenharmony_ci } 94123b3eb3cSopenharmony_ci if (ParseNapiDimensionNG(env, topDimen, topApi, DimensionUnit::VP, true)) { 94223b3eb3cSopenharmony_ci CheckNapiDimension(topDimen); 94323b3eb3cSopenharmony_ci borderWidthProps.topDimen = topDimen; 94423b3eb3cSopenharmony_ci } 94523b3eb3cSopenharmony_ci if (ParseNapiDimensionNG(env, bottomDimen, bottomApi, DimensionUnit::VP, true)) { 94623b3eb3cSopenharmony_ci CheckNapiDimension(bottomDimen); 94723b3eb3cSopenharmony_ci borderWidthProps.bottomDimen = bottomDimen; 94823b3eb3cSopenharmony_ci } 94923b3eb3cSopenharmony_ci borderWidthProps.multiValued = true; 95023b3eb3cSopenharmony_ci return borderWidthProps; 95123b3eb3cSopenharmony_ci } 95223b3eb3cSopenharmony_ci return std::nullopt; 95323b3eb3cSopenharmony_ci} 95423b3eb3cSopenharmony_ci 95523b3eb3cSopenharmony_cistd::optional<NG::BorderRadiusProperty> GetBorderRadiusProps( 95623b3eb3cSopenharmony_ci napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext) 95723b3eb3cSopenharmony_ci{ 95823b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 95923b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->borderRadiusApi, &valueType); 96023b3eb3cSopenharmony_ci if (valueType != napi_number && valueType != napi_object && valueType != napi_string) { 96123b3eb3cSopenharmony_ci return std::nullopt; 96223b3eb3cSopenharmony_ci } 96323b3eb3cSopenharmony_ci CalcDimension borderRadius; 96423b3eb3cSopenharmony_ci if (ParseNapiDimensionNG(env, borderRadius, asyncContext->borderRadiusApi, DimensionUnit::VP, true)) { 96523b3eb3cSopenharmony_ci CheckNapiDimension(borderRadius); 96623b3eb3cSopenharmony_ci return NG::BorderRadiusProperty(borderRadius); 96723b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 96823b3eb3cSopenharmony_ci NG::BorderRadiusProperty radiusProps; 96923b3eb3cSopenharmony_ci napi_value topLeft = nullptr; 97023b3eb3cSopenharmony_ci napi_value topRight = nullptr; 97123b3eb3cSopenharmony_ci napi_value bottomLeft = nullptr; 97223b3eb3cSopenharmony_ci napi_value bottomRight = nullptr; 97323b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderRadiusApi, "topLeft", &topLeft); 97423b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderRadiusApi, "topRight", &topRight); 97523b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderRadiusApi, "bottomLeft", &bottomLeft); 97623b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderRadiusApi, "bottomRight", &bottomRight); 97723b3eb3cSopenharmony_ci CalcDimension radiusTopLeft; 97823b3eb3cSopenharmony_ci CalcDimension radiusTopRight; 97923b3eb3cSopenharmony_ci CalcDimension radiusBottomLeft; 98023b3eb3cSopenharmony_ci CalcDimension radiusBottomRight; 98123b3eb3cSopenharmony_ci if (ParseNapiDimensionNG(env, radiusTopLeft, topLeft, DimensionUnit::VP, true)) { 98223b3eb3cSopenharmony_ci CheckNapiDimension(radiusTopLeft); 98323b3eb3cSopenharmony_ci radiusProps.radiusTopLeft = radiusTopLeft; 98423b3eb3cSopenharmony_ci } 98523b3eb3cSopenharmony_ci if (ParseNapiDimensionNG(env, radiusTopRight, topRight, DimensionUnit::VP, true)) { 98623b3eb3cSopenharmony_ci CheckNapiDimension(radiusTopRight); 98723b3eb3cSopenharmony_ci radiusProps.radiusTopRight = radiusTopRight; 98823b3eb3cSopenharmony_ci } 98923b3eb3cSopenharmony_ci if (ParseNapiDimensionNG(env, radiusBottomLeft, bottomLeft, DimensionUnit::VP, true)) { 99023b3eb3cSopenharmony_ci CheckNapiDimension(radiusBottomLeft); 99123b3eb3cSopenharmony_ci radiusProps.radiusBottomLeft = radiusBottomLeft; 99223b3eb3cSopenharmony_ci } 99323b3eb3cSopenharmony_ci if (ParseNapiDimensionNG(env, radiusBottomRight, bottomRight, DimensionUnit::VP, true)) { 99423b3eb3cSopenharmony_ci CheckNapiDimension(radiusBottomRight); 99523b3eb3cSopenharmony_ci radiusProps.radiusBottomRight = radiusBottomRight; 99623b3eb3cSopenharmony_ci } 99723b3eb3cSopenharmony_ci radiusProps.multiValued = true; 99823b3eb3cSopenharmony_ci return radiusProps; 99923b3eb3cSopenharmony_ci } 100023b3eb3cSopenharmony_ci return std::nullopt; 100123b3eb3cSopenharmony_ci} 100223b3eb3cSopenharmony_ci 100323b3eb3cSopenharmony_cistd::optional<Color> GetColorProps(napi_env env, napi_value value) 100423b3eb3cSopenharmony_ci{ 100523b3eb3cSopenharmony_ci Color color; 100623b3eb3cSopenharmony_ci if (ParseNapiColor(env, value, color)) { 100723b3eb3cSopenharmony_ci return color; 100823b3eb3cSopenharmony_ci } 100923b3eb3cSopenharmony_ci return std::nullopt; 101023b3eb3cSopenharmony_ci} 101123b3eb3cSopenharmony_ci 101223b3eb3cSopenharmony_cistd::optional<NG::BorderStyleProperty> GetBorderStyleProps( 101323b3eb3cSopenharmony_ci napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext) 101423b3eb3cSopenharmony_ci{ 101523b3eb3cSopenharmony_ci NG::BorderStyleProperty styleProps; 101623b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 101723b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->borderStyleApi, &valueType); 101823b3eb3cSopenharmony_ci if (valueType != napi_number && valueType != napi_object) { 101923b3eb3cSopenharmony_ci return std::nullopt; 102023b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 102123b3eb3cSopenharmony_ci napi_value leftApi = nullptr; 102223b3eb3cSopenharmony_ci napi_value rightApi = nullptr; 102323b3eb3cSopenharmony_ci napi_value topApi = nullptr; 102423b3eb3cSopenharmony_ci napi_value bottomApi = nullptr; 102523b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderStyleApi, "left", &leftApi); 102623b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderStyleApi, "right", &rightApi); 102723b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderStyleApi, "top", &topApi); 102823b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->borderStyleApi, "bottom", &bottomApi); 102923b3eb3cSopenharmony_ci std::optional<BorderStyle> styleLeft; 103023b3eb3cSopenharmony_ci std::optional<BorderStyle> styleRight; 103123b3eb3cSopenharmony_ci std::optional<BorderStyle> styleTop; 103223b3eb3cSopenharmony_ci std::optional<BorderStyle> styleBottom; 103323b3eb3cSopenharmony_ci if (ParseStyle(env, leftApi, styleLeft)) { 103423b3eb3cSopenharmony_ci styleProps.styleLeft = styleLeft; 103523b3eb3cSopenharmony_ci } 103623b3eb3cSopenharmony_ci if (ParseStyle(env, rightApi, styleRight)) { 103723b3eb3cSopenharmony_ci styleProps.styleRight = styleRight; 103823b3eb3cSopenharmony_ci } 103923b3eb3cSopenharmony_ci if (ParseStyle(env, topApi, styleTop)) { 104023b3eb3cSopenharmony_ci styleProps.styleTop = styleTop; 104123b3eb3cSopenharmony_ci } 104223b3eb3cSopenharmony_ci if (ParseStyle(env, bottomApi, styleBottom)) { 104323b3eb3cSopenharmony_ci styleProps.styleBottom = styleBottom; 104423b3eb3cSopenharmony_ci } 104523b3eb3cSopenharmony_ci styleProps.multiValued = true; 104623b3eb3cSopenharmony_ci return styleProps; 104723b3eb3cSopenharmony_ci } 104823b3eb3cSopenharmony_ci std::optional<BorderStyle> borderStyle; 104923b3eb3cSopenharmony_ci if (ParseStyle(env, asyncContext->borderStyleApi, borderStyle)) { 105023b3eb3cSopenharmony_ci styleProps = NG::BorderStyleProperty({ borderStyle, borderStyle, borderStyle, borderStyle }); 105123b3eb3cSopenharmony_ci return styleProps; 105223b3eb3cSopenharmony_ci } 105323b3eb3cSopenharmony_ci return std::nullopt; 105423b3eb3cSopenharmony_ci} 105523b3eb3cSopenharmony_ci 105623b3eb3cSopenharmony_civoid GetNapiObjectShadow(napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext, Shadow& shadow) 105723b3eb3cSopenharmony_ci{ 105823b3eb3cSopenharmony_ci napi_value radiusApi = nullptr; 105923b3eb3cSopenharmony_ci napi_value colorApi = nullptr; 106023b3eb3cSopenharmony_ci napi_value typeApi = nullptr; 106123b3eb3cSopenharmony_ci napi_value fillApi = nullptr; 106223b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->shadowApi, "radius", &radiusApi); 106323b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->shadowApi, "color", &colorApi); 106423b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->shadowApi, "type", &typeApi); 106523b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->shadowApi, "fill", &fillApi); 106623b3eb3cSopenharmony_ci double radius = 0.0; 106723b3eb3cSopenharmony_ci napi_get_value_double(env, radiusApi, &radius); 106823b3eb3cSopenharmony_ci if (LessNotEqual(radius, 0.0)) { 106923b3eb3cSopenharmony_ci radius = 0.0; 107023b3eb3cSopenharmony_ci } 107123b3eb3cSopenharmony_ci shadow.SetBlurRadius(radius); 107223b3eb3cSopenharmony_ci Color color; 107323b3eb3cSopenharmony_ci ShadowColorStrategy shadowColorStrategy; 107423b3eb3cSopenharmony_ci if (ParseShadowColorStrategy(env, colorApi, shadowColorStrategy)) { 107523b3eb3cSopenharmony_ci shadow.SetShadowColorStrategy(shadowColorStrategy); 107623b3eb3cSopenharmony_ci } else if (ParseNapiColor(env, colorApi, color)) { 107723b3eb3cSopenharmony_ci shadow.SetColor(color); 107823b3eb3cSopenharmony_ci } 107923b3eb3cSopenharmony_ci napi_valuetype valueType = GetValueType(env, typeApi); 108023b3eb3cSopenharmony_ci int32_t shadowType = static_cast<int32_t>(ShadowType::COLOR); 108123b3eb3cSopenharmony_ci if (valueType == napi_number) { 108223b3eb3cSopenharmony_ci napi_get_value_int32(env, typeApi, &shadowType); 108323b3eb3cSopenharmony_ci } 108423b3eb3cSopenharmony_ci if (shadowType != static_cast<int32_t>(ShadowType::BLUR)) { 108523b3eb3cSopenharmony_ci shadowType = static_cast<int32_t>(ShadowType::COLOR); 108623b3eb3cSopenharmony_ci } 108723b3eb3cSopenharmony_ci shadowType = 108823b3eb3cSopenharmony_ci std::clamp(shadowType, static_cast<int32_t>(ShadowType::COLOR), static_cast<int32_t>(ShadowType::BLUR)); 108923b3eb3cSopenharmony_ci shadow.SetShadowType(static_cast<ShadowType>(shadowType)); 109023b3eb3cSopenharmony_ci valueType = GetValueType(env, fillApi); 109123b3eb3cSopenharmony_ci bool isFilled = false; 109223b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 109323b3eb3cSopenharmony_ci napi_get_value_bool(env, fillApi, &isFilled); 109423b3eb3cSopenharmony_ci } 109523b3eb3cSopenharmony_ci shadow.SetIsFilled(isFilled); 109623b3eb3cSopenharmony_ci} 109723b3eb3cSopenharmony_ci 109823b3eb3cSopenharmony_cistd::optional<Shadow> GetShadowProps(napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext) 109923b3eb3cSopenharmony_ci{ 110023b3eb3cSopenharmony_ci Shadow shadow; 110123b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 110223b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->shadowApi, &valueType); 110323b3eb3cSopenharmony_ci if (valueType != napi_object && valueType != napi_number) { 110423b3eb3cSopenharmony_ci return std::nullopt; 110523b3eb3cSopenharmony_ci } 110623b3eb3cSopenharmony_ci if (valueType == napi_number) { 110723b3eb3cSopenharmony_ci int32_t num = 0; 110823b3eb3cSopenharmony_ci if (napi_get_value_int32(env, asyncContext->shadowApi, &num) == napi_ok) { 110923b3eb3cSopenharmony_ci auto style = static_cast<ShadowStyle>(num); 111023b3eb3cSopenharmony_ci GetShadowFromTheme(style, shadow); 111123b3eb3cSopenharmony_ci return shadow; 111223b3eb3cSopenharmony_ci } 111323b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 111423b3eb3cSopenharmony_ci napi_value offsetXApi = nullptr; 111523b3eb3cSopenharmony_ci napi_value offsetYApi = nullptr; 111623b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->shadowApi, "offsetX", &offsetXApi); 111723b3eb3cSopenharmony_ci napi_get_named_property(env, asyncContext->shadowApi, "offsetY", &offsetYApi); 111823b3eb3cSopenharmony_ci ResourceInfo recv; 111923b3eb3cSopenharmony_ci bool isRtl = AceApplicationInfo::GetInstance().IsRightToLeft(); 112023b3eb3cSopenharmony_ci if (ParseResourceParam(env, offsetXApi, recv)) { 112123b3eb3cSopenharmony_ci auto resourceWrapper = CreateResourceWrapper(recv); 112223b3eb3cSopenharmony_ci auto offsetX = resourceWrapper->GetDimension(recv.resId); 112323b3eb3cSopenharmony_ci double xValue = isRtl ? offsetX.Value() * (-1) : offsetX.Value(); 112423b3eb3cSopenharmony_ci shadow.SetOffsetX(xValue); 112523b3eb3cSopenharmony_ci } else { 112623b3eb3cSopenharmony_ci CalcDimension offsetX; 112723b3eb3cSopenharmony_ci if (ParseNapiDimension(env, offsetX, offsetXApi, DimensionUnit::VP)) { 112823b3eb3cSopenharmony_ci double xValue = isRtl ? offsetX.Value() * (-1) : offsetX.Value(); 112923b3eb3cSopenharmony_ci shadow.SetOffsetX(xValue); 113023b3eb3cSopenharmony_ci } 113123b3eb3cSopenharmony_ci } 113223b3eb3cSopenharmony_ci if (ParseResourceParam(env, offsetYApi, recv)) { 113323b3eb3cSopenharmony_ci auto resourceWrapper = CreateResourceWrapper(recv); 113423b3eb3cSopenharmony_ci auto offsetY = resourceWrapper->GetDimension(recv.resId); 113523b3eb3cSopenharmony_ci shadow.SetOffsetY(offsetY.Value()); 113623b3eb3cSopenharmony_ci } else { 113723b3eb3cSopenharmony_ci CalcDimension offsetY; 113823b3eb3cSopenharmony_ci if (ParseNapiDimension(env, offsetY, offsetYApi, DimensionUnit::VP)) { 113923b3eb3cSopenharmony_ci shadow.SetOffsetY(offsetY.Value()); 114023b3eb3cSopenharmony_ci } 114123b3eb3cSopenharmony_ci } 114223b3eb3cSopenharmony_ci GetNapiObjectShadow(env, asyncContext, shadow); 114323b3eb3cSopenharmony_ci return shadow; 114423b3eb3cSopenharmony_ci } 114523b3eb3cSopenharmony_ci return std::nullopt; 114623b3eb3cSopenharmony_ci} 114723b3eb3cSopenharmony_ci 114823b3eb3cSopenharmony_cistd::optional<CalcDimension> GetNapiDialogWidthProps( 114923b3eb3cSopenharmony_ci napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext) 115023b3eb3cSopenharmony_ci{ 115123b3eb3cSopenharmony_ci std::optional<CalcDimension> widthProperty; 115223b3eb3cSopenharmony_ci CalcDimension width; 115323b3eb3cSopenharmony_ci if (ParseNapiDimensionNG(env, width, asyncContext->widthApi, DimensionUnit::VP, true)) { 115423b3eb3cSopenharmony_ci widthProperty = width; 115523b3eb3cSopenharmony_ci } 115623b3eb3cSopenharmony_ci return widthProperty; 115723b3eb3cSopenharmony_ci} 115823b3eb3cSopenharmony_ci 115923b3eb3cSopenharmony_cistd::optional<CalcDimension> GetNapiDialogHeightProps( 116023b3eb3cSopenharmony_ci napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext) 116123b3eb3cSopenharmony_ci{ 116223b3eb3cSopenharmony_ci std::optional<CalcDimension> heightProperty; 116323b3eb3cSopenharmony_ci CalcDimension height; 116423b3eb3cSopenharmony_ci if (ParseNapiDimensionNG(env, height, asyncContext->heightApi, DimensionUnit::VP, true)) { 116523b3eb3cSopenharmony_ci heightProperty = height; 116623b3eb3cSopenharmony_ci } 116723b3eb3cSopenharmony_ci return heightProperty; 116823b3eb3cSopenharmony_ci} 116923b3eb3cSopenharmony_ci 117023b3eb3cSopenharmony_ciint32_t GetDialogKeyboardAvoidMode(napi_env env, napi_value keyboardAvoidModeApi) 117123b3eb3cSopenharmony_ci{ 117223b3eb3cSopenharmony_ci int32_t mode = 0; 117323b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 117423b3eb3cSopenharmony_ci napi_typeof(env, keyboardAvoidModeApi, &valueType); 117523b3eb3cSopenharmony_ci if (valueType == napi_number) { 117623b3eb3cSopenharmony_ci napi_get_value_int32(env, keyboardAvoidModeApi, &mode); 117723b3eb3cSopenharmony_ci } 117823b3eb3cSopenharmony_ci if (mode >= 0 && mode < static_cast<int32_t>(KEYBOARD_AVOID_MODE.size())) { 117923b3eb3cSopenharmony_ci return mode; 118023b3eb3cSopenharmony_ci } 118123b3eb3cSopenharmony_ci return 0; 118223b3eb3cSopenharmony_ci} 118323b3eb3cSopenharmony_ci 118423b3eb3cSopenharmony_civoid GetNapiNamedBoolProperties(napi_env env, std::shared_ptr<PromptAsyncContext>& asyncContext) 118523b3eb3cSopenharmony_ci{ 118623b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 118723b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->autoCancel, &valueType); 118823b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 118923b3eb3cSopenharmony_ci napi_get_value_bool(env, asyncContext->autoCancel, &asyncContext->autoCancelBool); 119023b3eb3cSopenharmony_ci } 119123b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->enableHoverMode, &valueType); 119223b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 119323b3eb3cSopenharmony_ci napi_get_value_bool(env, asyncContext->enableHoverMode, &asyncContext->enableHoverModeBool); 119423b3eb3cSopenharmony_ci } 119523b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->showInSubWindow, &valueType); 119623b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 119723b3eb3cSopenharmony_ci napi_get_value_bool(env, asyncContext->showInSubWindow, &asyncContext->showInSubWindowBool); 119823b3eb3cSopenharmony_ci } 119923b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->isModal, &valueType); 120023b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 120123b3eb3cSopenharmony_ci napi_get_value_bool(env, asyncContext->isModal, &asyncContext->isModalBool); 120223b3eb3cSopenharmony_ci } 120323b3eb3cSopenharmony_ci} 120423b3eb3cSopenharmony_ci 120523b3eb3cSopenharmony_civoid GetNapiNamedProperties(napi_env env, napi_value* argv, size_t index, 120623b3eb3cSopenharmony_ci std::shared_ptr<PromptAsyncContext>& asyncContext) 120723b3eb3cSopenharmony_ci{ 120823b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 120923b3eb3cSopenharmony_ci 121023b3eb3cSopenharmony_ci if (index == 0) { 121123b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "builder", &asyncContext->builder); 121223b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "backgroundColor", &asyncContext->backgroundColorApi); 121323b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "backgroundBlurStyle", &asyncContext->backgroundBlurStyleApi); 121423b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "hoverModeArea", &asyncContext->hoverModeAreaApi); 121523b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "cornerRadius", &asyncContext->borderRadiusApi); 121623b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "borderWidth", &asyncContext->borderWidthApi); 121723b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "borderColor", &asyncContext->borderColorApi); 121823b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "borderStyle", &asyncContext->borderStyleApi); 121923b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "shadow", &asyncContext->shadowApi); 122023b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "width", &asyncContext->widthApi); 122123b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "height", &asyncContext->heightApi); 122223b3eb3cSopenharmony_ci 122323b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->builder, &valueType); 122423b3eb3cSopenharmony_ci if (valueType == napi_function) { 122523b3eb3cSopenharmony_ci napi_create_reference(env, asyncContext->builder, 1, &asyncContext->builderRef); 122623b3eb3cSopenharmony_ci } 122723b3eb3cSopenharmony_ci } 122823b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "enableHoverMode", &asyncContext->enableHoverMode); 122923b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "showInSubWindow", &asyncContext->showInSubWindow); 123023b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "isModal", &asyncContext->isModal); 123123b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "alignment", &asyncContext->alignmentApi); 123223b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "offset", &asyncContext->offsetApi); 123323b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "maskRect", &asyncContext->maskRectApi); 123423b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "autoCancel", &asyncContext->autoCancel); 123523b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "maskColor", &asyncContext->maskColorApi); 123623b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "transition", &asyncContext->transitionApi); 123723b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "onWillDismiss", &asyncContext->onWillDismiss); 123823b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "onDidAppear", &asyncContext->onDidAppear); 123923b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "onDidDisappear", &asyncContext->onDidDisappear); 124023b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "onWillAppear", &asyncContext->onWillAppear); 124123b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "onWillDisappear", &asyncContext->onWillDisappear); 124223b3eb3cSopenharmony_ci napi_get_named_property(env, argv[index], "keyboardAvoidMode", &asyncContext->keyboardAvoidModeApi); 124323b3eb3cSopenharmony_ci 124423b3eb3cSopenharmony_ci GetNapiNamedBoolProperties(env, asyncContext); 124523b3eb3cSopenharmony_ci} 124623b3eb3cSopenharmony_ci 124723b3eb3cSopenharmony_cibool JSPromptParseParam(napi_env env, size_t argc, napi_value* argv, std::shared_ptr<PromptAsyncContext>& asyncContext) 124823b3eb3cSopenharmony_ci{ 124923b3eb3cSopenharmony_ci for (size_t i = 0; i < argc; i++) { 125023b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 125123b3eb3cSopenharmony_ci napi_typeof(env, argv[i], &valueType); 125223b3eb3cSopenharmony_ci if (i == 0 || i == 1) { 125323b3eb3cSopenharmony_ci if (valueType != napi_object) { 125423b3eb3cSopenharmony_ci DeleteContextAndThrowError(env, asyncContext, "The type of parameters is incorrect."); 125523b3eb3cSopenharmony_ci return false; 125623b3eb3cSopenharmony_ci } 125723b3eb3cSopenharmony_ci GetNapiNamedProperties(env, argv, i, asyncContext); 125823b3eb3cSopenharmony_ci auto result = napi_get_named_property(env, argv[0], "nodePtr_", &asyncContext->frameNodePtr); 125923b3eb3cSopenharmony_ci if (result == napi_ok) { 126023b3eb3cSopenharmony_ci napi_get_value_external(env, asyncContext->frameNodePtr, &asyncContext->nativePtr); 126123b3eb3cSopenharmony_ci } 126223b3eb3cSopenharmony_ci 126323b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->onWillDismiss, &valueType); 126423b3eb3cSopenharmony_ci if (valueType == napi_function) { 126523b3eb3cSopenharmony_ci napi_create_reference(env, asyncContext->onWillDismiss, 1, &asyncContext->onWillDismissRef); 126623b3eb3cSopenharmony_ci } 126723b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->onDidAppear, &valueType); 126823b3eb3cSopenharmony_ci if (valueType == napi_function) { 126923b3eb3cSopenharmony_ci napi_create_reference(env, asyncContext->onDidAppear, 1, &asyncContext->onDidAppearRef); 127023b3eb3cSopenharmony_ci } 127123b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->onDidDisappear, &valueType); 127223b3eb3cSopenharmony_ci if (valueType == napi_function) { 127323b3eb3cSopenharmony_ci napi_create_reference(env, asyncContext->onDidDisappear, 1, &asyncContext->onDidDisappearRef); 127423b3eb3cSopenharmony_ci } 127523b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->onWillAppear, &valueType); 127623b3eb3cSopenharmony_ci if (valueType == napi_function) { 127723b3eb3cSopenharmony_ci napi_create_reference(env, asyncContext->onWillAppear, 1, &asyncContext->onWillAppearRef); 127823b3eb3cSopenharmony_ci } 127923b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->onWillDisappear, &valueType); 128023b3eb3cSopenharmony_ci if (valueType == napi_function) { 128123b3eb3cSopenharmony_ci napi_create_reference(env, asyncContext->onWillDisappear, 1, &asyncContext->onWillDisappearRef); 128223b3eb3cSopenharmony_ci } 128323b3eb3cSopenharmony_ci } else { 128423b3eb3cSopenharmony_ci DeleteContextAndThrowError(env, asyncContext, "The type of parameters is incorrect."); 128523b3eb3cSopenharmony_ci return false; 128623b3eb3cSopenharmony_ci } 128723b3eb3cSopenharmony_ci } 128823b3eb3cSopenharmony_ci return true; 128923b3eb3cSopenharmony_ci} 129023b3eb3cSopenharmony_ci 129123b3eb3cSopenharmony_civoid JSPromptThrowInterError(napi_env env, std::shared_ptr<PromptAsyncContext>& asyncContext, std::string& strMsg) 129223b3eb3cSopenharmony_ci{ 129323b3eb3cSopenharmony_ci napi_value code = nullptr; 129423b3eb3cSopenharmony_ci std::string strCode = std::to_string(ERROR_CODE_INTERNAL_ERROR); 129523b3eb3cSopenharmony_ci napi_create_string_utf8(env, strCode.c_str(), strCode.length(), &code); 129623b3eb3cSopenharmony_ci napi_value msg = nullptr; 129723b3eb3cSopenharmony_ci napi_create_string_utf8(env, strMsg.c_str(), strMsg.length(), &msg); 129823b3eb3cSopenharmony_ci napi_value error = nullptr; 129923b3eb3cSopenharmony_ci napi_create_error(env, code, msg, &error); 130023b3eb3cSopenharmony_ci 130123b3eb3cSopenharmony_ci if (asyncContext->deferred) { 130223b3eb3cSopenharmony_ci napi_reject_deferred(env, asyncContext->deferred, error); 130323b3eb3cSopenharmony_ci } 130423b3eb3cSopenharmony_ci} 130523b3eb3cSopenharmony_ci 130623b3eb3cSopenharmony_civoid UpdatePromptAlignment(DialogAlignment& alignment) 130723b3eb3cSopenharmony_ci{ 130823b3eb3cSopenharmony_ci bool isRtl = AceApplicationInfo::GetInstance().IsRightToLeft(); 130923b3eb3cSopenharmony_ci if (alignment == DialogAlignment::TOP_START) { 131023b3eb3cSopenharmony_ci if (isRtl) { 131123b3eb3cSopenharmony_ci alignment = DialogAlignment::TOP_END; 131223b3eb3cSopenharmony_ci } 131323b3eb3cSopenharmony_ci } else if (alignment == DialogAlignment::TOP_END) { 131423b3eb3cSopenharmony_ci if (isRtl) { 131523b3eb3cSopenharmony_ci alignment = DialogAlignment::TOP_START; 131623b3eb3cSopenharmony_ci } 131723b3eb3cSopenharmony_ci } else if (alignment == DialogAlignment::CENTER_START) { 131823b3eb3cSopenharmony_ci if (isRtl) { 131923b3eb3cSopenharmony_ci alignment = DialogAlignment::CENTER_END; 132023b3eb3cSopenharmony_ci } 132123b3eb3cSopenharmony_ci } else if (alignment == DialogAlignment::CENTER_END) { 132223b3eb3cSopenharmony_ci if (isRtl) { 132323b3eb3cSopenharmony_ci alignment = DialogAlignment::CENTER_START; 132423b3eb3cSopenharmony_ci } 132523b3eb3cSopenharmony_ci } else if (alignment == DialogAlignment::BOTTOM_START) { 132623b3eb3cSopenharmony_ci if (isRtl) { 132723b3eb3cSopenharmony_ci alignment = DialogAlignment::BOTTOM_END; 132823b3eb3cSopenharmony_ci } 132923b3eb3cSopenharmony_ci } else if (alignment == DialogAlignment::BOTTOM_END) { 133023b3eb3cSopenharmony_ci if (isRtl) { 133123b3eb3cSopenharmony_ci alignment = DialogAlignment::BOTTOM_START; 133223b3eb3cSopenharmony_ci } 133323b3eb3cSopenharmony_ci } 133423b3eb3cSopenharmony_ci} 133523b3eb3cSopenharmony_ci 133623b3eb3cSopenharmony_cinapi_value JSPromptShowDialog(napi_env env, napi_callback_info info) 133723b3eb3cSopenharmony_ci{ 133823b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_DIALOG, "js prompt show dialog enter"); 133923b3eb3cSopenharmony_ci size_t requireArgc = 1; 134023b3eb3cSopenharmony_ci size_t argc = 2; 134123b3eb3cSopenharmony_ci napi_value argv[3] = { 0 }; 134223b3eb3cSopenharmony_ci napi_value thisVar = nullptr; 134323b3eb3cSopenharmony_ci void* data = nullptr; 134423b3eb3cSopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 134523b3eb3cSopenharmony_ci if (argc < requireArgc) { 134623b3eb3cSopenharmony_ci NapiThrow( 134723b3eb3cSopenharmony_ci env, "The number of parameters must be greater than or equal to 1.", ERROR_CODE_PARAM_INVALID); 134823b3eb3cSopenharmony_ci return nullptr; 134923b3eb3cSopenharmony_ci } 135023b3eb3cSopenharmony_ci if (thisVar == nullptr) { 135123b3eb3cSopenharmony_ci return nullptr; 135223b3eb3cSopenharmony_ci } 135323b3eb3cSopenharmony_ci napi_valuetype valueTypeOfThis = napi_undefined; 135423b3eb3cSopenharmony_ci napi_typeof(env, thisVar, &valueTypeOfThis); 135523b3eb3cSopenharmony_ci if (valueTypeOfThis == napi_undefined) { 135623b3eb3cSopenharmony_ci return nullptr; 135723b3eb3cSopenharmony_ci } 135823b3eb3cSopenharmony_ci 135923b3eb3cSopenharmony_ci auto asyncContext = std::make_shared<PromptAsyncContext>(); 136023b3eb3cSopenharmony_ci asyncContext->env = env; 136123b3eb3cSopenharmony_ci asyncContext->instanceId = Container::CurrentIdSafely(); 136223b3eb3cSopenharmony_ci 136323b3eb3cSopenharmony_ci std::optional<DialogAlignment> alignment; 136423b3eb3cSopenharmony_ci std::optional<DimensionOffset> offset; 136523b3eb3cSopenharmony_ci std::optional<DimensionRect> maskRect; 136623b3eb3cSopenharmony_ci std::optional<Shadow> shadowProps; 136723b3eb3cSopenharmony_ci std::optional<Color> backgroundColor; 136823b3eb3cSopenharmony_ci std::optional<int32_t> backgroundBlurStyle; 136923b3eb3cSopenharmony_ci std::optional<HoverModeAreaType> hoverModeArea; 137023b3eb3cSopenharmony_ci 137123b3eb3cSopenharmony_ci for (size_t i = 0; i < argc; i++) { 137223b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 137323b3eb3cSopenharmony_ci napi_typeof(env, argv[i], &valueType); 137423b3eb3cSopenharmony_ci if (i == 0) { 137523b3eb3cSopenharmony_ci if (valueType != napi_object) { 137623b3eb3cSopenharmony_ci DeleteContextAndThrowError(env, asyncContext, "The type of parameters is incorrect."); 137723b3eb3cSopenharmony_ci return nullptr; 137823b3eb3cSopenharmony_ci } 137923b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "title", &asyncContext->titleNApi); 138023b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "message", &asyncContext->messageNApi); 138123b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "buttons", &asyncContext->buttonsNApi); 138223b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "autoCancel", &asyncContext->autoCancel); 138323b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "showInSubWindow", &asyncContext->showInSubWindow); 138423b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "isModal", &asyncContext->isModal); 138523b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "alignment", &asyncContext->alignmentApi); 138623b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "offset", &asyncContext->offsetApi); 138723b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "maskRect", &asyncContext->maskRectApi); 138823b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "shadow", &asyncContext->shadowApi); 138923b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "backgroundColor", &asyncContext->backgroundColorApi); 139023b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "backgroundBlurStyle", &asyncContext->backgroundBlurStyleApi); 139123b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "enableHoverMode", &asyncContext->enableHoverMode); 139223b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "hoverModeArea", &asyncContext->hoverModeAreaApi); 139323b3eb3cSopenharmony_ci GetNapiString(env, asyncContext->titleNApi, asyncContext->titleString, valueType); 139423b3eb3cSopenharmony_ci GetNapiString(env, asyncContext->messageNApi, asyncContext->messageString, valueType); 139523b3eb3cSopenharmony_ci GetNapiDialogProps(env, asyncContext, alignment, offset, maskRect); 139623b3eb3cSopenharmony_ci backgroundColor = GetColorProps(env, asyncContext->backgroundColorApi); 139723b3eb3cSopenharmony_ci shadowProps = GetShadowProps(env, asyncContext); 139823b3eb3cSopenharmony_ci GetNapiBlurStyleAndHoverModeProps(env, asyncContext, backgroundBlurStyle, hoverModeArea); 139923b3eb3cSopenharmony_ci if (!ParseButtonsPara(env, asyncContext, SHOW_DIALOG_BUTTON_NUM_MAX, false)) { 140023b3eb3cSopenharmony_ci return nullptr; 140123b3eb3cSopenharmony_ci } 140223b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->enableHoverMode, &valueType); 140323b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 140423b3eb3cSopenharmony_ci napi_get_value_bool(env, asyncContext->enableHoverMode, &asyncContext->enableHoverModeBool); 140523b3eb3cSopenharmony_ci } 140623b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->autoCancel, &valueType); 140723b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 140823b3eb3cSopenharmony_ci napi_get_value_bool(env, asyncContext->autoCancel, &asyncContext->autoCancelBool); 140923b3eb3cSopenharmony_ci } 141023b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->showInSubWindow, &valueType); 141123b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 141223b3eb3cSopenharmony_ci napi_get_value_bool(env, asyncContext->showInSubWindow, &asyncContext->showInSubWindowBool); 141323b3eb3cSopenharmony_ci } 141423b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->isModal, &valueType); 141523b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 141623b3eb3cSopenharmony_ci napi_get_value_bool(env, asyncContext->isModal, &asyncContext->isModalBool); 141723b3eb3cSopenharmony_ci } 141823b3eb3cSopenharmony_ci } else if (valueType == napi_function) { 141923b3eb3cSopenharmony_ci napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef); 142023b3eb3cSopenharmony_ci } else { 142123b3eb3cSopenharmony_ci DeleteContextAndThrowError(env, asyncContext, "The type of parameters is incorrect."); 142223b3eb3cSopenharmony_ci return nullptr; 142323b3eb3cSopenharmony_ci } 142423b3eb3cSopenharmony_ci } 142523b3eb3cSopenharmony_ci auto onLanguageChange = [shadowProps, alignment, offset, maskRect, 142623b3eb3cSopenharmony_ci updateAlignment = UpdatePromptAlignment](DialogProperties& dialogProps) { 142723b3eb3cSopenharmony_ci bool isRtl = AceApplicationInfo::GetInstance().IsRightToLeft(); 142823b3eb3cSopenharmony_ci if (shadowProps.has_value()) { 142923b3eb3cSopenharmony_ci std::optional<Shadow> shadow = shadowProps.value(); 143023b3eb3cSopenharmony_ci double offsetX = isRtl ? shadow->GetOffset().GetX() * (-1) : shadow->GetOffset().GetX(); 143123b3eb3cSopenharmony_ci shadow->SetOffsetX(offsetX); 143223b3eb3cSopenharmony_ci dialogProps.shadow = shadow.value(); 143323b3eb3cSopenharmony_ci } 143423b3eb3cSopenharmony_ci if (alignment.has_value()) { 143523b3eb3cSopenharmony_ci std::optional<DialogAlignment> pmAlign = alignment.value(); 143623b3eb3cSopenharmony_ci updateAlignment(pmAlign.value()); 143723b3eb3cSopenharmony_ci dialogProps.alignment = pmAlign.value(); 143823b3eb3cSopenharmony_ci } 143923b3eb3cSopenharmony_ci if (offset.has_value()) { 144023b3eb3cSopenharmony_ci std::optional<DimensionOffset> pmOffset = offset.value(); 144123b3eb3cSopenharmony_ci Dimension offsetX = isRtl ? pmOffset->GetX() * (-1) : pmOffset->GetX(); 144223b3eb3cSopenharmony_ci pmOffset->SetX(offsetX); 144323b3eb3cSopenharmony_ci dialogProps.offset = pmOffset.value(); 144423b3eb3cSopenharmony_ci } 144523b3eb3cSopenharmony_ci if (maskRect.has_value()) { 144623b3eb3cSopenharmony_ci std::optional<DimensionRect> pmMaskRect = maskRect.value(); 144723b3eb3cSopenharmony_ci auto offset = pmMaskRect->GetOffset(); 144823b3eb3cSopenharmony_ci Dimension offsetX = isRtl ? offset.GetX() * (-1) : offset.GetX(); 144923b3eb3cSopenharmony_ci offset.SetX(offsetX); 145023b3eb3cSopenharmony_ci pmMaskRect->SetOffset(offset); 145123b3eb3cSopenharmony_ci dialogProps.maskRect = pmMaskRect.value(); 145223b3eb3cSopenharmony_ci } 145323b3eb3cSopenharmony_ci }; 145423b3eb3cSopenharmony_ci napi_value result = nullptr; 145523b3eb3cSopenharmony_ci if (asyncContext->callbackRef == nullptr) { 145623b3eb3cSopenharmony_ci napi_create_promise(env, &asyncContext->deferred, &result); 145723b3eb3cSopenharmony_ci } else { 145823b3eb3cSopenharmony_ci napi_get_undefined(env, &result); 145923b3eb3cSopenharmony_ci } 146023b3eb3cSopenharmony_ci asyncContext->callbacks.emplace("success"); 146123b3eb3cSopenharmony_ci asyncContext->callbacks.emplace("cancel"); 146223b3eb3cSopenharmony_ci 146323b3eb3cSopenharmony_ci auto callBack = [asyncContext](int32_t callbackType, int32_t successType) mutable { 146423b3eb3cSopenharmony_ci if (asyncContext == nullptr) { 146523b3eb3cSopenharmony_ci return; 146623b3eb3cSopenharmony_ci } 146723b3eb3cSopenharmony_ci 146823b3eb3cSopenharmony_ci asyncContext->callbackType = callbackType; 146923b3eb3cSopenharmony_ci asyncContext->successType = successType; 147023b3eb3cSopenharmony_ci auto container = AceEngine::Get().GetContainer(asyncContext->instanceId); 147123b3eb3cSopenharmony_ci if (!container) { 147223b3eb3cSopenharmony_ci return; 147323b3eb3cSopenharmony_ci } 147423b3eb3cSopenharmony_ci 147523b3eb3cSopenharmony_ci auto taskExecutor = container->GetTaskExecutor(); 147623b3eb3cSopenharmony_ci if (!taskExecutor) { 147723b3eb3cSopenharmony_ci return; 147823b3eb3cSopenharmony_ci } 147923b3eb3cSopenharmony_ci taskExecutor->PostTask( 148023b3eb3cSopenharmony_ci [asyncContext]() { 148123b3eb3cSopenharmony_ci if (asyncContext == nullptr) { 148223b3eb3cSopenharmony_ci return; 148323b3eb3cSopenharmony_ci } 148423b3eb3cSopenharmony_ci 148523b3eb3cSopenharmony_ci if (!asyncContext->valid) { 148623b3eb3cSopenharmony_ci return; 148723b3eb3cSopenharmony_ci } 148823b3eb3cSopenharmony_ci 148923b3eb3cSopenharmony_ci napi_handle_scope scope = nullptr; 149023b3eb3cSopenharmony_ci napi_open_handle_scope(asyncContext->env, &scope); 149123b3eb3cSopenharmony_ci if (scope == nullptr) { 149223b3eb3cSopenharmony_ci return; 149323b3eb3cSopenharmony_ci } 149423b3eb3cSopenharmony_ci 149523b3eb3cSopenharmony_ci napi_value ret; 149623b3eb3cSopenharmony_ci napi_value successIndex = nullptr; 149723b3eb3cSopenharmony_ci napi_create_int32(asyncContext->env, asyncContext->successType, &successIndex); 149823b3eb3cSopenharmony_ci napi_value indexObj = nullptr; 149923b3eb3cSopenharmony_ci napi_create_object(asyncContext->env, &indexObj); 150023b3eb3cSopenharmony_ci napi_set_named_property(asyncContext->env, indexObj, "index", successIndex); 150123b3eb3cSopenharmony_ci napi_value result[2] = { 0 }; 150223b3eb3cSopenharmony_ci napi_create_object(asyncContext->env, &result[1]); 150323b3eb3cSopenharmony_ci napi_set_named_property(asyncContext->env, result[1], "index", successIndex); 150423b3eb3cSopenharmony_ci bool dialogResult = true; 150523b3eb3cSopenharmony_ci switch (asyncContext->callbackType) { 150623b3eb3cSopenharmony_ci case 0: 150723b3eb3cSopenharmony_ci napi_get_undefined(asyncContext->env, &result[0]); 150823b3eb3cSopenharmony_ci dialogResult = true; 150923b3eb3cSopenharmony_ci break; 151023b3eb3cSopenharmony_ci case 1: 151123b3eb3cSopenharmony_ci napi_value message = nullptr; 151223b3eb3cSopenharmony_ci napi_create_string_utf8(asyncContext->env, "cancel", strlen("cancel"), &message); 151323b3eb3cSopenharmony_ci napi_create_error(asyncContext->env, nullptr, message, &result[0]); 151423b3eb3cSopenharmony_ci dialogResult = false; 151523b3eb3cSopenharmony_ci break; 151623b3eb3cSopenharmony_ci } 151723b3eb3cSopenharmony_ci if (asyncContext->deferred) { 151823b3eb3cSopenharmony_ci if (dialogResult) { 151923b3eb3cSopenharmony_ci napi_resolve_deferred(asyncContext->env, asyncContext->deferred, result[1]); 152023b3eb3cSopenharmony_ci } else { 152123b3eb3cSopenharmony_ci napi_reject_deferred(asyncContext->env, asyncContext->deferred, result[0]); 152223b3eb3cSopenharmony_ci } 152323b3eb3cSopenharmony_ci } else { 152423b3eb3cSopenharmony_ci napi_value callback = nullptr; 152523b3eb3cSopenharmony_ci napi_get_reference_value(asyncContext->env, asyncContext->callbackRef, &callback); 152623b3eb3cSopenharmony_ci napi_call_function( 152723b3eb3cSopenharmony_ci asyncContext->env, nullptr, callback, sizeof(result) / sizeof(result[0]), result, &ret); 152823b3eb3cSopenharmony_ci napi_delete_reference(asyncContext->env, asyncContext->callbackRef); 152923b3eb3cSopenharmony_ci } 153023b3eb3cSopenharmony_ci napi_close_handle_scope(asyncContext->env, scope); 153123b3eb3cSopenharmony_ci }, 153223b3eb3cSopenharmony_ci TaskExecutor::TaskType::JS, "ArkUIDialogParseDialogCallback"); 153323b3eb3cSopenharmony_ci asyncContext = nullptr; 153423b3eb3cSopenharmony_ci }; 153523b3eb3cSopenharmony_ci 153623b3eb3cSopenharmony_ci PromptDialogAttr promptDialogAttr = { 153723b3eb3cSopenharmony_ci .title = asyncContext->titleString, 153823b3eb3cSopenharmony_ci .message = asyncContext->messageString, 153923b3eb3cSopenharmony_ci .autoCancel = asyncContext->autoCancelBool, 154023b3eb3cSopenharmony_ci .showInSubWindow = asyncContext->showInSubWindowBool, 154123b3eb3cSopenharmony_ci .isModal = asyncContext->isModalBool, 154223b3eb3cSopenharmony_ci .enableHoverMode = asyncContext->enableHoverModeBool, 154323b3eb3cSopenharmony_ci .alignment = alignment, 154423b3eb3cSopenharmony_ci .offset = offset, 154523b3eb3cSopenharmony_ci .maskRect = maskRect, 154623b3eb3cSopenharmony_ci .backgroundColor = backgroundColor, 154723b3eb3cSopenharmony_ci .backgroundBlurStyle = backgroundBlurStyle, 154823b3eb3cSopenharmony_ci .shadow = shadowProps, 154923b3eb3cSopenharmony_ci .hoverModeArea = hoverModeArea, 155023b3eb3cSopenharmony_ci .onLanguageChange = onLanguageChange, 155123b3eb3cSopenharmony_ci }; 155223b3eb3cSopenharmony_ci 155323b3eb3cSopenharmony_ci#ifdef OHOS_STANDARD_SYSTEM 155423b3eb3cSopenharmony_ci // NG 155523b3eb3cSopenharmony_ci if (SystemProperties::GetExtSurfaceEnabled() || !ContainerIsService()) { 155623b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 155723b3eb3cSopenharmony_ci if (delegate) { 155823b3eb3cSopenharmony_ci delegate->ShowDialog(promptDialogAttr, asyncContext->buttons, std::move(callBack), asyncContext->callbacks); 155923b3eb3cSopenharmony_ci } else { 156023b3eb3cSopenharmony_ci // throw internal error 156123b3eb3cSopenharmony_ci napi_value code = nullptr; 156223b3eb3cSopenharmony_ci std::string strCode = std::to_string(ERROR_CODE_INTERNAL_ERROR); 156323b3eb3cSopenharmony_ci napi_create_string_utf8(env, strCode.c_str(), strCode.length(), &code); 156423b3eb3cSopenharmony_ci napi_value msg = nullptr; 156523b3eb3cSopenharmony_ci std::string strMsg = ErrorToMessage(ERROR_CODE_INTERNAL_ERROR) + "Can not get delegate."; 156623b3eb3cSopenharmony_ci napi_create_string_utf8(env, strMsg.c_str(), strMsg.length(), &msg); 156723b3eb3cSopenharmony_ci napi_value error = nullptr; 156823b3eb3cSopenharmony_ci napi_create_error(env, code, msg, &error); 156923b3eb3cSopenharmony_ci 157023b3eb3cSopenharmony_ci if (asyncContext->deferred) { 157123b3eb3cSopenharmony_ci napi_reject_deferred(env, asyncContext->deferred, error); 157223b3eb3cSopenharmony_ci } else { 157323b3eb3cSopenharmony_ci napi_value ret1; 157423b3eb3cSopenharmony_ci napi_value callback = nullptr; 157523b3eb3cSopenharmony_ci napi_get_reference_value(env, asyncContext->callbackRef, &callback); 157623b3eb3cSopenharmony_ci napi_call_function(env, nullptr, callback, 1, &error, &ret1); 157723b3eb3cSopenharmony_ci napi_delete_reference(env, asyncContext->callbackRef); 157823b3eb3cSopenharmony_ci } 157923b3eb3cSopenharmony_ci } 158023b3eb3cSopenharmony_ci } else if (SubwindowManager::GetInstance() != nullptr) { 158123b3eb3cSopenharmony_ci SubwindowManager::GetInstance()->ShowDialog( 158223b3eb3cSopenharmony_ci promptDialogAttr, asyncContext->buttons, std::move(callBack), asyncContext->callbacks); 158323b3eb3cSopenharmony_ci } 158423b3eb3cSopenharmony_ci#else 158523b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 158623b3eb3cSopenharmony_ci if (delegate) { 158723b3eb3cSopenharmony_ci delegate->ShowDialog(promptDialogAttr, asyncContext->buttons, std::move(callBack), asyncContext->callbacks); 158823b3eb3cSopenharmony_ci } else { 158923b3eb3cSopenharmony_ci // throw internal error 159023b3eb3cSopenharmony_ci napi_value code = nullptr; 159123b3eb3cSopenharmony_ci std::string strCode = std::to_string(ERROR_CODE_INTERNAL_ERROR); 159223b3eb3cSopenharmony_ci napi_create_string_utf8(env, strCode.c_str(), strCode.length(), &code); 159323b3eb3cSopenharmony_ci napi_value msg = nullptr; 159423b3eb3cSopenharmony_ci std::string strMsg = ErrorToMessage(ERROR_CODE_INTERNAL_ERROR) + "UI execution context not found."; 159523b3eb3cSopenharmony_ci napi_create_string_utf8(env, strMsg.c_str(), strMsg.length(), &msg); 159623b3eb3cSopenharmony_ci napi_value error = nullptr; 159723b3eb3cSopenharmony_ci napi_create_error(env, code, msg, &error); 159823b3eb3cSopenharmony_ci 159923b3eb3cSopenharmony_ci if (asyncContext->deferred) { 160023b3eb3cSopenharmony_ci napi_reject_deferred(env, asyncContext->deferred, error); 160123b3eb3cSopenharmony_ci } else { 160223b3eb3cSopenharmony_ci napi_value ret1; 160323b3eb3cSopenharmony_ci napi_value callback = nullptr; 160423b3eb3cSopenharmony_ci napi_get_reference_value(env, asyncContext->callbackRef, &callback); 160523b3eb3cSopenharmony_ci napi_call_function(env, nullptr, callback, 1, &error, &ret1); 160623b3eb3cSopenharmony_ci napi_delete_reference(env, asyncContext->callbackRef); 160723b3eb3cSopenharmony_ci } 160823b3eb3cSopenharmony_ci } 160923b3eb3cSopenharmony_ci#endif 161023b3eb3cSopenharmony_ci return result; 161123b3eb3cSopenharmony_ci} 161223b3eb3cSopenharmony_ci 161323b3eb3cSopenharmony_cinapi_value JSPromptShowActionMenu(napi_env env, napi_callback_info info) 161423b3eb3cSopenharmony_ci{ 161523b3eb3cSopenharmony_ci TAG_LOGD(AceLogTag::ACE_DIALOG, "js prompt show action menu enter"); 161623b3eb3cSopenharmony_ci size_t requireArgc = 1; 161723b3eb3cSopenharmony_ci size_t argc = 2; 161823b3eb3cSopenharmony_ci napi_value argv[3] = { 0 }; 161923b3eb3cSopenharmony_ci napi_value thisVar = nullptr; 162023b3eb3cSopenharmony_ci void* data = nullptr; 162123b3eb3cSopenharmony_ci napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); 162223b3eb3cSopenharmony_ci if (argc < requireArgc) { 162323b3eb3cSopenharmony_ci NapiThrow( 162423b3eb3cSopenharmony_ci env, "The number of parameters must be greater than or equal to 1.", ERROR_CODE_PARAM_INVALID); 162523b3eb3cSopenharmony_ci return nullptr; 162623b3eb3cSopenharmony_ci } 162723b3eb3cSopenharmony_ci if (thisVar == nullptr) { 162823b3eb3cSopenharmony_ci return nullptr; 162923b3eb3cSopenharmony_ci } 163023b3eb3cSopenharmony_ci napi_valuetype valueTypeOfThis = napi_undefined; 163123b3eb3cSopenharmony_ci napi_typeof(env, thisVar, &valueTypeOfThis); 163223b3eb3cSopenharmony_ci if (valueTypeOfThis == napi_undefined) { 163323b3eb3cSopenharmony_ci return nullptr; 163423b3eb3cSopenharmony_ci } 163523b3eb3cSopenharmony_ci 163623b3eb3cSopenharmony_ci auto asyncContext = std::make_shared<PromptAsyncContext>(); 163723b3eb3cSopenharmony_ci asyncContext->env = env; 163823b3eb3cSopenharmony_ci asyncContext->instanceId = Container::CurrentIdSafely(); 163923b3eb3cSopenharmony_ci for (size_t i = 0; i < argc; i++) { 164023b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 164123b3eb3cSopenharmony_ci napi_typeof(env, argv[i], &valueType); 164223b3eb3cSopenharmony_ci if (i == 0) { 164323b3eb3cSopenharmony_ci if (valueType != napi_object) { 164423b3eb3cSopenharmony_ci DeleteContextAndThrowError(env, asyncContext, "The type of parameters is incorrect."); 164523b3eb3cSopenharmony_ci return nullptr; 164623b3eb3cSopenharmony_ci } 164723b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "title", &asyncContext->titleNApi); 164823b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "showInSubWindow", &asyncContext->showInSubWindow); 164923b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "isModal", &asyncContext->isModal); 165023b3eb3cSopenharmony_ci GetNapiString(env, asyncContext->titleNApi, asyncContext->titleString, valueType); 165123b3eb3cSopenharmony_ci if (!HasProperty(env, argv[0], "buttons")) { 165223b3eb3cSopenharmony_ci DeleteContextAndThrowError(env, asyncContext, "Required input parameters are missing."); 165323b3eb3cSopenharmony_ci return nullptr; 165423b3eb3cSopenharmony_ci } 165523b3eb3cSopenharmony_ci napi_get_named_property(env, argv[0], "buttons", &asyncContext->buttonsNApi); 165623b3eb3cSopenharmony_ci if (!ParseButtonsPara(env, asyncContext, SHOW_ACTION_MENU_BUTTON_NUM_MAX, true)) { 165723b3eb3cSopenharmony_ci return nullptr; 165823b3eb3cSopenharmony_ci } 165923b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->showInSubWindow, &valueType); 166023b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 166123b3eb3cSopenharmony_ci napi_get_value_bool(env, asyncContext->showInSubWindow, &asyncContext->showInSubWindowBool); 166223b3eb3cSopenharmony_ci } 166323b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->isModal, &valueType); 166423b3eb3cSopenharmony_ci if (valueType == napi_boolean) { 166523b3eb3cSopenharmony_ci napi_get_value_bool(env, asyncContext->isModal, &asyncContext->isModalBool); 166623b3eb3cSopenharmony_ci } 166723b3eb3cSopenharmony_ci } else if (valueType == napi_function) { 166823b3eb3cSopenharmony_ci napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef); 166923b3eb3cSopenharmony_ci } else { 167023b3eb3cSopenharmony_ci DeleteContextAndThrowError(env, asyncContext, "The type of parameters is incorrect."); 167123b3eb3cSopenharmony_ci return nullptr; 167223b3eb3cSopenharmony_ci } 167323b3eb3cSopenharmony_ci } 167423b3eb3cSopenharmony_ci napi_value result = nullptr; 167523b3eb3cSopenharmony_ci if (asyncContext->callbackRef == nullptr) { 167623b3eb3cSopenharmony_ci napi_create_promise(env, &asyncContext->deferred, &result); 167723b3eb3cSopenharmony_ci } else { 167823b3eb3cSopenharmony_ci napi_get_undefined(env, &result); 167923b3eb3cSopenharmony_ci } 168023b3eb3cSopenharmony_ci 168123b3eb3cSopenharmony_ci auto callBack = [asyncContext](int32_t callbackType, int32_t successType) mutable { 168223b3eb3cSopenharmony_ci if (asyncContext == nullptr) { 168323b3eb3cSopenharmony_ci return; 168423b3eb3cSopenharmony_ci } 168523b3eb3cSopenharmony_ci 168623b3eb3cSopenharmony_ci asyncContext->callbackType = callbackType; 168723b3eb3cSopenharmony_ci asyncContext->successType = successType; 168823b3eb3cSopenharmony_ci auto container = AceEngine::Get().GetContainer(asyncContext->instanceId); 168923b3eb3cSopenharmony_ci if (!container) { 169023b3eb3cSopenharmony_ci return; 169123b3eb3cSopenharmony_ci } 169223b3eb3cSopenharmony_ci 169323b3eb3cSopenharmony_ci auto taskExecutor = container->GetTaskExecutor(); 169423b3eb3cSopenharmony_ci if (!taskExecutor) { 169523b3eb3cSopenharmony_ci return; 169623b3eb3cSopenharmony_ci } 169723b3eb3cSopenharmony_ci taskExecutor->PostTask( 169823b3eb3cSopenharmony_ci [asyncContext]() { 169923b3eb3cSopenharmony_ci if (asyncContext == nullptr) { 170023b3eb3cSopenharmony_ci return; 170123b3eb3cSopenharmony_ci } 170223b3eb3cSopenharmony_ci 170323b3eb3cSopenharmony_ci if (!asyncContext->valid) { 170423b3eb3cSopenharmony_ci return; 170523b3eb3cSopenharmony_ci } 170623b3eb3cSopenharmony_ci 170723b3eb3cSopenharmony_ci napi_handle_scope scope = nullptr; 170823b3eb3cSopenharmony_ci napi_open_handle_scope(asyncContext->env, &scope); 170923b3eb3cSopenharmony_ci if (scope == nullptr) { 171023b3eb3cSopenharmony_ci return; 171123b3eb3cSopenharmony_ci } 171223b3eb3cSopenharmony_ci 171323b3eb3cSopenharmony_ci napi_value ret; 171423b3eb3cSopenharmony_ci napi_value successIndex = nullptr; 171523b3eb3cSopenharmony_ci napi_create_int32(asyncContext->env, asyncContext->successType, &successIndex); 171623b3eb3cSopenharmony_ci asyncContext->callbackSuccessString = "showActionMenu:ok"; 171723b3eb3cSopenharmony_ci napi_value indexObj = GetReturnObject(asyncContext->env, asyncContext->callbackSuccessString); 171823b3eb3cSopenharmony_ci napi_set_named_property(asyncContext->env, indexObj, "index", successIndex); 171923b3eb3cSopenharmony_ci napi_value result[2] = { 0 }; 172023b3eb3cSopenharmony_ci napi_create_object(asyncContext->env, &result[1]); 172123b3eb3cSopenharmony_ci napi_set_named_property(asyncContext->env, result[1], "index", successIndex); 172223b3eb3cSopenharmony_ci bool dialogResult = true; 172323b3eb3cSopenharmony_ci switch (asyncContext->callbackType) { 172423b3eb3cSopenharmony_ci case 0: 172523b3eb3cSopenharmony_ci napi_get_undefined(asyncContext->env, &result[0]); 172623b3eb3cSopenharmony_ci dialogResult = true; 172723b3eb3cSopenharmony_ci break; 172823b3eb3cSopenharmony_ci case 1: 172923b3eb3cSopenharmony_ci napi_value message = nullptr; 173023b3eb3cSopenharmony_ci napi_create_string_utf8(asyncContext->env, "cancel", strlen("cancel"), &message); 173123b3eb3cSopenharmony_ci napi_create_error(asyncContext->env, nullptr, message, &result[0]); 173223b3eb3cSopenharmony_ci dialogResult = false; 173323b3eb3cSopenharmony_ci break; 173423b3eb3cSopenharmony_ci } 173523b3eb3cSopenharmony_ci if (asyncContext->deferred) { 173623b3eb3cSopenharmony_ci if (dialogResult) { 173723b3eb3cSopenharmony_ci napi_resolve_deferred(asyncContext->env, asyncContext->deferred, result[1]); 173823b3eb3cSopenharmony_ci } else { 173923b3eb3cSopenharmony_ci napi_reject_deferred(asyncContext->env, asyncContext->deferred, result[0]); 174023b3eb3cSopenharmony_ci } 174123b3eb3cSopenharmony_ci } else { 174223b3eb3cSopenharmony_ci napi_value callback = nullptr; 174323b3eb3cSopenharmony_ci napi_get_reference_value(asyncContext->env, asyncContext->callbackRef, &callback); 174423b3eb3cSopenharmony_ci napi_call_function( 174523b3eb3cSopenharmony_ci asyncContext->env, nullptr, callback, sizeof(result) / sizeof(result[0]), result, &ret); 174623b3eb3cSopenharmony_ci napi_delete_reference(asyncContext->env, asyncContext->callbackRef); 174723b3eb3cSopenharmony_ci } 174823b3eb3cSopenharmony_ci napi_close_handle_scope(asyncContext->env, scope); 174923b3eb3cSopenharmony_ci }, 175023b3eb3cSopenharmony_ci TaskExecutor::TaskType::JS, "ArkUIDialogParseActionMenuCallback"); 175123b3eb3cSopenharmony_ci asyncContext = nullptr; 175223b3eb3cSopenharmony_ci }; 175323b3eb3cSopenharmony_ci 175423b3eb3cSopenharmony_ci PromptDialogAttr promptDialogAttr = { 175523b3eb3cSopenharmony_ci .title = asyncContext->titleString, 175623b3eb3cSopenharmony_ci .showInSubWindow = asyncContext->showInSubWindowBool, 175723b3eb3cSopenharmony_ci .isModal = asyncContext->isModalBool, 175823b3eb3cSopenharmony_ci }; 175923b3eb3cSopenharmony_ci#ifdef OHOS_STANDARD_SYSTEM 176023b3eb3cSopenharmony_ci if (SystemProperties::GetExtSurfaceEnabled() || !ContainerIsService()) { 176123b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 176223b3eb3cSopenharmony_ci if (delegate) { 176323b3eb3cSopenharmony_ci delegate->ShowActionMenu(promptDialogAttr, asyncContext->buttons, std::move(callBack)); 176423b3eb3cSopenharmony_ci } else { 176523b3eb3cSopenharmony_ci napi_value code = nullptr; 176623b3eb3cSopenharmony_ci std::string strCode = std::to_string(ERROR_CODE_INTERNAL_ERROR); 176723b3eb3cSopenharmony_ci napi_create_string_utf8(env, strCode.c_str(), strCode.length(), &code); 176823b3eb3cSopenharmony_ci napi_value msg = nullptr; 176923b3eb3cSopenharmony_ci std::string strMsg = ErrorToMessage(ERROR_CODE_INTERNAL_ERROR) + "Can not get delegate."; 177023b3eb3cSopenharmony_ci napi_create_string_utf8(env, strMsg.c_str(), strMsg.length(), &msg); 177123b3eb3cSopenharmony_ci napi_value error = nullptr; 177223b3eb3cSopenharmony_ci napi_create_error(env, code, msg, &error); 177323b3eb3cSopenharmony_ci 177423b3eb3cSopenharmony_ci if (asyncContext->deferred) { 177523b3eb3cSopenharmony_ci napi_reject_deferred(env, asyncContext->deferred, error); 177623b3eb3cSopenharmony_ci } else { 177723b3eb3cSopenharmony_ci napi_value ret1; 177823b3eb3cSopenharmony_ci napi_value callback = nullptr; 177923b3eb3cSopenharmony_ci napi_get_reference_value(env, asyncContext->callbackRef, &callback); 178023b3eb3cSopenharmony_ci napi_call_function(env, nullptr, callback, 1, &error, &ret1); 178123b3eb3cSopenharmony_ci napi_delete_reference(env, asyncContext->callbackRef); 178223b3eb3cSopenharmony_ci } 178323b3eb3cSopenharmony_ci } 178423b3eb3cSopenharmony_ci } else if (SubwindowManager::GetInstance() != nullptr) { 178523b3eb3cSopenharmony_ci SubwindowManager::GetInstance()->ShowActionMenu( 178623b3eb3cSopenharmony_ci asyncContext->titleString, asyncContext->buttons, std::move(callBack)); 178723b3eb3cSopenharmony_ci } 178823b3eb3cSopenharmony_ci#else 178923b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 179023b3eb3cSopenharmony_ci if (delegate) { 179123b3eb3cSopenharmony_ci delegate->ShowActionMenu(promptDialogAttr, asyncContext->buttons, std::move(callBack)); 179223b3eb3cSopenharmony_ci } else { 179323b3eb3cSopenharmony_ci napi_value code = nullptr; 179423b3eb3cSopenharmony_ci std::string strCode = std::to_string(ERROR_CODE_INTERNAL_ERROR); 179523b3eb3cSopenharmony_ci napi_create_string_utf8(env, strCode.c_str(), strCode.length(), &code); 179623b3eb3cSopenharmony_ci napi_value msg = nullptr; 179723b3eb3cSopenharmony_ci std::string strMsg = ErrorToMessage(ERROR_CODE_INTERNAL_ERROR) + "UI execution context not found."; 179823b3eb3cSopenharmony_ci napi_create_string_utf8(env, strMsg.c_str(), strMsg.length(), &msg); 179923b3eb3cSopenharmony_ci napi_value error = nullptr; 180023b3eb3cSopenharmony_ci napi_create_error(env, code, msg, &error); 180123b3eb3cSopenharmony_ci 180223b3eb3cSopenharmony_ci if (asyncContext->deferred) { 180323b3eb3cSopenharmony_ci napi_reject_deferred(env, asyncContext->deferred, error); 180423b3eb3cSopenharmony_ci } else { 180523b3eb3cSopenharmony_ci napi_value ret1; 180623b3eb3cSopenharmony_ci napi_value callback = nullptr; 180723b3eb3cSopenharmony_ci napi_get_reference_value(env, asyncContext->callbackRef, &callback); 180823b3eb3cSopenharmony_ci napi_call_function(env, nullptr, callback, 1, &error, &ret1); 180923b3eb3cSopenharmony_ci napi_delete_reference(env, asyncContext->callbackRef); 181023b3eb3cSopenharmony_ci } 181123b3eb3cSopenharmony_ci } 181223b3eb3cSopenharmony_ci#endif 181323b3eb3cSopenharmony_ci return result; 181423b3eb3cSopenharmony_ci} 181523b3eb3cSopenharmony_ci 181623b3eb3cSopenharmony_cinapi_value JSRemoveCustomDialog(napi_env env, napi_callback_info info) 181723b3eb3cSopenharmony_ci{ 181823b3eb3cSopenharmony_ci size_t argc = 1; 181923b3eb3cSopenharmony_ci napi_value argv = nullptr; 182023b3eb3cSopenharmony_ci napi_value thisVar = nullptr; 182123b3eb3cSopenharmony_ci void* data = nullptr; 182223b3eb3cSopenharmony_ci napi_get_cb_info(env, info, &argc, &argv, &thisVar, &data); 182323b3eb3cSopenharmony_ci int32_t instanceId = Container::CurrentIdSafely(); 182423b3eb3cSopenharmony_ci if (data) { 182523b3eb3cSopenharmony_ci int32_t* instanceIdPtr = reinterpret_cast<int32_t*>(data); 182623b3eb3cSopenharmony_ci instanceId = *instanceIdPtr; 182723b3eb3cSopenharmony_ci } 182823b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 182923b3eb3cSopenharmony_ci if (delegate) { 183023b3eb3cSopenharmony_ci delegate->RemoveCustomDialog(instanceId); 183123b3eb3cSopenharmony_ci } 183223b3eb3cSopenharmony_ci return nullptr; 183323b3eb3cSopenharmony_ci} 183423b3eb3cSopenharmony_ci 183523b3eb3cSopenharmony_civoid ParseDialogCallback(std::shared_ptr<PromptAsyncContext>& asyncContext, 183623b3eb3cSopenharmony_ci std::function<void(const int32_t& info, const int32_t& instanceId)>& onWillDismiss) 183723b3eb3cSopenharmony_ci{ 183823b3eb3cSopenharmony_ci onWillDismiss = [env = asyncContext->env, onWillDismissRef = asyncContext->onWillDismissRef] 183923b3eb3cSopenharmony_ci (const int32_t& info, const int32_t& instanceId) { 184023b3eb3cSopenharmony_ci if (onWillDismissRef) { 184123b3eb3cSopenharmony_ci napi_value onWillDismissFunc = nullptr; 184223b3eb3cSopenharmony_ci napi_value value = nullptr; 184323b3eb3cSopenharmony_ci napi_value funcValue = nullptr; 184423b3eb3cSopenharmony_ci napi_value paramObj = nullptr; 184523b3eb3cSopenharmony_ci napi_create_object(env, ¶mObj); 184623b3eb3cSopenharmony_ci 184723b3eb3cSopenharmony_ci napi_value id = nullptr; 184823b3eb3cSopenharmony_ci napi_create_int32(env, instanceId, &id); 184923b3eb3cSopenharmony_ci napi_create_function(env, "dismiss", strlen("dismiss"), JSRemoveCustomDialog, id, &funcValue); 185023b3eb3cSopenharmony_ci napi_set_named_property(env, paramObj, "dismiss", funcValue); 185123b3eb3cSopenharmony_ci 185223b3eb3cSopenharmony_ci napi_create_int32(env, info, &value); 185323b3eb3cSopenharmony_ci napi_set_named_property(env, paramObj, "reason", value); 185423b3eb3cSopenharmony_ci napi_get_reference_value(env, onWillDismissRef, &onWillDismissFunc); 185523b3eb3cSopenharmony_ci napi_call_function(env, nullptr, onWillDismissFunc, 1, ¶mObj, nullptr); 185623b3eb3cSopenharmony_ci } 185723b3eb3cSopenharmony_ci }; 185823b3eb3cSopenharmony_ci} 185923b3eb3cSopenharmony_ci 186023b3eb3cSopenharmony_ciPromptDialogAttr GetDialogLifeCycleCallback(napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext) 186123b3eb3cSopenharmony_ci{ 186223b3eb3cSopenharmony_ci auto onDidAppear = [env = asyncContext->env, onDidAppearRef = asyncContext->onDidAppearRef]() { 186323b3eb3cSopenharmony_ci if (onDidAppearRef) { 186423b3eb3cSopenharmony_ci napi_value onDidAppearFunc = nullptr; 186523b3eb3cSopenharmony_ci napi_get_reference_value(env, onDidAppearRef, &onDidAppearFunc); 186623b3eb3cSopenharmony_ci napi_call_function(env, nullptr, onDidAppearFunc, 0, nullptr, nullptr); 186723b3eb3cSopenharmony_ci napi_delete_reference(env, onDidAppearRef); 186823b3eb3cSopenharmony_ci } 186923b3eb3cSopenharmony_ci }; 187023b3eb3cSopenharmony_ci auto onDidDisappear = [env = asyncContext->env, onDidDisappearRef = asyncContext->onDidDisappearRef]() { 187123b3eb3cSopenharmony_ci if (onDidDisappearRef) { 187223b3eb3cSopenharmony_ci napi_value onDidDisappearFunc = nullptr; 187323b3eb3cSopenharmony_ci napi_get_reference_value(env, onDidDisappearRef, &onDidDisappearFunc); 187423b3eb3cSopenharmony_ci napi_call_function(env, nullptr, onDidDisappearFunc, 0, nullptr, nullptr); 187523b3eb3cSopenharmony_ci napi_delete_reference(env, onDidDisappearRef); 187623b3eb3cSopenharmony_ci } 187723b3eb3cSopenharmony_ci }; 187823b3eb3cSopenharmony_ci auto onWillAppear = [env = asyncContext->env, onWillAppearRef = asyncContext->onWillAppearRef]() { 187923b3eb3cSopenharmony_ci if (onWillAppearRef) { 188023b3eb3cSopenharmony_ci napi_value onWillAppearFunc = nullptr; 188123b3eb3cSopenharmony_ci napi_get_reference_value(env, onWillAppearRef, &onWillAppearFunc); 188223b3eb3cSopenharmony_ci napi_call_function(env, nullptr, onWillAppearFunc, 0, nullptr, nullptr); 188323b3eb3cSopenharmony_ci napi_delete_reference(env, onWillAppearRef); 188423b3eb3cSopenharmony_ci } 188523b3eb3cSopenharmony_ci }; 188623b3eb3cSopenharmony_ci auto onWillDisappear = [env = asyncContext->env, onWillDisappearRef = asyncContext->onWillDisappearRef]() { 188723b3eb3cSopenharmony_ci if (onWillDisappearRef) { 188823b3eb3cSopenharmony_ci napi_value onWillDisappearFunc = nullptr; 188923b3eb3cSopenharmony_ci napi_get_reference_value(env, onWillDisappearRef, &onWillDisappearFunc); 189023b3eb3cSopenharmony_ci napi_call_function(env, nullptr, onWillDisappearFunc, 0, nullptr, nullptr); 189123b3eb3cSopenharmony_ci napi_delete_reference(env, onWillDisappearRef); 189223b3eb3cSopenharmony_ci } 189323b3eb3cSopenharmony_ci }; 189423b3eb3cSopenharmony_ci PromptDialogAttr promptDialogAttr = { 189523b3eb3cSopenharmony_ci .onDidAppear = std::move(onDidAppear), 189623b3eb3cSopenharmony_ci .onDidDisappear = std::move(onDidDisappear), 189723b3eb3cSopenharmony_ci .onWillAppear = std::move(onWillAppear), 189823b3eb3cSopenharmony_ci .onWillDisappear = std::move(onWillDisappear) }; 189923b3eb3cSopenharmony_ci return promptDialogAttr; 190023b3eb3cSopenharmony_ci} 190123b3eb3cSopenharmony_ci 190223b3eb3cSopenharmony_civoid ParseBorderColorAndStyle(napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext, 190323b3eb3cSopenharmony_ci std::optional<NG::BorderWidthProperty>& borderWidthProps, std::optional<NG::BorderColorProperty>& borderColorProps, 190423b3eb3cSopenharmony_ci std::optional<NG::BorderStyleProperty>& borderStyleProps) 190523b3eb3cSopenharmony_ci{ 190623b3eb3cSopenharmony_ci if (borderWidthProps.has_value()) { 190723b3eb3cSopenharmony_ci borderColorProps = GetBorderColorProps(env, asyncContext); 190823b3eb3cSopenharmony_ci if (!borderColorProps.has_value()) { 190923b3eb3cSopenharmony_ci NG::BorderColorProperty borderColor; 191023b3eb3cSopenharmony_ci borderColor.SetColor(Color::BLACK); 191123b3eb3cSopenharmony_ci borderColorProps = borderColor; 191223b3eb3cSopenharmony_ci } 191323b3eb3cSopenharmony_ci borderStyleProps = GetBorderStyleProps(env, asyncContext); 191423b3eb3cSopenharmony_ci if (!borderStyleProps.has_value()) { 191523b3eb3cSopenharmony_ci borderStyleProps = NG::BorderStyleProperty( 191623b3eb3cSopenharmony_ci { BorderStyle::SOLID, BorderStyle::SOLID, BorderStyle::SOLID, BorderStyle::SOLID }); 191723b3eb3cSopenharmony_ci } 191823b3eb3cSopenharmony_ci } 191923b3eb3cSopenharmony_ci} 192023b3eb3cSopenharmony_ci 192123b3eb3cSopenharmony_ciRefPtr<NG::ChainedTransitionEffect> GetTransitionProps( 192223b3eb3cSopenharmony_ci napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext) 192323b3eb3cSopenharmony_ci{ 192423b3eb3cSopenharmony_ci RefPtr<NG::ChainedTransitionEffect> transitionEffect = nullptr; 192523b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 192623b3eb3cSopenharmony_ci if (delegate) { 192723b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 192823b3eb3cSopenharmony_ci napi_typeof(env, asyncContext->transitionApi, &valueType); 192923b3eb3cSopenharmony_ci if (valueType == napi_object) { 193023b3eb3cSopenharmony_ci transitionEffect = delegate->GetTransitionEffect(asyncContext->transitionApi); 193123b3eb3cSopenharmony_ci } 193223b3eb3cSopenharmony_ci } 193323b3eb3cSopenharmony_ci return transitionEffect; 193423b3eb3cSopenharmony_ci} 193523b3eb3cSopenharmony_ci 193623b3eb3cSopenharmony_cistd::function<void()> GetCustomBuilder(napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext) 193723b3eb3cSopenharmony_ci{ 193823b3eb3cSopenharmony_ci auto builder = [env = asyncContext->env, builderRef = asyncContext->builderRef]() { 193923b3eb3cSopenharmony_ci if (builderRef) { 194023b3eb3cSopenharmony_ci napi_value builderFunc = nullptr; 194123b3eb3cSopenharmony_ci napi_get_reference_value(env, builderRef, &builderFunc); 194223b3eb3cSopenharmony_ci napi_call_function(env, nullptr, builderFunc, 0, nullptr, nullptr); 194323b3eb3cSopenharmony_ci napi_delete_reference(env, builderRef); 194423b3eb3cSopenharmony_ci } 194523b3eb3cSopenharmony_ci }; 194623b3eb3cSopenharmony_ci return builder; 194723b3eb3cSopenharmony_ci} 194823b3eb3cSopenharmony_ci 194923b3eb3cSopenharmony_ciPromptDialogAttr GetPromptActionDialog(napi_env env, const std::shared_ptr<PromptAsyncContext>& asyncContext, 195023b3eb3cSopenharmony_ci std::function<void(const int32_t& info, const int32_t& instanceId)> onWillDismiss) 195123b3eb3cSopenharmony_ci{ 195223b3eb3cSopenharmony_ci std::optional<DialogAlignment> alignment; 195323b3eb3cSopenharmony_ci std::optional<DimensionOffset> offset; 195423b3eb3cSopenharmony_ci std::optional<DimensionRect> maskRect; 195523b3eb3cSopenharmony_ci std::optional<int32_t> backgroundBlurStyle; 195623b3eb3cSopenharmony_ci std::optional<HoverModeAreaType> hoverModeArea; 195723b3eb3cSopenharmony_ci GetNapiDialogProps(env, asyncContext, alignment, offset, maskRect); 195823b3eb3cSopenharmony_ci GetNapiBlurStyleAndHoverModeProps(env, asyncContext, backgroundBlurStyle, hoverModeArea); 195923b3eb3cSopenharmony_ci auto borderWidthProps = GetBorderWidthProps(env, asyncContext); 196023b3eb3cSopenharmony_ci std::optional<NG::BorderColorProperty> borderColorProps; 196123b3eb3cSopenharmony_ci std::optional<NG::BorderStyleProperty> borderStyleProps; 196223b3eb3cSopenharmony_ci ParseBorderColorAndStyle(env, asyncContext, borderWidthProps, borderColorProps, borderStyleProps); 196323b3eb3cSopenharmony_ci auto backgroundColorProps = GetColorProps(env, asyncContext->backgroundColorApi); 196423b3eb3cSopenharmony_ci auto builder = GetCustomBuilder(env, asyncContext); 196523b3eb3cSopenharmony_ci auto* nodePtr = reinterpret_cast<OHOS::Ace::NG::UINode*>(asyncContext->nativePtr); 196623b3eb3cSopenharmony_ci auto maskColorProps = GetColorProps(env, asyncContext->maskColorApi); 196723b3eb3cSopenharmony_ci auto transitionEffectProps = GetTransitionProps(env, asyncContext); 196823b3eb3cSopenharmony_ci PromptDialogAttr lifeCycleAttr = GetDialogLifeCycleCallback(env, asyncContext); 196923b3eb3cSopenharmony_ci int32_t mode = GetDialogKeyboardAvoidMode(env, asyncContext->keyboardAvoidModeApi); 197023b3eb3cSopenharmony_ci PromptDialogAttr promptDialogAttr = { .autoCancel = asyncContext->autoCancelBool, 197123b3eb3cSopenharmony_ci .showInSubWindow = asyncContext->showInSubWindowBool, 197223b3eb3cSopenharmony_ci .isModal = asyncContext->isModalBool, 197323b3eb3cSopenharmony_ci .customBuilder = std::move(builder), 197423b3eb3cSopenharmony_ci .customOnWillDismiss = std::move(onWillDismiss), 197523b3eb3cSopenharmony_ci .alignment = alignment, 197623b3eb3cSopenharmony_ci .offset = offset, 197723b3eb3cSopenharmony_ci .maskRect = maskRect, 197823b3eb3cSopenharmony_ci .backgroundColor = backgroundColorProps, 197923b3eb3cSopenharmony_ci .backgroundBlurStyle = backgroundBlurStyle, 198023b3eb3cSopenharmony_ci .borderWidth = borderWidthProps, 198123b3eb3cSopenharmony_ci .borderColor = borderColorProps, 198223b3eb3cSopenharmony_ci .borderStyle = borderStyleProps, 198323b3eb3cSopenharmony_ci .borderRadius = GetBorderRadiusProps(env, asyncContext), 198423b3eb3cSopenharmony_ci .shadow = GetShadowProps(env, asyncContext), 198523b3eb3cSopenharmony_ci .width = GetNapiDialogWidthProps(env, asyncContext), 198623b3eb3cSopenharmony_ci .height = GetNapiDialogHeightProps(env, asyncContext), 198723b3eb3cSopenharmony_ci .contentNode = AceType::WeakClaim(nodePtr), 198823b3eb3cSopenharmony_ci .maskColor = maskColorProps, 198923b3eb3cSopenharmony_ci .transitionEffect = transitionEffectProps, 199023b3eb3cSopenharmony_ci .onDidAppear = lifeCycleAttr.onDidAppear, 199123b3eb3cSopenharmony_ci .onDidDisappear = lifeCycleAttr.onDidDisappear, 199223b3eb3cSopenharmony_ci .onWillAppear = lifeCycleAttr.onWillAppear, 199323b3eb3cSopenharmony_ci .onWillDisappear = lifeCycleAttr.onWillDisappear, 199423b3eb3cSopenharmony_ci .keyboardAvoidMode = KEYBOARD_AVOID_MODE[mode], 199523b3eb3cSopenharmony_ci .enableHoverMode = asyncContext->enableHoverModeBool, 199623b3eb3cSopenharmony_ci .hoverModeArea = hoverModeArea }; 199723b3eb3cSopenharmony_ci return promptDialogAttr; 199823b3eb3cSopenharmony_ci} 199923b3eb3cSopenharmony_ci 200023b3eb3cSopenharmony_cistd::string GetErrorMsg(int32_t errorCode) 200123b3eb3cSopenharmony_ci{ 200223b3eb3cSopenharmony_ci std::string strMsg; 200323b3eb3cSopenharmony_ci if (errorCode == ERROR_CODE_DIALOG_CONTENT_ERROR) { 200423b3eb3cSopenharmony_ci strMsg = ErrorToMessage(ERROR_CODE_DIALOG_CONTENT_ERROR) + "The ComponentContent is incorrect."; 200523b3eb3cSopenharmony_ci } else if (errorCode == ERROR_CODE_DIALOG_CONTENT_ALREADY_EXIST) { 200623b3eb3cSopenharmony_ci strMsg = ErrorToMessage(ERROR_CODE_DIALOG_CONTENT_ALREADY_EXIST) + 200723b3eb3cSopenharmony_ci "The ComponentContent has already been opened."; 200823b3eb3cSopenharmony_ci } else if (errorCode == ERROR_CODE_DIALOG_CONTENT_NOT_FOUND) { 200923b3eb3cSopenharmony_ci strMsg = ErrorToMessage(ERROR_CODE_DIALOG_CONTENT_NOT_FOUND) + "The ComponentContent cannot be found."; 201023b3eb3cSopenharmony_ci } else { 201123b3eb3cSopenharmony_ci strMsg = ErrorToMessage(ERROR_CODE_INTERNAL_ERROR) + "Build custom dialog failed."; 201223b3eb3cSopenharmony_ci } 201323b3eb3cSopenharmony_ci return strMsg; 201423b3eb3cSopenharmony_ci} 201523b3eb3cSopenharmony_ci 201623b3eb3cSopenharmony_cistd::string GetErrorCode(int32_t errorCode) 201723b3eb3cSopenharmony_ci{ 201823b3eb3cSopenharmony_ci std::string strCode; 201923b3eb3cSopenharmony_ci if (errorCode == ERROR_CODE_DIALOG_CONTENT_ERROR) { 202023b3eb3cSopenharmony_ci strCode = std::to_string(ERROR_CODE_DIALOG_CONTENT_ERROR); 202123b3eb3cSopenharmony_ci } else if (errorCode == ERROR_CODE_DIALOG_CONTENT_ALREADY_EXIST) { 202223b3eb3cSopenharmony_ci strCode = std::to_string(ERROR_CODE_DIALOG_CONTENT_ALREADY_EXIST); 202323b3eb3cSopenharmony_ci } else if (errorCode == ERROR_CODE_DIALOG_CONTENT_NOT_FOUND) { 202423b3eb3cSopenharmony_ci strCode = std::to_string(ERROR_CODE_DIALOG_CONTENT_NOT_FOUND); 202523b3eb3cSopenharmony_ci } else { 202623b3eb3cSopenharmony_ci strCode = std::to_string(ERROR_CODE_INTERNAL_ERROR); 202723b3eb3cSopenharmony_ci } 202823b3eb3cSopenharmony_ci return strCode; 202923b3eb3cSopenharmony_ci} 203023b3eb3cSopenharmony_ci 203123b3eb3cSopenharmony_civoid ParseCustomDialogContentCallback(std::shared_ptr<PromptAsyncContext>& asyncContext, 203223b3eb3cSopenharmony_ci std::function<void(int32_t)>& callBack) 203323b3eb3cSopenharmony_ci{ 203423b3eb3cSopenharmony_ci callBack = [asyncContext](int32_t errorCode) mutable { 203523b3eb3cSopenharmony_ci if (!asyncContext) { 203623b3eb3cSopenharmony_ci return; 203723b3eb3cSopenharmony_ci } 203823b3eb3cSopenharmony_ci auto container = AceEngine::Get().GetContainer(asyncContext->instanceId); 203923b3eb3cSopenharmony_ci if (!container) { 204023b3eb3cSopenharmony_ci return; 204123b3eb3cSopenharmony_ci } 204223b3eb3cSopenharmony_ci auto taskExecutor = container->GetTaskExecutor(); 204323b3eb3cSopenharmony_ci if (!taskExecutor) { 204423b3eb3cSopenharmony_ci return; 204523b3eb3cSopenharmony_ci } 204623b3eb3cSopenharmony_ci taskExecutor->PostTask( 204723b3eb3cSopenharmony_ci [asyncContext, errorCode]() { 204823b3eb3cSopenharmony_ci if (asyncContext == nullptr || !asyncContext->valid) { 204923b3eb3cSopenharmony_ci return; 205023b3eb3cSopenharmony_ci } 205123b3eb3cSopenharmony_ci napi_handle_scope scope = nullptr; 205223b3eb3cSopenharmony_ci napi_open_handle_scope(asyncContext->env, &scope); 205323b3eb3cSopenharmony_ci if (scope == nullptr) { 205423b3eb3cSopenharmony_ci return; 205523b3eb3cSopenharmony_ci } 205623b3eb3cSopenharmony_ci if (!asyncContext->deferred) { 205723b3eb3cSopenharmony_ci return; 205823b3eb3cSopenharmony_ci } 205923b3eb3cSopenharmony_ci if (errorCode == ERROR_CODE_NO_ERROR) { 206023b3eb3cSopenharmony_ci napi_value result = nullptr; 206123b3eb3cSopenharmony_ci napi_get_undefined(asyncContext->env, &result); 206223b3eb3cSopenharmony_ci napi_resolve_deferred(asyncContext->env, asyncContext->deferred, result); 206323b3eb3cSopenharmony_ci } else { 206423b3eb3cSopenharmony_ci std::string strMsg = GetErrorMsg(errorCode); 206523b3eb3cSopenharmony_ci std::string strCode = GetErrorCode(errorCode); 206623b3eb3cSopenharmony_ci napi_value code = nullptr; 206723b3eb3cSopenharmony_ci napi_create_string_utf8(asyncContext->env, strCode.c_str(), strCode.length(), &code); 206823b3eb3cSopenharmony_ci napi_value msg = nullptr; 206923b3eb3cSopenharmony_ci napi_create_string_utf8(asyncContext->env, strMsg.c_str(), strMsg.length(), &msg); 207023b3eb3cSopenharmony_ci napi_value error = nullptr; 207123b3eb3cSopenharmony_ci napi_create_error(asyncContext->env, code, msg, &error); 207223b3eb3cSopenharmony_ci napi_reject_deferred(asyncContext->env, asyncContext->deferred, error); 207323b3eb3cSopenharmony_ci } 207423b3eb3cSopenharmony_ci napi_close_handle_scope(asyncContext->env, scope); 207523b3eb3cSopenharmony_ci }, 207623b3eb3cSopenharmony_ci TaskExecutor::TaskType::JS, "ArkUIDialogParseCustomDialogContentCallback"); 207723b3eb3cSopenharmony_ci asyncContext = nullptr; 207823b3eb3cSopenharmony_ci }; 207923b3eb3cSopenharmony_ci} 208023b3eb3cSopenharmony_ci 208123b3eb3cSopenharmony_civoid ParseCustomDialogIdCallback(std::shared_ptr<PromptAsyncContext>& asyncContext, 208223b3eb3cSopenharmony_ci std::function<void(int32_t)>& callBack) 208323b3eb3cSopenharmony_ci{ 208423b3eb3cSopenharmony_ci callBack = [asyncContext](int32_t dialogId) mutable { 208523b3eb3cSopenharmony_ci if (!asyncContext) { 208623b3eb3cSopenharmony_ci return; 208723b3eb3cSopenharmony_ci } 208823b3eb3cSopenharmony_ci auto container = AceEngine::Get().GetContainer(asyncContext->instanceId); 208923b3eb3cSopenharmony_ci if (!container) { 209023b3eb3cSopenharmony_ci return; 209123b3eb3cSopenharmony_ci } 209223b3eb3cSopenharmony_ci auto taskExecutor = container->GetTaskExecutor(); 209323b3eb3cSopenharmony_ci if (!taskExecutor) { 209423b3eb3cSopenharmony_ci return; 209523b3eb3cSopenharmony_ci } 209623b3eb3cSopenharmony_ci taskExecutor->PostTask( 209723b3eb3cSopenharmony_ci [asyncContext, dialogId]() { 209823b3eb3cSopenharmony_ci if (asyncContext == nullptr || !asyncContext->valid) { 209923b3eb3cSopenharmony_ci return; 210023b3eb3cSopenharmony_ci } 210123b3eb3cSopenharmony_ci 210223b3eb3cSopenharmony_ci napi_handle_scope scope = nullptr; 210323b3eb3cSopenharmony_ci napi_open_handle_scope(asyncContext->env, &scope); 210423b3eb3cSopenharmony_ci if (scope == nullptr) { 210523b3eb3cSopenharmony_ci return; 210623b3eb3cSopenharmony_ci } 210723b3eb3cSopenharmony_ci 210823b3eb3cSopenharmony_ci napi_value ret = nullptr; 210923b3eb3cSopenharmony_ci if (!asyncContext->deferred) { 211023b3eb3cSopenharmony_ci return; 211123b3eb3cSopenharmony_ci } 211223b3eb3cSopenharmony_ci if (dialogId > 0) { 211323b3eb3cSopenharmony_ci napi_create_int32(asyncContext->env, dialogId, &ret); 211423b3eb3cSopenharmony_ci napi_resolve_deferred(asyncContext->env, asyncContext->deferred, ret); 211523b3eb3cSopenharmony_ci } else { 211623b3eb3cSopenharmony_ci std::string strMsg = GetErrorMsg(dialogId); 211723b3eb3cSopenharmony_ci std::string strCode = GetErrorCode(dialogId); 211823b3eb3cSopenharmony_ci napi_value code = nullptr; 211923b3eb3cSopenharmony_ci napi_create_string_utf8(asyncContext->env, strCode.c_str(), strCode.length(), &code); 212023b3eb3cSopenharmony_ci napi_value msg = nullptr; 212123b3eb3cSopenharmony_ci napi_create_string_utf8(asyncContext->env, strMsg.c_str(), strMsg.length(), &msg); 212223b3eb3cSopenharmony_ci napi_value error = nullptr; 212323b3eb3cSopenharmony_ci napi_create_error(asyncContext->env, code, msg, &error); 212423b3eb3cSopenharmony_ci napi_reject_deferred(asyncContext->env, asyncContext->deferred, error); 212523b3eb3cSopenharmony_ci } 212623b3eb3cSopenharmony_ci napi_close_handle_scope(asyncContext->env, scope); 212723b3eb3cSopenharmony_ci }, 212823b3eb3cSopenharmony_ci TaskExecutor::TaskType::JS, "ArkUIDialogParseCustomDialogIdCallback"); 212923b3eb3cSopenharmony_ci asyncContext = nullptr; 213023b3eb3cSopenharmony_ci }; 213123b3eb3cSopenharmony_ci} 213223b3eb3cSopenharmony_ci 213323b3eb3cSopenharmony_civoid OpenCustomDialog(napi_env env, std::shared_ptr<PromptAsyncContext>& asyncContext, 213423b3eb3cSopenharmony_ci PromptDialogAttr& promptDialogAttr, std::function<void(int32_t)>& openCallback) 213523b3eb3cSopenharmony_ci{ 213623b3eb3cSopenharmony_ci#ifdef OHOS_STANDARD_SYSTEM 213723b3eb3cSopenharmony_ci // NG 213823b3eb3cSopenharmony_ci if (SystemProperties::GetExtSurfaceEnabled() || !ContainerIsService()) { 213923b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 214023b3eb3cSopenharmony_ci if (delegate) { 214123b3eb3cSopenharmony_ci delegate->OpenCustomDialog(promptDialogAttr, std::move(openCallback)); 214223b3eb3cSopenharmony_ci } else { 214323b3eb3cSopenharmony_ci // throw internal error 214423b3eb3cSopenharmony_ci std::string strMsg = ErrorToMessage(ERROR_CODE_INTERNAL_ERROR) + "Can not get delegate."; 214523b3eb3cSopenharmony_ci JSPromptThrowInterError(env, asyncContext, strMsg); 214623b3eb3cSopenharmony_ci } 214723b3eb3cSopenharmony_ci } else if (SubwindowManager::GetInstance() != nullptr) { 214823b3eb3cSopenharmony_ci SubwindowManager::GetInstance()->OpenCustomDialog(promptDialogAttr, std::move(openCallback)); 214923b3eb3cSopenharmony_ci } 215023b3eb3cSopenharmony_ci#else 215123b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 215223b3eb3cSopenharmony_ci if (delegate) { 215323b3eb3cSopenharmony_ci delegate->OpenCustomDialog(promptDialogAttr, std::move(openCallback)); 215423b3eb3cSopenharmony_ci } else { 215523b3eb3cSopenharmony_ci // throw internal error 215623b3eb3cSopenharmony_ci std::string strMsg = ErrorToMessage(ERROR_CODE_INTERNAL_ERROR) + "UI execution context not found."; 215723b3eb3cSopenharmony_ci JSPromptThrowInterError(env, asyncContext, strMsg); 215823b3eb3cSopenharmony_ci } 215923b3eb3cSopenharmony_ci#endif 216023b3eb3cSopenharmony_ci} 216123b3eb3cSopenharmony_ci 216223b3eb3cSopenharmony_cinapi_value JSPromptOpenCustomDialog(napi_env env, napi_callback_info info) 216323b3eb3cSopenharmony_ci{ 216423b3eb3cSopenharmony_ci size_t argc = 2; 216523b3eb3cSopenharmony_ci napi_value argv[2] = { nullptr }; 216623b3eb3cSopenharmony_ci napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); 216723b3eb3cSopenharmony_ci if (argc < 1) { 216823b3eb3cSopenharmony_ci NapiThrow( 216923b3eb3cSopenharmony_ci env, "The number of parameters must be greater than or equal to 1.", ERROR_CODE_PARAM_INVALID); 217023b3eb3cSopenharmony_ci return nullptr; 217123b3eb3cSopenharmony_ci } 217223b3eb3cSopenharmony_ci 217323b3eb3cSopenharmony_ci auto asyncContext = std::make_shared<PromptAsyncContext>(); 217423b3eb3cSopenharmony_ci asyncContext->env = env; 217523b3eb3cSopenharmony_ci asyncContext->instanceId = Container::CurrentIdSafely(); 217623b3eb3cSopenharmony_ci bool parseOK = JSPromptParseParam(env, argc, argv, asyncContext); 217723b3eb3cSopenharmony_ci if (!parseOK) { 217823b3eb3cSopenharmony_ci return nullptr; 217923b3eb3cSopenharmony_ci } 218023b3eb3cSopenharmony_ci napi_value result = nullptr; 218123b3eb3cSopenharmony_ci napi_create_promise(env, &asyncContext->deferred, &result); 218223b3eb3cSopenharmony_ci 218323b3eb3cSopenharmony_ci std::function<void(const int32_t& info, const int32_t& instanceId)> onWillDismiss = nullptr; 218423b3eb3cSopenharmony_ci if (asyncContext->onWillDismissRef) { 218523b3eb3cSopenharmony_ci ParseDialogCallback(asyncContext, onWillDismiss); 218623b3eb3cSopenharmony_ci } 218723b3eb3cSopenharmony_ci std::function<void(int32_t)> openCallback = nullptr; 218823b3eb3cSopenharmony_ci PromptDialogAttr promptDialogAttr = GetPromptActionDialog(env, asyncContext, onWillDismiss); 218923b3eb3cSopenharmony_ci if (!asyncContext->builderRef) { 219023b3eb3cSopenharmony_ci ParseCustomDialogContentCallback(asyncContext, openCallback); 219123b3eb3cSopenharmony_ci promptDialogAttr.customStyle = true; 219223b3eb3cSopenharmony_ci promptDialogAttr.customBuilder = nullptr; 219323b3eb3cSopenharmony_ci } else { 219423b3eb3cSopenharmony_ci ParseCustomDialogIdCallback(asyncContext, openCallback); 219523b3eb3cSopenharmony_ci } 219623b3eb3cSopenharmony_ci 219723b3eb3cSopenharmony_ci OpenCustomDialog(env, asyncContext, promptDialogAttr, openCallback); 219823b3eb3cSopenharmony_ci 219923b3eb3cSopenharmony_ci return result; 220023b3eb3cSopenharmony_ci} 220123b3eb3cSopenharmony_ci 220223b3eb3cSopenharmony_civoid CloseCustomDialog(napi_env env, std::shared_ptr<PromptAsyncContext>& asyncContext, bool useDialogId, 220323b3eb3cSopenharmony_ci int32_t dialogId, const WeakPtr<NG::UINode>& nodeWk, std::function<void(int32_t)>& contentCallback) 220423b3eb3cSopenharmony_ci{ 220523b3eb3cSopenharmony_ci#ifdef OHOS_STANDARD_SYSTEM 220623b3eb3cSopenharmony_ci // NG 220723b3eb3cSopenharmony_ci if (SystemProperties::GetExtSurfaceEnabled() || !ContainerIsService()) { 220823b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 220923b3eb3cSopenharmony_ci if (delegate) { 221023b3eb3cSopenharmony_ci if (useDialogId) { 221123b3eb3cSopenharmony_ci delegate->CloseCustomDialog(dialogId); 221223b3eb3cSopenharmony_ci } else { 221323b3eb3cSopenharmony_ci delegate->CloseCustomDialog(nodeWk, std::move(contentCallback)); 221423b3eb3cSopenharmony_ci } 221523b3eb3cSopenharmony_ci } else { 221623b3eb3cSopenharmony_ci // throw internal error 221723b3eb3cSopenharmony_ci napi_create_promise(env, &asyncContext->deferred, nullptr); 221823b3eb3cSopenharmony_ci std::string strMsg = ErrorToMessage(ERROR_CODE_INTERNAL_ERROR) + "Can not get delegate."; 221923b3eb3cSopenharmony_ci JSPromptThrowInterError(env, asyncContext, strMsg); 222023b3eb3cSopenharmony_ci } 222123b3eb3cSopenharmony_ci } else if (SubwindowManager::GetInstance() != nullptr) { 222223b3eb3cSopenharmony_ci if (useDialogId) { 222323b3eb3cSopenharmony_ci SubwindowManager::GetInstance()->CloseCustomDialogNG(dialogId); 222423b3eb3cSopenharmony_ci } else { 222523b3eb3cSopenharmony_ci SubwindowManager::GetInstance()->CloseCustomDialogNG(nodeWk, std::move(contentCallback)); 222623b3eb3cSopenharmony_ci } 222723b3eb3cSopenharmony_ci } 222823b3eb3cSopenharmony_ci#else 222923b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 223023b3eb3cSopenharmony_ci if (delegate) { 223123b3eb3cSopenharmony_ci if (useDialogId) { 223223b3eb3cSopenharmony_ci delegate->CloseCustomDialog(dialogId); 223323b3eb3cSopenharmony_ci } else { 223423b3eb3cSopenharmony_ci delegate->CloseCustomDialog(nodeWk, std::move(contentCallback)); 223523b3eb3cSopenharmony_ci } 223623b3eb3cSopenharmony_ci } else { 223723b3eb3cSopenharmony_ci // throw internal error 223823b3eb3cSopenharmony_ci napi_create_promise(env, &asyncContext->deferred, nullptr); 223923b3eb3cSopenharmony_ci std::string strMsg = ErrorToMessage(ERROR_CODE_INTERNAL_ERROR) + "UI execution context not found."; 224023b3eb3cSopenharmony_ci JSPromptThrowInterError(env, asyncContext, strMsg); 224123b3eb3cSopenharmony_ci } 224223b3eb3cSopenharmony_ci#endif 224323b3eb3cSopenharmony_ci} 224423b3eb3cSopenharmony_ci 224523b3eb3cSopenharmony_cinapi_value JSPromptCloseCustomDialog(napi_env env, napi_callback_info info) 224623b3eb3cSopenharmony_ci{ 224723b3eb3cSopenharmony_ci size_t argc = 1; 224823b3eb3cSopenharmony_ci napi_value argv[1] = { 0 }; 224923b3eb3cSopenharmony_ci int32_t dialogId = -1; 225023b3eb3cSopenharmony_ci WeakPtr<NG::UINode> nodeWk; 225123b3eb3cSopenharmony_ci bool useDialogId = true; 225223b3eb3cSopenharmony_ci std::function<void(int32_t)> contentCallback = nullptr; 225323b3eb3cSopenharmony_ci napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); 225423b3eb3cSopenharmony_ci auto asyncContext = std::make_shared<PromptAsyncContext>(); 225523b3eb3cSopenharmony_ci asyncContext->env = env; 225623b3eb3cSopenharmony_ci asyncContext->instanceId = Container::CurrentIdSafely(); 225723b3eb3cSopenharmony_ci napi_value ret = nullptr; 225823b3eb3cSopenharmony_ci if (argc > 1) { 225923b3eb3cSopenharmony_ci NapiThrow(env, "The number of parameters is incorrect.", ERROR_CODE_PARAM_INVALID); 226023b3eb3cSopenharmony_ci return nullptr; 226123b3eb3cSopenharmony_ci } else if (argc == 0) { 226223b3eb3cSopenharmony_ci dialogId = -1; 226323b3eb3cSopenharmony_ci } else { 226423b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 226523b3eb3cSopenharmony_ci napi_typeof(env, argv[0], &valueType); 226623b3eb3cSopenharmony_ci if (valueType == napi_number) { 226723b3eb3cSopenharmony_ci napi_get_value_int32(env, argv[0], &dialogId); 226823b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 226923b3eb3cSopenharmony_ci napi_value frameNodePtr = nullptr; 227023b3eb3cSopenharmony_ci auto result = napi_get_named_property(env, argv[0], "nodePtr_", &frameNodePtr); 227123b3eb3cSopenharmony_ci if (result != napi_ok) { 227223b3eb3cSopenharmony_ci NapiThrow(env, "The type of parameters is incorrect.", ERROR_CODE_PARAM_INVALID); 227323b3eb3cSopenharmony_ci return nullptr; 227423b3eb3cSopenharmony_ci } 227523b3eb3cSopenharmony_ci void* nativePtr = nullptr; 227623b3eb3cSopenharmony_ci result = napi_get_value_external(env, frameNodePtr, &nativePtr); 227723b3eb3cSopenharmony_ci if (result != napi_ok) { 227823b3eb3cSopenharmony_ci NapiThrow(env, "The type of parameters is incorrect.", ERROR_CODE_PARAM_INVALID); 227923b3eb3cSopenharmony_ci return nullptr; 228023b3eb3cSopenharmony_ci } 228123b3eb3cSopenharmony_ci auto* uiNodePtr = reinterpret_cast<OHOS::Ace::NG::UINode*>(nativePtr); 228223b3eb3cSopenharmony_ci nodeWk = AceType::WeakClaim(uiNodePtr); 228323b3eb3cSopenharmony_ci useDialogId = false; 228423b3eb3cSopenharmony_ci napi_create_promise(env, &asyncContext->deferred, &ret); 228523b3eb3cSopenharmony_ci ParseCustomDialogContentCallback(asyncContext, contentCallback); 228623b3eb3cSopenharmony_ci } else { 228723b3eb3cSopenharmony_ci NapiThrow(env, "The type of parameters is incorrect.", ERROR_CODE_PARAM_INVALID); 228823b3eb3cSopenharmony_ci return nullptr; 228923b3eb3cSopenharmony_ci } 229023b3eb3cSopenharmony_ci } 229123b3eb3cSopenharmony_ci 229223b3eb3cSopenharmony_ci CloseCustomDialog(env, asyncContext, useDialogId, dialogId, nodeWk, contentCallback); 229323b3eb3cSopenharmony_ci 229423b3eb3cSopenharmony_ci return ret; 229523b3eb3cSopenharmony_ci} 229623b3eb3cSopenharmony_ci 229723b3eb3cSopenharmony_civoid UpdateCustomDialog(napi_env env, std::shared_ptr<PromptAsyncContext>& asyncContext, 229823b3eb3cSopenharmony_ci PromptDialogAttr& promptDialogAttr, const WeakPtr<NG::UINode>& nodeWk, 229923b3eb3cSopenharmony_ci std::function<void(int32_t)>& contentCallback) 230023b3eb3cSopenharmony_ci{ 230123b3eb3cSopenharmony_ci#ifdef OHOS_STANDARD_SYSTEM 230223b3eb3cSopenharmony_ci // NG 230323b3eb3cSopenharmony_ci if (SystemProperties::GetExtSurfaceEnabled() || !ContainerIsService()) { 230423b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 230523b3eb3cSopenharmony_ci if (delegate) { 230623b3eb3cSopenharmony_ci delegate->UpdateCustomDialog(nodeWk, promptDialogAttr, std::move(contentCallback)); 230723b3eb3cSopenharmony_ci } else { 230823b3eb3cSopenharmony_ci // throw internal error 230923b3eb3cSopenharmony_ci napi_create_promise(env, &asyncContext->deferred, nullptr); 231023b3eb3cSopenharmony_ci std::string strMsg = ErrorToMessage(ERROR_CODE_INTERNAL_ERROR) + "Can not get delegate."; 231123b3eb3cSopenharmony_ci JSPromptThrowInterError(env, asyncContext, strMsg); 231223b3eb3cSopenharmony_ci } 231323b3eb3cSopenharmony_ci } else if (SubwindowManager::GetInstance() != nullptr) { 231423b3eb3cSopenharmony_ci SubwindowManager::GetInstance()->UpdateCustomDialogNG(nodeWk, promptDialogAttr, std::move(contentCallback)); 231523b3eb3cSopenharmony_ci } 231623b3eb3cSopenharmony_ci#else 231723b3eb3cSopenharmony_ci auto delegate = EngineHelper::GetCurrentDelegateSafely(); 231823b3eb3cSopenharmony_ci if (delegate) { 231923b3eb3cSopenharmony_ci delegate->UpdateCustomDialog(nodeWk, promptDialogAttr, std::move(contentCallback)); 232023b3eb3cSopenharmony_ci } else { 232123b3eb3cSopenharmony_ci // throw internal error 232223b3eb3cSopenharmony_ci napi_create_promise(env, &asyncContext->deferred, nullptr); 232323b3eb3cSopenharmony_ci std::string strMsg = ErrorToMessage(ERROR_CODE_INTERNAL_ERROR) + "UI execution context not found."; 232423b3eb3cSopenharmony_ci JSPromptThrowInterError(env, asyncContext, strMsg); 232523b3eb3cSopenharmony_ci } 232623b3eb3cSopenharmony_ci#endif 232723b3eb3cSopenharmony_ci} 232823b3eb3cSopenharmony_ci 232923b3eb3cSopenharmony_cinapi_value JSPromptUpdateCustomDialog(napi_env env, napi_callback_info info) 233023b3eb3cSopenharmony_ci{ 233123b3eb3cSopenharmony_ci size_t argc = CUSTOM_DIALOG_PARAM_NUM; 233223b3eb3cSopenharmony_ci napi_value argv[CUSTOM_DIALOG_PARAM_NUM] = { nullptr }; 233323b3eb3cSopenharmony_ci WeakPtr<NG::UINode> nodeWk; 233423b3eb3cSopenharmony_ci napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); 233523b3eb3cSopenharmony_ci if (argc != CUSTOM_DIALOG_PARAM_NUM) { 233623b3eb3cSopenharmony_ci NapiThrow(env, "The number of parameters is incorrect.", ERROR_CODE_PARAM_INVALID); 233723b3eb3cSopenharmony_ci return nullptr; 233823b3eb3cSopenharmony_ci } 233923b3eb3cSopenharmony_ci auto asyncContext = std::make_shared<PromptAsyncContext>(); 234023b3eb3cSopenharmony_ci asyncContext->env = env; 234123b3eb3cSopenharmony_ci asyncContext->instanceId = Container::CurrentIdSafely(); 234223b3eb3cSopenharmony_ci napi_value ret = nullptr; 234323b3eb3cSopenharmony_ci 234423b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 234523b3eb3cSopenharmony_ci napi_typeof(env, argv[0], &valueType); 234623b3eb3cSopenharmony_ci if (valueType == napi_object) { 234723b3eb3cSopenharmony_ci napi_value frameNodePtr = nullptr; 234823b3eb3cSopenharmony_ci auto result = napi_get_named_property(env, argv[0], "nodePtr_", &frameNodePtr); 234923b3eb3cSopenharmony_ci if (result != napi_ok) { 235023b3eb3cSopenharmony_ci NapiThrow(env, "The type of parameters is incorrect.", ERROR_CODE_PARAM_INVALID); 235123b3eb3cSopenharmony_ci return nullptr; 235223b3eb3cSopenharmony_ci } 235323b3eb3cSopenharmony_ci void* nativePtr = nullptr; 235423b3eb3cSopenharmony_ci result = napi_get_value_external(env, frameNodePtr, &nativePtr); 235523b3eb3cSopenharmony_ci if (result != napi_ok) { 235623b3eb3cSopenharmony_ci NapiThrow(env, "The type of parameters is incorrect.", ERROR_CODE_PARAM_INVALID); 235723b3eb3cSopenharmony_ci return nullptr; 235823b3eb3cSopenharmony_ci } 235923b3eb3cSopenharmony_ci auto* uiNodePtr = reinterpret_cast<OHOS::Ace::NG::UINode*>(nativePtr); 236023b3eb3cSopenharmony_ci nodeWk = AceType::WeakClaim(uiNodePtr); 236123b3eb3cSopenharmony_ci } else { 236223b3eb3cSopenharmony_ci NapiThrow(env, "The type of parameters is incorrect.", ERROR_CODE_PARAM_INVALID); 236323b3eb3cSopenharmony_ci return nullptr; 236423b3eb3cSopenharmony_ci } 236523b3eb3cSopenharmony_ci 236623b3eb3cSopenharmony_ci napi_typeof(env, argv[1], &valueType); 236723b3eb3cSopenharmony_ci if (valueType != napi_object) { 236823b3eb3cSopenharmony_ci NapiThrow(env, "The type of parameters is incorrect.", ERROR_CODE_PARAM_INVALID); 236923b3eb3cSopenharmony_ci return nullptr; 237023b3eb3cSopenharmony_ci } 237123b3eb3cSopenharmony_ci GetNapiNamedProperties(env, argv, 1, asyncContext); 237223b3eb3cSopenharmony_ci 237323b3eb3cSopenharmony_ci napi_create_promise(env, &asyncContext->deferred, &ret); 237423b3eb3cSopenharmony_ci std::function<void(int32_t)> contentCallback = nullptr; 237523b3eb3cSopenharmony_ci ParseCustomDialogContentCallback(asyncContext, contentCallback); 237623b3eb3cSopenharmony_ci PromptDialogAttr promptDialogAttr = GetPromptActionDialog(env, asyncContext, nullptr); 237723b3eb3cSopenharmony_ci 237823b3eb3cSopenharmony_ci UpdateCustomDialog(env, asyncContext, promptDialogAttr, nodeWk, contentCallback); 237923b3eb3cSopenharmony_ci 238023b3eb3cSopenharmony_ci return ret; 238123b3eb3cSopenharmony_ci} 238223b3eb3cSopenharmony_ci 238323b3eb3cSopenharmony_ci} // namespace OHOS::Ace::Napi 2384