1 /*
2 * Copyright (c) 2021-2022 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/theme/app_theme.h"
17
18 #include "core/common/container.h"
19 #include "core/common/resource/resource_manager.h"
20 namespace OHOS::Ace {
21 namespace {
22 constexpr uint64_t FOCUS_COLOR = 125831021;
23 }
24
Build(const RefPtr<ThemeConstants> & themeConstants) const25 RefPtr<AppTheme> AppTheme::Builder::Build(const RefPtr<ThemeConstants>& themeConstants) const
26 {
27 RefPtr<AppTheme> theme = AceType::Claim(new AppTheme());
28 if (!themeConstants) {
29 return theme;
30 }
31 auto themeStyle = themeConstants->GetThemeStyle();
32 if (!themeStyle) {
33 return theme;
34 }
35
36 theme->backgroundColor_ = themeStyle->GetAttr<Color>(THEME_ATTR_BG_COLOR, Color::WHITE);
37
38 auto color = themeStyle->GetAttr<Color>("focus_color", Color());
39 if (color != Color(0xff000000)) {
40 theme->focusColor_ = color;
41 } else {
42 if (SystemProperties::GetResourceDecoupling()) {
43 auto resAdapter = ResourceManager::GetInstance().GetResourceAdapter(Container::CurrentIdSafely());
44 theme->focusColor_ = resAdapter->GetColor(FOCUS_COLOR);
45 }
46 }
47
48 auto hoverColor = themeStyle->GetAttr<Color>(THEME_ATTR_HOVER_COLOR, Color::FromRGBO(0, 0, 0, 0.05));
49 if (hoverColor != Color(0xff000000)) {
50 theme->hoverHighlightEnd_ = hoverColor;
51 }
52 RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_APP);
53 if (!pattern) {
54 LOGW("find pattern of app_theme fail");
55 return theme;
56 }
57 theme->focusWidthVp_ = pattern->GetAttr<Dimension>("app_theme_focus_width", 2.0_vp);
58 theme->focusOutPaddingVp_ = pattern->GetAttr<Dimension>("app_theme_focus_padding", 2.0_vp);
59 theme->focusBoxGlow_ = static_cast<bool>(pattern->GetAttr<double>("app_theme_focus_box_glow", 0.0));
60 theme->focusActiveByTab_ =
61 static_cast<bool>(pattern->GetAttr<double>("app_theme_focus_navigation_active_by_tab_key", 1.0));
62 theme->focusHandleClick_ =
63 static_cast<bool>(pattern->GetAttr<double>("app_theme_focus_navigation_always_handle_click", 1.0));
64 theme->pageTransitionAmplitudeRatio_ =
65 themeStyle->GetAttr<double>("page_transition_amplitude_ratio", DEFAULT_AMPLITUDE_RATIO);
66 theme->dragPanDistanceMouse_ =
67 pattern->GetAttr<Dimension>("app_theme_drag_pan_distance_mouse", 1.0_vp);
68 return theme;
69 }
70 } // namespace OHOS::Ace
71