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