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