• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "bridge/declarative_frontend/jsview/js_tabs_controller.h"
17 
18 #include "bridge/declarative_frontend/engine/bindings.h"
19 
20 namespace OHOS::Ace::Framework {
21 #ifndef NG_BUILD
22 // dismiss unused warning in NG_BUILD
23 namespace {
24 
25 int32_t g_tabControllerId = 0;
26 
27 } // namespace
28 #endif
29 
JSTabsController()30 JSTabsController::JSTabsController()
31 {
32     controller_ = CreateController();
33     swiperController_ = MakeRefPtr<SwiperController>();
34 }
35 
JSBind(BindingTarget globalObj)36 void JSTabsController::JSBind(BindingTarget globalObj)
37 {
38     JSClass<JSTabsController>::Declare("TabsController");
39     JSClass<JSTabsController>::Method("changeIndex", &JSTabsController::ChangeIndex);
40     JSClass<JSTabsController>::Bind(globalObj, JSTabsController::Constructor, JSTabsController::Destructor);
41 }
42 
Constructor(const JSCallbackInfo & args)43 void JSTabsController::Constructor(const JSCallbackInfo& args)
44 {
45     auto jsCalendarController = Referenced::MakeRefPtr<JSTabsController>();
46     jsCalendarController->IncRefCount();
47     args.SetReturnValue(Referenced::RawPtr(jsCalendarController));
48 }
49 
Destructor(JSTabsController * controller)50 void JSTabsController::Destructor(JSTabsController* controller)
51 {
52     if (controller != nullptr) {
53         controller->DecRefCount();
54     }
55 }
56 
CreateController()57 RefPtr<TabController> JSTabsController::CreateController()
58 {
59 #ifdef NG_BUILD
60     return nullptr;
61 #else
62     return TabController::GetController(++g_tabControllerId);
63 #endif
64 }
65 
ChangeIndex(int32_t index)66 void JSTabsController::ChangeIndex(int32_t index)
67 {
68     ContainerScope scope(instanceId_);
69     if (swiperController_) {
70         const auto& updateCubicCurveCallback = swiperController_->GetUpdateCubicCurveCallback();
71         if (updateCubicCurveCallback != nullptr) {
72             updateCubicCurveCallback();
73         }
74         swiperController_->SwipeTo(index);
75     }
76 
77 #ifndef NG_BUILD
78     if (controller_) {
79         controller_->SetIndexByController(index, false);
80     }
81 #endif
82 }
83 
84 } // namespace OHOS::Ace::Framework
85