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