From b2f64cec3c48c51afa60d153dbe7e806d8c2020a Mon Sep 17 00:00:00 2001 From: liujinwei Date: Wed, 19 Jan 2022 18:17:30 +0800 Subject: [PATCH 1/2] Add contextmenu close interface Signed-off-by: liujinwei Change-Id: Ifc5316e482f3daacb517a552195f7362203c2fd5 --- .../bridge/declarative_frontend/BUILD.gn | 1 + .../engine/jsi/jsi_view_register.cpp | 2 + .../engine/quickjs/qjs_view_register.cpp | 3 +- .../engine/v8/v8_view_register.cpp | 2 + .../jsview/menu/js_context_menu.cpp | 51 +++++++++++++++++++ .../jsview/menu/js_context_menu.h | 31 +++++++++++ .../core/components/menu/menu_element.cpp | 1 + frameworks/core/pipeline/pipeline_context.cpp | 9 ++++ frameworks/core/pipeline/pipeline_context.h | 8 +++ 9 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.cpp create mode 100644 frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.h diff --git a/frameworks/bridge/declarative_frontend/BUILD.gn b/frameworks/bridge/declarative_frontend/BUILD.gn index 6d7a91aa998..2a17580d774 100644 --- a/frameworks/bridge/declarative_frontend/BUILD.gn +++ b/frameworks/bridge/declarative_frontend/BUILD.gn @@ -213,6 +213,7 @@ template("declarative_js_engine") { "jsview/js_view_context.cpp", "jsview/js_view_functions.cpp", "jsview/js_view_stack_processor.cpp", + "jsview/menu/js_context_menu.cpp", "jsview/scroll_bar/js_scroll_bar.cpp", "sharedata/js_share_data.cpp", "view_stack_processor.cpp", diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register.cpp index 01554647344..0fee5a8cf10 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register.cpp +++ b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register.cpp @@ -137,6 +137,7 @@ #include "frameworks/bridge/declarative_frontend/jsview/js_xcomponent.h" #include "frameworks/bridge/declarative_frontend/jsview/js_xcomponent_controller.h" #endif +#include "frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.h" #include "frameworks/bridge/declarative_frontend/jsview/scroll_bar/js_scroll_bar.h" #include "frameworks/bridge/declarative_frontend/sharedata/js_share_data.h" #include "frameworks/core/common/container.h" @@ -806,6 +807,7 @@ static const std::unordered_map> { "Radio", JSRadio::JSBind }, { "ActionSheet", JSActionSheet::JSBind }, { "AlertDialog", JSAlertDialog::JSBind }, + { "ContextMenu", JSContextMenu::JSBind }, { "AbilityComponent", JSAbilityComponent::JSBind }, { "TextArea", JSTextArea::JSBind }, { "TextInput", JSTextInput::JSBind }, diff --git a/frameworks/bridge/declarative_frontend/engine/quickjs/qjs_view_register.cpp b/frameworks/bridge/declarative_frontend/engine/quickjs/qjs_view_register.cpp index cfdfdb15ebb..93902279cfa 100644 --- a/frameworks/bridge/declarative_frontend/engine/quickjs/qjs_view_register.cpp +++ b/frameworks/bridge/declarative_frontend/engine/quickjs/qjs_view_register.cpp @@ -136,6 +136,7 @@ #include "frameworks/bridge/declarative_frontend/jsview/js_xcomponent.h" #include "frameworks/bridge/declarative_frontend/jsview/js_xcomponent_controller.h" #endif +#include "frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.h" #include "frameworks/bridge/declarative_frontend/jsview/scroll_bar/js_scroll_bar.h" #include "frameworks/bridge/declarative_frontend/sharedata/js_share_data.h" #include "frameworks/bridge/js_frontend/engine/quickjs/qjs_utils.h" @@ -944,7 +945,7 @@ void JsRegisterViews(BindingTarget globalObj) JSActionSheet::JSBind(globalObj); JSAlertDialog::JSBind(globalObj); - + JSContextMenu::JSBind(globalObj); JSAbilityComponent::JSBind(globalObj); JSAbilityComponentController::JSBind(globalObj); diff --git a/frameworks/bridge/declarative_frontend/engine/v8/v8_view_register.cpp b/frameworks/bridge/declarative_frontend/engine/v8/v8_view_register.cpp index b6d923436d3..65f23d0bc8e 100644 --- a/frameworks/bridge/declarative_frontend/engine/v8/v8_view_register.cpp +++ b/frameworks/bridge/declarative_frontend/engine/v8/v8_view_register.cpp @@ -137,6 +137,7 @@ #include "frameworks/bridge/declarative_frontend/jsview/js_xcomponent.h" #include "frameworks/bridge/declarative_frontend/jsview/js_xcomponent_controller.h" #endif +#include "frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.h" #include "frameworks/bridge/declarative_frontend/jsview/scroll_bar/js_scroll_bar.h" #include "frameworks/bridge/declarative_frontend/sharedata/js_share_data.h" @@ -621,6 +622,7 @@ static const std::unordered_map> {"Radio", JSRadio::JSBind}, {"ActionSheet", JSActionSheet::JSBind}, {"AlertDialog", JSAlertDialog::JSBind}, + {"ContextMenu", JSContextMenu::JSBind }, {"AbilityComponent", JSAbilityComponent::JSBind}, {"TextArea", JSTextArea::JSBind}, {"TextInput", JSTextInput::JSBind}, diff --git a/frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.cpp b/frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.cpp new file mode 100644 index 00000000000..b0b9d349bf3 --- /dev/null +++ b/frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.h" + +#include "core/common/container.h" + +namespace OHOS::Ace::Framework { + +void JSContextMenu::Close(const JSCallbackInfo& args) +{ + // Close context menu. + auto container = Container::Current(); + if (container) { + auto context = container->GetPipelineContext(); + auto executor = container->GetTaskExecutor(); + if (executor) { + executor->PostTask( + [context]() { + if (context) { + context->CloseContextMenu(); + } + }, + TaskExecutor::TaskType::UI); + } + } + args.SetReturnValue(args.This()); +} + +void JSContextMenu::JSBind(BindingTarget globalObj) +{ + JSClass::Declare("ContextMenu"); + JSClass::StaticMethod("close", &JSContextMenu::Close); + + JSClass::Inherit(); + JSClass::Bind<>(globalObj); +} + +} // namespace OHOS::Ace::Framework diff --git a/frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.h b/frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.h new file mode 100644 index 00000000000..ab1053ab291 --- /dev/null +++ b/frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_MENU_CONTEXTMENU_H +#define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_MENU_CONTEXTMENU_H + +#include "frameworks/bridge/declarative_frontend/jsview/js_interactable_view.h" +#include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h" + +namespace OHOS::Ace::Framework { + +class JSContextMenu : public JSViewAbstract, public JSInteractableView { +public: + static void JSBind(BindingTarget globalObj); + static void Close(const JSCallbackInfo& args); +}; + +} // namespace OHOS::Ace::Framework +#endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_MENU_CONTEXTMENU_H diff --git a/frameworks/core/components/menu/menu_element.cpp b/frameworks/core/components/menu/menu_element.cpp index 1cbbe1b2ddc..bfa7aa5d999 100644 --- a/frameworks/core/components/menu/menu_element.cpp +++ b/frameworks/core/components/menu/menu_element.cpp @@ -85,6 +85,7 @@ void MenuElement::OnTargetCallback(const ComposeId& id, const Offset& point) auto popup = data_->GetPopup(); if (id.empty()) { popup->ShowDialog(stack, point, point, true); + context->SetContextMenu(popup); return; } diff --git a/frameworks/core/pipeline/pipeline_context.cpp b/frameworks/core/pipeline/pipeline_context.cpp index 3096e61fc75..c54cd8c07a0 100644 --- a/frameworks/core/pipeline/pipeline_context.cpp +++ b/frameworks/core/pipeline/pipeline_context.cpp @@ -61,6 +61,7 @@ #include "core/components/root/root_component.h" #include "core/components/root/root_element.h" #include "core/components/scroll/scrollable.h" +#include "core/components/select_popup/select_popup_component.h" #include "core/components/semi_modal/semi_modal_component.h" #include "core/components/semi_modal/semi_modal_element.h" #include "core/components/semi_modal/semi_modal_theme.h" @@ -967,6 +968,14 @@ RefPtr PipelineContext::ShowDialog(const DialogProperties& dial return dialog; } +void PipelineContext::CloseContextMenu() +{ + auto menu = AceType::DynamicCast(contextMenu_); + if (menu) { + menu->HideDialog(SELECT_INVALID_INDEX); + } +} + bool PipelineContext::CanPopPage() { auto stageElement = GetStageElement(); diff --git a/frameworks/core/pipeline/pipeline_context.h b/frameworks/core/pipeline/pipeline_context.h index 4792f704f7a..e4bdb5b4604 100644 --- a/frameworks/core/pipeline/pipeline_context.h +++ b/frameworks/core/pipeline/pipeline_context.h @@ -83,6 +83,7 @@ class AccessibilityManager; class RenderContext; struct PageTarget; class DialogComponent; +class SelectPopupComponent; struct WindowBlurInfo { float progress_; @@ -116,6 +117,7 @@ public: RefPtr SetupRootElement(); RefPtr ShowDialog(const DialogProperties& dialogProperties, bool isRightToLeft); + void CloseContextMenu(); void GetBoundingRectData(int32_t nodeId, Rect& rect); @@ -915,6 +917,11 @@ public: void SetPreTargetRenderNode(const RefPtr& preTargetRenderNode); const RefPtr GetPreTargetRenderNode() const; + void SetContextMenu(const RefPtr& contextMenu) + { + contextMenu_ = contextMenu; + } + double GetDensity() const { return density_; @@ -1123,6 +1130,7 @@ private: // strong deactivate element and it's id. std::map> deactivateElements_; + RefPtr contextMenu_; // animation frame callback AnimationCallback animationCallback_; -- Gitee From a953c19bcdabf38518bf8283e5130d97f6d287f8 Mon Sep 17 00:00:00 2001 From: liujinwei Date: Wed, 19 Jan 2022 20:35:16 +0800 Subject: [PATCH 2/2] use new GetTaskExecutor function Signed-off-by: liujinwei Change-Id: Ib824bf0c6080c0856c12b8e47f795fcdae60431c --- .../bridge/declarative_frontend/jsview/menu/js_context_menu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.cpp b/frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.cpp index b0b9d349bf3..a57c331e7ec 100644 --- a/frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.cpp +++ b/frameworks/bridge/declarative_frontend/jsview/menu/js_context_menu.cpp @@ -25,7 +25,7 @@ void JSContextMenu::Close(const JSCallbackInfo& args) auto container = Container::Current(); if (container) { auto context = container->GetPipelineContext(); - auto executor = container->GetTaskExecutor(); + auto executor = Container::CurrentTaskExecutor(); if (executor) { executor->PostTask( [context]() { -- Gitee