1 /* 2 * Copyright (c) 2022 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/menu/js_context_menu.h" 17 18 #include "base/subwindow/subwindow.h" 19 #include "base/subwindow/subwindow_manager.h" 20 #include "bridge/common/utils/engine_helper.h" 21 #include "core/common/container.h" 22 23 namespace OHOS::Ace::Framework { 24 Close(const JSCallbackInfo & args)25void JSContextMenu::Close(const JSCallbackInfo& args) 26 { 27 auto scopedDelegate = EngineHelper::GetCurrentDelegate(); 28 if (!scopedDelegate) { 29 // this case usually means there is no foreground container, need to figure out the reason. 30 LOGE("scopedDelegate is null, please check"); 31 return; 32 } 33 #if defined(MULTIPLE_WINDOW_SUPPORTED) 34 if (Container::IsCurrentUseNewPipeline()) { 35 SubwindowManager::GetInstance()->HideMenuNG(); 36 } else { 37 SubwindowManager::GetInstance()->CloseMenu(); 38 } 39 #else 40 // Close context menu. 41 auto container = Container::Current(); 42 if (container) { 43 auto context = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext()); 44 auto executor = Container::CurrentTaskExecutor(); 45 if (executor) { 46 executor->PostTask( 47 [context]() { 48 if (context) { 49 context->CloseContextMenu(); 50 } 51 }, 52 TaskExecutor::TaskType::UI); 53 } 54 } 55 #endif 56 args.SetReturnValue(args.This()); 57 } 58 JSBind(BindingTarget globalObj)59void JSContextMenu::JSBind(BindingTarget globalObj) 60 { 61 JSClass<JSContextMenu>::Declare("ContextMenu"); 62 JSClass<JSContextMenu>::StaticMethod("close", &JSContextMenu::Close); 63 64 JSClass<JSContextMenu>::Inherit<JSViewAbstract>(); 65 JSClass<JSContextMenu>::Bind<>(globalObj); 66 } 67 68 } // namespace OHOS::Ace::Framework 69