1 /*
2 * Copyright (c) 2021 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/common/focus_animation_manager.h"
17
18 #include "base/utils/utils.h"
19 #include "core/components/focus_animation/render_focus_animation.h"
20 #include "core/components/shadow/render_shadow.h"
21 #include "core/components/shadow/shadow_element.h"
22 #include "core/components/stack/stack_element.h"
23
24 namespace OHOS::Ace {
25
SetFocusAnimationProperties(const RRect & rrect,const Color & color,const Offset & offset,bool isIndented) const26 void FocusAnimationManager::SetFocusAnimationProperties(
27 const RRect& rrect, const Color& color, const Offset& offset, bool isIndented) const
28 {
29 if (focusAnimationStack_.empty() || (useRoot_ && rootFocusAnimationStack_.empty())) {
30 return;
31 }
32 auto focusAnimation = useRoot_ ? rootFocusAnimationStack_.top().Upgrade() : focusAnimationStack_.top().Upgrade();
33 CHECK_NULL_VOID(focusAnimation);
34 if (useRoot_) {
35 auto renderFocusAnimation = focusAnimationStack_.top().Upgrade();
36 if (renderFocusAnimation) {
37 renderFocusAnimation->CancelFocusAnimation();
38 }
39 }
40 focusAnimation->SetFocusAnimationProperties(rrect, color, offset, isIndented);
41 }
42
SetAvailableRect(const Rect & paintRect)43 void FocusAnimationManager::SetAvailableRect(const Rect& paintRect)
44 {
45 availableRect_ = paintRect;
46 }
47
CancelFocusAnimation() const48 void FocusAnimationManager::CancelFocusAnimation() const
49 {
50 if (focusAnimationStack_.empty() || (useRoot_ && rootFocusAnimationStack_.empty())) {
51 return;
52 }
53 auto focusAnimation = useRoot_ ? rootFocusAnimationStack_.top().Upgrade() : focusAnimationStack_.top().Upgrade();
54 CHECK_NULL_VOID(focusAnimation);
55 focusAnimation->CancelFocusAnimation();
56 }
57
PushFocusAnimationElement(const RefPtr<Element> & element)58 void FocusAnimationManager::PushFocusAnimationElement(const RefPtr<Element>& element)
59 {
60 auto focusElement = AceType::DynamicCast<FocusAnimationElement>(element);
61 CHECK_NULL_VOID(focusElement);
62 auto renderFocus = AceType::DynamicCast<RenderFocusAnimation>(focusElement->GetRenderNode());
63 CHECK_NULL_VOID(renderFocus);
64 renderFocus->SetPaintRect(availableRect_);
65 if (focusElement->IsRoot()) {
66 if (!rootFocusAnimationStack_.empty()) {
67 auto focusAnimation = rootFocusAnimationStack_.top().Upgrade();
68 if (focusAnimation) {
69 focusAnimation->CancelFocusAnimation();
70 }
71 }
72 rootFocusAnimationStack_.push(renderFocus);
73 } else {
74 if (!focusAnimationStack_.empty()) {
75 auto focusAnimation = focusAnimationStack_.top().Upgrade();
76 if (focusAnimation) {
77 focusAnimation->CancelFocusAnimation();
78 }
79 }
80 focusAnimationStack_.push(renderFocus);
81 }
82 }
83
PopFocusAnimationElement()84 void FocusAnimationManager::PopFocusAnimationElement()
85 {
86 if (focusAnimationStack_.empty()) {
87 return;
88 }
89 focusAnimationStack_.pop();
90 if (!focusAnimationStack_.empty()) {
91 auto focusAnimation = focusAnimationStack_.top().Upgrade();
92 if (!useRoot_ && focusAnimation) {
93 focusAnimation->StartFocusAnimation();
94 }
95 }
96 }
97
PopRootFocusAnimationElement()98 void FocusAnimationManager::PopRootFocusAnimationElement()
99 {
100 if (rootFocusAnimationStack_.empty()) {
101 return;
102 }
103 rootFocusAnimationStack_.pop();
104 }
105
StartFocusAnimation() const106 void FocusAnimationManager::StartFocusAnimation() const
107 {
108 if (focusAnimationStack_.empty()) {
109 return;
110 }
111 auto focusAnimation = focusAnimationStack_.top().Upgrade();
112 CHECK_NULL_VOID(focusAnimation);
113 focusAnimation->StartFocusAnimation();
114 }
115
StopFocusAnimation() const116 void FocusAnimationManager::StopFocusAnimation() const
117 {
118 if (focusAnimationStack_.empty()) {
119 return;
120 }
121 auto focusAnimation = focusAnimationStack_.top().Upgrade();
122 CHECK_NULL_VOID(focusAnimation);
123 focusAnimation->StopFocusAnimation();
124 }
125
SetFocusAnimationProperties(const RRect & rrect,const Color & color,const Offset & offset,const Rect & clipRect) const126 void FocusAnimationManager::SetFocusAnimationProperties(
127 const RRect& rrect, const Color& color, const Offset& offset, const Rect& clipRect) const
128 {
129 if (focusAnimationStack_.empty()) {
130 return;
131 }
132 auto focusAnimation = focusAnimationStack_.top().Upgrade();
133 CHECK_NULL_VOID(focusAnimation);
134 focusAnimation->SetFocusAnimationProperties(rrect, color, offset, clipRect);
135 }
136
PushShadow(const RefPtr<Element> & element)137 void FocusAnimationManager::PushShadow(const RefPtr<Element>& element)
138 {
139 auto shadowElement = AceType::DynamicCast<ShadowElement>(element);
140 CHECK_NULL_VOID(shadowElement);
141 auto renderShadow = AceType::DynamicCast<RenderShadow>(shadowElement->GetRenderNode());
142 CHECK_NULL_VOID(renderShadow);
143 shadowStack_.push(renderShadow);
144 }
145
PopShadow()146 void FocusAnimationManager::PopShadow()
147 {
148 if (shadowStack_.empty()) {
149 LOGE("shadow stack is empty");
150 return;
151 }
152 shadowStack_.pop();
153 }
154
SetShadowProperties(const RRect & rrect,const Offset & offset)155 void FocusAnimationManager::SetShadowProperties(const RRect& rrect, const Offset& offset)
156 {
157 if (shadowStack_.empty()) {
158 LOGE("shadow stack is empty");
159 return;
160 }
161 auto shadow = shadowStack_.top().Upgrade();
162 CHECK_NULL_VOID(shadow);
163 shadow->SetShadowProperties(rrect, offset);
164 }
165
SetShadowProperties(const RRect & rrect,const Offset & offset,const Rect & clipRect)166 void FocusAnimationManager::SetShadowProperties(const RRect& rrect, const Offset& offset, const Rect& clipRect)
167 {
168 if (shadowStack_.empty()) {
169 LOGE("shadow stack is empty");
170 return;
171 }
172 auto shadow = shadowStack_.top().Upgrade();
173 CHECK_NULL_VOID(shadow);
174 shadow->SetShadowProperties(rrect, offset, clipRect);
175 }
176
CancelShadow() const177 void FocusAnimationManager::CancelShadow() const
178 {
179 if (shadowStack_.empty()) {
180 LOGE("shadow stack is empty");
181 return;
182 }
183 auto shadow = shadowStack_.top().Upgrade();
184 CHECK_NULL_VOID(shadow);
185 shadow->CancelShadow();
186 }
187
GetRenderFocusAnimation() const188 RefPtr<RenderFocusAnimation> FocusAnimationManager::GetRenderFocusAnimation() const
189 {
190 if (focusAnimationStack_.empty() || (useRoot_ && rootFocusAnimationStack_.empty())) {
191 return nullptr;
192 }
193 auto focusAnimation = useRoot_ ? rootFocusAnimationStack_.top().Upgrade() : focusAnimationStack_.top().Upgrade();
194 CHECK_NULL_RETURN(focusAnimation, nullptr);
195 return focusAnimation;
196 }
197
SetUseRoot(bool useRoot)198 void FocusAnimationManager::SetUseRoot(bool useRoot)
199 {
200 useRoot_ = useRoot;
201 if (!useRoot_ && !rootFocusAnimationStack_.empty()) {
202 auto focusAnimation = rootFocusAnimationStack_.top().Upgrade();
203 if (focusAnimation) {
204 focusAnimation->CancelFocusAnimation();
205 }
206 }
207 }
208
SetIsKeyEvent(bool isKeyEvent)209 void FocusAnimationManager::SetIsKeyEvent(bool isKeyEvent)
210 {
211 if (focusAnimationStack_.empty() || (useRoot_ && rootFocusAnimationStack_.empty())) {
212 return;
213 }
214 auto focusAnimation = useRoot_ ? rootFocusAnimationStack_.top().Upgrade() : focusAnimationStack_.top().Upgrade();
215 CHECK_NULL_VOID(focusAnimation);
216 focusAnimation->SetIsKeyEvent(isKeyEvent);
217 }
218
219 } // namespace OHOS::Ace