• 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 namespace {
22 
23 int32_t g_tabControllerId = 0;
24 
25 } // namespace
26 
JSTabsController()27 JSTabsController::JSTabsController()
28 {
29     controller_ = CreateController();
30     swiperController_ = MakeRefPtr<SwiperController>();
31 }
32 
JSBind(BindingTarget globalObj)33 void JSTabsController::JSBind(BindingTarget globalObj)
34 {
35     JSClass<JSTabsController>::Declare("TabsController");
36     JSClass<JSTabsController>::Method("changeIndex", &JSTabsController::ChangeIndex);
37     JSClass<JSTabsController>::Bind(globalObj, JSTabsController::Constructor, JSTabsController::Destructor);
38 }
39 
Constructor(const JSCallbackInfo & args)40 void JSTabsController::Constructor(const JSCallbackInfo& args)
41 {
42     auto jsCalendarController = Referenced::MakeRefPtr<JSTabsController>();
43     jsCalendarController->IncRefCount();
44     args.SetReturnValue(Referenced::RawPtr(jsCalendarController));
45 }
46 
Destructor(JSTabsController * controller)47 void JSTabsController::Destructor(JSTabsController* controller)
48 {
49     if (controller != nullptr) {
50         controller->DecRefCount();
51     }
52 }
53 
CreateController()54 RefPtr<TabController> JSTabsController::CreateController()
55 {
56     return TabController::GetController(++g_tabControllerId);
57 }
58 
ChangeIndex(int32_t index)59 void JSTabsController::ChangeIndex(int32_t index)
60 {
61     if (swiperController_) {
62         swiperController_->SwipeTo(index);
63     }
64 
65     if (controller_) {
66         controller_->SetIndexByController(index, false);
67     }
68 }
69 
70 } // namespace OHOS::Ace::Framework
71