1 /* 2 * Copyright (c) 2022-2024 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUBBLE_BUBBLE_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUBBLE_BUBBLE_PATTERN_H 18 19 #include <optional> 20 21 #include "base/geometry/ng/offset_t.h" 22 #include "base/geometry/ng/size_t.h" 23 #include "base/memory/referenced.h" 24 #include "core/common/autofill/auto_fill_trigger_state_holder.h" 25 #include "core/components/common/properties/popup_param.h" 26 #include "core/components/popup/popup_theme.h" 27 #include "core/components_ng/base/frame_node.h" 28 #include "core/components_ng/event/focus_hub.h" 29 #include "core/components_ng/manager/focus/focus_view.h" 30 #include "core/components_ng/pattern/bubble/bubble_accessibility_property.h" 31 #include "core/components_ng/pattern/bubble/bubble_event_hub.h" 32 #include "core/components_ng/pattern/bubble/bubble_layout_algorithm.h" 33 #include "core/components_ng/pattern/bubble/bubble_layout_property.h" 34 #include "core/components_ng/pattern/bubble/bubble_paint_method.h" 35 #include "core/components_ng/pattern/bubble/bubble_render_property.h" 36 #include "core/components_ng/pattern/overlay/popup_base_pattern.h" 37 38 namespace OHOS::Ace::NG { 39 40 enum class TransitionStatus { 41 INVISIABLE, 42 ENTERING, 43 NORMAL, 44 EXITING, 45 }; 46 47 enum class DismissReason { 48 BACK_PRESSED = 0, 49 TOUCH_OUTSIDE, 50 CLOSE_BUTTON, 51 }; 52 53 class BubblePattern : public PopupBasePattern, public FocusView, public AutoFillTriggerStateHolder { 54 DECLARE_ACE_TYPE(BubblePattern, PopupBasePattern, FocusView, AutoFillTriggerStateHolder); 55 56 public: 57 BubblePattern() = default; BubblePattern(int32_t id,const std::string & tag)58 BubblePattern(int32_t id, const std::string& tag) : targetNodeId_(id), targetTag_(tag) {} 59 ~BubblePattern() override = default; 60 IsAtomicNode()61 bool IsAtomicNode() const override 62 { 63 return false; 64 } 65 CreateNodePaintMethod()66 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 67 { 68 auto bubbleMethod = AceType::MakeRefPtr<BubblePaintMethod>(); 69 bubbleMethod->SetArrowPosition(arrowPosition_); 70 bubbleMethod->SetChildOffset(childOffset_); 71 bubbleMethod->SetChildSize(childSize_); 72 bubbleMethod->SetShowArrow(showArrow_); 73 bubbleMethod->SetClipPath(clipPath_); 74 bubbleMethod->SetClipFrameNode(clipFrameNode_); 75 bubbleMethod->SetArrowOffsetsFromClip(arrowOffsetsFromClip_); 76 bubbleMethod->SetArrowWidth(arrowWidth_); 77 bubbleMethod->SetArrowHeight(arrowHeight_); 78 bubbleMethod->SetBorder(border_); 79 bubbleMethod->SetArrowBuildPlacement(arrowBuildPlacement_); 80 auto pipeline = PipelineBase::GetCurrentContext(); 81 CHECK_NULL_RETURN(pipeline, bubbleMethod); 82 auto theme = pipeline->GetTheme<PopupTheme>(); 83 CHECK_NULL_RETURN(theme, bubbleMethod); 84 bubbleMethod->SetOuterBorderWidth(theme->GetPopupOuterBorderWidth()); 85 bubbleMethod->SetInnerBorderWidth(theme->GetPopupInnerBorderWidth()); 86 return bubbleMethod; 87 } 88 CreateLayoutProperty()89 RefPtr<LayoutProperty> CreateLayoutProperty() override 90 { 91 auto bubbleProp = AceType::MakeRefPtr<BubbleLayoutProperty>(); 92 bubbleProp->UpdatePropertyChangeFlag(PROPERTY_UPDATE_MEASURE); 93 return bubbleProp; 94 } 95 CreateLayoutAlgorithm()96 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 97 { 98 return MakeRefPtr<BubbleLayoutAlgorithm>(targetNodeId_, targetTag_, targetOffset_, targetSize_); 99 } 100 CreatePaintProperty()101 RefPtr<PaintProperty> CreatePaintProperty() override 102 { 103 return MakeRefPtr<BubbleRenderProperty>(); 104 } 105 CreateEventHub()106 RefPtr<EventHub> CreateEventHub() override 107 { 108 return MakeRefPtr<BubbleEventHub>(); 109 } 110 CreateAccessibilityProperty()111 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 112 { 113 return MakeRefPtr<BubbleAccessibilityProperty>(); 114 } 115 GetChildOffset()116 OffsetF GetChildOffset() 117 { 118 return childOffset_; 119 } 120 GetChildSize()121 SizeF GetChildSize() 122 { 123 return childSize_; 124 } 125 GetFocusPattern()126 FocusPattern GetFocusPattern() const override 127 { 128 return { FocusType::SCOPE, true }; 129 } 130 GetRouteOfFirstScope()131 std::list<int32_t> GetRouteOfFirstScope() override 132 { 133 return { 0, 0, 0 }; 134 } 135 136 void OnWindowHide() override; 137 void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override; 138 void StartEnteringTransitionEffects(const RefPtr<FrameNode>& popupNode, const std::function<void()>& finish); 139 void StartExitingTransitionEffects(const RefPtr<FrameNode>& popupNode, const std::function<void()>& finish); 140 void StartEnteringAnimation(std::function<void()> finish); 141 void StartExitingAnimation(std::function<void()> finish); 142 bool IsOnShow(); 143 bool IsExiting(); 144 void OnColorConfigurationUpdate() override; 145 void UpdateBubbleText(); 146 void UpdateText(const RefPtr<UINode>& node, const RefPtr<PopupTheme>& popupTheme); 147 void AddPipelineCallBack(); 148 void UpdateAgingTextSize(); 149 void DumpInfo() override; 150 SetMessageColor(bool isSetMessageColor)151 void SetMessageColor(bool isSetMessageColor) 152 { 153 isSetMessageColor_ = isSetMessageColor; 154 } 155 SetMessageNode(RefPtr<FrameNode> messageNode)156 void SetMessageNode(RefPtr<FrameNode> messageNode) 157 { 158 messageNode_ = messageNode; 159 } 160 SetCustomPopupTag(bool isCustomPopup)161 void SetCustomPopupTag(bool isCustomPopup) 162 { 163 isCustomPopup_ = isCustomPopup; 164 } 165 SetTransitionStatus(TransitionStatus transitionStatus)166 void SetTransitionStatus(TransitionStatus transitionStatus) 167 { 168 transitionStatus_ = transitionStatus; 169 } 170 GetTransitionStatus()171 TransitionStatus GetTransitionStatus() const 172 { 173 return transitionStatus_; 174 } 175 SetInteractiveDismiss(bool interactiveDismiss)176 void SetInteractiveDismiss(bool interactiveDismiss) 177 { 178 interactiveDismiss_ = interactiveDismiss; 179 } 180 GetInteractiveDismiss()181 bool GetInteractiveDismiss() 182 { 183 if (interactiveDismiss_) { 184 return true; 185 } 186 return false; 187 } 188 UpdateOnWillDismiss(const std::function<void (int32_t)> && onWillDismiss)189 void UpdateOnWillDismiss(const std::function<void(int32_t)>&& onWillDismiss) 190 { 191 onWillDismiss_ = std::move(onWillDismiss); 192 } 193 HasOnWillDismiss()194 bool HasOnWillDismiss() 195 { 196 if (onWillDismiss_) { 197 return true; 198 } 199 return false; 200 } 201 CallOnWillDismiss(int32_t reason)202 void CallOnWillDismiss(int32_t reason) 203 { 204 if (onWillDismiss_) { 205 TAG_LOGD(AceLogTag::ACE_OVERLAY, 206 "Popup CallOnWillDismiss, reason: %{public}d", reason); 207 onWillDismiss_(reason); 208 } 209 } 210 SetEnableHoverMode(bool enableHoverMode)211 void SetEnableHoverMode(bool enableHoverMode) 212 { 213 enableHoverMode_ = enableHoverMode; 214 } 215 GetEnableHoverMode()216 bool GetEnableHoverMode() const 217 { 218 return enableHoverMode_; 219 } 220 SetHasTransition(bool hasTransition)221 void SetHasTransition(bool hasTransition) 222 { 223 hasTransition_ = hasTransition; 224 } 225 SetAvoidKeyboard(bool avoidKeyboard)226 void SetAvoidKeyboard(bool avoidKeyboard) 227 { 228 avoidKeyboard_ = avoidKeyboard; 229 } 230 GetAvoidKeyboard()231 bool GetAvoidKeyboard() 232 { 233 return avoidKeyboard_; 234 } 235 GetHasTransition()236 bool GetHasTransition() const 237 { 238 return hasTransition_; 239 } 240 ResetFocusState()241 void ResetFocusState() 242 { 243 FocusViewDidShow(nullptr); 244 SetIsViewRootScopeFocused(true); 245 SetIsViewHasFocused(false); 246 } 247 GetHostWindowRect()248 Rect GetHostWindowRect() const 249 { 250 return hostWindowRect_; 251 } 252 RegisterDoubleBindCallback(const std::function<void (const std::string &)> & callback)253 void RegisterDoubleBindCallback(const std::function<void(const std::string&)>& callback) 254 { 255 doubleBindCallback_ = callback; 256 } 257 CallDoubleBindCallback(const std::string & value)258 void CallDoubleBindCallback(const std::string& value) 259 { 260 if (doubleBindCallback_) { 261 doubleBindCallback_(value); 262 } 263 } 264 SetPopupParam(const RefPtr<PopupParam> & popupParam)265 void SetPopupParam(const RefPtr<PopupParam>& popupParam) 266 { 267 popupParam_ = popupParam; 268 } 269 GetPopupParam()270 const RefPtr<PopupParam>& GetPopupParam() const 271 { 272 return popupParam_; 273 } 274 SetCustomNode(const WeakPtr<UINode> & customNode)275 void SetCustomNode(const WeakPtr<UINode>& customNode) 276 { 277 customNode_ = customNode; 278 } 279 GetCustomNode()280 const RefPtr<UINode> GetCustomNode() const 281 { 282 return customNode_.Upgrade(); 283 } 284 285 protected: 286 void OnDetachFromFrameNode(FrameNode* frameNode) override; 287 AvoidKeyboard()288 bool AvoidKeyboard() const override 289 { 290 return false; 291 } 292 AvoidBottom()293 bool AvoidBottom() const override 294 { 295 return false; 296 } 297 298 private: 299 void OnModifyDone() override; 300 void OnAttachToFrameNode() override; 301 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, bool skipMeasure, bool skipLayout) override; 302 303 RefPtr<FrameNode> GetButtonRowNode(); 304 RefPtr<PopupTheme> GetPopupTheme(); 305 void InitTouchEvent(); 306 void HandleTouchEvent(const TouchEventInfo& info); 307 void HandleTouchDown(const Offset& clickPosition); 308 void RegisterButtonOnHover(); 309 void RegisterButtonOnTouch(); 310 void ButtonOnHover(bool isHover, const RefPtr<NG::FrameNode>& buttonNode); 311 void ButtonOnPress(const TouchEventInfo& info, const RefPtr<NG::FrameNode>& buttonNode); 312 void PopBubble(); 313 void Animation( 314 RefPtr<RenderContext>& renderContext, const Color& endColor, int32_t duration, const RefPtr<Curve>& curve); 315 316 OffsetT<Dimension> GetInvisibleOffset(); 317 RefPtr<RenderContext> GetRenderContext(); 318 void ResetToInvisible(); 319 bool PostTask(const TaskExecutor::Task& task, const std::string& name); 320 void StartOffsetEnteringAnimation(); 321 void StartAlphaEnteringAnimation(std::function<void()> finish); 322 void StartOffsetExitingAnimation(); 323 void StartAlphaExitingAnimation(std::function<void()> finish); 324 325 int32_t targetNodeId_ = -1; 326 std::string targetTag_; 327 328 RefPtr<TouchEventImpl> touchEvent_; 329 bool mouseEventInitFlag_ = false; 330 bool touchEventInitFlag_ = false; 331 bool isHover_ = false; 332 bool interactiveDismiss_ = true; 333 std::function<void(int32_t)> onWillDismiss_; 334 OffsetF childOffset_; 335 OffsetF arrowPosition_; 336 SizeF childSize_; 337 RectF touchRegion_; 338 Rect hostWindowRect_; 339 BubbleDumpInfo dumpInfo_; 340 // top right bottom left 341 std::vector<float> arrowOffsetByClips_ = { 0.0f, 0.0f, 0.0f, 0.0f }; 342 std::optional<Placement> arrowPlacement_; 343 std::vector<std::vector<float>> arrowOffsetsFromClip_ 344 = { {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f}, {0.0f, 0.0f} }; 345 float arrowWidth_ = Dimension(16.0_vp).ConvertToPx(); 346 float arrowHeight_ = Dimension(8.0_vp).ConvertToPx(); 347 Placement arrowBuildPlacement_ = Placement::BOTTOM; 348 349 bool showArrow_ = false; 350 ColorMode colorMode_ = ColorMode::COLOR_MODE_UNDEFINED; 351 bool isSetMessageColor_ = false; 352 Border border_; 353 bool avoidKeyboard_ = false; 354 355 TransitionStatus transitionStatus_ = TransitionStatus::INVISIABLE; 356 357 bool delayShow_ = false; 358 std::function<void()> finish_; 359 360 std::optional<OffsetF> targetOffset_; 361 std::optional<SizeF> targetSize_; 362 363 bool isCustomPopup_ = false; 364 RefPtr<FrameNode> messageNode_; 365 366 std::string clipPath_; 367 RefPtr<FrameNode> clipFrameNode_; 368 ACE_DISALLOW_COPY_AND_MOVE(BubblePattern); 369 370 bool hasTransition_ = false; 371 bool hasOnAreaChange_ = false; 372 bool enableHoverMode_ = false; 373 int32_t halfFoldHoverCallbackId_ = -1; 374 std::function<void(const std::string&)> onStateChangeCallback_ = nullptr; 375 std::function<void(const std::string&)> doubleBindCallback_ = nullptr; 376 RefPtr<PopupParam> popupParam_ = nullptr; 377 WeakPtr<UINode> customNode_ = nullptr; 378 }; 379 } // namespace OHOS::Ace::NG 380 381 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUBBLE_BUBBLE_PATTERN_H 382