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 "frameworks/bridge/common/dom/dom_refresh.h"
17
18 #include "core/components/progress/progress_theme.h"
19 #include "core/components/refresh/refresh_theme.h"
20 #include "frameworks/bridge/common/dom/dom_type.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22
23 namespace OHOS::Ace::Framework {
24
DOMRefresh(NodeId nodeId,const std::string & nodeName)25 DOMRefresh::DOMRefresh(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
26 {
27 refreshChild_ = AceType::MakeRefPtr<RefreshComponent>();
28 columnChild_ = AceType::MakeRefPtr<ColumnComponent>(
29 FlexAlign::FLEX_START, FlexAlign::FLEX_START, std::list<RefPtr<Component>>());
30 }
31
InitializeStyle()32 void DOMRefresh::InitializeStyle()
33 {
34 ResetInitializedStyle();
35 }
36
ResetInitializedStyle()37 void DOMRefresh::ResetInitializedStyle()
38 {
39 RefPtr<RefreshTheme> theme = GetTheme<RefreshTheme>();
40 if (theme) {
41 refreshChild_->SetLoadingDistance(theme->GetLoadingDistance());
42 refreshChild_->SetRefreshDistance(theme->GetRefreshDistance());
43 refreshChild_->SetProgressDistance(theme->GetProgressDistance());
44 refreshChild_->SetProgressDiameter(theme->GetProgressDiameter());
45 refreshChild_->SetMaxDistance(theme->GetMaxDistance());
46 refreshChild_->SetShowTimeDistance(theme->GetShowTimeDistance());
47 refreshChild_->SetTextStyle(theme->GetTextStyle());
48 refreshChild_->SetProgressColor(theme->GetProgressColor());
49 refreshChild_->SetBackgroundColor(theme->GetBackgroundColor());
50 }
51 }
52
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)53 bool DOMRefresh::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
54 {
55 if (attr.first == DOM_REFRESH_OFFSET) {
56 Dimension offset = ParseDimension(attr.second);
57 refreshChild_->SetUseOffset(true);
58 refreshChild_->SetIndicatorOffset(offset);
59 } else if (attr.first == DOM_REFRESH_REFRESHING) {
60 refreshing_ = StringToBool(attr.second);
61 refreshChild_->SetRefreshing(refreshing_);
62 } else if (attr.first == DOM_REFRESH_TYPE) {
63 if (attr.second == "pulldown") {
64 refreshType_ = RefreshType::PULL_DOWN;
65 } else {
66 refreshType_ = RefreshType::AUTO;
67 }
68 refreshChild_->SetRefreshType(refreshType_);
69 } else if (attr.first == DOM_REFRESH_LASTTIME) {
70 showLastTime_ = StringToBool(attr.second);
71 refreshChild_->SetShowLastTime(showLastTime_);
72 } else if (attr.first == DOM_REFRESH_FRICTION) {
73 friction_ = StringToInt(attr.second);
74 refreshChild_->SetFriction(friction_);
75 } else if (attr.first == DOM_REFRESH_TIME_OFFSET) {
76 refreshChild_->SetTimeOffset(ParseDimension(attr.second));
77 } else {
78 return false;
79 }
80 return true;
81 }
82
SetSpecializedStyle(const std::pair<std::string,std::string> & style)83 bool DOMRefresh::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
84 {
85 if (style.first == DOM_REFRESH_PROGRESS_COLOR) {
86 progressColor_.first = ParseColor(style.second);
87 progressColor_.second = true;
88 return true;
89 }
90 if (style.first == DOM_REFRESH_BACKGROUND_COLOR) {
91 backgroundColor_ = ParseColor(style.second);
92 refreshChild_->SetBackgroundColor(backgroundColor_);
93 return true;
94 }
95 return false;
96 }
97
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)98 void DOMRefresh::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
99 {
100 if (columnChild_) {
101 columnChild_->AppendChild(child->GetRootComponent());
102 }
103 }
104
OnChildNodeRemoved(const RefPtr<DOMNode> & child)105 void DOMRefresh::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
106 {
107 if (columnChild_) {
108 columnChild_->RemoveChild(child->GetRootComponent());
109 }
110 }
111
AddSpecializedEvent(int32_t pageId,const std::string & event)112 bool DOMRefresh::AddSpecializedEvent(int32_t pageId, const std::string& event)
113 {
114 if (event == DOM_REFRESH) {
115 refreshEventId_ = EventMarker(GetNodeIdForEvent(), event, pageId);
116 refreshChild_->SetRefreshEventId(refreshEventId_);
117 return true;
118 }
119 if (event == DOM_REFRESH_EVENT_PULL_DOWN) {
120 pullDownEventId_ = EventMarker(GetNodeIdForEvent(), event, pageId);
121 refreshChild_->SetPulldownEventId(pullDownEventId_);
122 return true;
123 }
124 return false;
125 }
126
GetSpecializedComponent()127 RefPtr<Component> DOMRefresh::GetSpecializedComponent()
128 {
129 if (refreshChild_) {
130 refreshChild_->SetChild(columnChild_);
131 if (!progressColor_.second) {
132 auto theme = GetTheme<ProgressTheme>();
133 progressColor_.first = theme->GetProgressColor();
134 }
135 refreshChild_->SetProgressColor(progressColor_.first);
136 }
137 return refreshChild_;
138 }
139
PrepareSpecializedComponent()140 void DOMRefresh::PrepareSpecializedComponent()
141 {
142 refreshChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
143 columnChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
144 }
145
146 } // namespace OHOS::Ace::Framework
147