1 /*
2  * Copyright (c) 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 #include "bridge/declarative_frontend/jsview/models/scroll_bar_model_impl.h"
16 
17 #include "base/geometry/axis.h"
18 #include "bridge/declarative_frontend/view_stack_processor.h"
19 #include "core/components/common/layout/constants.h"
20 #include "core/components/proxy/proxy_component.h"
21 #include "core/components/scroll_bar/scroll_bar_component.h"
22 
23 namespace OHOS::Ace::Framework {
24 namespace {
25 const std::vector<DisplayMode> DISPLAY_MODE = { DisplayMode::OFF, DisplayMode::AUTO, DisplayMode::ON };
26 const std::vector<Axis> AXIS = { Axis::VERTICAL, Axis::HORIZONTAL, Axis::NONE };
27 }
28 
GetScrollBarProxy(const RefPtr<ScrollProxy> & scrollProxy)29 RefPtr<ScrollProxy> ScrollBarModelImpl::GetScrollBarProxy(const RefPtr<ScrollProxy>& scrollProxy)
30 {
31     auto proxy = AceType::DynamicCast<ScrollBarProxy>(scrollProxy);
32     if (!proxy) {
33         proxy = AceType::MakeRefPtr<ScrollBarProxy>();
34     }
35 
36     return proxy;
37 }
38 
Create(const RefPtr<ScrollProxy> & proxy,bool infoflag,bool proxyFlag,int directionValue,int stateValue)39 void ScrollBarModelImpl::Create(const RefPtr<ScrollProxy>& proxy, bool infoflag, bool proxyFlag,
40     int directionValue, int stateValue)
41 {
42     RefPtr<Component> child;
43     auto scrollBarComponent = AceType::MakeRefPtr<OHOS::Ace::ScrollBarComponent>(child);
44 
45     if (infoflag) {
46         if (proxyFlag) {
47             auto scrollBarProxy = AceType::DynamicCast<ScrollBarProxy>(proxy);
48             scrollBarComponent->SetScrollBarProxy(scrollBarProxy);
49         }
50 
51         if (directionValue != -1 && directionValue < static_cast<int>(AXIS.size())) {
52             scrollBarComponent->SetAxis(AXIS[directionValue]);
53         }
54 
55         if (stateValue != -1) {
56             scrollBarComponent->SetDisplayMode(DISPLAY_MODE[stateValue]);
57         }
58     }
59 
60     ViewStackProcessor::GetInstance()->ClaimElementId(scrollBarComponent);
61     ViewStackProcessor::GetInstance()->Push(scrollBarComponent);
62     ViewStackProcessor::GetInstance()->GetDisplayComponent();
63 }
64 } // namespace OHOS::Ace::Framework
65