• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #include "bridge/declarative_frontend/jsview/models/view_abstract_model_impl.h"
17 
18 #include <functional>
19 
20 #include "base/geometry/animatable_dimension.h"
21 #include "base/log/ace_scoring_log.h"
22 #include "base/memory/ace_type.h"
23 #include "base/memory/referenced.h"
24 #include "base/utils/utils.h"
25 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
26 #include "bridge/declarative_frontend/jsview/models/grid_container_model_impl.h"
27 #include "bridge/declarative_frontend/view_stack_processor.h"
28 #include "core/components/box/box_component_helper.h"
29 #include "core/components/box/drag_drop_event.h"
30 #include "core/components/common/layout/grid_layout_info.h"
31 #include "core/components/common/properties/border_image.h"
32 #include "core/components/common/properties/decoration.h"
33 #include "core/components/common/properties/placement.h"
34 #include "core/components_ng/pattern/menu/menu_pattern.h"
35 #include "core/event/ace_event_handler.h"
36 #include "core/event/touch_event.h"
37 #include "core/gestures/gesture_info.h"
38 #include "core/gestures/long_press_gesture.h"
39 #include "core/image/image_source_info.h"
40 
41 // avoid windows build error about macro defined in winuser.h
42 #ifdef GetMessage
43 #undef GetMessage
44 #endif
45 
46 namespace OHOS::Ace::Framework {
47 
48 constexpr int32_t DEFAULT_LONG_PRESS_FINGER = 1;
49 constexpr int32_t DEFAULT_LONG_PRESS_DURATION = 500;
50 
GetBackDecoration()51 RefPtr<Decoration> GetBackDecoration()
52 {
53     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
54     auto decoration = box->GetBackDecoration();
55     if (!decoration) {
56         decoration = AceType::MakeRefPtr<Decoration>();
57         box->SetBackDecoration(decoration);
58     }
59     return decoration;
60 }
61 
GetFrontDecoration()62 RefPtr<Decoration> GetFrontDecoration()
63 {
64     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
65     auto decoration = box->GetFrontDecoration();
66     if (!decoration) {
67         decoration = AceType::MakeRefPtr<Decoration>();
68         box->SetFrontDecoration(decoration);
69     }
70 
71     return decoration;
72 }
73 
ToAnimatableDimension(const Dimension & dimension)74 AnimatableDimension ToAnimatableDimension(const Dimension& dimension)
75 {
76     AnimatableDimension result(dimension);
77     AnimationOption option = ViewStackProcessor::GetInstance()->GetImplicitAnimationOption();
78     result.SetAnimationOption(option);
79     return result;
80 }
81 
ToGradient(const NG::Gradient & gradient)82 Gradient ToGradient(const NG::Gradient& gradient)
83 {
84     Gradient retGradient;
85     retGradient.SetType(static_cast<GradientType>(gradient.GetType()));
86     AnimationOption option = ViewStackProcessor::GetInstance()->GetImplicitAnimationOption();
87     if (retGradient.GetType() == GradientType::LINEAR) {
88         auto angle = gradient.GetLinearGradient()->angle;
89         if (angle.has_value()) {
90             retGradient.GetLinearGradient().angle = ToAnimatableDimension(angle.value());
91         }
92         auto linearX = gradient.GetLinearGradient()->linearX;
93         if (linearX.has_value()) {
94             retGradient.GetLinearGradient().linearX = static_cast<GradientDirection>(linearX.value());
95         }
96         auto linearY = gradient.GetLinearGradient()->linearY;
97         if (linearY.has_value()) {
98             retGradient.GetLinearGradient().linearY = static_cast<GradientDirection>(linearY.value());
99         }
100     }
101 
102     if (retGradient.GetType() == GradientType::RADIAL) {
103         auto radialCenterX = gradient.GetRadialGradient()->radialCenterX;
104         if (radialCenterX.has_value()) {
105             retGradient.GetRadialGradient().radialCenterX = ToAnimatableDimension(radialCenterX.value());
106         }
107         auto radialCenterY = gradient.GetRadialGradient()->radialCenterY;
108         if (radialCenterY.has_value()) {
109             retGradient.GetRadialGradient().radialCenterY = ToAnimatableDimension(radialCenterY.value());
110         }
111         auto radialVerticalSize = gradient.GetRadialGradient()->radialVerticalSize;
112         if (radialVerticalSize.has_value()) {
113             retGradient.GetRadialGradient().radialVerticalSize = ToAnimatableDimension(radialVerticalSize.value());
114         }
115         auto radialHorizontalSize = gradient.GetRadialGradient()->radialHorizontalSize;
116         if (radialVerticalSize.has_value()) {
117             retGradient.GetRadialGradient().radialHorizontalSize = ToAnimatableDimension(radialHorizontalSize.value());
118         }
119     }
120 
121     if (retGradient.GetType() == GradientType::SWEEP) {
122         auto centerX = gradient.GetSweepGradient()->centerX;
123         if (centerX.has_value()) {
124             retGradient.GetSweepGradient().centerX = ToAnimatableDimension(centerX.value());
125         }
126         auto centerY = gradient.GetSweepGradient()->centerY;
127         if (centerY.has_value()) {
128             retGradient.GetSweepGradient().centerY = ToAnimatableDimension(centerY.value());
129         }
130         auto startAngle = gradient.GetSweepGradient()->startAngle;
131         if (startAngle.has_value()) {
132             retGradient.GetSweepGradient().startAngle = ToAnimatableDimension(startAngle.value());
133         }
134         auto endAngle = gradient.GetSweepGradient()->endAngle;
135         if (endAngle.has_value()) {
136             retGradient.GetSweepGradient().endAngle = ToAnimatableDimension(endAngle.value());
137         }
138         auto rotation = gradient.GetSweepGradient()->rotation;
139         if (rotation.has_value()) {
140             retGradient.GetSweepGradient().rotation = ToAnimatableDimension(rotation.value());
141         }
142     }
143 
144     retGradient.SetRepeat(gradient.GetRepeat());
145     const auto& colorStops = gradient.GetColors();
146 
147     for (const auto& item : colorStops) {
148         GradientColor gradientColor;
149         gradientColor.SetColor(item.GetColor());
150         gradientColor.SetHasValue(item.GetHasValue());
151         gradientColor.SetDimension(item.GetDimension());
152         retGradient.AddColor(gradientColor);
153     }
154     return retGradient;
155 }
156 
SwapBackBorder(const RefPtr<Decoration> & decoration)157 void ViewAbstractModelImpl::SwapBackBorder(const RefPtr<Decoration>& decoration)
158 {
159     CHECK_NULL_VOID(decoration);
160     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
161     auto boxDecoration = box->GetBackDecoration();
162     if (boxDecoration) {
163         decoration->SetBorder(boxDecoration->GetBorder());
164         boxDecoration->SetBorder({});
165     }
166 }
167 
ToDragFunc(NG::OnDragStartFunc && onDragStart)168 OnDragFunc ViewAbstractModelImpl::ToDragFunc(NG::OnDragStartFunc&& onDragStart)
169 {
170     auto dragStart = [dragStartFunc = std::move(onDragStart)](
171                          const RefPtr<DragEvent>& event, const std::string& extraParams) -> DragItemInfo {
172         auto dragInfo = dragStartFunc(event, extraParams);
173         DragItemInfo info;
174         info.extraInfo = dragInfo.extraInfo;
175         info.pixelMap = dragInfo.pixelMap;
176         info.customComponent = AceType::DynamicCast<Component>(dragInfo.node);
177         return info;
178     };
179     return dragStart;
180 }
181 
SetWidth(const CalcDimension & width)182 void ViewAbstractModelImpl::SetWidth(const CalcDimension& width)
183 {
184     bool isPercentSize = (width.Unit() == DimensionUnit::PERCENT);
185     if (isPercentSize) {
186         auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
187         auto renderComponent = AceType::DynamicCast<RenderComponent>(component);
188         if (renderComponent) {
189             renderComponent->SetIsPercentSize(isPercentSize);
190         }
191     }
192 
193     auto* stack = ViewStackProcessor::GetInstance();
194     auto box = stack->GetBoxComponent();
195     auto option = stack->GetImplicitAnimationOption();
196     if (!stack->IsVisualStateSet()) {
197         box->SetWidth(width, option);
198     } else {
199         box->GetStateAttributes()->AddAttribute<AnimatableDimension>(
200             BoxStateAttribute::WIDTH, AnimatableDimension(width, option), stack->GetVisualState());
201         if (!box->GetStateAttributes()->HasAttribute(BoxStateAttribute::WIDTH, VisualState::NORMAL)) {
202             box->GetStateAttributes()->AddAttribute<AnimatableDimension>(
203                 BoxStateAttribute::WIDTH, AnimatableDimension(box->GetWidth(), option), VisualState::NORMAL);
204         }
205     }
206 }
207 
SetHeight(const CalcDimension & height)208 void ViewAbstractModelImpl::SetHeight(const CalcDimension& height)
209 {
210     bool isPercentSize = (height.Unit() == DimensionUnit::PERCENT);
211     if (isPercentSize) {
212         auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
213         auto renderComponent = AceType::DynamicCast<RenderComponent>(component);
214         if (renderComponent) {
215             renderComponent->SetIsPercentSize(isPercentSize);
216         }
217     }
218 
219     auto* stack = ViewStackProcessor::GetInstance();
220     auto box = stack->GetBoxComponent();
221     auto option = stack->GetImplicitAnimationOption();
222     if (!stack->IsVisualStateSet()) {
223         box->SetHeight(height, option);
224     } else {
225         box->GetStateAttributes()->AddAttribute<AnimatableDimension>(
226             BoxStateAttribute::HEIGHT, AnimatableDimension(height, option), stack->GetVisualState());
227         if (!box->GetStateAttributes()->HasAttribute(BoxStateAttribute::HEIGHT, VisualState::NORMAL)) {
228             box->GetStateAttributes()->AddAttribute<AnimatableDimension>(
229                 BoxStateAttribute::HEIGHT, AnimatableDimension(box->GetHeight(), option), VisualState::NORMAL);
230         }
231     }
232 }
233 
SetMinWidth(const CalcDimension & minWidth)234 void ViewAbstractModelImpl::SetMinWidth(const CalcDimension& minWidth)
235 {
236     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
237     auto flexItem = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
238     box->SetMinWidth(minWidth);
239     flexItem->SetMinWidth(minWidth);
240 }
241 
SetMinHeight(const CalcDimension & minHeight)242 void ViewAbstractModelImpl::SetMinHeight(const CalcDimension& minHeight)
243 {
244     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
245     auto flexItem = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
246     box->SetMinHeight(minHeight);
247     flexItem->SetMinHeight(minHeight);
248 }
249 
SetMaxWidth(const CalcDimension & maxWidth)250 void ViewAbstractModelImpl::SetMaxWidth(const CalcDimension& maxWidth)
251 {
252     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
253     auto flexItem = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
254     box->SetMaxWidth(maxWidth);
255     flexItem->SetMaxWidth(maxWidth);
256 }
257 
SetMaxHeight(const CalcDimension & maxHeight)258 void ViewAbstractModelImpl::SetMaxHeight(const CalcDimension& maxHeight)
259 {
260     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
261     auto flexItem = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
262     box->SetMaxHeight(maxHeight);
263     flexItem->SetMaxHeight(maxHeight);
264 }
265 
SetBackgroundColor(const Color & color)266 void ViewAbstractModelImpl::SetBackgroundColor(const Color& color)
267 {
268     auto* stack = ViewStackProcessor::GetInstance();
269     auto boxComponent = stack->GetBoxComponent();
270     auto option = stack->GetImplicitAnimationOption();
271     if (!stack->IsVisualStateSet()) {
272         boxComponent->SetColor(color, option);
273     } else {
274         boxComponent->GetStateAttributes()->AddAttribute<AnimatableColor>(
275             BoxStateAttribute::COLOR, AnimatableColor(color, option), stack->GetVisualState());
276         if (!boxComponent->GetStateAttributes()->HasAttribute(BoxStateAttribute::COLOR, VisualState::NORMAL)) {
277             boxComponent->GetStateAttributes()->AddAttribute<AnimatableColor>(
278                 BoxStateAttribute::COLOR, AnimatableColor(boxComponent->GetColor(), option), VisualState::NORMAL);
279         }
280     }
281 }
282 
SetBackgroundImage(const ImageSourceInfo & src,RefPtr<ThemeConstants> themeConstant)283 void ViewAbstractModelImpl::SetBackgroundImage(const ImageSourceInfo& src, RefPtr<ThemeConstants> themeConstant)
284 {
285     auto decoration = GetBackDecoration();
286     auto image = decoration->GetImage();
287     if (!image) {
288         image = AceType::MakeRefPtr<BackgroundImage>();
289     }
290 
291     if (themeConstant) {
292         image->SetSrc(src.GetSrc(), themeConstant);
293     } else {
294         image->SetParsedSrc(src.GetSrc());
295     }
296 
297     decoration->SetImage(image);
298 }
299 
SetBackgroundImageRepeat(const ImageRepeat & imageRepeat)300 void ViewAbstractModelImpl::SetBackgroundImageRepeat(const ImageRepeat& imageRepeat)
301 {
302     auto decoration = GetBackDecoration();
303     auto image = decoration->GetImage();
304     if (!image) {
305         image = AceType::MakeRefPtr<BackgroundImage>();
306     }
307     image->SetImageRepeat(imageRepeat);
308     decoration->SetImage(image);
309 }
310 
SetBackgroundImageSize(const BackgroundImageSize & bgImgSize)311 void ViewAbstractModelImpl::SetBackgroundImageSize(const BackgroundImageSize& bgImgSize)
312 {
313     auto decoration = GetBackDecoration();
314     auto image = decoration->GetImage();
315     if (!image) {
316         image = AceType::MakeRefPtr<BackgroundImage>();
317     }
318     image->SetImageSize(bgImgSize);
319     decoration->SetImage(image);
320 }
321 
SetBackgroundImagePosition(const BackgroundImagePosition & bgImgPosition)322 void ViewAbstractModelImpl::SetBackgroundImagePosition(const BackgroundImagePosition& bgImgPosition)
323 {
324     auto decoration = GetBackDecoration();
325     auto image = decoration->GetImage();
326     if (!image) {
327         image = AceType::MakeRefPtr<BackgroundImage>();
328     }
329     image->SetImagePosition(bgImgPosition);
330     decoration->SetImage(image);
331 }
332 
SetBackgroundBlurStyle(const BlurStyleOption & bgBlurStyle)333 void ViewAbstractModelImpl::SetBackgroundBlurStyle(const BlurStyleOption& bgBlurStyle)
334 {
335     auto decoration = GetBackDecoration();
336     decoration->SetBlurStyle(bgBlurStyle);
337     double radius = 0.0;
338     Dimension dimensionRadius(radius, DimensionUnit::PX);
339     decoration->SetBlurRadius(ToAnimatableDimension(dimensionRadius));
340 }
341 
SetPadding(const CalcDimension & value)342 void ViewAbstractModelImpl::SetPadding(const CalcDimension& value)
343 {
344     AnimatableDimension animValue = ToAnimatableDimension(value);
345     SetPaddings(animValue, animValue, animValue, animValue);
346 }
347 
SetPaddings(const std::optional<CalcDimension> & top,const std::optional<CalcDimension> & bottom,const std::optional<CalcDimension> & left,const std::optional<CalcDimension> & right)348 void ViewAbstractModelImpl::SetPaddings(const std::optional<CalcDimension>& top,
349     const std::optional<CalcDimension>& bottom, const std::optional<CalcDimension>& left,
350     const std::optional<CalcDimension>& right)
351 {
352     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
353     Edge padding = box->GetPadding();
354     if (top.has_value()) {
355         padding.SetTop(ToAnimatableDimension(top.value()));
356     }
357     if (bottom.has_value()) {
358         padding.SetBottom(ToAnimatableDimension(bottom.value()));
359     }
360     if (left.has_value()) {
361         padding.SetLeft(ToAnimatableDimension(left.value()));
362     }
363     if (right.has_value()) {
364         padding.SetRight(ToAnimatableDimension(right.value()));
365     }
366     box->SetPadding(padding);
367 }
368 
SetMargin(const CalcDimension & value)369 void ViewAbstractModelImpl::SetMargin(const CalcDimension& value)
370 {
371     AnimatableDimension animValue = ToAnimatableDimension(value);
372     SetMargins(animValue, animValue, animValue, animValue);
373 }
374 
SetMargins(const std::optional<CalcDimension> & top,const std::optional<CalcDimension> & bottom,const std::optional<CalcDimension> & left,const std::optional<CalcDimension> & right)375 void ViewAbstractModelImpl::SetMargins(const std::optional<CalcDimension>& top,
376     const std::optional<CalcDimension>& bottom, const std::optional<CalcDimension>& left,
377     const std::optional<CalcDimension>& right)
378 {
379     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
380     Edge margin = box->GetMargin();
381     if (top.has_value()) {
382         margin.SetTop(ToAnimatableDimension(top.value()));
383     }
384     if (bottom.has_value()) {
385         margin.SetBottom(ToAnimatableDimension(bottom.value()));
386     }
387     if (left.has_value()) {
388         margin.SetLeft(ToAnimatableDimension(left.value()));
389     }
390     if (right.has_value()) {
391         margin.SetRight(ToAnimatableDimension(right.value()));
392     }
393     box->SetMargin(margin);
394 }
395 
SetBorderRadius(const Dimension & value)396 void ViewAbstractModelImpl::SetBorderRadius(const Dimension& value)
397 {
398     SetBorderRadius(value, value, value, value);
399 }
400 
SetBorderRadius(const std::optional<Dimension> & radiusTopLeft,const std::optional<Dimension> & radiusTopRight,const std::optional<Dimension> & radiusBottomLeft,const std::optional<Dimension> & radiusBottomRight)401 void ViewAbstractModelImpl::SetBorderRadius(const std::optional<Dimension>& radiusTopLeft,
402     const std::optional<Dimension>& radiusTopRight, const std::optional<Dimension>& radiusBottomLeft,
403     const std::optional<Dimension>& radiusBottomRight)
404 {
405     auto decoration = GetBackDecoration();
406     Dimension topLeft = radiusTopLeft.has_value() ? radiusTopLeft.value()
407                                                   : BoxComponentHelper::GetBorderRadiusTopLeft(decoration).GetX();
408     Dimension topRight = radiusTopRight.has_value() ? radiusTopRight.value()
409                                                     : BoxComponentHelper::GetBorderRadiusTopRight(decoration).GetX();
410     Dimension bottomLeft = radiusBottomLeft.has_value()
411                                ? radiusBottomLeft.value()
412                                : BoxComponentHelper::GetBorderRadiusBottomLeft(decoration).GetX();
413     Dimension bottomRight = radiusBottomRight.has_value()
414                                 ? radiusBottomRight.value()
415                                 : BoxComponentHelper::GetBorderRadiusBottomRight(decoration).GetX();
416     auto* stack = ViewStackProcessor::GetInstance();
417     AnimationOption option = stack->GetImplicitAnimationOption();
418     if (!stack->IsVisualStateSet()) {
419         BoxComponentHelper::SetBorderRadius(decoration, topLeft, topRight, bottomLeft, bottomRight, option);
420     } else {
421         auto boxComponent = stack->GetBoxComponent();
422         boxComponent->GetStateAttributes()->AddAttribute<AnimatableDimension>(
423             BoxStateAttribute::BORDER_RADIUS, AnimatableDimension(topLeft, option), stack->GetVisualState());
424         if (!boxComponent->GetStateAttributes()->HasAttribute(BoxStateAttribute::BORDER_RADIUS, VisualState::NORMAL)) {
425             boxComponent->GetStateAttributes()->AddAttribute<AnimatableDimension>(
426                 BoxStateAttribute::BORDER_RADIUS, AnimatableDimension(topLeft, option), VisualState::NORMAL);
427         }
428     }
429 }
430 
SetBorderColor(const Color & value)431 void ViewAbstractModelImpl::SetBorderColor(const Color& value)
432 {
433     SetBorderColor(value, value, value, value);
434 }
435 
SetBorderColor(const std::optional<Color> & colorLeft,const std::optional<Color> & colorRight,const std::optional<Color> & colorTop,const std::optional<Color> & colorBottom)436 void ViewAbstractModelImpl::SetBorderColor(const std::optional<Color>& colorLeft,
437     const std::optional<Color>& colorRight, const std::optional<Color>& colorTop,
438     const std::optional<Color>& colorBottom)
439 {
440     auto decoration = GetBackDecoration();
441     Color leftColor = colorLeft.has_value() ? colorLeft.value() : BoxComponentHelper::GetBorderColorTop(decoration);
442     Color rightColor =
443         colorRight.has_value() ? colorRight.value() : BoxComponentHelper::GetBorderColorBottom(decoration);
444     Color topColor = colorTop.has_value() ? colorTop.value() : BoxComponentHelper::GetBorderColorLeft(decoration);
445     Color bottomColor =
446         colorBottom.has_value() ? colorBottom.value() : BoxComponentHelper::GetBorderColorRight(decoration);
447     auto* stack = ViewStackProcessor::GetInstance();
448     AnimationOption option = stack->GetImplicitAnimationOption();
449     if (!stack->IsVisualStateSet()) {
450         BoxComponentHelper::SetBorderColor(decoration, leftColor, rightColor, topColor, bottomColor, option);
451     } else {
452         auto boxComponent = stack->GetBoxComponent();
453         boxComponent->GetStateAttributes()->AddAttribute<AnimatableColor>(
454             BoxStateAttribute::BORDER_COLOR, AnimatableColor(leftColor, option), stack->GetVisualState());
455         if (!boxComponent->GetStateAttributes()->HasAttribute(BoxStateAttribute::BORDER_COLOR, VisualState::NORMAL)) {
456             boxComponent->GetStateAttributes()->AddAttribute<AnimatableColor>(BoxStateAttribute::BORDER_COLOR,
457                 AnimatableColor(BoxComponentHelper::GetBorderColor(decoration), option), VisualState::NORMAL);
458         }
459     }
460 }
461 
SetBorderWidth(const Dimension & value)462 void ViewAbstractModelImpl::SetBorderWidth(const Dimension& value)
463 {
464     SetBorderWidth(value, value, value, value);
465 }
466 
SetBorderWidth(const std::optional<Dimension> & left,const std::optional<Dimension> & right,const std::optional<Dimension> & top,const std::optional<Dimension> & bottom)467 void ViewAbstractModelImpl::SetBorderWidth(const std::optional<Dimension>& left, const std::optional<Dimension>& right,
468     const std::optional<Dimension>& top, const std::optional<Dimension>& bottom)
469 {
470     auto decoration = GetBackDecoration();
471     Dimension leftDimen =
472         left.has_value() ? left.value() : Dimension(BoxComponentHelper::GetBorderLeftWidth(decoration));
473     Dimension rightDimen =
474         right.has_value() ? right.value() : Dimension(BoxComponentHelper::GetBorderRightWidth(decoration));
475     Dimension topDimen = top.has_value() ? top.value() : Dimension(BoxComponentHelper::GetBorderTopWidth(decoration));
476     Dimension bottomDimen =
477         bottom.has_value() ? bottom.value() : Dimension(BoxComponentHelper::GetBorderBottomWidth(decoration));
478     auto* stack = ViewStackProcessor::GetInstance();
479     AnimationOption option = stack->GetImplicitAnimationOption();
480     if (!stack->IsVisualStateSet()) {
481         BoxComponentHelper::SetBorderWidth(decoration, leftDimen, rightDimen, topDimen, bottomDimen, option);
482     } else {
483         auto boxComponent = stack->GetBoxComponent();
484         boxComponent->GetStateAttributes()->AddAttribute<AnimatableDimension>(
485             BoxStateAttribute::BORDER_WIDTH, AnimatableDimension(leftDimen, option), stack->GetVisualState());
486         if (!boxComponent->GetStateAttributes()->HasAttribute(BoxStateAttribute::BORDER_WIDTH, VisualState::NORMAL)) {
487             boxComponent->GetStateAttributes()->AddAttribute<AnimatableDimension>(BoxStateAttribute::BORDER_WIDTH,
488                 AnimatableDimension(BoxComponentHelper::GetBorderWidth(decoration), option), VisualState::NORMAL);
489         }
490     }
491 }
492 
SetBorderStyle(const BorderStyle & value)493 void ViewAbstractModelImpl::SetBorderStyle(const BorderStyle& value)
494 {
495     SetBorderStyle(value, value, value, value);
496 }
497 
SetBorderStyle(const std::optional<BorderStyle> & styleLeft,const std::optional<BorderStyle> & styleRight,const std::optional<BorderStyle> & styleTop,const std::optional<BorderStyle> & styleBottom)498 void ViewAbstractModelImpl::SetBorderStyle(const std::optional<BorderStyle>& styleLeft,
499     const std::optional<BorderStyle>& styleRight, const std::optional<BorderStyle>& styleTop,
500     const std::optional<BorderStyle>& styleBottom)
501 {
502     auto decoration = GetBackDecoration();
503     BorderStyle left = styleLeft.value_or(BorderStyle::SOLID);
504     BorderStyle right = styleRight.value_or(BorderStyle::SOLID);
505     BorderStyle top = styleTop.value_or(BorderStyle::SOLID);
506     BorderStyle bottom = styleBottom.value_or(BorderStyle::SOLID);
507     auto* stack = ViewStackProcessor::GetInstance();
508     AnimationOption option = stack->GetImplicitAnimationOption();
509     if (!stack->IsVisualStateSet()) {
510         BoxComponentHelper::SetBorderStyle(decoration, left, right, top, bottom);
511     } else {
512         auto boxComponent = stack->GetBoxComponent();
513         boxComponent->GetStateAttributes()->AddAttribute<BorderStyle>(
514             BoxStateAttribute::BORDER_STYLE, left, stack->GetVisualState());
515         if (!boxComponent->GetStateAttributes()->HasAttribute(BoxStateAttribute::BORDER_STYLE, VisualState::NORMAL)) {
516             boxComponent->GetStateAttributes()->AddAttribute<BorderStyle>(
517                 BoxStateAttribute::BORDER_STYLE, BoxComponentHelper::GetBorderStyle(decoration), VisualState::NORMAL);
518         }
519     }
520 }
521 
SetBorderImage(const RefPtr<BorderImage> & borderImage,uint8_t bitset)522 void ViewAbstractModelImpl::SetBorderImage(const RefPtr<BorderImage>& borderImage, uint8_t bitset)
523 {
524     auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
525     auto boxDecoration = GetBackDecoration();
526     if (bitset | BorderImage::OUTSET_BIT) {
527         boxDecoration->SetHasBorderImageOutset(true);
528     }
529     if (bitset | BorderImage::REPEAT_BIT) {
530         boxDecoration->SetHasBorderImageRepeat(true);
531     }
532     if (bitset | BorderImage::SLICE_BIT) {
533         boxDecoration->SetHasBorderImageSlice(true);
534     }
535     if (bitset | BorderImage::SOURCE_BIT) {
536         boxDecoration->SetHasBorderImageSource(true);
537     }
538     if (bitset | BorderImage::WIDTH_BIT) {
539         boxDecoration->SetHasBorderImageWidth(true);
540     }
541     if (bitset | BorderImage::GRADIENT_BIT) {
542         boxDecoration->SetHasBorderImageGradient(true);
543     }
544     boxDecoration->SetBorderImage(borderImage);
545     boxComponent->SetBackDecoration(boxDecoration);
546 }
547 
SetBorderImageGradient(const NG::Gradient & gradient)548 void ViewAbstractModelImpl::SetBorderImageGradient(const NG::Gradient& gradient)
549 {
550     auto boxDecoration = GetBackDecoration();
551     Gradient borderGradient = ToGradient(gradient);
552     boxDecoration->SetBorderImageGradient(borderGradient);
553     boxDecoration->SetHasBorderImageGradient(true);
554 }
555 
SetLayoutPriority(int32_t priority)556 void ViewAbstractModelImpl::SetLayoutPriority(int32_t priority)
557 {
558     auto flex = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
559     flex->SetDisplayIndex(priority);
560 }
561 
SetPixelRound(uint8_t value)562 void ViewAbstractModelImpl::SetPixelRound(uint8_t value) {}
563 
SetLayoutWeight(float value)564 void ViewAbstractModelImpl::SetLayoutWeight(float value)
565 {
566     auto flex = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
567     flex->SetFlexWeight(value);
568 }
569 
SetLayoutDirection(TextDirection value)570 void ViewAbstractModelImpl::SetLayoutDirection(TextDirection value)
571 {
572     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
573     box->SetTextDirection(value);
574     box->SetInspectorDirection(value);
575     if (value == TextDirection::AUTO) {
576         box->SetTextDirection(
577             AceApplicationInfo::GetInstance().IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
578     }
579 }
580 
SetAspectRatio(float ratio)581 void ViewAbstractModelImpl::SetAspectRatio(float ratio)
582 {
583     auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
584     AnimationOption option = ViewStackProcessor::GetInstance()->GetImplicitAnimationOption();
585     boxComponent->SetAspectRatio(ratio, option);
586 }
587 
SetAlign(const Alignment & alignment)588 void ViewAbstractModelImpl::SetAlign(const Alignment& alignment)
589 {
590     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
591     box->SetAlignment(alignment);
592 }
593 
SetAlignRules(const std::map<AlignDirection,AlignRule> & alignRules)594 void ViewAbstractModelImpl::SetAlignRules(const std::map<AlignDirection, AlignRule>& alignRules)
595 {
596     auto flexItem = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
597     flexItem->SetAlignRules(alignRules);
598 }
599 
SetUseAlign(AlignDeclarationPtr declaration,AlignDeclaration::Edge edge,const std::optional<Dimension> & offset)600 void ViewAbstractModelImpl::SetUseAlign(
601     AlignDeclarationPtr declaration, AlignDeclaration::Edge edge, const std::optional<Dimension>& offset)
602 {
603     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
604     box->SetAlignDeclarationPtr(declaration);
605     box->SetUseAlignSide(edge);
606     if (offset.has_value()) {
607         box->SetUseAlignOffset(offset.value());
608     }
609 }
610 
SetGrid(std::optional<uint32_t> span,std::optional<int32_t> offset,GridSizeType type)611 void ViewAbstractModelImpl::SetGrid(std::optional<uint32_t> span, std::optional<int32_t> offset, GridSizeType type)
612 {
613     auto info = GridContainerModelImpl::GetContainer();
614     if (info != nullptr) {
615         auto builder = ViewStackProcessor::GetInstance()->GetBoxComponent()->GetGridColumnInfoBuilder();
616         builder->SetParent(info);
617         if (span.has_value()) {
618             if (type == GridSizeType::UNDEFINED) {
619                 builder->SetColumns(span.value());
620             } else {
621                 builder->SetSizeColumn(type, span.value());
622             }
623         }
624         if (offset.has_value()) {
625             if (type == GridSizeType::UNDEFINED) {
626                 builder->SetOffset(offset.value());
627             } else {
628                 builder->SetOffset(offset.value(), type);
629             }
630         }
631     }
632 }
633 
SetPosition(const Dimension & x,const Dimension & y)634 void ViewAbstractModelImpl::SetPosition(const Dimension& x, const Dimension& y)
635 {
636     auto flexItemComponent = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
637     flexItemComponent->SetLeft(ToAnimatableDimension(x));
638     flexItemComponent->SetTop(ToAnimatableDimension(y));
639     flexItemComponent->SetPositionType(PositionType::PTABSOLUTE);
640 }
641 
SetOffset(const Dimension & x,const Dimension & y)642 void ViewAbstractModelImpl::SetOffset(const Dimension& x, const Dimension& y)
643 {
644     auto flexItemComponent = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
645     flexItemComponent->SetLeft(ToAnimatableDimension(x));
646     flexItemComponent->SetTop(ToAnimatableDimension(y));
647     flexItemComponent->SetPositionType(PositionType::PTOFFSET);
648 }
649 
MarkAnchor(const Dimension & x,const Dimension & y)650 void ViewAbstractModelImpl::MarkAnchor(const Dimension& x, const Dimension& y)
651 {
652     auto flexItemComponent = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
653     flexItemComponent->SetAnchorX(ToAnimatableDimension(x));
654     flexItemComponent->SetAnchorY(ToAnimatableDimension(y));
655 }
656 
SetScale(float x,float y,float z)657 void ViewAbstractModelImpl::SetScale(float x, float y, float z)
658 {
659     RefPtr<TransformComponent> transform = ViewStackProcessor::GetInstance()->GetTransformComponent();
660     AnimationOption option = ViewStackProcessor::GetInstance()->GetImplicitAnimationOption();
661     transform->Scale(x, y, z, option);
662 }
663 
SetPivot(const Dimension & x,const Dimension & y,const Dimension &)664 void ViewAbstractModelImpl::SetPivot(const Dimension& x, const Dimension& y, const Dimension& /* z */)
665 {
666     RefPtr<TransformComponent> transform = ViewStackProcessor::GetInstance()->GetTransformComponent();
667     transform->SetOriginDimension(DimensionOffset(x, y));
668 }
669 
SetTranslate(const Dimension & x,const Dimension & y,const Dimension & z)670 void ViewAbstractModelImpl::SetTranslate(const Dimension& x, const Dimension& y, const Dimension& z)
671 {
672     RefPtr<TransformComponent> transform = ViewStackProcessor::GetInstance()->GetTransformComponent();
673     AnimationOption option = ViewStackProcessor::GetInstance()->GetImplicitAnimationOption();
674     transform->Translate(x, y, z, option);
675 }
676 
SetRotate(float x,float y,float z,float angle,float perspective)677 void ViewAbstractModelImpl::SetRotate(float x, float y, float z, float angle, float perspective)
678 {
679     RefPtr<TransformComponent> transform = ViewStackProcessor::GetInstance()->GetTransformComponent();
680     AnimationOption option = ViewStackProcessor::GetInstance()->GetImplicitAnimationOption();
681     if (!option.IsValid()) {
682         auto pipeline = PipelineBase::GetCurrentContext();
683         if (pipeline) {
684             option = pipeline->GetSyncAnimationOption();
685         }
686     }
687 
688     option.SetAllowRunningAsynchronously(false);
689     transform->Rotate(x, y, z, angle, option);
690 }
691 
SetTransformMatrix(const std::vector<float> & matrix)692 void ViewAbstractModelImpl::SetTransformMatrix(const std::vector<float>& matrix)
693 {
694     RefPtr<TransformComponent> transform = ViewStackProcessor::GetInstance()->GetTransformComponent();
695     AnimationOption option = ViewStackProcessor::GetInstance()->GetImplicitAnimationOption();
696     transform->Matrix3d(matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6], matrix[7],
697         matrix[8], matrix[9], matrix[10], matrix[11], matrix[12], matrix[13], matrix[14], matrix[15], option);
698 }
699 
SetOpacity(double opacity,bool passThrough)700 void ViewAbstractModelImpl::SetOpacity(double opacity, bool passThrough)
701 {
702     auto display = ViewStackProcessor::GetInstance()->GetDisplayComponent();
703     auto stack = ViewStackProcessor::GetInstance();
704     auto option = stack->GetImplicitAnimationOption();
705     if (!stack->IsVisualStateSet()) {
706         display->SetOpacity(opacity, option);
707     } else {
708         display->GetStateAttributes()->AddAttribute<AnimatableDouble>(
709             DisplayStateAttribute::OPACITY, AnimatableDouble(opacity, option), stack->GetVisualState());
710         if (!display->GetStateAttributes()->HasAttribute(DisplayStateAttribute::OPACITY, VisualState::NORMAL)) {
711             display->GetStateAttributes()->AddAttribute<AnimatableDouble>(
712                 DisplayStateAttribute::OPACITY, AnimatableDouble(display->GetOpacity(), option), VisualState::NORMAL);
713         }
714     }
715     if (passThrough && ViewStackProcessor::GetInstance()->HasDisplayComponent()) {
716         auto display = ViewStackProcessor::GetInstance()->GetDisplayComponent();
717         display->DisableLayer(true);
718     }
719 }
720 
SetTransition(const NG::TransitionOptions & transitionOptions,bool passThrough)721 void ViewAbstractModelImpl::SetTransition(const NG::TransitionOptions& transitionOptions, bool passThrough)
722 {
723     if (transitionOptions.HasOpacity()) {
724         auto display = ViewStackProcessor::GetInstance()->GetDisplayComponent();
725         display->SetTransition(transitionOptions.Type, transitionOptions.GetOpacityValue());
726     }
727     if (transitionOptions.HasTranslate()) {
728         auto transform = ViewStackProcessor::GetInstance()->GetTransformComponent();
729         const auto& value = transitionOptions.GetTranslateValue();
730         transform->SetTranslateTransition(transitionOptions.Type, value.x, value.y, value.z);
731     }
732     if (transitionOptions.HasScale()) {
733         auto transform = ViewStackProcessor::GetInstance()->GetTransformComponent();
734         const auto& value = transitionOptions.GetScaleValue();
735         transform->SetScaleTransition(transitionOptions.Type, value.xScale, value.yScale, value.zScale);
736         transform->SetOriginDimension(DimensionOffset(value.centerX, value.centerY));
737     }
738     if (transitionOptions.HasRotate()) {
739         auto transform = ViewStackProcessor::GetInstance()->GetTransformComponent();
740         const auto& value = transitionOptions.GetRotateValue();
741         transform->SetRotateTransition(
742             transitionOptions.Type, value.xDirection, value.yDirection, value.zDirection, value.angle);
743         transform->SetOriginDimension(DimensionOffset(value.centerX, value.centerY));
744     }
745     if (passThrough && ViewStackProcessor::GetInstance()->HasDisplayComponent()) {
746         auto display = ViewStackProcessor::GetInstance()->GetDisplayComponent();
747         display->DisableLayer(true);
748     }
749 }
750 
SetOverlay(const std::string & text,const std::function<void ()> && buildFunc,const std::optional<Alignment> & align,const std::optional<Dimension> & offsetX,const std::optional<Dimension> & offsetY)751 void ViewAbstractModelImpl::SetOverlay(const std::string& text, const std::function<void()>&& buildFunc,
752     const std::optional<Alignment>& align, const std::optional<Dimension>& offsetX,
753     const std::optional<Dimension>& offsetY)
754 {
755     if (buildFunc) {
756         return;
757     }
758     auto coverageComponent = ViewStackProcessor::GetInstance()->GetCoverageComponent();
759     coverageComponent->SetTextVal(text);
760     coverageComponent->SetIsOverLay(true);
761     coverageComponent->SetAlignment(align.value_or(Alignment::TOP_LEFT));
762     if (offsetX.has_value()) {
763         coverageComponent->SetX(offsetX.value());
764     }
765     if (offsetY.has_value()) {
766         coverageComponent->SetY(offsetY.value());
767     }
768 }
769 
SetVisibility(VisibleType visible,std::function<void (int32_t)> && changeEventFunc)770 void ViewAbstractModelImpl::SetVisibility(VisibleType visible, std::function<void(int32_t)>&& changeEventFunc)
771 {
772     auto display = ViewStackProcessor::GetInstance()->GetDisplayComponent();
773     display->SetVisible(visible);
774     auto eventMarker = EventMarker([func = std::move(changeEventFunc)](const BaseEventInfo* info) {
775         const auto& param = info->GetType();
776         int32_t newValue = StringToInt(param);
777         func(newValue);
778     });
779 
780     display->SetVisibleChangeEvent(eventMarker);
781 }
782 
SetSharedTransition(const std::string & shareId,const std::shared_ptr<SharedTransitionOption> & option)783 void ViewAbstractModelImpl::SetSharedTransition(
784     const std::string& shareId, const std::shared_ptr<SharedTransitionOption>& option)
785 {
786     auto sharedTransitionComponent = ViewStackProcessor::GetInstance()->GetSharedTransitionComponent();
787     sharedTransitionComponent->SetShareId(shareId);
788     if (!option) {
789         return;
790     }
791     TweenOption tweenOption;
792     tweenOption.SetCurve(option->curve);
793     tweenOption.SetDuration(option->duration);
794     tweenOption.SetDelay(option->delay);
795     tweenOption.SetMotionPathOption(option->motionPathOption);
796     auto sharedTransitionEffect =
797         SharedTransitionEffect::GetSharedTransitionEffect(option->type, sharedTransitionComponent->GetShareId());
798     sharedTransitionComponent->SetEffect(sharedTransitionEffect);
799     sharedTransitionComponent->SetOption(tweenOption);
800     if (option->zIndex != 0) {
801         sharedTransitionComponent->SetZIndex(option->zIndex);
802     }
803 }
804 
SetGeometryTransition(const std::string & id,bool followWithoutTransition)805 void ViewAbstractModelImpl::SetGeometryTransition(const std::string& id, bool followWithoutTransition)
806 {
807     auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
808     boxComponent->SetGeometryTransitionId(id);
809 }
810 
SetMotionPath(const MotionPathOption & option)811 void ViewAbstractModelImpl::SetMotionPath(const MotionPathOption& option)
812 {
813     if (option.GetRotate()) {
814         ViewStackProcessor::GetInstance()->GetTransformComponent();
815     }
816     auto flexItem = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
817     flexItem->SetMotionPathOption(option);
818 }
819 
SetFlexBasis(const Dimension & value)820 void ViewAbstractModelImpl::SetFlexBasis(const Dimension& value)
821 {
822     auto flexItem = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
823     flexItem->SetFlexBasis(value);
824 }
825 
SetAlignSelf(FlexAlign value)826 void ViewAbstractModelImpl::SetAlignSelf(FlexAlign value)
827 {
828     auto flexItem = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
829     flexItem->SetAlignSelf(value);
830 }
831 
SetFlexShrink(float value)832 void ViewAbstractModelImpl::SetFlexShrink(float value)
833 {
834     auto flexItem = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
835     flexItem->SetFlexShrink(value);
836 }
837 
SetFlexGrow(float value)838 void ViewAbstractModelImpl::SetFlexGrow(float value)
839 {
840     auto flexItem = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
841     flexItem->SetFlexGrow(value);
842 }
843 
SetDisplayIndex(int32_t value)844 void ViewAbstractModelImpl::SetDisplayIndex(int32_t value)
845 {
846     auto flexItem = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
847     flexItem->SetDisplayIndex(value);
848 }
849 
SetZIndex(int32_t value)850 void ViewAbstractModelImpl::SetZIndex(int32_t value)
851 {
852     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
853     auto renderComponent = AceType::DynamicCast<RenderComponent>(component);
854     if (renderComponent) {
855         renderComponent->SetZIndex(value);
856     }
857 }
858 
SetLinearGradient(const NG::Gradient & gradient)859 void ViewAbstractModelImpl::SetLinearGradient(const NG::Gradient& gradient)
860 {
861     auto lineGradient = ToGradient(gradient);
862     auto* stack = ViewStackProcessor::GetInstance();
863     if (!stack->IsVisualStateSet()) {
864         auto decoration = GetBackDecoration();
865         if (decoration) {
866             decoration->SetGradient(lineGradient);
867         }
868     } else {
869         auto boxComponent = stack->GetBoxComponent();
870         boxComponent->GetStateAttributes()->AddAttribute<Gradient>(
871             BoxStateAttribute::GRADIENT, lineGradient, stack->GetVisualState());
872         if (!boxComponent->GetStateAttributes()->HasAttribute(BoxStateAttribute::GRADIENT, VisualState::NORMAL)) {
873             boxComponent->GetStateAttributes()->AddAttribute<Gradient>(
874                 BoxStateAttribute::GRADIENT, GetBackDecoration()->GetGradient(), VisualState::NORMAL);
875         }
876     }
877 }
878 
SetSweepGradient(const NG::Gradient & gradient)879 void ViewAbstractModelImpl::SetSweepGradient(const NG::Gradient& gradient)
880 {
881     auto sweepGradient = ToGradient(gradient);
882     auto* stack = ViewStackProcessor::GetInstance();
883     if (!stack->IsVisualStateSet()) {
884         auto decoration = GetBackDecoration();
885         if (decoration) {
886             decoration->SetGradient(sweepGradient);
887         }
888     } else {
889         auto boxComponent = stack->GetBoxComponent();
890         boxComponent->GetStateAttributes()->AddAttribute<Gradient>(
891             BoxStateAttribute::GRADIENT, sweepGradient, stack->GetVisualState());
892         if (!boxComponent->GetStateAttributes()->HasAttribute(BoxStateAttribute::GRADIENT, VisualState::NORMAL)) {
893             boxComponent->GetStateAttributes()->AddAttribute<Gradient>(
894                 BoxStateAttribute::GRADIENT, GetBackDecoration()->GetGradient(), VisualState::NORMAL);
895         }
896     }
897 }
898 
SetRadialGradient(const NG::Gradient & gradient)899 void ViewAbstractModelImpl::SetRadialGradient(const NG::Gradient& gradient)
900 {
901     auto radialGradient = ToGradient(gradient);
902     auto* stack = ViewStackProcessor::GetInstance();
903     if (!stack->IsVisualStateSet()) {
904         auto decoration = GetBackDecoration();
905         if (decoration) {
906             decoration->SetGradient(radialGradient);
907         }
908     } else {
909         auto boxComponent = stack->GetBoxComponent();
910         boxComponent->GetStateAttributes()->AddAttribute<Gradient>(
911             BoxStateAttribute::GRADIENT, radialGradient, stack->GetVisualState());
912         if (!boxComponent->GetStateAttributes()->HasAttribute(BoxStateAttribute::GRADIENT, VisualState::NORMAL)) {
913             boxComponent->GetStateAttributes()->AddAttribute<Gradient>(
914                 BoxStateAttribute::GRADIENT, GetBackDecoration()->GetGradient(), VisualState::NORMAL);
915         }
916     }
917 }
918 
SetClipShape(const RefPtr<BasicShape> & shape)919 void ViewAbstractModelImpl::SetClipShape(const RefPtr<BasicShape>& shape)
920 {
921     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
922     auto clipPath = AceType::MakeRefPtr<ClipPath>();
923     clipPath->SetBasicShape(shape);
924     box->SetClipPath(clipPath);
925 }
926 
SetClipEdge(bool isClip)927 void ViewAbstractModelImpl::SetClipEdge(bool isClip)
928 {
929     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
930     box->SetBoxClipFlag(isClip);
931 }
932 
SetMask(const RefPtr<BasicShape> & shape)933 void ViewAbstractModelImpl::SetMask(const RefPtr<BasicShape>& shape)
934 {
935     auto maskPath = AceType::MakeRefPtr<MaskPath>();
936     maskPath->SetBasicShape(shape);
937     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
938     box->SetMask(maskPath);
939 }
940 
SetBackdropBlur(const Dimension & radius,const BlurOption & blurOption)941 void ViewAbstractModelImpl::SetBackdropBlur(const Dimension& radius, const BlurOption& blurOption)
942 {
943     auto decoration = GetBackDecoration();
944     decoration->SetBlurRadius(ToAnimatableDimension(radius));
945     decoration->SetBlurStyle(BlurStyleOption());
946 }
947 
SetFrontBlur(const Dimension & radius,const BlurOption & blurOption)948 void ViewAbstractModelImpl::SetFrontBlur(const Dimension& radius, const BlurOption& blurOption)
949 {
950     auto decoration = GetFrontDecoration();
951     decoration->SetBlurRadius(ToAnimatableDimension(radius));
952 }
953 
SetBackShadow(const std::vector<Shadow> & shadows)954 void ViewAbstractModelImpl::SetBackShadow(const std::vector<Shadow>& shadows)
955 {
956     auto backDecoration = GetBackDecoration();
957     backDecoration->SetShadows(shadows);
958 }
959 
SetBlendMode(BlendMode blendMode)960 void ViewAbstractModelImpl::SetBlendMode(BlendMode blendMode)
961 {
962     auto backDecoration = GetBackDecoration();
963     backDecoration->SetBlendMode(blendMode);
964 }
965 
SetBlendApplyType(BlendApplyType blendApplyType)966 void ViewAbstractModelImpl::SetBlendApplyType(BlendApplyType blendApplyType)
967 {
968     auto backDecoration = GetBackDecoration();
969     backDecoration->SetBlendApplyType(blendApplyType);
970 }
971 
SetColorBlend(const Color & value)972 void ViewAbstractModelImpl::SetColorBlend(const Color& value)
973 {
974     auto decoration = GetFrontDecoration();
975     decoration->SetColorBlend(value);
976 }
977 
SetWindowBlur(float progress,WindowBlurStyle blurStyle)978 void ViewAbstractModelImpl::SetWindowBlur(float progress, WindowBlurStyle blurStyle)
979 {
980     auto decoration = GetBackDecoration();
981     decoration->SetWindowBlurProgress(progress);
982     decoration->SetWindowBlurStyle(blurStyle);
983 }
984 
SetBrightness(const Dimension & value)985 void ViewAbstractModelImpl::SetBrightness(const Dimension& value)
986 {
987     auto frontDecoration = GetFrontDecoration();
988     frontDecoration->SetBrightness(value);
989 }
990 
SetGrayScale(const Dimension & value)991 void ViewAbstractModelImpl::SetGrayScale(const Dimension& value)
992 {
993     auto frontDecoration = GetFrontDecoration();
994     frontDecoration->SetGrayScale(value);
995 }
996 
SetContrast(const Dimension & value)997 void ViewAbstractModelImpl::SetContrast(const Dimension& value)
998 {
999     auto frontDecoration = GetFrontDecoration();
1000     frontDecoration->SetContrast(value);
1001 }
1002 
SetSaturate(const Dimension & value)1003 void ViewAbstractModelImpl::SetSaturate(const Dimension& value)
1004 {
1005     auto frontDecoration = GetFrontDecoration();
1006     frontDecoration->SetSaturate(value);
1007 }
1008 
SetSepia(const Dimension & value)1009 void ViewAbstractModelImpl::SetSepia(const Dimension& value)
1010 {
1011     auto frontDecoration = GetFrontDecoration();
1012     frontDecoration->SetSepia(value);
1013 }
1014 
SetInvert(const InvertVariant & value)1015 void ViewAbstractModelImpl::SetInvert(const InvertVariant& value)
1016 {
1017     auto frontDecoration = GetFrontDecoration();
1018     if (value.index() == 0) {
1019         float invert = std::get<float>(value);
1020         frontDecoration->SetInvert(Dimension(invert, DimensionUnit::VP));
1021     }
1022 }
1023 
SetHueRotate(float value)1024 void ViewAbstractModelImpl::SetHueRotate(float value)
1025 {
1026     auto frontDecoration = GetFrontDecoration();
1027     frontDecoration->SetHueRotate(value);
1028 }
1029 
SetOnClick(GestureEventFunc && tapEventFunc,ClickEventFunc && clickEventFunc)1030 void ViewAbstractModelImpl::SetOnClick(GestureEventFunc&& tapEventFunc, ClickEventFunc&& clickEventFunc)
1031 {
1032     auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
1033     CHECK_NULL_VOID(inspector);
1034     auto impl = inspector->GetInspectorFunctionImpl();
1035     RefPtr<Gesture> tapGesture = AceType::MakeRefPtr<TapGesture>(1, 1);
1036     tapGesture->SetOnActionId([func = std::move(tapEventFunc), impl](GestureEvent& info) {
1037         if (impl) {
1038             impl->UpdateEventInfo(info);
1039         }
1040         func(info);
1041     });
1042     auto click = ViewStackProcessor::GetInstance()->GetBoxComponent();
1043     click->SetOnClick(tapGesture);
1044 
1045     auto onClickId = EventMarker([func = std::move(clickEventFunc), impl](const BaseEventInfo* info) {
1046         const auto* clickInfo = TypeInfoHelper::DynamicCast<ClickInfo>(info);
1047         if (!clickInfo) {
1048             return;
1049         }
1050         auto newInfo = *clickInfo;
1051         if (impl) {
1052             impl->UpdateEventInfo(newInfo);
1053         }
1054         func(clickInfo);
1055     });
1056     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(false);
1057     if (focusableComponent) {
1058         focusableComponent->SetOnClickId(onClickId);
1059     }
1060 }
1061 
SetOnTouch(TouchEventFunc && touchEventFunc)1062 void ViewAbstractModelImpl::SetOnTouch(TouchEventFunc&& touchEventFunc)
1063 {
1064     auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
1065     CHECK_NULL_VOID(inspector);
1066     auto impl = inspector->GetInspectorFunctionImpl();
1067     auto onTouchId = EventMarker(
1068         [func = std::move(touchEventFunc), impl](BaseEventInfo* info) {
1069             if (impl) {
1070                 impl->UpdateEventInfo(*info);
1071             }
1072             auto* touchInfo = TypeInfoHelper::DynamicCast<TouchEventInfo>(info);
1073             func(*touchInfo);
1074         },
1075         "onTouch");
1076     auto touchComponent = ViewStackProcessor::GetInstance()->GetTouchListenerComponent();
1077     touchComponent->SetOnTouchId(onTouchId);
1078 }
1079 
SetOnKeyEvent(OnKeyCallbackFunc && onKeyCallback)1080 void ViewAbstractModelImpl::SetOnKeyEvent(OnKeyCallbackFunc&& onKeyCallback)
1081 {
1082     auto onKeyId = EventMarker(
1083         [func = std::move(onKeyCallback)](BaseEventInfo* info) {
1084             auto* keyInfo = TypeInfoHelper::DynamicCast<KeyEventInfo>(info);
1085             func(*keyInfo);
1086         },
1087         "onKey", 0);
1088     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(true);
1089     if (focusableComponent) {
1090         focusableComponent->SetOnKeyId(onKeyId);
1091     }
1092 }
1093 
SetOnMouse(OnMouseEventFunc && onMouseEventFunc)1094 void ViewAbstractModelImpl::SetOnMouse(OnMouseEventFunc&& onMouseEventFunc)
1095 {
1096     auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
1097     CHECK_NULL_VOID(inspector);
1098     auto impl = inspector->GetInspectorFunctionImpl();
1099     auto onMouseId = [func = std::move(onMouseEventFunc), impl](MouseInfo& mouseInfo) {
1100         if (impl) {
1101             impl->UpdateEventInfo(mouseInfo);
1102         }
1103         func(mouseInfo);
1104     };
1105     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
1106     box->SetOnMouseId(onMouseId);
1107 }
1108 
SetOnHover(OnHoverFunc && onHoverEventFunc)1109 void ViewAbstractModelImpl::SetOnHover(OnHoverFunc&& onHoverEventFunc)
1110 {
1111     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
1112     box->SetOnHoverId(onHoverEventFunc);
1113 }
1114 
SetOnDelete(std::function<void ()> && onDeleteCallback)1115 void ViewAbstractModelImpl::SetOnDelete(std::function<void()>&& onDeleteCallback)
1116 {
1117     auto onDeleteId = EventMarker(std::move(onDeleteCallback));
1118     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(false);
1119     if (focusableComponent) {
1120         focusableComponent->SetOnDeleteId(onDeleteId);
1121     }
1122 }
1123 
SetOnAppear(std::function<void ()> && onAppearCallback)1124 void ViewAbstractModelImpl::SetOnAppear(std::function<void()>&& onAppearCallback)
1125 {
1126     auto onAppearId = EventMarker(std::move(onAppearCallback));
1127     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
1128     CHECK_NULL_VOID(component);
1129     component->SetOnAppearEventId(onAppearId);
1130 }
1131 
SetOnDisAppear(std::function<void ()> && onDisAppearCallback)1132 void ViewAbstractModelImpl::SetOnDisAppear(std::function<void()>&& onDisAppearCallback)
1133 {
1134     auto onDisAppearId = EventMarker(std::move(onDisAppearCallback));
1135     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
1136     CHECK_NULL_VOID(component);
1137     component->SetOnDisappearEventId(onDisAppearId);
1138 }
1139 
SetOnAccessibility(std::function<void (const std::string &)> && onAccessibilityCallback)1140 void ViewAbstractModelImpl::SetOnAccessibility(std::function<void(const std::string&)>&& onAccessibilityCallback)
1141 {
1142     auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
1143     CHECK_NULL_VOID(inspector);
1144     inspector->SetAccessibilityEvent(EventMarker(std::move(onAccessibilityCallback)));
1145 }
1146 
SetOnRemoteMessage(RemoteCallback && onRemoteCallback)1147 void ViewAbstractModelImpl::SetOnRemoteMessage(RemoteCallback&& onRemoteCallback)
1148 {
1149     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
1150     box->SetRemoteMessageEvent(EventMarker(std::move(onRemoteCallback)));
1151 }
1152 
SetOnFocusMove(std::function<void (int32_t)> && onFocusMoveCallback)1153 void ViewAbstractModelImpl::SetOnFocusMove(std::function<void(int32_t)>&& onFocusMoveCallback)
1154 {
1155     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(false);
1156     if (focusableComponent) {
1157         focusableComponent->SetOnFocusMove(onFocusMoveCallback);
1158     }
1159 }
1160 
SetOnFocus(OnFocusFunc && onFocusCallback)1161 void ViewAbstractModelImpl::SetOnFocus(OnFocusFunc&& onFocusCallback)
1162 {
1163     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(true);
1164     if (focusableComponent) {
1165         focusableComponent->SetOnFocus(onFocusCallback);
1166     }
1167 }
1168 
SetOnBlur(OnBlurFunc && onBlurCallback)1169 void ViewAbstractModelImpl::SetOnBlur(OnBlurFunc&& onBlurCallback)
1170 {
1171     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(true);
1172     if (focusableComponent) {
1173         focusableComponent->SetOnBlur(onBlurCallback);
1174     }
1175 }
1176 
SetOnDragStart(NG::OnDragStartFunc && onDragStart)1177 void ViewAbstractModelImpl::SetOnDragStart(NG::OnDragStartFunc&& onDragStart)
1178 {
1179     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
1180     box->SetOnDragStartId(ToDragFunc(std::move(onDragStart)));
1181 }
1182 
SetOnDragEnter(NG::OnDragDropFunc && onDragEnter)1183 void ViewAbstractModelImpl::SetOnDragEnter(NG::OnDragDropFunc&& onDragEnter)
1184 {
1185     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
1186     box->SetOnDragEnterId(onDragEnter);
1187 }
1188 
SetOnDragEnd(OnNewDragFunc && onDragEnd)1189 void ViewAbstractModelImpl::SetOnDragEnd(OnNewDragFunc&& onDragEnd)
1190 {
1191     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
1192     box->SetOnDragEndId(onDragEnd);
1193 }
1194 
SetOnDragLeave(NG::OnDragDropFunc && onDragLeave)1195 void ViewAbstractModelImpl::SetOnDragLeave(NG::OnDragDropFunc&& onDragLeave)
1196 {
1197     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
1198     box->SetOnDragLeaveId(onDragLeave);
1199 }
1200 
SetOnDragMove(NG::OnDragDropFunc && onDragMove)1201 void ViewAbstractModelImpl::SetOnDragMove(NG::OnDragDropFunc&& onDragMove)
1202 {
1203     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
1204     box->SetOnDragMoveId(onDragMove);
1205 }
1206 
SetOnDrop(NG::OnDragDropFunc && onDrop)1207 void ViewAbstractModelImpl::SetOnDrop(NG::OnDragDropFunc&& onDrop)
1208 {
1209     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
1210     box->SetOnDropId(onDrop);
1211 }
1212 
SetOnVisibleChange(std::function<void (bool,double)> && onVisibleChange,const std::vector<double> & ratios)1213 void ViewAbstractModelImpl::SetOnVisibleChange(
1214     std::function<void(bool, double)>&& onVisibleChange, const std::vector<double>& ratios)
1215 {
1216     auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
1217     CHECK_NULL_VOID(inspector);
1218     auto container = Container::Current();
1219     CHECK_NULL_VOID(container);
1220     auto context = AceType::DynamicCast<PipelineContext>(container->GetPipelineContext());
1221     CHECK_NULL_VOID(context);
1222     auto nodeId = inspector->GetId();
1223 
1224     for (const auto& ratio : ratios) {
1225         context->AddVisibleAreaChangeNode(nodeId, ratio, onVisibleChange);
1226     }
1227 }
1228 
SetOnAreaChanged(std::function<void (const Rect &,const Offset &,const Rect &,const Offset &)> && onAreaChanged)1229 void ViewAbstractModelImpl::SetOnAreaChanged(
1230     std::function<void(const Rect&, const Offset&, const Rect&, const Offset&)>&& onAreaChanged)
1231 {
1232     auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
1233     boxComponent->GetEventExtensions()->GetOnAreaChangeExtension()->AddOnAreaChangeEvent(std::move(onAreaChanged));
1234 }
1235 
SetResponseRegion(const std::vector<DimensionRect> & responseRegion)1236 void ViewAbstractModelImpl::SetResponseRegion(const std::vector<DimensionRect>& responseRegion)
1237 {
1238     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
1239     auto renderComponent = AceType::DynamicCast<RenderComponent>(component);
1240     if (renderComponent) {
1241         renderComponent->SetResponseRegion(responseRegion);
1242         renderComponent->MarkResponseRegion(true);
1243     }
1244     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
1245     box->SetResponseRegion(responseRegion);
1246     box->MarkResponseRegion(true);
1247     if (ViewStackProcessor::GetInstance()->HasClickGestureListenerComponent()) {
1248         auto click = ViewStackProcessor::GetInstance()->GetClickGestureListenerComponent();
1249         click->SetResponseRegion(responseRegion);
1250         click->MarkResponseRegion(true);
1251     }
1252     if (ViewStackProcessor::GetInstance()->HasTouchListenerComponent()) {
1253         auto touch = ViewStackProcessor::GetInstance()->GetTouchListenerComponent();
1254         touch->SetResponseRegion(responseRegion);
1255         touch->MarkResponseRegion(true);
1256     }
1257 }
1258 
SetEnabled(bool enabled)1259 void ViewAbstractModelImpl::SetEnabled(bool enabled)
1260 {
1261     auto mainComponent = ViewStackProcessor::GetInstance()->GetMainComponent();
1262     if (mainComponent) {
1263         mainComponent->SetDisabledStatus(!enabled);
1264     }
1265 
1266     auto focusComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(!enabled);
1267     if (focusComponent) {
1268         focusComponent->SetEnabled(enabled);
1269     }
1270 }
1271 
SetTouchable(bool touchable)1272 void ViewAbstractModelImpl::SetTouchable(bool touchable)
1273 {
1274     auto mainComponent = ViewStackProcessor::GetInstance()->GetMainComponent();
1275     CHECK_NULL_VOID(mainComponent);
1276     mainComponent->SetTouchable(touchable);
1277 }
1278 
SetFocusable(bool focusable)1279 void ViewAbstractModelImpl::SetFocusable(bool focusable)
1280 {
1281     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent();
1282     if (focusableComponent) {
1283         focusableComponent->SetFocusable(focusable);
1284     }
1285 }
1286 
SetFocusNode(bool focus)1287 void ViewAbstractModelImpl::SetFocusNode(bool focus)
1288 {
1289     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(false);
1290     if (focusableComponent) {
1291         focusableComponent->SetFocusNode(!focus);
1292     }
1293 }
1294 
SetTabIndex(int32_t index)1295 void ViewAbstractModelImpl::SetTabIndex(int32_t index)
1296 {
1297     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(true);
1298     if (focusableComponent) {
1299         focusableComponent->SetFocusable(true);
1300         focusableComponent->SetTabIndex(index);
1301     }
1302 }
1303 
SetFocusOnTouch(bool isSet)1304 void ViewAbstractModelImpl::SetFocusOnTouch(bool isSet)
1305 {
1306     auto touchComponent = ViewStackProcessor::GetInstance()->GetTouchListenerComponent();
1307     if (!touchComponent) {
1308         LOGE("Touch listener component get failed!");
1309         return;
1310     }
1311     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(true);
1312     if (!focusableComponent) {
1313         LOGE("focusable component get failed!");
1314         return;
1315     }
1316     focusableComponent->SetIsFocusOnTouch(isSet);
1317     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
1318     if (!component) {
1319         LOGE("main component get failed!");
1320         return;
1321     }
1322     component->SetIsFocusOnTouch(isSet);
1323 }
1324 
SetDefaultFocus(bool isSet)1325 void ViewAbstractModelImpl::SetDefaultFocus(bool isSet)
1326 {
1327     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(true);
1328     if (!focusableComponent) {
1329         LOGE("focusable component get failed!");
1330         return;
1331     }
1332     focusableComponent->SetIsDefaultFocus(isSet);
1333 }
1334 
SetGroupDefaultFocus(bool isSet)1335 void ViewAbstractModelImpl::SetGroupDefaultFocus(bool isSet)
1336 {
1337     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(true);
1338     if (!focusableComponent) {
1339         LOGE("focusable component get failed!");
1340         return;
1341     }
1342     focusableComponent->SetIsDefaultGroupFocus(isSet);
1343 }
1344 
SetInspectorId(const std::string & inspectorId)1345 void ViewAbstractModelImpl::SetInspectorId(const std::string& inspectorId)
1346 {
1347     auto component = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
1348     if (component) {
1349         component->SetInspectorKey(inspectorId);
1350     }
1351 
1352     if (!AceType::InstanceOf<TextSpanComponent>(ViewStackProcessor::GetInstance()->GetMainComponent())) {
1353         auto flexItem = ViewStackProcessor::GetInstance()->GetFlexItemComponent();
1354         if (flexItem) {
1355             flexItem->SetInspectorKey(inspectorId);
1356         }
1357     }
1358 
1359     if (!AceType::InstanceOf<TextSpanComponent>(ViewStackProcessor::GetInstance()->GetMainComponent())) {
1360         auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent();
1361         if (focusableComponent) {
1362             focusableComponent->SetInspectorKey(inspectorId);
1363         }
1364     }
1365 }
1366 
SetRestoreId(int32_t restoreId)1367 void ViewAbstractModelImpl::SetRestoreId(int32_t restoreId)
1368 {
1369     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
1370     if (component) {
1371         component->SetRestoreId(restoreId);
1372     }
1373 }
1374 
SetDebugLine(const std::string & line)1375 void ViewAbstractModelImpl::SetDebugLine(const std::string& line)
1376 {
1377     auto component = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
1378     if (component) {
1379         component->SetDebugLine(line);
1380     }
1381 }
1382 
SetHoverEffect(HoverEffectType hoverEffect)1383 void ViewAbstractModelImpl::SetHoverEffect(HoverEffectType hoverEffect)
1384 {
1385     auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
1386     if (!boxComponent) {
1387         LOGE("boxComponent is null");
1388         return;
1389     }
1390     boxComponent->SetMouseAnimationType(static_cast<HoverAnimationType>(hoverEffect));
1391 }
1392 
SetHitTestMode(NG::HitTestMode hitTestMode)1393 void ViewAbstractModelImpl::SetHitTestMode(NG::HitTestMode hitTestMode)
1394 {
1395     auto mode = static_cast<HitTestMode>(hitTestMode);
1396     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
1397     if (component) {
1398         component->SetHitTestMode(mode);
1399     }
1400 }
1401 
BindPopup(const RefPtr<PopupParam> & param,const RefPtr<AceType> & customNode)1402 void ViewAbstractModelImpl::BindPopup(const RefPtr<PopupParam>& param, const RefPtr<AceType>& customNode)
1403 {
1404     ViewStackProcessor::GetInstance()->GetCoverageComponent();
1405     auto popupComponent = ViewStackProcessor::GetInstance()->GetPopupComponent(true);
1406     CHECK_NULL_VOID(popupComponent);
1407 
1408     auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
1409     param->SetTargetMargin(boxComponent->GetMargin());
1410     auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
1411     CHECK_NULL_VOID(inspector);
1412     param->SetTargetId(inspector->GetId());
1413 
1414     popupComponent->SetPopupParam(param);
1415     if (param->GetOnStateChange()) {
1416         auto changeEvent = EventMarker(param->GetOnStateChange());
1417         popupComponent->SetOnStateChange(changeEvent);
1418     }
1419     popupComponent->SetMessage(param->GetMessage());
1420     popupComponent->SetPlacementOnTop(param->GetPlacement() == Placement::TOP);
1421 
1422     auto btnPropFirst = param->GetPrimaryButtonProperties();
1423     if (btnPropFirst.touchFunc) {
1424         btnPropFirst.actionId = EventMarker([onTouch = btnPropFirst.touchFunc]() {
1425             TouchEventInfo info("unknown");
1426             onTouch(info);
1427         });
1428     }
1429     popupComponent->SetPrimaryButtonProperties(btnPropFirst);
1430 
1431     auto btnPropSecond = param->GetSecondaryButtonProperties();
1432     if (btnPropSecond.touchFunc) {
1433         btnPropSecond.actionId = EventMarker([onTouch = btnPropSecond.touchFunc]() {
1434             TouchEventInfo info("unknown");
1435             onTouch(info);
1436         });
1437     }
1438     popupComponent->SetSecondaryButtonProperties(btnPropSecond);
1439 
1440     auto customComponent = AceType::DynamicCast<Component>(customNode);
1441     if (customComponent) {
1442         popupComponent->SetCustomComponent(customComponent);
1443     }
1444 }
1445 
GetSelectTheme()1446 RefPtr<SelectTheme> GetSelectTheme()
1447 {
1448     auto container = Container::Current();
1449     CHECK_NULL_RETURN(container, nullptr);
1450     auto context = container->GetPipelineContext();
1451     CHECK_NULL_RETURN(context, nullptr);
1452     return context->GetTheme<SelectTheme>();
1453 }
1454 
BindBackground(std::function<void ()> && buildFunc,const Alignment & align)1455 void ViewAbstractModelImpl::BindBackground(std::function<void()>&& buildFunc, const Alignment& align) {}
1456 
CreateMenuEventWithParams(const WeakPtr<OHOS::Ace::MenuComponent> & weak,std::vector<NG::OptionParam> && params)1457 GestureEventFunc CreateMenuEventWithParams(
1458     const WeakPtr<OHOS::Ace::MenuComponent>& weak, std::vector<NG::OptionParam>&& params)
1459 {
1460     return [weak, params](const GestureEvent& info) {
1461         auto menuComponent = weak.Upgrade();
1462         CHECK_NULL_VOID(menuComponent);
1463         auto menuTheme = GetSelectTheme();
1464         if (menuTheme) {
1465             menuComponent->SetTheme(menuTheme);
1466         }
1467         menuComponent->ClearOptions();
1468 
1469         for (const auto& param : params) {
1470             auto optionTheme = GetSelectTheme();
1471             if (!optionTheme) {
1472                 continue;
1473             }
1474             auto optionComponent = AceType::MakeRefPtr<OHOS::Ace::OptionComponent>(optionTheme);
1475             auto textComponent = AceType::MakeRefPtr<OHOS::Ace::TextComponent>(param.value);
1476 
1477             optionComponent->SetTextStyle(optionTheme->GetOptionTextStyle());
1478             optionComponent->SetTheme(optionTheme);
1479             optionComponent->SetText(textComponent);
1480             optionComponent->SetValue(param.value);
1481             optionComponent->SetCustomizedCallback(param.action);
1482             optionComponent->SetSelectedBackgroundColor(optionTheme->GetSelectedColor());
1483             menuComponent->AppendOption(optionComponent);
1484         }
1485 
1486         auto showDialog = menuComponent->GetTargetCallback();
1487         showDialog("BindMenu", info.GetGlobalLocation());
1488     };
1489 }
1490 
ExecMenuBuilder(const std::function<void ()> & builderFunc,const RefPtr<MenuComponent> & menuComponent)1491 void ExecMenuBuilder(const std::function<void()>& builderFunc, const RefPtr<MenuComponent>& menuComponent)
1492 {
1493     // use another VSP instance while executing the builder function
1494     ScopedViewStackProcessor builderViewStackProcessor;
1495     {
1496         ACE_SCORING_EVENT("contextMenu.builder");
1497         builderFunc();
1498     }
1499     auto customComponent = ViewStackProcessor::GetInstance()->Finish();
1500     CHECK_NULL_VOID(customComponent);
1501 
1502     // Set the theme
1503     auto menuTheme = GetSelectTheme();
1504     if (menuTheme) {
1505         menuComponent->SetTheme(menuTheme);
1506     }
1507     auto optionTheme = GetSelectTheme();
1508     auto optionComponent = AceType::MakeRefPtr<OHOS::Ace::OptionComponent>(optionTheme);
1509 
1510     // Set the custom component
1511     optionComponent->SetCustomComponent(customComponent);
1512     menuComponent->ClearOptions();
1513     menuComponent->AppendOption(optionComponent);
1514 }
1515 
CreateMenuEventWithBuilder(const WeakPtr<OHOS::Ace::MenuComponent> & weak,std::function<void ()> && buildFunc)1516 GestureEventFunc CreateMenuEventWithBuilder(
1517     const WeakPtr<OHOS::Ace::MenuComponent>& weak, std::function<void()>&& buildFunc)
1518 {
1519     return [weak, builderFunc = std::move(buildFunc)](const GestureEvent& info) {
1520         auto menuComponent = weak.Upgrade();
1521         CHECK_NULL_VOID(menuComponent);
1522         menuComponent->SetIsCustomMenu(true);
1523         ExecMenuBuilder(builderFunc, menuComponent);
1524         auto showDialog = menuComponent->GetTargetCallback();
1525         showDialog("BindMenu", info.GetGlobalLocation());
1526     };
1527 }
1528 
BindMenu(std::vector<NG::OptionParam> && params,std::function<void ()> && buildFunc,const NG::MenuParam &)1529 void ViewAbstractModelImpl::BindMenu(
1530     std::vector<NG::OptionParam>&& params, std::function<void()>&& buildFunc, const NG::MenuParam&)
1531 {
1532     ViewStackProcessor::GetInstance()->GetCoverageComponent();
1533     auto menuComponent = ViewStackProcessor::GetInstance()->GetMenuComponent(true);
1534     CHECK_NULL_VOID(menuComponent);
1535     auto weak = WeakPtr<OHOS::Ace::MenuComponent>(menuComponent);
1536     GestureEventFunc eventFunc;
1537     if (!params.empty()) {
1538         eventFunc = CreateMenuEventWithParams(weak, std::move(params));
1539     } else if (buildFunc) {
1540         eventFunc = CreateMenuEventWithBuilder(weak, std::move(buildFunc));
1541     } else {
1542         LOGE("No param object.");
1543         return;
1544     }
1545     auto click = ViewStackProcessor::GetInstance()->GetBoxComponent();
1546     RefPtr<Gesture> tapGesture = AceType::MakeRefPtr<TapGesture>();
1547     tapGesture->SetOnActionId(eventFunc);
1548     click->SetOnClick(tapGesture);
1549 }
1550 
BindContextMenu(ResponseType type,std::function<void ()> & buildFunc,const NG::MenuParam & menuParam,std::function<void ()> & previewBuildFunc)1551 void ViewAbstractModelImpl::BindContextMenu(ResponseType type, std::function<void()>& buildFunc,
1552     const NG::MenuParam& menuParam, std::function<void()>& previewBuildFunc)
1553 {
1554     ViewStackProcessor::GetInstance()->GetCoverageComponent();
1555     auto menuComponent = ViewStackProcessor::GetInstance()->GetMenuComponent(true);
1556     CHECK_NULL_VOID(menuComponent);
1557 #if defined(MULTIPLE_WINDOW_SUPPORTED)
1558     menuComponent->SetIsContextMenu(true);
1559 #endif
1560 
1561     auto weak = WeakPtr<OHOS::Ace::MenuComponent>(menuComponent);
1562     if (type == ResponseType::RIGHT_CLICK) {
1563         auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
1564         box->SetOnMouseId([weak, builderFunc = std::move(buildFunc)](MouseInfo& info) {
1565             auto menuComponent = weak.Upgrade();
1566             CHECK_NULL_VOID(menuComponent);
1567             if (info.GetButton() == MouseButton::RIGHT_BUTTON && info.GetAction() == MouseAction::RELEASE) {
1568                 ExecMenuBuilder(builderFunc, menuComponent);
1569                 auto showMenu = menuComponent->GetTargetCallback();
1570                 info.SetStopPropagation(true);
1571                 LOGI("Context menu is triggered, type is right click.");
1572 #if defined(MULTIPLE_WINDOW_SUPPORTED)
1573                 showMenu("", info.GetScreenLocation());
1574 #else
1575                 showMenu("", info.GetGlobalLocation());
1576 #endif
1577             }
1578         });
1579     } else if (type == ResponseType::LONG_PRESS) {
1580         auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
1581         RefPtr<Gesture> longGesture = AceType::MakeRefPtr<LongPressGesture>(
1582             DEFAULT_LONG_PRESS_FINGER, false, DEFAULT_LONG_PRESS_DURATION, false, true);
1583         longGesture->SetOnActionId([weak, builderFunc = std::move(buildFunc)](const GestureEvent& info) mutable {
1584             auto menuComponent = weak.Upgrade();
1585             CHECK_NULL_VOID(menuComponent);
1586             ExecMenuBuilder(builderFunc, menuComponent);
1587             auto showMenu = menuComponent->GetTargetCallback();
1588 #if defined(MULTIPLE_WINDOW_SUPPORTED)
1589             showMenu("", info.GetScreenLocation());
1590 #else
1591             showMenu("", info.GetGlobalLocation());
1592 #endif
1593         });
1594         box->SetOnLongPress(longGesture);
1595     } else {
1596         LOGE("The arg responseType is invalid.");
1597     }
1598 }
1599 
SetAccessibilityGroup(bool accessible)1600 void ViewAbstractModelImpl::SetAccessibilityGroup(bool accessible)
1601 {
1602     auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
1603     if (!inspector) {
1604         LOGE("this component does not have inspector");
1605         return;
1606     }
1607     inspector->SetAccessibilityGroup(accessible);
1608 }
1609 
SetAccessibilityText(const std::string & text)1610 void ViewAbstractModelImpl::SetAccessibilityText(const std::string& text)
1611 {
1612     auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
1613     if (!inspector) {
1614         LOGE("this component does not have inspector");
1615         return;
1616     }
1617     inspector->SetAccessibilitytext(text);
1618 }
1619 
SetAccessibilityVirtualNode(std::function<void ()> && buildFunc)1620 void ViewAbstractModelImpl::SetAccessibilityVirtualNode(std::function<void()>&& buildFunc) {}
1621 
SetAccessibilityDescription(const std::string & description)1622 void ViewAbstractModelImpl::SetAccessibilityDescription(const std::string& description)
1623 {
1624     auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
1625     if (!inspector) {
1626         LOGE("this component does not have inspector");
1627         return;
1628     }
1629     inspector->SetAccessibilityDescription(description);
1630 }
1631 
SetAccessibilityImportance(const std::string & importance)1632 void ViewAbstractModelImpl::SetAccessibilityImportance(const std::string& importance)
1633 {
1634     auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
1635     if (!inspector) {
1636         LOGE("this component does not have inspector");
1637         return;
1638     }
1639     inspector->SetAccessibilityImportance(importance);
1640 }
1641 
1642 } // namespace OHOS::Ace::Framework
1643