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 DividerModel::GetInstance()->ResetResObj("divider.color");
74 auto theme = GetTheme<DividerTheme>();
75 CHECK_NULL_VOID(theme);
76 Color dividerColor = theme->GetColor();
77 RefPtr<ResourceObject> dividerResObj;
78 if (!ParseJsColor(info[0], dividerColor, dividerResObj)) {
79 DividerModel::GetInstance()->ResetDividerColor();
80 return;
81 }
82 if (SystemProperties::ConfigChangePerform() && dividerResObj) {
83 DividerModel::GetInstance()->DividerColor(dividerResObj);
84 } else {
85 DividerModel::GetInstance()->DividerColor(dividerColor);
86 }
87 }
88
SetStrokeWidth(const JSCallbackInfo & info)89 void JSDivider::SetStrokeWidth(const JSCallbackInfo& info)
90 {
91 if (info.Length() < 1) {
92 return;
93 }
94 auto theme = GetTheme<DividerTheme>();
95 CHECK_NULL_VOID(theme);
96 CalcDimension strokeWidth = theme->GetStokeWidth();
97 if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
98 strokeWidth = 1.0_px;
99 }
100 if (!ParseJsDimensionVpNG(info[0], strokeWidth, false)) {
101 strokeWidth = 1.0_px;
102 }
103 DividerModel::GetInstance()->StrokeWidth(strokeWidth);
104 }
105
JSBind(BindingTarget globalObj)106 void JSDivider::JSBind(BindingTarget globalObj)
107 {
108 JSClass<JSDivider>::Declare("Divider");
109 MethodOptions opt = MethodOptions::NONE;
110 JSClass<JSDivider>::StaticMethod("create", &JSDivider::Create, opt);
111 JSClass<JSDivider>::StaticMethod("color", &JSDivider::SetDividerColor, opt);
112 JSClass<JSDivider>::StaticMethod("vertical", &JSDivider::SetVertical, opt);
113 JSClass<JSDivider>::StaticMethod("strokeWidth", &JSDivider::SetStrokeWidth, opt);
114 JSClass<JSDivider>::StaticMethod("lineCap", &JSDivider::SetLineCap, opt);
115 JSClass<JSDivider>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
116 JSClass<JSDivider>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
117 JSClass<JSDivider>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
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