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_NAVIGATION_TITLE_BAR_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_TITLE_BAR_PATTERN_H 18 19 #include "base/memory/referenced.h" 20 #include "core/components_ng/base/ui_node.h" 21 #include "core/components_ng/pattern/navigation/navigation_options.h" 22 #include "core/components_ng/pattern/navigation/title_bar_accessibility_property.h" 23 #include "core/components_ng/pattern/navigation/title_bar_layout_algorithm.h" 24 #include "core/components_ng/pattern/navigation/title_bar_layout_property.h" 25 #include "core/components_ng/pattern/pattern.h" 26 27 namespace OHOS::Ace::NG { 28 29 class TitleBarPattern : public Pattern { 30 DECLARE_ACE_TYPE(TitleBarPattern, Pattern); 31 32 public: 33 TitleBarPattern() = default; 34 ~TitleBarPattern() override = default; 35 CreateLayoutProperty()36 RefPtr<LayoutProperty> CreateLayoutProperty() override 37 { 38 return MakeRefPtr<TitleBarLayoutProperty>(); 39 } 40 CreateAccessibilityProperty()41 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 42 { 43 return MakeRefPtr<TitleBarAccessibilityProperty>(); 44 } 45 46 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override; 47 IsAtomicNode()48 bool IsAtomicNode() const override 49 { 50 return false; 51 } 52 53 void OnModifyDone() override; 54 GetTempTitleBarHeight()55 float GetTempTitleBarHeight() const 56 { 57 return static_cast<float>(tempTitleBarHeight_.ConvertToPx()); 58 } 59 SetTempTitleBarHeightVp(float value)60 void SetTempTitleBarHeightVp(float value) 61 { 62 tempTitleBarHeight_.SetValue(Dimension(value).ConvertToVp()); 63 } 64 GetDefaultTitleBarHeight()65 float GetDefaultTitleBarHeight() const 66 { 67 return defaultTitleBarHeight_; 68 } 69 GetTempTitleOffsetY()70 float GetTempTitleOffsetY() const 71 { 72 return tempTitleOffsetY_; 73 } 74 GetTempSubTitleOffsetY()75 float GetTempSubTitleOffsetY() const 76 { 77 return tempSubTitleOffsetY_; 78 } 79 GetMaxTitleBarHeight()80 float GetMaxTitleBarHeight() const 81 { 82 return maxTitleBarHeight_; 83 } 84 85 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 86 87 void InitTitleParam(); 88 89 bool IsHidden(); 90 IsInitialTitle()91 bool IsInitialTitle() const 92 { 93 return isInitialTitle_; 94 } 95 MarkIsInitialTitle(bool isInitialTitle)96 void MarkIsInitialTitle(bool isInitialTitle) 97 { 98 isInitialTitle_ = isInitialTitle; 99 } 100 IsInitialSubtitle()101 bool IsInitialSubtitle() const 102 { 103 return isInitialSubtitle_; 104 } 105 MarkIsInitialSubtitle(bool isInitialSubtitle)106 void MarkIsInitialSubtitle(bool isInitialSubtitle) 107 { 108 isInitialSubtitle_ = isInitialSubtitle; 109 } 110 SetIsFirstTimeSetSystemTitle(bool isFirstTime)111 void SetIsFirstTimeSetSystemTitle(bool isFirstTime) 112 { 113 isFirstTimeSetSystemTitle_ = isFirstTime; 114 } IsFirstTimeSetSystemTitle()115 bool IsFirstTimeSetSystemTitle() const 116 { 117 return isFirstTimeSetSystemTitle_; 118 } 119 120 void ProcessTitleDragUpdate(float offset); 121 122 void OnColorConfigurationUpdate() override; 123 124 bool OnThemeScopeUpdate(int32_t themeScopeId) override; 125 GetCurrentOffset()126 float GetCurrentOffset() 127 { 128 return GetTempTitleBarHeight() - defaultTitleBarHeight_; 129 } 130 SetOverDragOffset(float overDragOffset)131 void SetOverDragOffset(float overDragOffset) 132 { 133 overDragOffset_ = overDragOffset; 134 } 135 GetOverDragOffset()136 float GetOverDragOffset() const 137 { 138 return overDragOffset_; 139 } 140 IsTitleDraggedDown()141 bool IsTitleDraggedDown() const 142 { 143 if (NearZero(tempTitleBarHeight_.Value())) { 144 return true; 145 } 146 return GreatNotEqual(GetTempTitleBarHeight(), static_cast<float>(SINGLE_LINE_TITLEBAR_HEIGHT.ConvertToPx())); 147 } 148 IsTitleFullStatus()149 bool IsTitleFullStatus() const 150 { 151 if (NearZero(tempTitleBarHeight_.Value())) { 152 return true; 153 } 154 GetMaxTitleBarHeight(); 155 return GreatOrEqual(GetTempTitleBarHeight(), maxTitleBarHeight_); 156 } 157 IsMinTitle()158 bool IsMinTitle() const 159 { 160 if (NearZero(tempTitleBarHeight_.Value())) { 161 return true; 162 } 163 GetMaxTitleBarHeight(); 164 return LessOrEqual(GetTempTitleBarHeight(), maxTitleBarHeight_); 165 } 166 IsCurrentMinTitle()167 bool IsCurrentMinTitle() const 168 { 169 if (NearZero(tempTitleBarHeight_.Value())) { 170 return true; 171 } 172 return LessOrEqual(GetTempTitleBarHeight(), static_cast<float>(SINGLE_LINE_TITLEBAR_HEIGHT.ConvertToPx())); 173 } 174 IsCurrentMaxTitle()175 bool IsCurrentMaxTitle() const 176 { 177 if (NearZero(tempTitleBarHeight_.Value())) { 178 return false; 179 } 180 GetMaxTitleBarHeight(); 181 return GreatOrEqual(GetTempTitleBarHeight(), maxTitleBarHeight_); 182 } 183 IsFreeTitleUpdated()184 bool IsFreeTitleUpdated() const 185 { 186 return isFreeTitleUpdated_; 187 } 188 GetNavigationTitleMode()189 NavigationTitleMode GetNavigationTitleMode() const 190 { 191 return titleMode_; 192 } 193 SetCanOverDrag(bool CanOverDrag)194 void SetCanOverDrag(bool CanOverDrag) 195 { 196 CanOverDrag_ = CanOverDrag; 197 } 198 SetTitleScaleChange(bool isTitleScaleChange)199 void SetTitleScaleChange(bool isTitleScaleChange) 200 { 201 isTitleScaleChange_ = isTitleScaleChange; 202 } 203 SetCurrentTitleOffsetY(float currentTitleOffsetY)204 void SetCurrentTitleOffsetY(float currentTitleOffsetY) 205 { 206 currentTitleOffsetY_ = currentTitleOffsetY; 207 } 208 SetCurrentTitleBarHeight(float currentTitleBarHeight)209 void SetCurrentTitleBarHeight(float currentTitleBarHeight) 210 { 211 currentTitleBarHeight_ = currentTitleBarHeight; 212 } 213 SetIsTitleChanged(bool isTitleChanged)214 void SetIsTitleChanged(bool isTitleChanged) 215 { 216 isTitleChanged_ = isTitleChanged; 217 } 218 219 void OnCoordScrollStart(); 220 float OnCoordScrollUpdate(float offset); 221 void OnCoordScrollEnd(); 222 223 void SetTitlebarOptions(NavigationTitlebarOptions&& opt); 224 GetTitleBarOptions()225 NavigationTitlebarOptions GetTitleBarOptions() const 226 { 227 return options_; 228 } 229 GetLargeFontPopUpDialogNode()230 RefPtr<FrameNode> GetLargeFontPopUpDialogNode() const 231 { 232 return largeFontPopUpDialogNode_.Upgrade(); 233 } 234 SetLargeFontPopUpDialogNode(const WeakPtr<FrameNode> & dialogNode)235 void SetLargeFontPopUpDialogNode(const WeakPtr<FrameNode>& dialogNode) 236 { 237 largeFontPopUpDialogNode_ = dialogNode; 238 } 239 GetMoveIndex()240 std::optional<int32_t> GetMoveIndex() const 241 { 242 return moveIndex_; 243 } 244 SetMoveIndex(int32_t index)245 void SetMoveIndex(int32_t index) 246 { 247 moveIndex_ = index; 248 } 249 250 void UpdateNavBarTitleProperty(const RefPtr<TitleBarNode>& hostNode); 251 void UpdateNavDesTitleProperty(const RefPtr<TitleBarNode>& hostNode); 252 IsFontSizeSettedByDeveloper()253 bool IsFontSizeSettedByDeveloper() const 254 { 255 return isFontSizeSettedByDeveloper_; 256 } 257 SetNeedResetMainTitleProperty(bool reset)258 void SetNeedResetMainTitleProperty(bool reset) 259 { 260 shouldResetMainTitleProperty_ = reset; 261 } SetNeedResetSubTitleProperty(bool reset)262 void SetNeedResetSubTitleProperty(bool reset) 263 { 264 shouldResetSubTitleProperty_ = reset; 265 } 266 267 void OnLanguageConfigurationUpdate() override; 268 UpdateHalfFoldHoverChangedCallbackId(std::optional<int32_t> id)269 void UpdateHalfFoldHoverChangedCallbackId(std::optional<int32_t> id) 270 { 271 halfFoldHoverChangedCallbackId_ = id; 272 } 273 HasHalfFoldHoverChangedCallbackId()274 bool HasHalfFoldHoverChangedCallbackId() 275 { 276 return halfFoldHoverChangedCallbackId_.has_value(); 277 } 278 279 void InitFoldCreaseRegion(); 280 GetFoldCreaseRects()281 std::vector<Rect> GetFoldCreaseRects() 282 { 283 return currentFoldCreaseRegion_; 284 } 285 286 float GetTitleBarHeightLessThanMaxBarHeight() const; 287 288 void InitBackButtonLongPressEvent(const RefPtr<FrameNode>& backButtonNode); 289 GetBackButtonDialogNode()290 RefPtr<FrameNode> GetBackButtonDialogNode() const 291 { 292 return dialogNode_; 293 } 294 SetBackButtonDialogNode(const RefPtr<FrameNode> & dialogNode)295 void SetBackButtonDialogNode(const RefPtr<FrameNode>& dialogNode) 296 { 297 dialogNode_ = dialogNode; 298 } 299 300 void InitMenuDragAndLongPressEvent(const RefPtr<FrameNode>& menuNode, const std::vector<NG::BarItem>& menuItems); 301 302 private: 303 void TransformScale(float overDragOffset, const RefPtr<FrameNode>& frameNode); 304 305 void ClearDragState(); 306 float GetSubtitleOpacity(); 307 Dimension GetFontSize(float offset); 308 float GetMappedOffset(float offset); 309 void SpringAnimation(float startPos, float endPos); 310 void UpdateScaleByDragOverDragOffset(float overDragOffset); 311 void AnimateTo(float offset, bool isFullTitleMode = false); 312 313 void OnAttachToFrameNode() override; 314 void OnDetachFromFrameNode(FrameNode* frameNode) override; 315 316 void HandleDragStart(const GestureEvent& info); 317 void HandleDragUpdate(const GestureEvent& info); 318 void HandleDragEnd(double dragVelocity); 319 320 void SetMaxTitleBarHeight(); 321 void SetTempTitleBarHeight(float offsetY); 322 void SetTempTitleOffsetY(); 323 void SetTempSubTitleOffsetY(); 324 void SetDefaultTitleFontSize(); 325 void SetDefaultSubtitleOpacity(); 326 327 float GetTitleHeight(); 328 float GetSubTitleOffsetY(); 329 void UpdateTitleFontSize(const Dimension& tempTitleFontSize); 330 void UpdateSubTitleOpacity(const double &value); 331 void UpdateTitleModeChange(); 332 void MountTitle(const RefPtr<TitleBarNode>& hostNode); 333 334 void UpdateTitleBarByCoordScroll(float offset); 335 void SetTitleStyleByCoordScrollOffset(float offset); 336 float CalculateHandledOffsetMinTitle(float offset, float lastCordScrollOffset); 337 float CalculateHandledOffsetMaxTitle(float offset, float lastCordScrollOffset); 338 float CalculateHandledOffsetBetweenMinAndMaxTitle(float offset, float lastCordScrollOffset); CleanSpringAnimation()339 void CleanSpringAnimation() 340 { 341 springAnimation_.reset(); 342 } CleanAnimation()343 void CleanAnimation() 344 { 345 animation_.reset(); 346 } 347 void UpdateBackgroundStyle(RefPtr<FrameNode>& host); 348 void MountSubTitle(const RefPtr<TitleBarNode>& hostNode); 349 void ResetMainTitleProperty(const RefPtr<FrameNode>& textNode, 350 const RefPtr<TitleBarLayoutProperty>& titleBarLayoutProperty, 351 NavigationTitleMode titleMode, bool hasSubTitle, bool parentIsNavDest); 352 void ResetSubTitleProperty(const RefPtr<FrameNode>& textNode, 353 NavigationTitleMode titleMode, bool parentIsNavDest); 354 void ApplyTitleModifierIfNeeded(const RefPtr<TitleBarNode>& hostNode); 355 void ApplyTitleModifier(const RefPtr<FrameNode>& textNode, 356 const TextStyleApplyFunc& applyFunc, bool needCheckFontSizeIsSetted); 357 void DumpInfo() override; DumpSimplifyInfo(std::unique_ptr<JsonValue> & json)358 void DumpSimplifyInfo(std::unique_ptr<JsonValue>& json) override {} 359 360 void HandleLongPress(const RefPtr<FrameNode>& backButtonNode); 361 void HandleLongPressActionEnd(); 362 void OnFontScaleConfigurationUpdate() override; 363 364 void InitMenuDragEvent(const RefPtr<GestureEventHub>& gestureHub, const RefPtr<FrameNode>& menuNode, 365 const std::vector<NG::BarItem>& menuItems); 366 void InitMenuLongPressEvent(const RefPtr<GestureEventHub>& gestureHub, const RefPtr<FrameNode>& menuNode, 367 const std::vector<NG::BarItem>& menuItems); 368 void HandleMenuLongPress( 369 const GestureEvent& info, const RefPtr<FrameNode>& menuNode, const std::vector<NG::BarItem>& menuItems); 370 void HandleMenuLongPressActionEnd(); 371 372 RefPtr<PanEvent> panEvent_; 373 std::shared_ptr<AnimationUtils::Animation> springAnimation_; 374 std::shared_ptr<AnimationUtils::Animation> animation_; 375 std::optional<Dimension> fontSize_ = 0.0_fp; 376 std::optional<float> opacity_; 377 378 float overDragOffset_ = 0.0f; 379 float maxTitleBarHeight_ = 0.0f; 380 float defaultTitleBarHeight_ = 0.0f; 381 Dimension titleSpaceVertical_; 382 Dimension tempTitleBarHeight_ = 0.0_vp; 383 float minTitleOffsetY_ = 0.0f; 384 float maxTitleOffsetY_ = 0.0f; 385 // ratio of titleOffsetY difference and titleBarHeight difference 386 float moveRatio_ = 0.0f; 387 float titleMoveDistance_ = 0.0f; 388 float defaultTitleOffsetY_ = 0.0f; 389 float defaultSubtitleOffsetY_ = 0.0f; 390 float tempTitleOffsetY_ = 0.0f; 391 float tempSubTitleOffsetY_ = 0.0f; 392 393 Dimension defaultTitleFontSize_; 394 // ratio of fontSize difference and titleBarHeight difference 395 double fontSizeRatio_ = 0.0f; 396 397 float defaultSubtitleOpacity_; 398 // ratio of opacity difference and titleBarHeight difference 399 double opacityRatio_ = 0.0f; 400 401 float initialTitleOffsetY_ = 0.0f; 402 bool isFirstTimeSetSystemTitle_ = true; 403 bool isInitialTitle_ = true; 404 float initialSubtitleOffsetY_ = 0.0f; 405 bool isInitialSubtitle_ = true; 406 float minTitleHeight_ = 0.0f; 407 bool CanOverDrag_ = true; 408 bool isTitleScaleChange_ = true; 409 bool isTitleChanged_ = false; // navigation Non-custom title changed 410 NavigationTitleMode titleMode_ = NavigationTitleMode::FREE; 411 412 bool isFreeTitleUpdated_ = false; 413 414 float coordScrollOffset_ = 0.0f; 415 float coordScrollFinalOffset_ = 0.0f; 416 417 // the value before title bar expand safe area 418 float currentTitleOffsetY_ = 0.0f; 419 float currentTitleBarHeight_ = 0.0f; 420 421 NavigationTitlebarOptions options_; 422 423 WeakPtr<FrameNode> largeFontPopUpDialogNode_; 424 std::optional<int32_t> moveIndex_; 425 426 bool isFontSizeSettedByDeveloper_ = false; 427 bool shouldResetMainTitleProperty_ = true; 428 bool shouldResetSubTitleProperty_ = true; 429 430 std::optional<int32_t> halfFoldHoverChangedCallbackId_; 431 std::vector<Rect> currentFoldCreaseRegion_; 432 433 RefPtr<LongPressEvent> longPressEvent_; 434 RefPtr<FrameNode> dialogNode_; 435 }; 436 437 } // namespace OHOS::Ace::NG 438 439 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_TITLE_BAR_PATTERN_H 440