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 "bridge/declarative_frontend/jsview/models/shape_abstract_model_impl.h"
17 
18 #include "base/memory/referenced.h"
19 #include "bridge/declarative_frontend/view_stack_processor.h"
20 #include "core/components/shape/shape_component.h"
21 
22 namespace OHOS::Ace::Framework {
23 
SetStroke(const Color & color)24 void ShapeAbstractModelImpl::SetStroke(const Color& color)
25 {
26     auto stack = ViewStackProcessor::GetInstance();
27     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
28     if (component) {
29         AnimationOption option = stack->GetImplicitAnimationOption();
30         component->SetStroke(color, option);
31     }
32 }
33 
SetFill(const Color & color)34 void ShapeAbstractModelImpl::SetFill(const Color& color)
35 {
36     auto stack = ViewStackProcessor::GetInstance();
37     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
38     if (!component) {
39         LOGE("component is null");
40         return;
41     }
42     AnimationOption option = stack->GetImplicitAnimationOption();
43     component->SetFill(color, option);
44 }
45 
SetStrokeDashOffset(const Ace::Dimension & dashOffset)46 void ShapeAbstractModelImpl::SetStrokeDashOffset(const Ace::Dimension& dashOffset)
47 {
48     auto stack = ViewStackProcessor::GetInstance();
49     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
50     if (component) {
51         AnimationOption option = stack->GetImplicitAnimationOption();
52         component->SetStrokeDashOffset(dashOffset, option);
53     }
54 }
55 
SetStrokeLineCap(int lineCapStyle)56 void ShapeAbstractModelImpl::SetStrokeLineCap(int lineCapStyle)
57 {
58     auto stack = ViewStackProcessor::GetInstance();
59     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
60     if (!component) {
61         LOGE("ShapeComponent is null");
62         return;
63     }
64     if (static_cast<int>(LineCapStyle::SQUARE) == lineCapStyle) {
65         component->SetStrokeLineCap(LineCapStyle::SQUARE);
66     } else if (static_cast<int>(LineCapStyle::ROUND) == lineCapStyle) {
67         component->SetStrokeLineCap(LineCapStyle::ROUND);
68     } else {
69         component->SetStrokeLineCap(LineCapStyle::BUTT);
70     }
71 }
72 
SetStrokeLineJoin(int lineJoinStyle)73 void ShapeAbstractModelImpl::SetStrokeLineJoin(int lineJoinStyle)
74 {
75     auto stack = ViewStackProcessor::GetInstance();
76     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
77     if (!component) {
78         LOGE("ShapeComponent is null");
79         return;
80     }
81     if (static_cast<int>(LineJoinStyle::BEVEL) == lineJoinStyle) {
82         component->SetStrokeLineJoin(LineJoinStyle::BEVEL);
83     } else if (static_cast<int>(LineJoinStyle::ROUND) == lineJoinStyle) {
84         component->SetStrokeLineJoin(LineJoinStyle::ROUND);
85     } else {
86         component->SetStrokeLineJoin(LineJoinStyle::MITER);
87     }
88 }
89 
SetStrokeMiterLimit(double miterLimit)90 void ShapeAbstractModelImpl::SetStrokeMiterLimit(double miterLimit)
91 {
92     auto stack = ViewStackProcessor::GetInstance();
93     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
94     if (!component) {
95         LOGE("ShapeComponent is null");
96         return;
97     }
98     component->SetStrokeMiterLimit(miterLimit);
99 }
100 
SetStrokeOpacity(double opacity)101 void ShapeAbstractModelImpl::SetStrokeOpacity(double opacity)
102 {
103     auto stack = ViewStackProcessor::GetInstance();
104     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
105     if (component) {
106         AnimationOption option = stack->GetImplicitAnimationOption();
107         component->SetStrokeOpacity(opacity, option);
108     }
109 }
110 
SetFillOpacity(double opacity)111 void ShapeAbstractModelImpl::SetFillOpacity(double opacity)
112 {
113     auto stack = ViewStackProcessor::GetInstance();
114     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
115     if (component) {
116         AnimationOption option = stack->GetImplicitAnimationOption();
117         component->SetFillOpacity(opacity, option);
118     }
119 }
120 
SetStrokeWidth(const Ace::Dimension & lineWidth)121 void ShapeAbstractModelImpl::SetStrokeWidth(const Ace::Dimension& lineWidth)
122 {
123     auto stack = ViewStackProcessor::GetInstance();
124     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
125     if (!component) {
126         LOGE("ShapeComponent is null");
127         return;
128     }
129     AnimationOption option = stack->GetImplicitAnimationOption();
130     component->SetStrokeWidth(lineWidth, option);
131 }
132 
SetStrokeDashArray(const std::vector<Ace::Dimension> & dashArray)133 void ShapeAbstractModelImpl::SetStrokeDashArray(const std::vector<Ace::Dimension>& dashArray)
134 {
135     auto stack = ViewStackProcessor::GetInstance();
136     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
137     if (!component) {
138         LOGE("component is null");
139         return;
140     }
141     component->SetStrokeDashArray(dashArray);
142 }
SetAntiAlias(bool antiAlias)143 void ShapeAbstractModelImpl::SetAntiAlias(bool antiAlias)
144 {
145     auto stack = ViewStackProcessor::GetInstance();
146     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
147     if (component) {
148         component->SetAntiAlias(antiAlias);
149     }
150 }
151 
SetWidth(Dimension & width)152 void ShapeAbstractModelImpl::SetWidth(Dimension& width)
153 {
154     auto stack = ViewStackProcessor::GetInstance();
155     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
156     if (!component) {
157         LOGE("ShapeComponent is null");
158         return;
159     }
160     AnimationOption option = stack->GetImplicitAnimationOption();
161     component->SetWidth(width, option);
162 }
163 
SetHeight(Dimension & height)164 void ShapeAbstractModelImpl::SetHeight(Dimension& height)
165 {
166     auto stack = ViewStackProcessor::GetInstance();
167     auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
168     if (!component) {
169         LOGE("ShapeComponent is null");
170         return;
171     }
172     AnimationOption option = stack->GetImplicitAnimationOption();
173     component->SetHeight(height, option);
174 }
175 
176 } // namespace OHOS::Ace::Framework