1 /*
2  * Copyright (c) 2023 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 #include "bridge/declarative_frontend/jsview/models/action_sheet_model_impl.h"
16 
17 #include "base/thread/task_executor.h"
18 #include "core/components/dialog/dialog_component.h"
19 #include "core/components/stack/stack_element.h"
20 #include "core/event/ace_event_handler.h"
21 #include "core/gestures/tap_gesture.h"
22 #include "core/pipeline/pipeline_context.h"
23 
24 namespace OHOS::Ace::Framework {
ShowActionSheet(const DialogProperties & arg)25 void ActionSheetModelImpl::ShowActionSheet(const DialogProperties& arg)
26 {
27     LOGE("ActionSheetModelImpl::ShowActionSheet");
28     auto container = Container::Current();
29     if (container) {
30         auto context = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
31         auto executor = container->GetTaskExecutor();
32         if (executor) {
33             executor->PostTask(
34                 [context, arg]() {
35                     if (context) {
36                         context->ShowDialog(arg, false, "ActionSheet");
37                     }
38                 },
39                 TaskExecutor::TaskType::UI, "ArkUIDialogShowActionSheet");
40         }
41     }
42 }
43 
SetAction(GestureEventFunc && eventFunc,ActionSheetInfo & sheetInfo)44 void ActionSheetModelImpl::SetAction(GestureEventFunc&& eventFunc, ActionSheetInfo& sheetInfo)
45 {
46     LOGE("ActionSheetModelImpl::SetAction");
47     RefPtr<Gesture> tapGesture = AceType::MakeRefPtr<TapGesture>();
48     tapGesture->SetOnActionId([func = std::move(eventFunc)](GestureEvent& info) {
49         auto container = Container::Current();
50         if (!container) {
51             return;
52         }
53         auto context = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
54         if (!context) {
55             return;
56         }
57         auto stack = context->GetLastStack();
58         if (!stack) {
59             return;
60         }
61         stack->PopDialog();
62         func(info);
63     });
64     sheetInfo.gesture = tapGesture;
65 }
66 
SetCancel(std::function<void ()> && eventFunc,DialogProperties & arg)67 void ActionSheetModelImpl::SetCancel(std::function<void()>&& eventFunc, DialogProperties& arg)
68 {
69     LOGE("ActionSheetModelImpl::SetCancel");
70     EventMarker cancelId(std::move(eventFunc));
71     arg.callbacks.try_emplace("cancel", cancelId);
72 }
73 
SetConfirm(GestureEventFunc && gestureEvent,std::function<void ()> && eventFunc,ButtonInfo & buttonInfo,DialogProperties & arg)74 void ActionSheetModelImpl::SetConfirm(GestureEventFunc&& gestureEvent, std::function<void()>&& eventFunc,
75     ButtonInfo& buttonInfo, DialogProperties& arg)
76 {
77     EventMarker actionId(std::move(eventFunc));
78     arg.primaryId = actionId;
79 }
80 } // namespace OHOS::Ace::Framework
81