• 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_piece.h"
17 
18 #include "base/log/ace_scoring_log.h"
19 #include "bridge/declarative_frontend/engine/functions/js_click_function.h"
20 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
21 #include "bridge/declarative_frontend/view_stack_processor.h"
22 #include "core/components/box/box_component.h"
23 #include "core/components/piece/piece_component.h"
24 #include "core/components/piece/piece_theme.h"
25 
26 namespace OHOS::Ace::Framework {
27 
28 const std::vector<FontStyle> FONT_STYLES = { FontStyle::NORMAL, FontStyle::ITALIC };
29 
Create(const JSCallbackInfo & info)30 void JSPiece::Create(const JSCallbackInfo& info)
31 {
32     if (info.Length() < 1 || !info[0]->IsObject()) {
33         return;
34     }
35     auto paramObject = JSRef<JSObject>::Cast(info[0]);
36     auto getContent = paramObject->GetProperty("content");
37     auto getIcon = paramObject->GetProperty("icon");
38     std::string content;
39     std::string icon;
40     if (getContent->IsString()) {
41         content = getContent->ToString();
42     }
43     if (getIcon->IsString()) {
44         icon = getIcon->ToString();
45     }
46     auto component = AceType::MakeRefPtr<PieceComponent>();
47     component->SetContent(content);
48     component->SetIcon(icon);
49     auto theme = GetTheme<PieceTheme>();
50     if (!theme) {
51         return;
52     }
53     component->InitializeStyle(theme);
54     Border border;
55     border.SetBorderRadius(Radius(theme->GetHeight() / 2.0));
56     component->SetBorder(border);
57     ViewStackProcessor::GetInstance()->Push(component);
58 
59     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
60     AnimationOption option = ViewStackProcessor::GetInstance()->GetImplicitAnimationOption();
61     box->SetHeight(theme->GetHeight(), option);
62     Edge edge;
63     edge.SetLeft(theme->GetPaddingHorizontal());
64     edge.SetRight(theme->GetPaddingHorizontal());
65     edge.SetTop(theme->GetPaddingVertical());
66     edge.SetBottom(theme->GetPaddingVertical());
67     box->SetPadding(edge);
68 }
69 
JSBind(BindingTarget globalObj)70 void JSPiece::JSBind(BindingTarget globalObj)
71 {
72     JSClass<JSPiece>::Declare("Piece");
73     MethodOptions opt = MethodOptions::NONE;
74     JSClass<JSPiece>::StaticMethod("create", &JSPiece::Create, opt);
75     JSClass<JSPiece>::StaticMethod("iconPosition", &JSPiece::SetIconPosition, opt);
76     JSClass<JSPiece>::StaticMethod("showDelete", &JSPiece::SetShowDelete, opt);
77     JSClass<JSPiece>::StaticMethod("fontColor", &JSPiece::SetTextColor, opt);
78     JSClass<JSPiece>::StaticMethod("fontSize", &JSPiece::SetFontSize, opt);
79     JSClass<JSPiece>::StaticMethod("fontStyle", &JSPiece::SetFontStyle, opt);
80     JSClass<JSPiece>::StaticMethod("fontWeight", &JSPiece::SetFontWeight, opt);
81     JSClass<JSPiece>::StaticMethod("fontFamily", &JSPiece::SetFontFamily, opt);
82     JSClass<JSPiece>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
83     JSClass<JSPiece>::StaticMethod("onClose", &JSPiece::JsOnClose);
84     JSClass<JSPiece>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
85     JSClass<JSPiece>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
86     JSClass<JSPiece>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
87     JSClass<JSPiece>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
88     JSClass<JSPiece>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
89     JSClass<JSPiece>::InheritAndBind<JSViewAbstract>(globalObj);
90 }
91 
92 // showDelete Parameters should be bool type,but after click event triggering,
93 // The callback function transfers parameters, and the parameter type changes to number.
SetShowDelete(const JSCallbackInfo & info)94 void JSPiece::SetShowDelete(const JSCallbackInfo& info)
95 {
96     bool showDelete = false;
97     auto stack = ViewStackProcessor::GetInstance();
98     auto component = AceType::DynamicCast<PieceComponent>(stack->GetMainComponent());
99     if (!component) {
100         return;
101     }
102     if (info[0]->IsBoolean()) {
103         showDelete = info[0]->ToBoolean();
104         component->SetShowDelete(showDelete);
105     } else if (info[0]->IsNumber()) {
106         int32_t arg = info[0]->ToNumber<int32_t>();
107         if (arg == 0 || arg == 1) {
108             showDelete = static_cast<bool>(arg);
109             component->SetShowDelete(showDelete);
110         }
111     } else {
112         component->SetShowDelete(showDelete);
113     }
114 }
115 
JsOnClose(const JSCallbackInfo & info)116 void JSPiece::JsOnClose(const JSCallbackInfo& info)
117 {
118     if (info[0]->IsFunction()) {
119         JSRef<JSFunc> clickFunction = JSRef<JSFunc>::Cast(info[0]);
120         auto onClickFunc = AceType::MakeRefPtr<JsClickFunction>(clickFunction);
121         EventMarker clickEventId(
122             [execCtx = info.GetExecutionContext(), func = std::move(onClickFunc)](const BaseEventInfo* info) {
123                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
124                 ACE_SCORING_EVENT("Piece.onClose");
125                 func->Execute();
126             });
127         auto pieceComponent =
128             AceType::DynamicCast<PieceComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
129         if (pieceComponent) {
130             pieceComponent->SetOnDelete(clickEventId);
131         }
132     }
133 }
134 
SetTextColor(const JSCallbackInfo & info)135 void JSPiece::SetTextColor(const JSCallbackInfo& info)
136 {
137     Color textColor;
138     if (!ParseJsColor(info[0], textColor)) {
139         return;
140     }
141     auto stack = ViewStackProcessor::GetInstance();
142     auto component = AceType::DynamicCast<PieceComponent>(stack->GetMainComponent());
143     if (!component) {
144         return;
145     }
146     auto textStyle = component->GetTextStyle();
147     textStyle.SetTextColor(textColor);
148     component->SetTextStyle(std::move(textStyle));
149 }
150 
SetFontSize(const JSCallbackInfo & info)151 void JSPiece::SetFontSize(const JSCallbackInfo& info)
152 {
153     CalcDimension fontSize;
154     if (!ParseJsDimensionFp(info[0], fontSize)) {
155         return;
156     }
157     auto stack = ViewStackProcessor::GetInstance();
158     auto component = AceType::DynamicCast<PieceComponent>(stack->GetMainComponent());
159     if (!component) {
160         return;
161     }
162     auto textStyle = component->GetTextStyle();
163     textStyle.SetFontSize(fontSize);
164     component->SetTextStyle(std::move(textStyle));
165 }
166 
SetFontStyle(int32_t value)167 void JSPiece::SetFontStyle(int32_t value)
168 {
169     auto stack = ViewStackProcessor::GetInstance();
170     auto component = AceType::DynamicCast<PieceComponent>(stack->GetMainComponent());
171     if (!component) {
172         return;
173     }
174     if (value >= 0 && value < static_cast<int32_t>(FONT_STYLES.size())) {
175         auto textStyle = component->GetTextStyle();
176         textStyle.SetFontStyle(FONT_STYLES[value]);
177         component->SetTextStyle(std::move(textStyle));
178     }
179 }
180 
SetFontWeight(const std::string & value)181 void JSPiece::SetFontWeight(const std::string& value)
182 {
183     auto stack = ViewStackProcessor::GetInstance();
184     auto component = AceType::DynamicCast<PieceComponent>(stack->GetMainComponent());
185     if (!component) {
186         return;
187     }
188 
189     auto textStyle = component->GetTextStyle();
190     textStyle.SetFontWeight(ConvertStrToFontWeight(value));
191     component->SetTextStyle(std::move(textStyle));
192 }
193 
SetFontFamily(const JSCallbackInfo & info)194 void JSPiece::SetFontFamily(const JSCallbackInfo& info)
195 {
196     std::vector<std::string> fontFamilies;
197     if (!ParseJsFontFamilies(info[0], fontFamilies)) {
198         return;
199     }
200     auto stack = ViewStackProcessor::GetInstance();
201     auto component = AceType::DynamicCast<PieceComponent>(stack->GetMainComponent());
202     if (!component) {
203         return;
204     }
205     auto textStyle = component->GetTextStyle();
206     textStyle.SetFontFamilies(fontFamilies);
207     component->SetTextStyle(std::move(textStyle));
208 }
209 
SetIconPosition(const JSCallbackInfo & info)210 void JSPiece::SetIconPosition(const JSCallbackInfo& info)
211 {
212     if (!info[0]->IsNumber()) {
213         return;
214     }
215 
216     auto stack = ViewStackProcessor::GetInstance();
217     auto component = AceType::DynamicCast<PieceComponent>(stack->GetMainComponent());
218     if (!component) {
219         return;
220     }
221 
222     auto pieceIconPosition = static_cast<OHOS::Ace::IconPosition>(info[0]->ToNumber<int32_t>());
223     component->SetIconPosition(pieceIconPosition);
224 }
225 
226 } // namespace OHOS::Ace::Framework
227