1 /* 2 * Copyright (c) 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_SCROLLABLE_SCROLLABLE_THEME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLLABLE_SCROLLABLE_THEME_H 18 19 #include "core/components/theme/theme.h" 20 #include "core/components/theme/theme_constants.h" 21 #include "core/components/theme/theme_constants_defines.h" 22 23 namespace OHOS::Ace::NG { 24 25 class ScrollableTheme : public virtual Theme { 26 DECLARE_ACE_TYPE(ScrollableTheme, Theme); 27 28 public: 29 class Builder { 30 public: 31 Builder() = default; 32 ~Builder() = default; 33 Build(const RefPtr<ThemeConstants> & themeConstants)34 RefPtr<ScrollableTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const 35 { 36 RefPtr<ScrollableTheme> theme = AceType::Claim(new ScrollableTheme()); 37 if (!themeConstants) { 38 return theme; 39 } 40 ParsePattern(themeConstants, theme); 41 return theme; 42 } 43 44 private: ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<ScrollableTheme> & theme)45 void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<ScrollableTheme>& theme) const 46 { 47 if (!theme) { 48 return; 49 } 50 RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_SCROLLABLE); 51 if (!pattern) { 52 return; 53 } 54 theme->friction_ = pattern->GetAttr<double>("scroll_able_friction", 0.75f); 55 theme->flingVelocityScale_ = pattern->GetAttr<double>("scroll_able_fling_velocity_scale", 1.5f); 56 theme->springVelocityScale_ = pattern->GetAttr<double>("scroll_able_spring_velocity_scale", 1.5f); 57 if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN)) { 58 theme->ratio_ = pattern->GetAttr<double>("scroll_able_over_edge_following_ratio_api_eighteen", 5.0f); 59 } else { 60 theme->ratio_ = pattern->GetAttr<double>("scroll_able_over_edge_following_ratio", 1.848f); 61 } 62 theme->springResponse_ = pattern->GetAttr<double>("scroll_able_spring_response", 0.417f); 63 theme->touchPadVelocityScaleRate_ = 64 pattern->GetAttr<double>("scroll_able_touch_pad_velocity_scale_rate", 1.0f); 65 } 66 }; 67 68 ~ScrollableTheme() override = default; 69 GetFriction()70 float GetFriction() const 71 { 72 return friction_; 73 } 74 GetFlingVelocityScale()75 float GetFlingVelocityScale() const 76 { 77 return flingVelocityScale_; 78 } 79 GetSpringVelocityScale()80 float GetSpringVelocityScale() const 81 { 82 return springVelocityScale_; 83 } 84 GetRatio()85 float GetRatio() const 86 { 87 return ratio_; 88 } 89 GetSpringResponse()90 float GetSpringResponse() const 91 { 92 return springResponse_; 93 } 94 GetTouchPadVelocityScaleRate()95 float GetTouchPadVelocityScaleRate() const 96 { 97 return touchPadVelocityScaleRate_; 98 } 99 100 protected: 101 ScrollableTheme() = default; 102 103 private: 104 float friction_ = 0.75f; 105 float flingVelocityScale_ = 1.5f; 106 float springVelocityScale_ = 1.5f; 107 float ratio_ = 1.848f; 108 float springResponse_ = 0.416f; 109 float touchPadVelocityScaleRate_ = 1.0f; 110 }; 111 } // namespace OHOS::Ace::NG 112 113 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCROLLABLE_SCROLLABLE_THEME_H 114