1 /*
2 * Copyright (c) 2023 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 "bridge/declarative_frontend/jsview/models/indexer_model_impl.h"
17
18 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
19 #include "bridge/declarative_frontend/view_stack_processor.h"
20
21 namespace OHOS::Ace::Framework {
Create(std::vector<std::string> & indexerArray,int32_t selectedVal)22 void IndexerModelImpl::Create(std::vector<std::string>& indexerArray, int32_t selectedVal)
23 {
24 if (static_cast<int32_t>(indexerArray.size()) <= 0) {
25 return;
26 }
27 auto indexerComponent = AceType::MakeRefPtr<V2::IndexerComponent>(indexerArray, selectedVal);
28 ViewStackProcessor::GetInstance()->ClaimElementId(indexerComponent);
29 ViewStackProcessor::GetInstance()->Push(indexerComponent);
30 auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent();
31 CHECK_NULL_VOID(focusableComponent);
32 focusableComponent->SetFocusable(true);
33 focusableComponent->SetFocusNode(true);
34 }
35
SetSelectedColor(const std::optional<Color> & color)36 void IndexerModelImpl::SetSelectedColor(const std::optional<Color>& color)
37 {
38 auto indexerComponent =
39 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
40 if (indexerComponent && color.has_value()) {
41 auto textStyle = indexerComponent->GetActiveTextStyle();
42 textStyle.SetTextColor(color.value());
43 indexerComponent->SetActiveTextStyle(std::move(textStyle));
44 }
45 }
46
SetColor(const std::optional<Color> & color)47 void IndexerModelImpl::SetColor(const std::optional<Color>& color)
48 {
49 auto indexerComponent =
50 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
51 if (indexerComponent && color.has_value()) {
52 auto textStyle = indexerComponent->GetNormalTextStyle();
53 textStyle.SetTextColor(color.value());
54 indexerComponent->SetNormalTextStyle(std::move(textStyle));
55 }
56 }
57
SetPopupColor(const std::optional<Color> & color)58 void IndexerModelImpl::SetPopupColor(const std::optional<Color>& color)
59 {
60 auto indexerComponent =
61 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
62 if (indexerComponent && color.has_value()) {
63 auto textStyle = indexerComponent->GetBubbleTextStyle();
64 textStyle.SetTextColor(color.value());
65 indexerComponent->SetBubbleTextStyle(std::move(textStyle));
66 }
67 }
68
SetSelectedBackgroundColor(const std::optional<Color> & color)69 void IndexerModelImpl::SetSelectedBackgroundColor(const std::optional<Color>& color)
70 {
71 auto indexerComponent =
72 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
73 if (indexerComponent && color.has_value()) {
74 indexerComponent->SetSelectedBackgroundColor(color.value());
75 }
76 }
77
SetPopupBackground(const std::optional<Color> & color)78 void IndexerModelImpl::SetPopupBackground(const std::optional<Color>& color)
79 {
80 auto indexerComponent =
81 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
82 if (indexerComponent && color.has_value()) {
83 indexerComponent->SetBubbleBackgroundColor(color.value());
84 }
85 }
86
SetUsingPopup(bool state)87 void IndexerModelImpl::SetUsingPopup(bool state)
88 {
89 auto indexerComponent =
90 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
91 if (indexerComponent) {
92 indexerComponent->SetBubbleEnabled(state);
93 }
94 }
95
SetSelectedFont(std::optional<Dimension> & fontSize,std::optional<FontWeight> & fontWeight,std::optional<std::vector<std::string>> & fontFamily,std::optional<FontStyle> & fontStyle)96 void IndexerModelImpl::SetSelectedFont(std::optional<Dimension>& fontSize, std::optional<FontWeight>& fontWeight,
97 std::optional<std::vector<std::string>>& fontFamily, std::optional<FontStyle>& fontStyle)
98 {
99 auto indexerComponent =
100 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
101 if (indexerComponent) {
102 TextStyle textStyle;
103 SetTextStyle(textStyle, fontSize, fontWeight, fontFamily, fontStyle);
104 indexerComponent->SetActiveTextStyle(textStyle);
105 }
106 }
107
SetPopupFont(std::optional<Dimension> & fontSize,std::optional<FontWeight> & fontWeight,std::optional<std::vector<std::string>> & fontFamily,std::optional<FontStyle> & fontStyle)108 void IndexerModelImpl::SetPopupFont(std::optional<Dimension>& fontSize, std::optional<FontWeight>& fontWeight,
109 std::optional<std::vector<std::string>>& fontFamily, std::optional<FontStyle>& fontStyle)
110 {
111 auto indexerComponent =
112 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
113 if (indexerComponent) {
114 TextStyle textStyle;
115 SetTextStyle(textStyle, fontSize, fontWeight, fontFamily, fontStyle);
116 indexerComponent->SetBubbleTextStyle(textStyle);
117 }
118 }
119
SetFont(std::optional<Dimension> & fontSize,std::optional<FontWeight> & fontWeight,std::optional<std::vector<std::string>> & fontFamily,std::optional<FontStyle> & fontStyle)120 void IndexerModelImpl::SetFont(std::optional<Dimension>& fontSize, std::optional<FontWeight>& fontWeight,
121 std::optional<std::vector<std::string>>& fontFamily, std::optional<FontStyle>& fontStyle)
122 {
123 auto indexerComponent =
124 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
125 if (indexerComponent) {
126 TextStyle textStyle;
127 SetTextStyle(textStyle, fontSize, fontWeight, fontFamily, fontStyle);
128 indexerComponent->SetNormalTextStyle(textStyle);
129 }
130 }
131
SetItemSize(const Dimension & value)132 void IndexerModelImpl::SetItemSize(const Dimension& value)
133 {
134 auto indexerComponent =
135 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
136 if (indexerComponent) {
137 indexerComponent->SetItemSize(value);
138 }
139 }
140
SetAlignStyle(int32_t value)141 void IndexerModelImpl::SetAlignStyle(int32_t value)
142 {
143 auto indexerComponent =
144 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
145 if (indexerComponent) {
146 indexerComponent->SetAlignStyle(ALIGN_STYLE[value]);
147 }
148 }
149
SetOnSelected(std::function<void (const int32_t selected)> && onSelect)150 void IndexerModelImpl::SetOnSelected(std::function<void(const int32_t selected)>&& onSelect)
151 {
152 auto fun = onSelect;
153 auto onSelectEvent = [fun](const BaseEventInfo* info) {
154 auto eventInfo = TypeInfoHelper::DynamicCast<V2::IndexerEventInfo>(info);
155 if (!eventInfo) {
156 return;
157 }
158 fun(eventInfo->GetSelectedIndex());
159 };
160
161 auto indexerComponent =
162 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
163 auto eventMarker = EventMarker(std::move(onSelectEvent));
164 if (indexerComponent) {
165 indexerComponent->SetSelectedEvent(eventMarker);
166 }
167 }
168
SetOnRequestPopupData(std::function<std::vector<std::string> (const int32_t selected)> && RequestPopupData)169 void IndexerModelImpl::SetOnRequestPopupData(
170 std::function<std::vector<std::string>(const int32_t selected)>&& RequestPopupData)
171 {
172 auto fun = RequestPopupData;
173 auto onSelectEvent = [fun](std::shared_ptr<V2::IndexerEventInfo> info) -> std::vector<std::string> {
174 return fun(info->GetSelectedIndex());
175 };
176
177 auto indexerComponent =
178 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
179 if (indexerComponent) {
180 indexerComponent->SetRequestPopupDataFunc(std::move(onSelectEvent));
181 }
182 }
183
SetOnPopupSelected(std::function<void (const int32_t selected)> && onPopupSelected)184 void IndexerModelImpl::SetOnPopupSelected(std::function<void(const int32_t selected)>&& onPopupSelected)
185 {
186 auto fun = onPopupSelected;
187 auto onPopupSelectedEvent = [fun](const BaseEventInfo* info) {
188 auto eventInfo = TypeInfoHelper::DynamicCast<V2::IndexerEventInfo>(info);
189 if (!eventInfo) {
190 return;
191 }
192 fun(eventInfo->GetSelectedIndex());
193 };
194
195 auto indexerComponent =
196 AceType::DynamicCast<V2::IndexerComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
197 auto eventMarker = EventMarker(std::move(onPopupSelectedEvent));
198 if (indexerComponent) {
199 indexerComponent->SetPopupSelectedEvent(eventMarker);
200 }
201 }
202
SetTextStyle(TextStyle & textStyle,std::optional<Dimension> & fontSize,std::optional<FontWeight> & fontWeight,std::optional<std::vector<std::string>> & fontFamily,std::optional<FontStyle> & fontStyle)203 void IndexerModelImpl::SetTextStyle(TextStyle& textStyle, std::optional<Dimension>& fontSize,
204 std::optional<FontWeight>& fontWeight, std::optional<std::vector<std::string>>& fontFamily,
205 std::optional<FontStyle>& fontStyle)
206 {
207 if (fontSize.has_value()) {
208 textStyle.SetFontSize(fontSize.value());
209 }
210 if (fontWeight.has_value()) {
211 textStyle.SetFontWeight(fontWeight.value());
212 }
213 if (fontFamily.has_value()) {
214 textStyle.SetFontFamilies(fontFamily.value());
215 }
216 if (fontStyle.has_value()) {
217 textStyle.SetFontStyle(fontStyle.value());
218 }
219 }
220 } // namespace OHOS::Ace::Framework
221