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/navigation_bar/navigation_bar_component_base.h"
17 
18 #include "core/components/box/box_component.h"
19 #include "core/components/padding/padding_component.h"
20 
21 namespace OHOS::Ace {
22 
BuildIconButton(const RefPtr<NavigationBarTheme> & theme,const Dimension & width,const Dimension & height,const IconImage & icon,const EventMarker & clickMarker)23 RefPtr<ButtonComponent> NavigationBarComponentBase::BuildIconButton(const RefPtr<NavigationBarTheme>& theme,
24     const Dimension& width, const Dimension& height, const IconImage& icon, const EventMarker& clickMarker)
25 {
26     std::list<RefPtr<Component>> buttonChildren;
27     buttonChildren.emplace_back(icon.image);
28     auto button = AceType::MakeRefPtr<ButtonComponent>(buttonChildren);
29     button->SetType(ButtonType::NORMAL);
30     button->SetWidth(width);
31     button->SetHeight(height);
32     button->SetBackgroundColor(theme->GetButtonNormalColor());
33     button->SetClickedColor(theme->GetButtonPressedColor());
34     button->SetFocusColor(theme->GetButtonFocusColor());
35     button->SetHoverColor(theme->GetButtonHoverColor());
36     button->SetClickedEventId(clickMarker);
37     button->SetRectRadius(theme->GetButtonCornerRadius());
38     return button;
39 }
40 
BuildTitleText(const std::string & value,const Color & color,const Dimension & fontSize,const FontWeight & fontWeight)41 RefPtr<TextComponent> NavigationBarComponentBase::BuildTitleText(
42     const std::string& value, const Color& color, const Dimension& fontSize, const FontWeight& fontWeight)
43 {
44     auto text = AceType::MakeRefPtr<TextComponent>(value);
45     TextStyle textStyle;
46     textStyle.SetTextColor(color);
47     textStyle.SetFontSize(fontSize);
48     textStyle.SetAllowScale(false);
49     textStyle.SetFontWeight(fontWeight);
50     textStyle.SetMaxLines(1);
51     textStyle.SetTextOverflow(TextOverflow::ELLIPSIS);
52     text->SetTextStyle(textStyle);
53     return text;
54 }
55 
BuildPadding(double size,bool isVertical)56 RefPtr<PaddingComponent> NavigationBarComponentBase::BuildPadding(double size, bool isVertical)
57 {
58     auto padding = AceType::MakeRefPtr<PaddingComponent>();
59     Edge edge;
60     if (isVertical) {
61         edge.SetBottom(Dimension(size, DimensionUnit::VP));
62     } else {
63         edge.SetRight(Dimension(size, DimensionUnit::VP));
64     }
65     padding->SetPadding(edge);
66     return padding;
67 }
68 
GenerateAccessibilityComposed(int32_t parentId,const std::string & name,const RefPtr<Component> & child)69 RefPtr<ComposedComponent> NavigationBarComponentBase::GenerateAccessibilityComposed(
70     int32_t parentId, const std::string& name, const RefPtr<Component>& child)
71 {
72     const auto& pipelineContext = context_.Upgrade();
73     if (!pipelineContext) {
74         return nullptr;
75     }
76 
77     const auto& accessibilityManager = pipelineContext->GetAccessibilityManager();
78     if (accessibilityManager) {
79         auto composedId = accessibilityManager->GenerateNextAccessibilityId();
80         auto node = accessibilityManager->CreateSpecializedNode(name, composedId, parentId);
81         if (node) {
82             node->SetFocusableState(true);
83         }
84         auto box = AceType::MakeRefPtr<BoxComponent>();
85         box->SetChild(child);
86         auto composed = AceType::MakeRefPtr<ComposedComponent>(std::to_string(composedId), name, box);
87         composed->MarkNeedUpdate();
88         return composed;
89     }
90     return nullptr;
91 }
92 
93 } // namespace OHOS::Ace
94