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