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 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
80 return;
81 }
82 auto theme = GetTheme<DividerTheme>();
83 CHECK_NULL_VOID(theme);
84 Color dividerColor = theme->GetColor();
85 ParseJsColor(info[0], dividerColor);
86 DividerModel::GetInstance()->DividerColor(dividerColor);
87 }
88
SetStrokeWidth(const JSCallbackInfo & info)89 void JSDivider::SetStrokeWidth(const JSCallbackInfo& info)
90 {
91 if (info.Length() < 1) {
92 LOGE("The arg is wrong, it is supposed to have at least 1 argument");
93 return;
94 }
95 auto theme = GetTheme<DividerTheme>();
96 CHECK_NULL_VOID(theme);
97 CalcDimension strokeWidth = theme->GetStokeWidth();
98 auto pipeline = PipelineBase::GetCurrentContext();
99 CHECK_NULL_VOID(pipeline);
100 const static int32_t PLATFORM_VERSION_TEN = 10;
101 if (pipeline->GetMinPlatformVersion() >= PLATFORM_VERSION_TEN) {
102 strokeWidth = 1.0_px;
103 }
104 ParseJsDimensionVp(info[0], strokeWidth);
105 DividerModel::GetInstance()->StrokeWidth(strokeWidth);
106 }
107
JSBind(BindingTarget globalObj)108 void JSDivider::JSBind(BindingTarget globalObj)
109 {
110 JSClass<JSDivider>::Declare("Divider");
111 MethodOptions opt = MethodOptions::NONE;
112 JSClass<JSDivider>::StaticMethod("create", &JSDivider::Create, opt);
113 JSClass<JSDivider>::StaticMethod("color", &JSDivider::SetDividerColor, opt);
114 JSClass<JSDivider>::StaticMethod("vertical", &JSDivider::SetVertical, opt);
115 JSClass<JSDivider>::StaticMethod("strokeWidth", &JSDivider::SetStrokeWidth, opt);
116 JSClass<JSDivider>::StaticMethod("lineCap", &JSDivider::SetLineCap, opt);
117 JSClass<JSDivider>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
118 JSClass<JSDivider>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
119 JSClass<JSDivider>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
120 JSClass<JSDivider>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
121 JSClass<JSDivider>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
122 JSClass<JSDivider>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
123 JSClass<JSDivider>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
124 JSClass<JSDivider>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
125
126 JSClass<JSDivider>::InheritAndBind<JSViewAbstract>(globalObj);
127 }
128 } // namespace OHOS::Ace::Framework
129