1 /* 2 * Copyright (c) 2021 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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_VIEW_STACK_PROCESSOR_H 17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_VIEW_STACK_PROCESSOR_H 18 19 #include <memory> 20 #include <stack> 21 #include <unordered_map> 22 #include <vector> 23 24 #include "core/accessibility/accessibility_node.h" 25 #include "core/components/common/properties/animation_option.h" 26 #include "core/pipeline/base/component.h" 27 #include "frameworks/core/components/box/box_component.h" 28 #include "frameworks/core/components/checkable/radio_group_component.h" 29 #include "frameworks/core/components/coverage/coverage_component.h" 30 #include "frameworks/core/components/display/display_component.h" 31 #include "frameworks/core/components/flex/flex_item_component.h" 32 #include "frameworks/core/components/focusable/focusable_component.h" 33 #include "frameworks/core/components/gesture_listener/gesture_component.h" 34 #include "frameworks/core/components/gesture_listener/gesture_listener_component.h" 35 #include "frameworks/core/components/menu/menu_component.h" 36 #include "frameworks/core/components/mouse_listener/mouse_listener_component.h" 37 #include "frameworks/core/components/navigation_bar/navigation_container_component.h" 38 #include "frameworks/core/components/page_transition/page_transition_component.h" 39 #include "frameworks/core/components/scroll/scroll_component.h" 40 #include "frameworks/core/components/stepper/stepper_item_component.h" 41 #ifndef WEARABLE_PRODUCT 42 #include "frameworks/core/components/popup/popup_component_v2.h" 43 #endif 44 #include "frameworks/core/components/positioned/positioned_component.h" 45 #include "frameworks/core/components/shared_transition/shared_transition_component.h" 46 #include "frameworks/core/components/touch_listener/touch_listener_component.h" 47 #include "frameworks/core/components/transform/transform_component.h" 48 #include "frameworks/core/components_v2/inspector/inspector_composed_component.h" 49 50 namespace OHOS::Ace::Framework { 51 using JsPageRadioGroups = std::unordered_map<std::string, RadioGroupComponent<std::string>>; 52 using JsPageCheckboxGroups = std::unordered_map<std::string, RefPtr<CheckboxComponent>>; 53 54 class ViewStackProcessor final { 55 public: 56 using SaveComponentEvent = std::function<void(std::unordered_map<std::string, RefPtr<Component>>)>; 57 friend class ScopedViewStackProcessor; 58 59 static ViewStackProcessor* GetInstance(); 60 ~ViewStackProcessor() = default; 61 // possible wrapping components 62 static std::string GenerateId(); 63 RefPtr<FlexItemComponent> GetFlexItemComponent(); 64 RefPtr<StepperItemComponent> GetStepperItemComponent(); 65 RefPtr<DisplayComponent> GetStepperDisplayComponent(); 66 RefPtr<ScrollComponent> GetStepperScrollComponent(); 67 RefPtr<BoxComponent> GetBoxComponent(); 68 RefPtr<Component> GetMainComponent() const; 69 RefPtr<DisplayComponent> GetDisplayComponent(); 70 bool HasDisplayComponent() const; 71 RefPtr<TransformComponent> GetTransformComponent(); 72 RefPtr<TouchListenerComponent> GetTouchListenerComponent(); 73 bool HasTouchListenerComponent() const; 74 RefPtr<MouseListenerComponent> GetMouseListenerComponent(); 75 RefPtr<GestureListenerComponent> GetClickGestureListenerComponent(); 76 bool HasClickGestureListenerComponent() const; 77 RefPtr<GestureListenerComponent> GetPanGestureListenerComponent(); 78 RefPtr<FocusableComponent> GetFocusableComponent(bool createIfNotExist = true); 79 RefPtr<SharedTransitionComponent> GetSharedTransitionComponent(); 80 RefPtr<GestureComponent> GetGestureComponent(); 81 RefPtr<PositionedComponent> GetPositionedComponent(); 82 RefPtr<ComposedComponent> GetRootComponent(const std::string& id = "", const std::string& name = ""); 83 RefPtr<PageTransitionComponent> GetPageTransitionComponent(); 84 RefPtr<CoverageComponent> GetCoverageComponent(); 85 void ClearPageTransitionComponent(); 86 RefPtr<MenuComponent> GetMenuComponent(bool createNewComponent = true); 87 #ifndef WEARABLE_PRODUCT 88 RefPtr<PopupComponentV2> GetPopupComponent(bool createNewComponent = true); 89 #endif 90 91 // create wrappingComponentsMap and the component to map and then Push 92 // the map to the stack. 93 // use flag: isCustomView to avoid creating redundant Components. 94 void Push(const RefPtr<Component>& component, bool isCustomView = false); 95 96 // Push special tabs component for tabs, tabContent. PushTabs(const RefPtr<Component> & component)97 void PushTabs(const RefPtr<Component>& component) 98 { 99 tabsStack_.push(component); 100 } 101 PopTabs()102 void PopTabs() 103 { 104 tabsStack_.pop(); 105 } 106 GetTopTabs()107 RefPtr<Component> GetTopTabs() const 108 { 109 if (tabsStack_.empty()) { 110 return nullptr; 111 } 112 return tabsStack_.top(); 113 } 114 115 // Wrap the components map for the stack top and then pop the stack. 116 // Add the wrappedcomponent has child of the new stack top's main component. 117 void Pop(); 118 119 // pop the last container 120 void PopContainer(); 121 122 // End of Render function, create component tree. 123 RefPtr<Component> Finish(); 124 125 // Set key to be used for next custom component on the stack 126 void PushKey(const std::string& key); 127 128 // Returns a key for the CustomComponent if it has been pushed to the stack. Default is "" 129 std::string GetKey(); 130 131 // Takes care of the viewId wrt to foreach 132 std::string ProcessViewId(const std::string& viewId); 133 134 // Clear the key pushed to the stack 135 void PopKey(); 136 137 // Sets the implicit animation option. All the views use the same option. 138 void SetImplicitAnimationOption(const AnimationOption& option); 139 140 // Returns implicit animation option. 141 const AnimationOption& GetImplicitAnimationOption() const; 142 143 void SetZIndex(RefPtr<Component>& component); 144 145 void SetIsPercentSize(RefPtr<Component>& component); 146 std::shared_ptr<JsPageRadioGroups> GetRadioGroupCompnent(); 147 std::shared_ptr<JsPageCheckboxGroups> GetCheckboxGroupCompnent(); 148 149 RefPtr<Component> GetNewComponent(); 150 RefPtr<V2::InspectorComposedComponent> GetInspectorComposedComponent() const; 151 SetVisualState(VisualState state)152 void SetVisualState(VisualState state) 153 { 154 visualState_ = state; 155 } 156 GetVisualState()157 VisualState GetVisualState() 158 { 159 return visualState_; 160 } 161 IsVisualStateSet()162 bool IsVisualStateSet() 163 { 164 return visualState_ != VisualState::NOTSET; 165 } 166 ClearVisualState()167 void ClearVisualState() 168 { 169 visualState_ = VisualState::NOTSET; 170 } 171 ClearStack()172 void ClearStack() 173 { 174 auto emptyStack = std::stack<std::unordered_map<std::string, RefPtr<Component>>>(); 175 componentsStack_.swap(emptyStack); 176 } 177 RefPtr<ComposedComponent> CreateInspectorWrapper(const std::string& inspectorTag); 178 179 private: 180 ViewStackProcessor(); 181 182 bool ShouldPopImmediately(); 183 184 #ifdef ACE_DEBUG_LOG 185 // Dump view stack comtent 186 void DumpStack(); 187 #endif 188 189 // Go through the wrappingComponentsMap and wrap the components 190 // should be done before pushing to the stack. 191 RefPtr<Component> WrapComponents(); 192 193 // Update position and enabled status 194 void UpdateTopComponentProps(const RefPtr<Component>& component); 195 196 void CreateInspectorComposedComponent(const std::string& inspectorTag); 197 void CreateScoringComponent(const std::string& tag); 198 RefPtr<Component> GetScoringComponent() const; 199 200 // Singleton instance 201 static thread_local std::unique_ptr<ViewStackProcessor> instance; 202 203 // stack 204 std::stack<std::unordered_map<std::string, RefPtr<Component>>> componentsStack_; 205 std::shared_ptr<JsPageRadioGroups> radioGroups_; 206 std::shared_ptr<JsPageCheckboxGroups> checkboxGroups_; 207 // stack for tabs component. 208 std::stack<RefPtr<Component>> tabsStack_; 209 210 RefPtr<PageTransitionComponent> pageTransitionComponent_; 211 212 std::string viewKey_; 213 std::stack<size_t> keyStack_; 214 215 RefPtr<AccessibilityManager> accessibilityManager_; 216 std::stack<int32_t> parentIdStack_; 217 218 AnimationOption implicitAnimationOption_; 219 VisualState visualState_ = VisualState::NOTSET; 220 221 static thread_local int32_t composedElementId_; 222 223 bool isScoringEnable_ = false; 224 225 ACE_DISALLOW_COPY_AND_MOVE(ViewStackProcessor); 226 }; 227 228 class ScopedViewStackProcessor final { 229 public: 230 ScopedViewStackProcessor(); 231 ~ScopedViewStackProcessor(); 232 233 private: 234 std::unique_ptr<ViewStackProcessor> instance_; 235 236 ACE_DISALLOW_COPY_AND_MOVE(ScopedViewStackProcessor); 237 }; 238 239 } // namespace OHOS::Ace::Framework 240 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_VIEW_STACK_PROCESSOR_H 241