1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "frameworks/bridge/declarative_frontend/jsview/text_menu/js_text_menu.h"
17
18 #include "base/log/log_wrapper.h"
19 #include "bridge/common/utils/engine_helper.h"
20 #include "core/common/container.h"
21
22 namespace OHOS::Ace::Framework {
23
SetMenuOptions(const JSCallbackInfo & info)24 void JSTextMenu::SetMenuOptions(const JSCallbackInfo& info)
25 {
26 TAG_LOGI(AceLogTag::ACE_SELECT_OVERLAY, "TextMenu SetMenuOptions enter");
27 auto scopedDelegate = EngineHelper::GetCurrentDelegateSafely();
28 if (!scopedDelegate) {
29 // this case usually means there is no foreground container, need to figure out the reason.
30 TAG_LOGE(AceLogTag::ACE_SELECT_OVERLAY, "scopedDelegate is null, please check");
31 return;
32 }
33 if (info.Length() < 1 || !info[0]->IsObject()) {
34 info.SetReturnValue(info.This());
35 return;
36 }
37 NG::TextMenuOptions textMenuOptions;
38 auto paramObject = JSRef<JSObject>::Cast(info[0]);
39 auto showModeJsVal = paramObject->GetProperty("showMode");
40 if (showModeJsVal->IsNull() || showModeJsVal->IsUndefined() || !showModeJsVal->IsNumber()) {
41 textMenuOptions.showMode = NG::TextMenuShowMode::DEFAULT;
42 } else {
43 textMenuOptions.showMode = NG::CastToTextMenuShowMode(showModeJsVal->ToNumber<int32_t>());
44 }
45 auto container = Container::CurrentSafely();
46 if (!container) {
47 info.SetReturnValue(info.This());
48 return;
49 }
50 auto pipelineContext = AceType::DynamicCast<NG::PipelineContext>(container->GetPipelineContext());
51 if (!pipelineContext) {
52 info.SetReturnValue(info.This());
53 return;
54 }
55 auto selectOverlayManager = pipelineContext->GetSelectOverlayManager();
56 if (selectOverlayManager) {
57 selectOverlayManager->SetTextMenuOptions(textMenuOptions);
58 }
59 info.SetReturnValue(info.This());
60 }
61
JSBind(BindingTarget globalObj)62 void JSTextMenu::JSBind(BindingTarget globalObj)
63 {
64 JSClass<JSTextMenu>::Declare("TextMenu");
65 JSClass<JSTextMenu>::StaticMethod("setMenuOptions", &JSTextMenu::SetMenuOptions);
66 JSClass<JSTextMenu>::InheritAndBind<JSViewAbstract>(globalObj);
67 }
68
69 } // namespace OHOS::Ace::Framework