1 /* 2 * Copyright (c) 2022-2023 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_PATTERN_TABS_TAB_CONTENT_MODEL_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TABS_TAB_CONTENT_MODEL_H 18 19 #include <mutex> 20 21 #include "base/geometry/axis.h" 22 #include "base/geometry/dimension.h" 23 #include "base/image/pixel_map.h" 24 #include "base/memory/referenced.h" 25 #include "core/common/resource/resource_object.h" 26 #include "core/components/common/layout/constants.h" 27 #include "core/components/common/properties/color.h" 28 #include "core/components/tab_bar/tab_theme.h" 29 #include "core/components_ng/property/measure_property.h" 30 #include "core/event/ace_events.h" 31 #include "core/pipeline/pipeline_context.h" 32 33 namespace OHOS::Ace { 34 enum class SelectedMode { 35 INDICATOR, 36 BOARD, 37 }; 38 39 enum class LayoutMode { 40 AUTO, 41 VERTICAL, 42 HORIZONTAL, 43 }; 44 45 struct BottomTabBarStyle { 46 bool symmetricExtensible = false; 47 LayoutMode layoutMode = LayoutMode::VERTICAL; 48 FlexAlign verticalAlign = FlexAlign::CENTER; 49 }; 50 51 struct IndicatorStyle final { 52 Color color = Color::WHITE; 53 Dimension height = 0.0_vp; 54 Dimension width = 0.0_vp; 55 Dimension borderRadius = 0.0_vp; 56 Dimension marginTop = 0.0_vp; IndicatorStylefinal57 IndicatorStyle() 58 { 59 auto pipelineContext = PipelineContext::GetCurrentContextSafelyWithCheck(); 60 if (!pipelineContext) { 61 return; 62 } 63 auto tabTheme = pipelineContext->GetTheme<TabTheme>(); 64 if (!tabTheme) { 65 return; 66 } 67 color = tabTheme->GetActiveIndicatorColor(); 68 height = tabTheme->GetSubTabIndicatorHeight(); 69 marginTop = tabTheme->GetSubTabIndicatorGap(); 70 } 71 bool operator==(const IndicatorStyle& indicator) const 72 { 73 return (color == indicator.color) && (height == indicator.height) && 74 (width == indicator.width) && (borderRadius == indicator.borderRadius) 75 && (marginTop == indicator.marginTop); 76 } 77 }; 78 79 struct BoardStyle final { 80 Dimension borderRadius = 0.0_vp; BoardStylefinal81 BoardStyle() 82 { 83 auto pipelineContext = PipelineContext::GetCurrentContextSafelyWithCheck(); 84 if (!pipelineContext) { 85 return; 86 } 87 auto tabTheme = pipelineContext->GetTheme<TabTheme>(); 88 if (!tabTheme) { 89 return; 90 } 91 borderRadius = tabTheme->GetFocusIndicatorRadius(); 92 } 93 bool operator==(const BoardStyle& board) const 94 { 95 return borderRadius == board.borderRadius; 96 } 97 }; 98 99 struct LabelStyle { 100 std::optional<Ace::TextOverflow> textOverflow; 101 std::optional<uint32_t> maxLines; 102 std::optional<Dimension> minFontSize; 103 std::optional<Dimension> maxFontSize; 104 std::optional<Ace::TextHeightAdaptivePolicy> heightAdaptivePolicy; 105 std::optional<Dimension> fontSize; 106 std::optional<Ace::FontWeight> fontWeight; 107 std::optional<std::vector<std::string>> fontFamily; 108 std::optional<Ace::FontStyle> fontStyle; 109 std::optional<Color> unselectedColor; 110 std::optional<Color> selectedColor; 111 }; 112 113 struct IconStyle { 114 std::optional<Color> unselectedColor; 115 std::optional<Color> selectedColor; 116 }; 117 118 struct TabBarSymbol { 119 std::function<void(WeakPtr<NG::FrameNode>, std::string)> onApply = nullptr; 120 bool selectedFlag = false; 121 }; 122 123 enum class TabContentJsType { 124 INDICATOR_COLOR, 125 FONT_SIZE, 126 FONT_FAMILY, 127 MIN_FONT_SIZE, 128 MAX_FONT_SIZE, 129 TEXT_CONTENT, 130 TAB_BAR_OPTIONS_ICON, 131 BOTTOM_TAB_BAR_STYLE_ICON, 132 ICON_UNSELECT_COLOR, 133 ICON_SELECT_COLOR, 134 LABEL_UNSELECT_COLOR, 135 LABEL_SELECT_COLOR, 136 PADDING, 137 BORDER_RADIUS, 138 INDICATOR_HEIGHT, 139 INDICATOR_WIDTH, 140 INDICATOR_RADIUS, 141 INDICATOR_MARGIN_TOP, 142 }; 143 144 class ACE_FORCE_EXPORT TabContentModel { 145 public: 146 static TabContentModel* GetInstance(); 147 virtual ~TabContentModel() = default; 148 149 virtual void Create() = 0; 150 virtual void Create(std::function<void()>&& deepRenderFunc) = 0; 151 virtual void Pop() = 0; CreateWithResourceObj(TabContentJsType jsType,const RefPtr<ResourceObject> & resObj)152 virtual void CreateWithResourceObj(TabContentJsType jsType, const RefPtr<ResourceObject>& resObj) {}; CreatePaddingHorWithResourceObj(const RefPtr<ResourceObject> & resObjLeft,const RefPtr<ResourceObject> & resObjRight,bool isSubTabStyle,bool useLocalizedPadding)153 virtual void CreatePaddingHorWithResourceObj(const RefPtr<ResourceObject>& resObjLeft, 154 const RefPtr<ResourceObject>& resObjRight, bool isSubTabStyle, bool useLocalizedPadding) {}; CreatePaddingVerWithResourceObj(const RefPtr<ResourceObject> & resObjTop,const RefPtr<ResourceObject> & resObjBottom,bool isSubTabStyle,bool useLocalizedPadding)155 virtual void CreatePaddingVerWithResourceObj(const RefPtr<ResourceObject>& resObjTop, 156 const RefPtr<ResourceObject>& resObjBottom, bool isSubTabStyle, bool useLocalizedPadding) {}; 157 virtual void SetTabBar(const std::optional<std::string>& text, const std::optional<std::string>& icon, 158 const std::optional<TabBarSymbol>& tabBarSymbol, std::function<void()>&& builder, bool useContentOnly) = 0; 159 virtual void SetTabBarWithContent(const RefPtr<NG::UINode>& content) = 0; 160 virtual void SetTabBarStyle(TabBarStyle tabBarStyle) = 0; 161 virtual void SetIndicator(const IndicatorStyle& indicator) = 0; 162 virtual void SetIndicatorColorByUser(bool isByUser) = 0; 163 virtual void SetBoard(const BoardStyle& board) = 0; 164 virtual void SetSelectedMode(SelectedMode selectedMode) = 0; 165 virtual void SetLabelStyle(const LabelStyle& labelStyle) = 0; 166 virtual void SetLabelUnselectedColorByUser(bool isByUser) = 0; 167 virtual void SetLabelSelectedColorByUser(bool isByUser) = 0; SetIconStyle(const IconStyle & iconStyle)168 virtual void SetIconStyle(const IconStyle& iconStyle) {} 169 virtual void SetIconUnselectedColorByUser(bool isByUser) = 0; 170 virtual void SetIconSelectedColorByUser(bool isByUser) = 0; 171 virtual void SetPadding(const NG::PaddingProperty& padding) = 0; 172 virtual void SetUseLocalizedPadding(bool useLocalizedPadding) = 0; 173 virtual void SetLayoutMode(LayoutMode layoutMode) = 0; 174 virtual void SetVerticalAlign(FlexAlign verticalAlign) = 0; 175 virtual void SetSymmetricExtensible(bool isExtensible) = 0; 176 virtual void SetId(const std::string& id) = 0; SetOnWillShow(std::function<void ()> && onWillShow)177 virtual void SetOnWillShow(std::function<void()>&& onWillShow) {} SetOnWillHide(std::function<void ()> && onWillHide)178 virtual void SetOnWillHide(std::function<void()>&& onWillHide) {} SetCustomStyleNode(const RefPtr<NG::FrameNode> & customStyleNode)179 virtual void SetCustomStyleNode(const RefPtr<NG::FrameNode>& customStyleNode) {} 180 181 private: 182 static std::unique_ptr<TabContentModel> instance_; 183 static std::mutex mutex_; 184 }; 185 186 } // namespace OHOS::Ace 187 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TABS_TAB_CONTENT_MODEL_H 188