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_swiper.h"
17
18 #include "base/utils/linear_map.h"
19 #include "base/utils/utils.h"
20 #include "core/components/common/properties/color.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22
23 namespace OHOS::Ace::Framework {
24 namespace {
25
26 constexpr double MAX_OPACITY = 255.0;
27 const std::string DISPLAY_COMPOSED_NAME = "SwiperDisplayChild";
28
GetDisplayComposedId(const RefPtr<DOMNode> & child)29 std::string GetDisplayComposedId(const RefPtr<DOMNode>& child)
30 {
31 return "display" + std::to_string(child->GetNodeId());
32 }
33
34 } // namespace
35
DOMSwiper(NodeId nodeId,const std::string & nodeName)36 DOMSwiper::DOMSwiper(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
37 {
38 swiperChild_ = AceType::MakeRefPtr<SwiperComponent>(std::list<RefPtr<Component>>());
39 }
40
InitializeStyle()41 void DOMSwiper::InitializeStyle()
42 {
43 if (declaration_) {
44 declaration_->InitializeStyle();
45 }
46 }
47
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)48 void DOMSwiper::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
49 {
50 ACE_DCHECK(child);
51 auto display = AceType::MakeRefPtr<DisplayComponent>(child->GetRootComponent());
52 display->SetOpacity(MAX_OPACITY);
53 swiperChild_->InsertChild(
54 slot, AceType::MakeRefPtr<ComposedComponent>(GetDisplayComposedId(child), DISPLAY_COMPOSED_NAME, display));
55 }
56
OnChildNodeRemoved(const RefPtr<DOMNode> & child)57 void DOMSwiper::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
58 {
59 ACE_DCHECK(child);
60 swiperChild_->RemoveChildByComposedId(GetDisplayComposedId(child));
61 }
62
PrepareSpecializedComponent()63 void DOMSwiper::PrepareSpecializedComponent()
64 {
65 swiperChild_->SetShow(GetDisplay() == DisplayType::NO_SETTING || GetDisplay() == DisplayType::FLEX);
66 swiperChild_->SetDeclaration(AceType::DynamicCast<SwiperDeclaration>(declaration_));
67 swiperChild_->SetTextDirection(IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR);
68 for (const auto& item : swiperChild_->GetChildren()) {
69 auto composedDisplay = AceType::DynamicCast<ComposedComponent>(item);
70 if (composedDisplay) {
71 composedDisplay->MarkNeedUpdate();
72 }
73 }
74 }
75
AdjustSpecialParamInLiteMode()76 void DOMSwiper::AdjustSpecialParamInLiteMode()
77 {
78 auto declaration = AceType::DynamicCast<SwiperDeclaration>(declaration_);
79 if (declaration) {
80 declaration->SetShowIndicator(false);
81 }
82 }
83
84 } // namespace OHOS::Ace::Framework
85