• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "core/components/option/option_component.h"
17 
18 #include "base/utils/string_utils.h"
19 #include "base/utils/system_properties.h"
20 #include "core/common/container.h"
21 #include "core/components/box/box_component.h"
22 #include "core/components/common/properties/text_style.h"
23 #include "core/components/flex/flex_item_component.h"
24 #include "core/components/image/image_component.h"
25 #include "core/components/list/list_component.h"
26 #include "core/components/option/option_element.h"
27 #include "core/components/option/render_option.h"
28 #include "core/components/positioned/positioned_component.h"
29 #include "core/components/select/select_theme.h"
30 
31 namespace OHOS::Ace {
32 namespace {
33 
34 constexpr uint32_t SELECT_OPTION_TEXT_LINES = 2;
35 constexpr double MIN_TEXT_SIZE_TV = 14.0;
36 constexpr double MIN_TEXT_SIZE_PHONE = 9.0;
37 
38 } // namespace
39 
OptionComponent(const RefPtr<SelectTheme> & theme)40 OptionComponent::OptionComponent(const RefPtr<SelectTheme>& theme)
41 {
42     theme_ = theme->clone();
43     theme_->SetFontWeight(FontWeight::W400);
44 }
45 
InitTheme(const RefPtr<ThemeManager> & themeManager)46 void OptionComponent::InitTheme(const RefPtr<ThemeManager>& themeManager)
47 {
48     if (!themeManager) {
49         return;
50     }
51     auto selectTheme = themeManager->GetTheme<SelectTheme>();
52     if (!selectTheme) {
53         return;
54     }
55     theme_ = selectTheme->clone();
56     theme_->SetFontWeight(FontWeight::W400);
57 }
58 
Initialize(const RefPtr<AccessibilityManager> & manager)59 bool OptionComponent::Initialize(const RefPtr<AccessibilityManager>& manager)
60 {
61     if (customComponent_) {
62         ClearChildren();
63         AppendChild(customComponent_);
64         return true;
65     }
66 
67     if (!text_ || value_.empty()) {
68         LOGW("can not initialize now, text null or value empty.");
69         return false;
70     }
71 
72     ClearChildren();
73 
74     if (icon_) {
75         icon_->SetImageFit(ImageFit::SCALEDOWN);
76         icon_->SetAlignment((SystemProperties::GetDeviceType() == DeviceType::TV ?
77             Alignment::CENTER : Alignment::CENTER_LEFT));
78         icon_->SetWidth(24.0_vp); // icon is only for phone which is fixes size 24dp*24dp
79         icon_->SetHeight(24.0_vp);
80         AppendChild(icon_);
81     }
82 
83     auto container = Container::Current();
84     if (!container) {
85         return false;
86     }
87     auto context = container->GetPipelineContext();
88     if (!context) {
89         return false;
90     }
91     TextStyle textStyle;
92     if (context->GetIsDeclarative()) {
93         if (GetSelected()) {
94             textStyle = GetSelectedTextStyle();
95             text_->SetTextStyle(textStyle);
96         } else {
97             textStyle = GetTextStyle();
98             text_->SetTextStyle(textStyle);
99         }
100         textStyle.SetTextDecoration(GetTextDecoration());
101         text_->SetData(value_);
102     } else {
103         textStyle = text_->GetTextStyle();
104         std::vector<std::string> fontFamilies;
105         StringUtils::StringSpliter(GetFontFamily(), ',', fontFamilies);
106         if (!fontFamilies.empty()) {
107             textStyle.SetFontFamilies(fontFamilies);
108         }
109         textStyle.SetFontSize(GetFontSize());
110         textStyle.SetFontWeight(GetFontWeight());
111         textStyle.SetTextDecoration(GetTextDecoration());
112         if (GetDisabled() && theme_) {
113             textStyle.SetTextColor(theme_->GetFocusedTextDisableColor());
114         } else if (GetSelected() && theme_) {
115             textStyle.SetTextColor(theme_->GetSelectedColorText());
116         } else {
117             textStyle.SetTextColor(GetFontColor());
118         }
119     }
120     textStyle.SetAllowScale(IsAllowScale());
121     double minFontSize = (SystemProperties::GetDeviceType() == DeviceType::TV ?
122         MIN_TEXT_SIZE_TV : MIN_TEXT_SIZE_PHONE);
123     textStyle.SetAdaptTextSize(textStyle.GetFontSize(), Dimension(minFontSize, DimensionUnit::FP));
124     if (SystemProperties::GetDeviceType() == DeviceType::TV) {
125         textStyle.SetTextAlign(TextAlign::CENTER);
126     } else if (GetTextDirection() == TextDirection::RTL) {
127         textStyle.SetTextAlign(TextAlign::RIGHT);
128     } else {
129         textStyle.SetTextAlign(TextAlign::LEFT);
130     }
131     // use single line, do not change line.
132     textStyle.SetMaxLines(SELECT_OPTION_TEXT_LINES);
133     textStyle.SetTextOverflow(TextOverflow::ELLIPSIS);
134     text_->SetTextStyle(textStyle);
135     text_->SetFocusColor(textStyle.GetTextColor());
136     AppendChild(text_);
137     SetNode((!manager ? nullptr
138                   : manager->CreateAccessibilityNode("option", StringUtils::StringToInt(GetId()), GetParentId(), -1)));
139 #if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM)
140     if (node_) {
141         node_->SetAttr(GetAttr());
142         node_->SetStyle(GetStyle());
143     }
144 #endif
145     return true;
146 }
147 
CheckOptionModify()148 void OptionComponent::CheckOptionModify()
149 {
150     if (!text_) {
151         LOGE("text is null of option component.");
152         return;
153     }
154     if (!modifiedCallback_) {
155         LOGD("modify callback of option component is null.");
156         return;
157     }
158     if (text_->GetData() == lastText_) {
159         return;
160     }
161     modifiedCallback_(GetIndex());
162     lastText_ = text_->GetData();
163 }
164 
CreateRenderNode()165 RefPtr<RenderNode> OptionComponent::CreateRenderNode()
166 {
167     return RenderOption::Create();
168 }
169 
CreateElement()170 RefPtr<Element> OptionComponent::CreateElement()
171 {
172     return AceType::MakeRefPtr<OptionElement>();
173 }
174 
175 } // namespace OHOS::Ace
176