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