• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_PICKER_TEXT_PICKER_LAYOUT_PROPERTY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_PICKER_TEXT_PICKER_LAYOUT_PROPERTY_H
18 
19 #include "base/geometry/dimension.h"
20 #include "core/components/common/layout/constants.h"
21 #include "core/components_ng/layout/layout_property.h"
22 #include "core/components_ng/pattern/linear_layout/linear_layout_property.h"
23 #include "core/components_ng/pattern/text/text_styles.h"
24 #include "core/components_ng/property/property.h"
25 
26 namespace OHOS::Ace::NG {
27 class ACE_EXPORT TextPickerLayoutProperty : public LinearLayoutProperty {
28     DECLARE_ACE_TYPE(TextPickerLayoutProperty, LinearLayoutProperty);
29 
30 public:
TextPickerLayoutProperty()31     TextPickerLayoutProperty() : LinearLayoutProperty(false) {};
32     ~TextPickerLayoutProperty() override = default;
33 
Clone()34     RefPtr<LayoutProperty> Clone() const override
35     {
36         auto value = MakeRefPtr<TextPickerLayoutProperty>();
37         value->LayoutProperty::UpdateLayoutProperty(DynamicCast<LayoutProperty>(this));
38         value->propDefaultPickerItemHeight_ = CloneDefaultPickerItemHeight();
39         value->propSelected_ = CloneSelected();
40         value->propValue_ = CloneValue();
41         value->propSelecteds_ = CloneSelecteds();
42         value->propValues_ = CloneValues();
43         value->propSelectedIndex_ = CloneSelectedIndex();
44         value->propDisappearTextStyle_ = CloneDisappearTextStyle();
45         value->propTextStyle_ = CloneTextStyle();
46         value->propSelectedTextStyle_ = CloneSelectedTextStyle();
47         value->propCanLoop_ = CloneCanLoop();
48         return value;
49     }
50 
Reset()51     void Reset() override
52     {
53         LinearLayoutProperty::Reset();
54         ResetDefaultPickerItemHeight();
55         ResetSelected();
56         ResetValue();
57         ResetSelecteds();
58         ResetValues();
59         ResetSelectedIndex();
60         ResetDisappearTextStyle();
61         ResetTextStyle();
62         ResetSelectedTextStyle();
63         ResetCanLoop();
64     }
65 
ToJsonValue(std::unique_ptr<JsonValue> & json)66     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override
67     {
68         LayoutProperty::ToJsonValue(json);
69 
70         json->Put("defaultPickerItemHeight", GetDefaultPickerItemHeightValue(Dimension(0)).ToString().c_str());
71         json->Put("selected", std::to_string(GetSelectedValue(0)).c_str());
72         json->Put("value", GetValueValue("").c_str());
73 
74         auto jsonArraySelected = JsonUtil::CreateArray(true);
75         auto arraySelected = CloneSelecteds().value_or(std::vector<uint32_t>());
76         for (uint32_t i = 0; i < arraySelected.size(); i++) {
77             auto index = std::to_string(i);
78             jsonArraySelected->Put(index.c_str(), std::to_string(arraySelected[i]).c_str());
79         }
80         json->Put("selecteds", jsonArraySelected);
81 
82         auto jsonArraySelectedIndex = JsonUtil::CreateArray(true);
83         auto arraySelectedIndex = CloneSelecteds().value_or(std::vector<uint32_t>());
84         for (uint32_t i = 0; i < arraySelectedIndex.size(); i++) {
85             auto index = std::to_string(i);
86             jsonArraySelectedIndex->Put(index.c_str(), std::to_string(arraySelectedIndex[i]).c_str());
87         }
88         json->Put("selectedIndex", jsonArraySelectedIndex);
89 
90         auto jsonArrayValue = JsonUtil::CreateArray(true);
91         auto arrayValue = CloneValues().value_or(std::vector<std::string>());
92         for (uint32_t i = 0; i < arrayValue.size(); i++) {
93             auto index = std::to_string(i);
94             jsonArrayValue->Put(index.c_str(), arrayValue[i].c_str());
95         }
96         json->Put("values", jsonArrayValue);
97 
98         auto disappearFont = JsonUtil::Create(true);
99         disappearFont->Put("size", GetDisappearFontSizeValue(Dimension(0)).ToString().c_str());
100         disappearFont->Put("weight", V2::ConvertWrapFontWeightToStirng(
101             GetDisappearWeight().value_or(FontWeight::NORMAL)).c_str());
102         auto disappearTextStyle = JsonUtil::Create(true);
103         disappearTextStyle->Put("color", GetDisappearColor().value_or(Color::BLACK).ColorToString().c_str());
104         disappearTextStyle->Put("font", disappearFont);
105         json->Put("disappearTextStyle", disappearTextStyle);
106 
107         auto normalFont = JsonUtil::Create(true);
108         normalFont->Put("size", GetFontSizeValue(Dimension(0)).ToString().c_str());
109         normalFont->Put("weight", V2::ConvertWrapFontWeightToStirng(GetWeight().value_or(FontWeight::NORMAL)).c_str());
110         auto normalTextStyle = JsonUtil::Create(true);
111         normalTextStyle->Put("color", GetColor().value_or(Color::BLACK).ColorToString().c_str());
112         normalTextStyle->Put("font", normalFont);
113         json->Put("textStyle", normalTextStyle);
114 
115         auto selectedFont = JsonUtil::Create(true);
116         selectedFont->Put("size", GetSelectedFontSizeValue(Dimension(0)).ToString().c_str());
117         selectedFont->Put("weight", V2::ConvertWrapFontWeightToStirng(
118             GetSelectedWeight().value_or(FontWeight::NORMAL)).c_str());
119         auto selectedTextStyle = JsonUtil::Create(true);
120         selectedTextStyle->Put("color", GetSelectedColor().value_or(Color::BLACK).ColorToString().c_str());
121         selectedTextStyle->Put("font", selectedFont);
122         json->Put("selectedTextStyle", selectedTextStyle);
123         auto canLoop = GetCanLoopValue();
124         json->Put("canLoop", canLoop ? "true" : "false");
125     }
126 
127     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(DefaultPickerItemHeight, Dimension, PROPERTY_UPDATE_MEASURE);
128     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(CanLoop, bool, PROPERTY_UPDATE_MEASURE);
129     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(Selected, uint32_t, PROPERTY_UPDATE_MEASURE);
130     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(Value, std::string, PROPERTY_UPDATE_MEASURE);
131     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(Selecteds, std::vector<uint32_t>, PROPERTY_UPDATE_MEASURE);
132     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(Values, std::vector<std::string>, PROPERTY_UPDATE_MEASURE);
133     ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(SelectedIndex, std::vector<uint32_t>, PROPERTY_UPDATE_MEASURE);
134     ACE_DEFINE_PROPERTY_GROUP(DisappearTextStyle, FontStyle);
135     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(
136         DisappearTextStyle, FontSize, DisappearFontSize, Dimension, PROPERTY_UPDATE_MEASURE);
137     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(
138         DisappearTextStyle, TextColor, DisappearColor, Color, PROPERTY_UPDATE_MEASURE_SELF);
139     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(
140         DisappearTextStyle, FontWeight, DisappearWeight, FontWeight, PROPERTY_UPDATE_MEASURE);
141     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(
142         DisappearTextStyle, FontFamily, DisappearFontFamily, std::vector<std::string>, PROPERTY_UPDATE_MEASURE);
143     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(
144         DisappearTextStyle, ItalicFontStyle, DisappearFontStyle, Ace::FontStyle, PROPERTY_UPDATE_MEASURE);
145 
146     ACE_DEFINE_PROPERTY_GROUP(TextStyle, FontStyle);
147     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(TextStyle, FontSize, FontSize, Dimension, PROPERTY_UPDATE_MEASURE);
148     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(TextStyle, TextColor, Color, Color, PROPERTY_UPDATE_MEASURE_SELF);
149     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(TextStyle, FontWeight, Weight, FontWeight, PROPERTY_UPDATE_MEASURE);
150     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(
151         TextStyle, FontFamily, FontFamily, std::vector<std::string>, PROPERTY_UPDATE_MEASURE);
152     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(
153         TextStyle, ItalicFontStyle, FontStyle, Ace::FontStyle, PROPERTY_UPDATE_MEASURE);
154 
155     ACE_DEFINE_PROPERTY_GROUP(SelectedTextStyle, FontStyle);
156     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(
157         SelectedTextStyle, FontSize, SelectedFontSize, Dimension, PROPERTY_UPDATE_MEASURE);
158     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(
159         SelectedTextStyle, TextColor, SelectedColor, Color, PROPERTY_UPDATE_MEASURE_SELF);
160     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(
161         SelectedTextStyle, FontWeight, SelectedWeight, FontWeight, PROPERTY_UPDATE_MEASURE);
162     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(
163         SelectedTextStyle, FontFamily, SelectedFontFamily, std::vector<std::string>, PROPERTY_UPDATE_MEASURE);
164     ACE_DEFINE_PROPERTY_ITEM_WITH_GROUP_ITEM(
165         SelectedTextStyle, ItalicFontStyle, SelectedFontStyle, Ace::FontStyle, PROPERTY_UPDATE_MEASURE);
166 private:
167     ACE_DISALLOW_COPY_AND_MOVE(TextPickerLayoutProperty);
168 };
169 } // namespace OHOS::Ace::NG
170 
171 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_PICKER_TEXT_PICKER_LAYOUT_PROPERTY_H
172