1 /*
2  * Copyright (c) 2024 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/cj_frontend/interfaces/cj_ffi/cj_alphabet_indexer_ffi.h"
17 
18 #include "cj_lambda.h"
19 #include "bridge/common/utils/utils.h"
20 #include "core/components_ng/base/view_abstract.h"
21 #include "core/components_ng/base/view_stack_processor.h"
22 #include "core/components_ng/pattern/indexer/indexer_event_hub.h"
23 #include "core/components_ng/pattern/indexer/indexer_theme.h"
24 #include "core/components_ng/pattern/indexer/indexer_model.h"
25 
26 using namespace OHOS::Ace;
27 using namespace OHOS::Ace::Framework;
28 
29 namespace {
30 const std::vector<FontStyle> FONT_STYLES = { FontStyle::NORMAL, FontStyle::ITALIC };
31 const std::vector<V2::AlignStyle> ALIGN_STYLE_ = { V2::AlignStyle::LEFT, V2::AlignStyle::RIGHT };
32 const std::vector<NG::AlignStyle> NG_ALIGN_STYLE_ = { NG::AlignStyle::LEFT, NG::AlignStyle::RIGHT };
33 }; // namespace
34 
35 extern "C" {
FfiOHOSAceFrameworkAlphabetIndexerCreate(VectorStringPtr vecContent,int32_t selected)36 void FfiOHOSAceFrameworkAlphabetIndexerCreate(VectorStringPtr vecContent, int32_t selected)
37 {
38     auto actualVec = reinterpret_cast<std::vector<std::string>*>(vecContent);
39     IndexerModel::GetInstance()->Create(*actualVec, selected);
40 }
FfiOHOSAceFrameworkAlphabetIndexerColor(int32_t color)41 void FfiOHOSAceFrameworkAlphabetIndexerColor(int32_t color)
42 {
43     IndexerModel::GetInstance()->SetColor(Color(color));
44 }
FfiOHOSAceFrameworkAlphabetIndexerSelectedColor(int32_t color)45 void FfiOHOSAceFrameworkAlphabetIndexerSelectedColor(int32_t color)
46 {
47     IndexerModel::GetInstance()->SetSelectedColor(Color(color));
48 }
FfiOHOSAceFrameworkAlphabetIndexerPopupColor(int32_t color)49 void FfiOHOSAceFrameworkAlphabetIndexerPopupColor(int32_t color)
50 {
51     IndexerModel::GetInstance()->SetPopupColor(Color(color));
52 }
FfiOHOSAceFrameworkAlphabetIndexerSelectedBackgroundColor(int32_t color)53 void FfiOHOSAceFrameworkAlphabetIndexerSelectedBackgroundColor(int32_t color)
54 {
55     IndexerModel::GetInstance()->SetSelectedBackgroundColor(Color(color));
56 }
FfiOHOSAceFrameworkAlphabetIndexerPopupBackground(int32_t color)57 void FfiOHOSAceFrameworkAlphabetIndexerPopupBackground(int32_t color)
58 {
59     IndexerModel::GetInstance()->SetPopupBackground(Color(color));
60 }
FfiOHOSAceFrameworkAlphabetIndexerUsingPopup(bool usingPop)61 void FfiOHOSAceFrameworkAlphabetIndexerUsingPopup(bool usingPop)
62 {
63     IndexerModel::GetInstance()->SetUsingPopup(usingPop);
64 }
FfiOHOSAceFrameworkAlphabetIndexerSelectedFont(double size,int32_t unit,const char * weight,const char * family,int32_t style)65 void FfiOHOSAceFrameworkAlphabetIndexerSelectedFont(
66     double size, int32_t unit, const char* weight, const char* family, int32_t style)
67 {
68     std::optional<Dimension> fontSize = Dimension(size, DimensionUnit(unit));
69     std::optional<FontWeight> fontWeight = FontWeight::REGULAR;
70     std::optional<std::vector<std::string>> fontFamily = std::vector<std::string>{family};
71     std::optional<FontStyle> fontStyle = static_cast<FontStyle>(style);
72     IndexerModel::GetInstance()->SetSelectedFont(fontSize, fontWeight, fontFamily, fontStyle);
73 }
FfiOHOSAceFrameworkAlphabetIndexerPopupFont(double size,int32_t unit,const char * weight,const char * family,int32_t style)74 void FfiOHOSAceFrameworkAlphabetIndexerPopupFont(
75     double size, int32_t unit, const char* weight, const char* family, int32_t style)
76 {
77     std::optional<Dimension> fontSize = Dimension(size, DimensionUnit(unit));
78     std::optional<FontWeight> fontWeight = FontWeight::REGULAR;
79     std::optional<std::vector<std::string>> fontFamily = std::vector<std::string>{family};
80     std::optional<FontStyle> fontStyle = static_cast<FontStyle>(style);
81     IndexerModel::GetInstance()->SetPopupFont(fontSize, fontWeight, fontFamily, fontStyle);
82 }
FfiOHOSAceFrameworkAlphabetIndexerFont(double size,int32_t unit,const char * weight,const char * family,int32_t style)83 void FfiOHOSAceFrameworkAlphabetIndexerFont(double size, int32_t unit, const char* weight,
84                                             const char* family, int32_t style)
85 {
86     std::optional<Dimension> fontSize = Dimension(size, DimensionUnit(unit));
87     std::optional<FontWeight> fontWeight = FontWeight::REGULAR;
88     std::optional<std::vector<std::string>> fontFamily = std::vector<std::string>{family};
89     std::optional<FontStyle> fontStyle = static_cast<FontStyle>(style);
90     IndexerModel::GetInstance()->SetFont(fontSize, fontWeight, fontFamily, fontStyle);
91 }
FfiOHOSAceFrameworkAlphabetIndexerItemSize(double size,int32_t unit)92 void FfiOHOSAceFrameworkAlphabetIndexerItemSize(double size, int32_t unit)
93 {
94     IndexerModel::GetInstance()->SetItemSize(Dimension(size, static_cast<DimensionUnit>(unit)));
95 }
FfiOHOSAceFrameworkAlphabetIndexerAlignStyle(int32_t alignStyle)96 void FfiOHOSAceFrameworkAlphabetIndexerAlignStyle(int32_t alignStyle)
97 {
98     if (alignStyle >= 0 && alignStyle < static_cast<int32_t>(ALIGN_STYLE_.size())) {
99         IndexerModel::GetInstance()->SetAlignStyle(static_cast<int32_t>(NG_ALIGN_STYLE_[alignStyle]));
100     }
101 }
FfiOHOSAceFrameworkAlphabetIndexerSelected(int32_t selected)102 void FfiOHOSAceFrameworkAlphabetIndexerSelected(int32_t selected)
103 {
104     IndexerModel::GetInstance()->SetSelected(selected);
105 }
FfiOHOSAceFrameworkAlphabetIndexerPopupPosition(double x,double y)106 void FfiOHOSAceFrameworkAlphabetIndexerPopupPosition(double x, double y)
107 {
108     IndexerModel::GetInstance()->SetPopupPositionX(Dimension(x));
109     IndexerModel::GetInstance()->SetPopupPositionY(Dimension(y));
110 }
FfiOHOSAceFrameworkAlphabetIndexerOnSelected(void (* callback)(int32_t idx))111 void FfiOHOSAceFrameworkAlphabetIndexerOnSelected(void (*callback)(int32_t idx))
112 {
113     auto onSelected = [ffiCallback = CJLambda::Create(callback)](int64_t idx) { ffiCallback(idx); };
114     IndexerModel::GetInstance()->SetOnSelected(std::move(onSelected));
115 }
FfiOHOSAceFrameworkAlphabetIndexerOnRequestPopupData(VectorStringHandle (* callback)(int32_t idx))116 void FfiOHOSAceFrameworkAlphabetIndexerOnRequestPopupData(VectorStringHandle (*callback)(int32_t idx))
117 {
118     auto onRequestPopupData = [ffiCallback = CJLambda::Create(callback)](int64_t idx) {
119         auto callbackRes = ffiCallback(idx);
120         auto resPtr = reinterpret_cast<std::vector<std::string>*>(callbackRes);
121         auto result = *resPtr;
122         FFICJCommonVectorStringDelete(callbackRes);
123         return result;
124     };
125     IndexerModel::GetInstance()->SetOnRequestPopupData(std::move(onRequestPopupData));
126 }
FfiOHOSAceFrameworkAlphabetIndexerOnPopupSelected(void (* callback)(int32_t idx))127 void FfiOHOSAceFrameworkAlphabetIndexerOnPopupSelected(void (*callback)(int32_t idx))
128 {
129     auto onSelected = [ffiCallback = CJLambda::Create(callback)](int64_t idx) { ffiCallback(idx); };
130     IndexerModel::GetInstance()->SetOnPopupSelected(std::move(onSelected));
131 }
132 }
133