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