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