1 /*
2 * Copyright (c) 2021-2022 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 "core/components/flex/flex_component_v2.h"
17
18 #include "core/components/flex/flex_item_component.h"
19 #include "core/components/scroll/scroll_component.h"
20 #include "core/pipeline/base/composed_component.h"
21 #include "frameworks/core/components/foreach/for_each_component.h"
22 #include "frameworks/core/components/ifelse/if_else_component.h"
23
24 namespace OHOS::Ace {
25
26 // If flexShrink is not defined, use 1.0 as default in Flex
ChangeShrinkValueInFlexComponent(const RefPtr<Component> & component)27 void FlexComponentV2::ChangeShrinkValueInFlexComponent(const RefPtr<Component>& component)
28 {
29 auto flexItem = AceType::DynamicCast<FlexItemComponent>(component);
30 if (!flexItem) {
31 return;
32 }
33 if (flexItem->GetFlexShrink() < 0) {
34 flexItem->SetFlexShrink(1.0);
35 }
36 }
37
AddFlexItemComponent(const RefPtr<Component> & component)38 RefPtr<Component> FlexComponentV2::AddFlexItemComponent(const RefPtr<Component>& component)
39 {
40 if (AceType::InstanceOf<FlexItemComponent>(component)) {
41 ChangeShrinkValueInFlexComponent(component);
42 return component;
43 }
44 if (!component) {
45 return component;
46 }
47
48 auto composedComponent = AceType::DynamicCast<ComposedComponent>(component);
49 if (composedComponent) {
50 auto composedChild = composedComponent->GetChild();
51 if (AceType::InstanceOf<FlexItemComponent>(composedChild)) {
52 ChangeShrinkValueInFlexComponent(composedChild);
53 return component;
54 }
55
56 if (AceType::InstanceOf<ComposedComponent>(composedChild)) {
57 auto newComposedChild = AddFlexItemComponent(composedChild);
58 composedComponent->SetChild(newComposedChild);
59 return composedComponent;
60 }
61
62 composedComponent->SetChild(nullptr);
63 auto newFlexItem = AceType::MakeRefPtr<FlexItemComponent>(0.0, 1.0, 0.0);
64 newFlexItem->SetChild(composedChild);
65 composedComponent->SetChild(newFlexItem);
66 if (composedChild && !composedChild->IsTailComponent()) {
67 // extend already marked component group with flex item component
68 Component::ExtendRSNode(newFlexItem, composedChild);
69 }
70 return component;
71 }
72
73 auto multiComposedComponent = AceType::DynamicCast<MultiComposedComponent>(component);
74 if (!multiComposedComponent) {
75 auto newFlexItem = AceType::MakeRefPtr<FlexItemComponent>(0.0, 1.0, 0.0);
76 newFlexItem->SetChild(component);
77 if (component && !component->IsTailComponent()) {
78 // extend already marked component group with flex item component
79 Component::ExtendRSNode(newFlexItem, component);
80 }
81 return newFlexItem;
82 }
83
84 auto children = multiComposedComponent->GetChildren();
85 multiComposedComponent->RemoveChildren();
86 for (const auto& childComponent : children) {
87 multiComposedComponent->AddChild(AddFlexItemComponent(childComponent));
88 }
89
90 return component;
91 }
92
OnChildAppended(const RefPtr<Component> & child)93 void FlexComponentV2::OnChildAppended(const RefPtr<Component>& child)
94 {
95 RefPtr<Component> newChild = AddFlexItemComponent(child);
96 if (newChild != child) {
97 RemoveChildDirectly(child);
98 AppendChildDirectly(newChild);
99 }
100 }
101
102 } // namespace OHOS::Ace
103