1 /*
2 * Copyright (c) 2020-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 "image_component.h"
17 #include "ace_log.h"
18 #include "ace_mem_base.h"
19 #include "js_app_context.h"
20 #include "js_fwk_common.h"
21 #include "key_parser.h"
22 #include "keys.h"
23
24 namespace OHOS {
25 namespace ACELite {
ImageComponent(jerry_value_t options,jerry_value_t children,AppStyleManager * styleManager)26 ImageComponent::ImageComponent(jerry_value_t options, jerry_value_t children, AppStyleManager *styleManager)
27 : Component(options, children, styleManager),
28 fitOriginalSize_(0),
29 hasSetWidth_(false),
30 hasSetHeight_(false)
31 {
32 SetComponentName(K_IMAGE);
33 }
34
GetSrc()35 const char *ImageComponent::GetSrc()
36 {
37 return imageView_.GetPath();
38 }
39
CreateNativeViews()40 bool ImageComponent::CreateNativeViews()
41 {
42 // set default value
43 imageView_.SetAutoEnable(false);
44 imageView_.SetResizeMode(resizeMode_);
45 const int16_t defaultOpacity = 0;
46 imageView_.SetStyle(STYLE_BACKGROUND_OPA, defaultOpacity);
47 return true;
48 }
49
GetComponentRootView() const50 inline UIView *ImageComponent::GetComponentRootView() const
51 {
52 return const_cast<UIImageView *>(&imageView_);
53 }
54
SetPrivateAttribute(uint16_t attrKeyId,jerry_value_t attrValue)55 bool ImageComponent::SetPrivateAttribute(uint16_t attrKeyId, jerry_value_t attrValue)
56 {
57 bool setResult = true;
58 switch (attrKeyId) {
59 case K_SRC: {
60 char *src = const_cast<char *>(ParseImageSrc(attrValue));
61 imageView_.SetSrc(src);
62 ACE_FREE(src);
63 break;
64 }
65 default:
66 setResult = false;
67 break;
68 }
69
70 return setResult;
71 }
72
UpdateWidgetFitMode()73 void ImageComponent::UpdateWidgetFitMode()
74 {
75 // update the imageView
76 if (fitOriginalSize_ && !(hasSetWidth_ || hasSetHeight_)) {
77 imageView_.SetAutoEnable(true);
78 } else {
79 imageView_.SetAutoEnable(false);
80 }
81 imageView_.SetResizeMode(resizeMode_);
82 }
83
ApplyPrivateStyle(const AppStyleItem * style)84 bool ImageComponent::ApplyPrivateStyle(const AppStyleItem *style)
85 {
86 uint16_t styleKey = style->GetPropNameId();
87 bool setResult = true;
88 switch (styleKey) {
89 case K_OBJECT_FIT: {
90 const char * const strValue = GetStyleStrValue(style);
91 if (strValue == nullptr) {
92 return false;
93 }
94 uint16_t mode = KeyParser::ParseKeyId(strValue, GetStyleStrValueLen(style));
95 UpdateResizeMode(mode);
96 break;
97 }
98 case K_FIT_ORIGINAL_SIZE: {
99 fitOriginalSize_ = style->GetBoolValue();
100 break;
101 }
102 case K_HEIGHT: {
103 int16_t value = GetStyleNumValue(style);
104 imageView_.SetHeight(value);
105 hasSetHeight_ = true;
106 setResult = false;
107 break;
108 }
109 case K_WIDTH: {
110 int16_t value = GetStyleNumValue(style);
111 imageView_.SetWidth(value);
112 hasSetWidth_ = true;
113 setResult = false;
114 break;
115 }
116 default:
117 return false;
118 }
119 UpdateWidgetFitMode();
120 return setResult;
121 }
122
UpdateResizeMode(uint16_t mode)123 void ImageComponent::UpdateResizeMode(uint16_t mode)
124 {
125 switch (mode) {
126 case K_COVER:
127 resizeMode_ = UIImageView::ImageResizeMode::COVER;
128 break;
129 case K_CONTAIN:
130 resizeMode_ = UIImageView::ImageResizeMode::CONTAIN;
131 break;
132 case K_FILL:
133 resizeMode_ = UIImageView::ImageResizeMode::FILL;
134 break;
135 case K_NONE:
136 resizeMode_ = UIImageView::ImageResizeMode::CENTER;
137 break;
138 case K_SCALE_DOWN:
139 resizeMode_ = UIImageView::ImageResizeMode::SCALE_DOWN;
140 break;
141 default:
142 break;
143 }
144 }
145 } // namespace ACELite
146 } // namespace OHOS
147