• 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 "horizon_progress_component.h"
17 
18 namespace OHOS {
19 namespace ACELite {
HorizonProgressComponent(jerry_value_t options,jerry_value_t children,AppStyleManager * styleManager)20 HorizonProgressComponent::HorizonProgressComponent(jerry_value_t options,
21                                                    jerry_value_t children,
22                                                    AppStyleManager *styleManager)
23     : Component(options, children, styleManager), hStrokeWidth_(0)
24 {
25 }
26 
SetPrivateAttribute(uint16_t attrKeyId,jerry_value_t attrValue)27 bool HorizonProgressComponent::SetPrivateAttribute(uint16_t attrKeyId, jerry_value_t attrValue)
28 {
29     if (attrKeyId == K_PERCENT) {
30         int16_t rangeMax = 100;
31         int16_t rangeMin = 0;
32         progressView_.SetRange(rangeMax, rangeMin);
33         progressView_.SetValue(IntegerOf(attrValue));
34         return true;
35     }
36 
37     return false;
38 }
39 
ApplyPrivateStyle(const AppStyleItem * style)40 bool HorizonProgressComponent::ApplyPrivateStyle(const AppStyleItem *style)
41 {
42     return SetHorizonProgressStyle(style);
43 }
44 
CreateNativeViews()45 bool HorizonProgressComponent::CreateNativeViews()
46 {
47     // set default style for progress & background
48     Style progressStyle = StyleDefault::GetBrightColorStyle();
49     Style backStyle = StyleDefault::GetBrightStyle();
50     progressView_.SetForegroundStyle(progressStyle);
51     progressView_.SetBackgroundStyle(backStyle);
52 
53     // set default progress self border width
54     progressView_.SetForegroundStyle(STYLE_BORDER_WIDTH, 0);
55     progressView_.SetBackgroundStyle(STYLE_BORDER_WIDTH, 0);
56     const double alpha = 0.15;
57     progressView_.SetBackgroundStyle(
58         STYLE_BACKGROUND_COLOR, Color::GetColorFromRGBA(0xFF, 0xFF, 0xFF, static_cast<uint8_t>(alpha * 0xFF)).full);
59     // set defaut progress stroke width & canvas width & canvas height & border width
60     const int16_t width = 4;
61     hStrokeWidth_ = width;
62     return true;
63 }
64 
GetComponentRootView() const65 UIView *HorizonProgressComponent::GetComponentRootView() const
66 {
67     return const_cast<UIBoxProgress *>(&progressView_);
68 }
69 
SetHorizonProgressStyle(const AppStyleItem * style)70 bool HorizonProgressComponent::SetHorizonProgressStyle(const AppStyleItem *style)
71 {
72     uint16_t stylePropNameId = GetStylePropNameId(style);
73 
74     switch (stylePropNameId) {
75         // Get stroke width.
76         case K_STROKE_WIDTH: {
77             hStrokeWidth_ = GetStylePixelValue(style);
78             break;
79         }
80         // Set horizon progress style: color.
81         case K_COLOR: {
82             uint32_t color = 0;
83             uint8_t alpha = OPA_OPAQUE;
84             if (!GetStyleColorValue(style, color, alpha)) {
85                 return false;
86             }
87             progressView_.SetForegroundStyle(STYLE_BACKGROUND_COLOR, GetRGBColor(color).full);
88             progressView_.SetForegroundStyle(STYLE_BACKGROUND_OPA, alpha);
89             break;
90         }
91         case K_BACKGROUND_COLOR: {
92             uint32_t color = 0;
93             uint8_t alpha = OPA_OPAQUE;
94             if (!GetStyleColorValue(style, color, alpha)) {
95                 return false;
96             }
97             progressView_.SetBackgroundStyle(STYLE_BACKGROUND_COLOR, GetRGBColor(color).full);
98             progressView_.SetBackgroundStyle(STYLE_BACKGROUND_OPA, alpha);
99             break;
100         }
101         default: {
102             return false;
103         }
104     }
105     return true;
106 }
107 
OnViewAttached()108 void HorizonProgressComponent::OnViewAttached()
109 {
110     HandleExtraUpdate();
111 }
112 
PostUpdate(uint16_t attrKeyId)113 void HorizonProgressComponent::PostUpdate(uint16_t attrKeyId)
114 {
115     UNUSED(attrKeyId);
116     HandleExtraUpdate();
117 }
118 
HandleExtraUpdate()119 void HorizonProgressComponent::HandleExtraUpdate()
120 {
121     // set width & height of progress
122     if (progressView_.GetWidth() < 0) {
123         progressView_.SetValidWidth(0);
124     } else {
125         progressView_.SetValidWidth(progressView_.GetWidth());
126     }
127     progressView_.SetValidHeight(hStrokeWidth_);
128 }
129 } // namespace ACELite
130 } // namespace OHOS
131