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