• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "frameworks/bridge/declarative_frontend/jsview/js_divider.h"
17 
18 #include "base/geometry/dimension.h"
19 #include "bridge/declarative_frontend/jsview/models/divider_model_impl.h"
20 #include "core/components/divider/divider_theme.h"
21 #include "core/components_ng/pattern/divider/divider_model_ng.h"
22 #include "core/pipeline/pipeline_base.h"
23 
24 namespace OHOS::Ace {
25 
26 std::unique_ptr<DividerModel> DividerModel::instance_ = nullptr;
27 std::mutex DividerModel::mutex_;
28 
GetInstance()29 DividerModel* DividerModel::GetInstance()
30 {
31     if (!instance_) {
32         std::lock_guard<std::mutex> lock(mutex_);
33         if (!instance_) {
34 #ifdef NG_BUILD
35             instance_.reset(new NG::DividerModelNG());
36 #else
37             if (Container::IsCurrentUseNewPipeline()) {
38                 instance_.reset(new NG::DividerModelNG());
39             } else {
40                 instance_.reset(new Framework::DividerModelImpl());
41             }
42 #endif
43         }
44     }
45     return instance_.get();
46 }
47 
48 } // namespace OHOS::Ace
49 
50 namespace OHOS::Ace::Framework {
Create()51 void JSDivider::Create()
52 {
53     DividerModel::GetInstance()->Create();
54 }
55 
SetVertical(bool isVertical)56 void JSDivider::SetVertical(bool isVertical)
57 {
58     DividerModel::GetInstance()->Vertical(isVertical);
59 }
60 
SetLineCap(int lineCap)61 void JSDivider::SetLineCap(int lineCap)
62 {
63     auto lineCapStyle = LineCap::BUTT;
64 
65     if (static_cast<int>(LineCap::SQUARE) == lineCap) {
66         lineCapStyle = LineCap::SQUARE;
67     } else if (static_cast<int>(LineCap::ROUND) == lineCap) {
68         lineCapStyle = LineCap::ROUND;
69     } else {
70         // default linecap of divider
71         lineCapStyle = LineCap::BUTT;
72     }
73     DividerModel::GetInstance()->LineCap(lineCapStyle);
74 }
75 
SetDividerColor(const JSCallbackInfo & info)76 void JSDivider::SetDividerColor(const JSCallbackInfo& info)
77 {
78     if (info.Length() < 1) {
79         return;
80     }
81     auto theme = GetTheme<DividerTheme>();
82     CHECK_NULL_VOID(theme);
83     Color dividerColor = theme->GetColor();
84     ParseJsColor(info[0], dividerColor);
85     DividerModel::GetInstance()->DividerColor(dividerColor);
86 }
87 
SetStrokeWidth(const JSCallbackInfo & info)88 void JSDivider::SetStrokeWidth(const JSCallbackInfo& info)
89 {
90     if (info.Length() < 1) {
91         return;
92     }
93     auto theme = GetTheme<DividerTheme>();
94     CHECK_NULL_VOID(theme);
95     CalcDimension strokeWidth = theme->GetStokeWidth();
96     if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
97         strokeWidth = 1.0_px;
98     }
99     if (!ParseJsDimensionVpNG(info[0], strokeWidth, false)) {
100         strokeWidth = 1.0_px;
101     }
102     DividerModel::GetInstance()->StrokeWidth(strokeWidth);
103 }
104 
JSBind(BindingTarget globalObj)105 void JSDivider::JSBind(BindingTarget globalObj)
106 {
107     JSClass<JSDivider>::Declare("Divider");
108     MethodOptions opt = MethodOptions::NONE;
109     JSClass<JSDivider>::StaticMethod("create", &JSDivider::Create, opt);
110     JSClass<JSDivider>::StaticMethod("color", &JSDivider::SetDividerColor, opt);
111     JSClass<JSDivider>::StaticMethod("vertical", &JSDivider::SetVertical, opt);
112     JSClass<JSDivider>::StaticMethod("strokeWidth", &JSDivider::SetStrokeWidth, opt);
113     JSClass<JSDivider>::StaticMethod("lineCap", &JSDivider::SetLineCap, opt);
114     JSClass<JSDivider>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
115     JSClass<JSDivider>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
116     JSClass<JSDivider>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
117     JSClass<JSDivider>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
118     JSClass<JSDivider>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
119     JSClass<JSDivider>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
120     JSClass<JSDivider>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
121     JSClass<JSDivider>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
122 
123     JSClass<JSDivider>::InheritAndBind<JSViewAbstract>(globalObj);
124 }
125 } // namespace OHOS::Ace::Framework
126