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/picker/picker_option_component.h"
17 
18 #include "core/components/picker/picker_option_element.h"
19 #include "core/components/picker/render_picker_option.h"
20 
21 namespace OHOS::Ace {
22 namespace {
23 
24 constexpr uint32_t PICKER_OPTION_TEXT_MAX_LINES = 1;
25 
26 } // namespace
27 
PickerOptionComponent()28 PickerOptionComponent::PickerOptionComponent()
29 {
30     textComponent_ = AceType::MakeRefPtr<TextComponent>("");
31     boxComponent_ = AceType::MakeRefPtr<BoxComponent>();
32 }
33 
CreateRenderNode()34 RefPtr<RenderNode> PickerOptionComponent::CreateRenderNode()
35 {
36     return AceType::MakeRefPtr<RenderPickerOption>();
37 }
38 
CreateElement()39 RefPtr<Element> PickerOptionComponent::CreateElement()
40 {
41     auto element = AceType::MakeRefPtr<PickerOptionElement>();
42     element->SetAutoAccessibility(false);
43     return element;
44 }
45 
Initialize()46 void PickerOptionComponent::Initialize()
47 {
48     if (!theme_) {
49         LOGE("Theme is null.");
50         return;
51     }
52     auto style = GetDisappear() ? theme_->GetDisappearOptionStyle() : theme_->GetOptionStyle(GetSelected(), false);
53     auto isRtl = GetTextDirection() == TextDirection::RTL;
54     style.SetMaxLines(PICKER_OPTION_TEXT_MAX_LINES);
55     style.SetTextOverflow(TextOverflow::ELLIPSIS);
56     textComponent_->SetData(text_);
57     textComponent_->SetFocusColor(style.GetTextColor());
58     textComponent_->SetTextStyle(style);
59     textComponent_->SetTextDirection(GetTextDirection());
60 
61     boxComponent_->SetDeliverMinToChild(false);
62     if (GetSelected()) {
63         boxComponent_->SetBackDecoration(theme_->GetOptionDecoration(false));
64     } else {
65         auto back = AceType::MakeRefPtr<Decoration>();
66         boxComponent_->SetBackDecoration(back);
67     }
68     boxComponent_->SetAlignment(Alignment::CENTER);
69     boxComponent_->SetChild(textComponent_);
70     boxComponent_->SetTextDirection(GetTextDirection());
71     if (isRtl) {
72         boxComponent_->SetAlignment(Alignment::CENTER_RIGHT);
73     }
74     boxComponent_->SetEnableDebugBoundary(true);
75 
76     SetChild(boxComponent_);
77 }
78 
79 } // namespace OHOS::Ace