• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bridge/declarative_frontend/jsview/models/divider_model_impl.h"
19 #include "core/components/divider/divider_theme.h"
20 #include "core/components_ng/pattern/divider/divider_model_ng.h"
21 
22 namespace OHOS::Ace {
23 
24 std::unique_ptr<DividerModel> DividerModel::instance_ = nullptr;
25 
GetInstance()26 DividerModel* DividerModel::GetInstance()
27 {
28     if (!instance_) {
29 #ifdef NG_BUILD
30         instance_.reset(new NG::DividerModelNG());
31 #else
32         if (Container::IsCurrentUseNewPipeline()) {
33             instance_.reset(new NG::DividerModelNG());
34         } else {
35             instance_.reset(new Framework::DividerModelImpl());
36         }
37 #endif
38     }
39     return instance_.get();
40 }
41 
42 } // namespace OHOS::Ace
43 
44 namespace OHOS::Ace::Framework {
Create()45 void JSDivider::Create()
46 {
47     DividerModel::GetInstance()->Create();
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         LOGE("The argv is wrong, it is supposed to have at least 1 argument");
74         return;
75     }
76     Color dividerColor;
77     if (!ParseJsColor(info[0], dividerColor)) {
78         auto theme = GetTheme<DividerTheme>();
79         if (theme) {
80             dividerColor = theme->GetColor();
81         }
82         DividerModel::GetInstance()->DividerColor(dividerColor);
83         return;
84     }
85     DividerModel::GetInstance()->DividerColor(dividerColor);
86 }
87 
SetStrokeWidth(const JSCallbackInfo & info)88 void JSDivider::SetStrokeWidth(const JSCallbackInfo& info)
89 {
90     if (info.Length() < 1) {
91         LOGE("The arg is wrong, it is supposed to have at least 1 argument");
92         return;
93     }
94     Dimension strokeWidth;
95     if (!ParseJsDimensionVp(info[0], strokeWidth)) {
96         return;
97     }
98     DividerModel::GetInstance()->StrokeWidth(strokeWidth);
99 }
100 
JSBind(BindingTarget globalObj)101 void JSDivider::JSBind(BindingTarget globalObj)
102 {
103     JSClass<JSDivider>::Declare("Divider");
104     MethodOptions opt = MethodOptions::NONE;
105     JSClass<JSDivider>::StaticMethod("create", &JSDivider::Create, opt);
106     JSClass<JSDivider>::StaticMethod("color", &JSDivider::SetDividerColor, opt);
107     JSClass<JSDivider>::StaticMethod("vertical", &JSDivider::SetVertical, opt);
108     JSClass<JSDivider>::StaticMethod("strokeWidth", &JSDivider::SetStrokeWidth, opt);
109     JSClass<JSDivider>::StaticMethod("lineCap", &JSDivider::SetLineCap, opt);
110     JSClass<JSDivider>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
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>::Inherit<JSViewAbstract>();
120     JSClass<JSDivider>::Bind<>(globalObj);
121 }
122 } // namespace OHOS::Ace::Framework
123