1 /*
2  * Copyright (c) 2023-2024 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 "bridge/declarative_frontend/jsview/models/canvas/canvas_model_impl.h"
17 
18 #include "bridge/declarative_frontend/view_stack_processor.h"
19 #include "core/components/custom_paint/custom_paint_component.h"
20 #include "core/event/ace_event_handler.h"
21 
22 namespace OHOS::Ace::Framework {
Create()23 RefPtr<AceType> CanvasModelImpl::Create()
24 {
25     auto paintChild = AceType::MakeRefPtr<OHOS::Ace::CustomPaintComponent>();
26     if (!paintChild) {
27         return nullptr;
28     }
29     ViewStackProcessor::GetInstance()->ClaimElementId(paintChild);
30     ViewStackProcessor::GetInstance()->Push(paintChild);
31     return AceType::DynamicCast<OHOS::Ace::CustomPaintComponent>(paintChild)->GetTaskPool();
32 }
33 
SetOnReady(std::function<void (uint32_t)> && readyEvent)34 void CanvasModelImpl::SetOnReady(std::function<void(uint32_t)>&& readyEvent)
35 {
36     auto container = Container::Current();
37     if (!container) {
38         LOGE("No container");
39         return;
40     }
41     auto context = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
42     if (!context) {
43         LOGE("No PipelineContext");
44         return;
45     }
46     auto component = AceType::DynamicCast<CustomPaintComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
47     if (!component) {
48         LOGE("No CustomPaintComponent");
49         return;
50     }
51 
52     auto elmtId = component->GetElementId();
53     auto fun = readyEvent;
54     auto onReadEvent = [fun, elmtId]() { fun(elmtId); };
55 
56     auto onReadyEvent = EventMarker(std::move(onReadEvent));
57 
58     component->SetOnReadyEvent(onReadyEvent, context);
59 }
60 } // namespace OHOS::Ace::Framework
61