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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_INDICATOR_H 17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_INDICATOR_H 18 19 #include "frameworks/bridge/declarative_frontend/jsview/js_view_abstract.h" 20 #include "frameworks/core/components_ng/base/ui_node.h" 21 #include "frameworks/core/components_ng/pattern/swiper_indicator/indicator_common/indicator_controller.h" 22 23 namespace OHOS::Ace::Framework { 24 25 class JSIndicator : public JSViewAbstract { 26 public: 27 static void JSBind(BindingTarget globalObj); 28 static void Create(const JSCallbackInfo& info); 29 30 protected: 31 static void SetLoop(const JSCallbackInfo& info); 32 static void SetOnChange(const JSCallbackInfo& info); 33 static void SetInitialIndex(const JSCallbackInfo& info); 34 static void SetStyle(const JSCallbackInfo& info); 35 static void SetCount(const JSCallbackInfo& info); 36 static void SetVertical(const JSCallbackInfo& info); 37 static SwiperParameters GetDotIndicatorInfo(const JSRef<JSObject>& obj); 38 static SwiperDigitalParameters GetDigitIndicatorInfo(const JSRef<JSObject>& obj); 39 static std::optional<Dimension> ParseIndicatorDimension(const JSRef<JSVal>& value); 40 static void GetFontContent(const JSRef<JSVal>& font, bool isSelected, SwiperDigitalParameters& digitalParameters); 41 static void SetDotIndicatorInfo(const JSRef<JSObject>& obj, SwiperParameters& swiperParameters, 42 const RefPtr<SwiperIndicatorTheme>& swiperIndicatorTheme); 43 }; 44 45 class JSIndicatorController final : public Referenced { 46 public: 47 JSIndicatorController() = default; 48 ~JSIndicatorController() override = default; 49 static void JSBind(BindingTarget globalObj); 50 static void Constructor(const JSCallbackInfo& args); 51 static void Destructor(JSIndicatorController* scroller); 52 ShowNext(const JSCallbackInfo & args)53 void ShowNext(const JSCallbackInfo& args) 54 { 55 ContainerScope scope(instanceId_); 56 if (controller_) { 57 controller_->ShowNext(); 58 } 59 } 60 ShowPrevious(const JSCallbackInfo & args)61 void ShowPrevious(const JSCallbackInfo& args) 62 { 63 ContainerScope scope(instanceId_); 64 if (controller_) { 65 controller_->ShowPrevious(); 66 } 67 } 68 69 void ChangeIndex(const JSCallbackInfo& args); SetController(const RefPtr<NG::IndicatorController> & controller,WeakPtr<NG::UINode> & indicatorNode)70 void SetController(const RefPtr<NG::IndicatorController>& controller, WeakPtr<NG::UINode>& indicatorNode) 71 { 72 controller_ = controller; 73 indicatorNode_ = indicatorNode; 74 hasController_ = true; 75 if (hasSwiperNode_) { 76 controller_->SetSwiperNode(swiperNode_, indicatorNode_); 77 } 78 } 79 GetController()80 RefPtr<NG::IndicatorController> GetController() 81 { 82 return controller_; 83 } 84 SetInstanceId(int32_t id)85 void SetInstanceId(int32_t id) 86 { 87 instanceId_ = id; 88 } 89 SetSwiperNode(WeakPtr<NG::UINode> & node)90 void SetSwiperNode(WeakPtr<NG::UINode>& node) 91 { 92 swiperNode_ = node; 93 hasSwiperNode_ = true; 94 if (hasController_) { 95 controller_->SetSwiperNode(swiperNode_, indicatorNode_); 96 } 97 } 98 ResetSwiperNode()99 void ResetSwiperNode() 100 { 101 swiperNode_ = nullptr; 102 auto controller = GetController(); 103 if (hasController_ && GetController()) { 104 controller->ResetSwiperNode(); 105 } 106 } 107 108 private: 109 int32_t instanceId_ = INSTANCE_ID_UNDEFINED; 110 RefPtr<NG::IndicatorController> controller_; 111 bool hasSwiperNode_ = false; 112 bool hasController_ = false; 113 WeakPtr<NG::UINode> swiperNode_; 114 WeakPtr<NG::UINode> indicatorNode_; 115 ACE_DISALLOW_COPY_AND_MOVE(JSIndicatorController); 116 }; 117 118 } // namespace OHOS::Ace::Framework 119 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_INDICATOR_H