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 "core/components/picker/picker_theme.h"
17 
18 //In landscape mode, if the height is below 640, no aging adaptation will be performed
19 constexpr double DEFAULT_LIMIT_WIDTH = 640.0;
20 
21 namespace OHOS::Ace {
Parse(const RefPtr<ThemeStyle> & style,const RefPtr<PickerTheme> & theme) const22 void PickerTheme::Builder::Parse(const RefPtr<ThemeStyle>& style, const RefPtr<PickerTheme>& theme) const
23 {
24     if (!style || !theme) {
25         return;
26     }
27     auto pattern = style->GetAttr<RefPtr<ThemeStyle>>("picker_pattern", nullptr);
28     if (!pattern) {
29         LOGE("Pattern of picker is null, please check!");
30         return;
31     }
32 
33     theme->popupDecoration_ = AceType::MakeRefPtr<Decoration>();
34     if (!theme->popupDecoration_) {
35         return;
36     }
37     theme->popupDecoration_->SetBackgroundColor(pattern->GetAttr<Color>("popup_bg_color", Color()));
38     theme->popupDecoration_->SetBorderRadius(Radius(pattern->GetAttr<Dimension>("picker_popup_radius", 0.0_vp)));
39     theme->popupEdge_.SetLeft(pattern->GetAttr<Dimension>("picker_popup_padding", 0.0_vp));
40     theme->popupEdge_.SetTop(pattern->GetAttr<Dimension>("picker_popup_padding", 0.0_vp));
41     theme->popupEdge_.SetRight(pattern->GetAttr<Dimension>("picker_popup_padding", 0.0_vp));
42     theme->popupEdge_.SetBottom(pattern->GetAttr<Dimension>("picker_popup_padding_bottom", 0.0_vp));
43     auto showOptionCount = static_cast<int32_t>(pattern->GetAttr<double>("picker_show_option_count", 0.0));
44     theme->showOptionCount_ = showOptionCount < 0 ? theme->showOptionCount_ : static_cast<uint32_t>(showOptionCount);
45     theme->showButtons_ = static_cast<bool>(pattern->GetAttr<double>("picker_show_buttons", 0.0));
46     theme->focusColor_ = pattern->GetAttr<Color>("picker_focus_color", Color());
47     theme->focusRadius_ = Radius(pattern->GetAttr<Dimension>("picker_focus_radius", 0.0_vp));
48     theme->selectedOptionSize_ = Size(pattern->GetAttr<double>("picker_option_width", 0.0),
49         pattern->GetAttr<double>("picker_select_option_height", 0.0));
50     theme->normalOptionSize_ = Size(pattern->GetAttr<double>("picker_option_width", 0.0),
51         pattern->GetAttr<double>("picker_normal_option_height", 0.0));
52     theme->optionPadding_ = pattern->GetAttr<double>("picker_option_padding", 0.0);
53     theme->jumpInterval_ = pattern->GetAttr<Dimension>("picker_jump_interval", 0.0_vp);
54     theme->columnIntervalMargin_ = pattern->GetAttr<Dimension>("picker_column_margin", 0.0_vp);
55     theme->selectedOptionDecoration_ = AceType::MakeRefPtr<Decoration>();
56     if (!theme->selectedOptionDecoration_) {
57         return;
58     }
59     theme->selectedOptionDecoration_->SetBackgroundColor(
60         pattern->GetAttr<Color>("picker_select_option_back_color", Color()));
61     theme->selectedOptionDecoration_->SetBorderRadius(
62         Radius(pattern->GetAttr<Dimension>("picker_select_option_radius", 0.0_vp)));
63     theme->focusOptionDecoration_ = AceType::MakeRefPtr<Decoration>();
64     if (!theme->focusOptionDecoration_) {
65         return;
66     }
67     theme->focusOptionDecoration_->SetBackgroundColor(
68         pattern->GetAttr<Color>("picker_focus_option_back_color", Color()));
69     theme->focusOptionDecoration_->SetBorderRadius(
70         Radius(pattern->GetAttr<Dimension>("picker_focus_option_radius", 0.0_vp)));
71     theme->buttonWidth_ = pattern->GetAttr<Dimension>("picker_button_width", 0.0_vp);
72     theme->buttonHeight_ = pattern->GetAttr<Dimension>("picker_button_height", 0.0_vp);
73     theme->buttonTopPadding_ = pattern->GetAttr<Dimension>("picker_button_top_padding", 0.0_vp);
74     theme->titleBottomPadding_ = pattern->GetAttr<Dimension>("picker_title_bottom_padding", 0.0_vp);
75     theme->popupOutDecoration_ = AceType::MakeRefPtr<Decoration>();
76     if (!theme->popupOutDecoration_) {
77         return;
78     }
79     theme->popupOutDecoration_->SetBackgroundColor(pattern->GetAttr<Color>("picker_dialog_mask_color", Color()));
80     auto timeSplitter = static_cast<int32_t>(pattern->GetAttr<double>("picker_time_splitter", 0.0));
81     theme->timeSplitter_ = timeSplitter < 0 ? theme->timeSplitter_ : static_cast<uint32_t>(timeSplitter);
82     theme->dividerSpacing_ = pattern->GetAttr<Dimension>("picker_select_divider_spacing", 0.0_vp);
83     theme->dividerColor_ = pattern->GetAttr<Color>("picker_select_divider_color", Color());
84     theme->gradientHeight_ = pattern->GetAttr<Dimension>("picker_select_gradient_height", 0.0_vp);
85     theme->columnFixedWidth_ = pattern->GetAttr<Dimension>("picker_column_fixed_width", 0.0_vp);
86     theme->pressColor_ = pattern->GetAttr<Color>("picker_press_color", Color());
87     theme->hoverColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_HOVERED, theme->hoverColor_);
88     theme->pressColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_PRESSED, theme->pressColor_);
89     theme->paddingHorizontal_ = pattern->GetAttr<Dimension>("padding_horizontal", 24.0_vp);
90     theme->popupDecoration_->SetBackgroundColor(
91         pattern->GetAttr<Color>("popup_bg_color", theme->popupDecoration_->GetBackgroundColor()));
92     theme->focusColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_FOCUSED, theme->focusColor_);
93     theme->selectedOptionStyle_.SetTextColor(
94         pattern->GetAttr<Color>("selected_text_color", theme->selectedOptionStyle_.GetTextColor()));
95     theme->focusOptionStyle_.SetTextColor(theme->selectedOptionStyle_.GetTextColor());
96     theme->normalOptionStyle_.SetTextColor(
97         pattern->GetAttr<Color>(PATTERN_TEXT_COLOR, theme->normalOptionStyle_.GetTextColor()));
98     theme->disappearOptionStyle_.SetTextColor(theme->normalOptionStyle_.GetTextColor());
99     theme->titleStyle_.SetTextColor(theme->normalOptionStyle_.GetTextColor());
100     theme->dividerColor_ = pattern->GetAttr<Color>("divider_color", theme->dividerColor_);
101     theme->dividerThickness_ = pattern->GetAttr<Dimension>("divider_thickness", 2.0_px);
102     theme->contentMarginVertical_ = pattern->GetAttr<Dimension>("content_margin_vertical", 8.0_vp);
103     theme->lunarswitchTextSize_ = pattern->GetAttr<Dimension>("lunarswitch_text_size", theme->lunarswitchTextSize_);
104     theme->lunarswitchTextColor_ = pattern->GetAttr<Color>("lunarswitch_text_color", theme->lunarswitchTextColor_);
105     auto showCountLandscape = static_cast<int32_t>(pattern->GetAttr<double>("picker_show_count_landscape", 3));
106     theme->showCountLandscape_ =
107         showCountLandscape < 0 ? theme->showCountLandscape_ : static_cast<uint32_t>(showCountLandscape);
108     auto showCountPortrait = static_cast<int32_t>(pattern->GetAttr<double>("picker_show_count_portrait", 5));
109     theme->showCountPortrait_ =
110         showCountPortrait < 0 ? theme->showCountPortrait_ : static_cast<uint32_t>(showCountPortrait);
111     theme->deviceHeightLimit_ = pattern->GetAttr<double>("picker_device_height_limit", DEFAULT_LIMIT_WIDTH);
112     theme->gradientHeightLimit_ = pattern->GetAttr<Dimension>("picker_gradient_limit", 54.0_vp);
113     theme->dividerSpacingHeightLimit_ = pattern->GetAttr<Dimension>("picker_divider_spacing_limit", 64.0_vp);
114     theme->pickerDialogFontPadding_ = pattern->GetAttr<Dimension>("picker_divider_spacing_padding", 8.0_vp);
115     theme->selectedTextStyle_ = pattern->GetAttr<Dimension>("picker_select_Font_limit", 40.0_vp);
116     theme->normalTextStyle_ = pattern->GetAttr<Dimension>("picker_normal_Font_limit", 32.0_vp);
117     theme->disappearTextStyle_ = pattern->GetAttr<Dimension>("picker_disappear_Font_limit", 28.0_vp);
118     theme->pickerDialogNormalFontScale_ = pattern->GetAttr<double>("picker_dialog_normal", 1.0f);
119     theme->pickerDialogMaxOneFontScale_ = pattern->GetAttr<double>("picker_dialog_maxone", 1.75f);
120     theme->pickerDialogMaxTwoFontScale_ = pattern->GetAttr<double>("picker_dialog_maxtwo", 2.0f);
121     theme->pickerDialogMaxThirdFontScale_ = pattern->GetAttr<double>("picker_dialog_maxthird", 3.2f);
122     theme->titleFontScaleLimit_ = pattern->GetAttr<double>("picker_dialog_max", 1.45f);
123 }
124 } // namespace OHOS::Ace