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