1 /*
2  * Copyright (c) 2021-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 "core/components/select_popup/select_popup_element.h"
17 
18 #include "core/components/select_popup/render_select_popup.h"
19 
20 namespace OHOS::Ace {
21 
PerformBuild()22 void SelectPopupElement::PerformBuild()
23 {
24     auto component = AceType::DynamicCast<SelectPopupComponent>(component_);
25     auto context = context_.Upgrade();
26     if (!context || !context->GetAccessibilityManager() || !component) {
27         LOGE("select: select popup component is null or initialize failed.");
28         return;
29     }
30     component->Initialize(context->GetAccessibilityManager());
31     component->SetRefreshAnimationCallback([weak = WeakClaim(this)](const TweenOption& option, bool isIn) {
32         auto element = weak.Upgrade();
33         if (element) {
34             element->OnRefreshAnimation(option, isIn);
35         }
36     });
37     SoleChildElement::PerformBuild();
38     auto tween = GetTween(AceType::Claim(this));
39     if (tween && tween->GetController() && component) {
40         component->SetAnimationController(tween->GetController());
41     }
42     auto render = AceType::DynamicCast<RenderSelectPopup>(renderNode_);
43     if (render) {
44         render->UpdateRenders();
45     }
46 #if defined(PREVIEW)
47     if (context && component) {
48         auto manager = context->GetAccessibilityManager();
49         if (manager) {
50             auto node = manager->GetAccessibilityNodeById(component->GetSelectPopupId());
51             auto stackElement = component->GetStackElement();
52             if (node && stackElement) {
53                 node->SetZIndexToChild(stackElement->GetChildrenSize());
54                 manager->ClearNodeRectInfo(node, false);
55             }
56         }
57     }
58 #endif
59 }
60 
GetTween(const RefPtr<Element> & element) const61 RefPtr<TweenElement> SelectPopupElement::GetTween(const RefPtr<Element>& element) const
62 {
63     if (!element) {
64         return nullptr;
65     }
66 
67     if (AceType::InstanceOf<TweenElement>(element)) {
68         return AceType::DynamicCast<TweenElement>(element);
69     }
70 
71     for (const auto& child : element->GetChildren()) {
72         auto tween = GetTween(child);
73         if (tween) {
74             return tween;
75         }
76     }
77 
78     return nullptr;
79 }
80 
OnRefreshAnimation(const TweenOption & option,bool isIn)81 void SelectPopupElement::OnRefreshAnimation(const TweenOption& option, bool isIn)
82 {
83     auto tween = GetTween(AceType::Claim(this));
84     if (!tween) {
85         LOGE("the tween element is null.");
86         return;
87     }
88 
89     auto controller = tween->GetController();
90     if (controller) {
91         controller->ClearInterpolators();
92         controller->ClearAllListeners();
93     }
94     tween->SetOption(option);
95     tween->ApplyOptions();
96     tween->ApplyKeyframes();
97     isInAnimation_ = isIn;
98 }
99 
100 } // namespace OHOS::Ace
101