1 /*
2 * Copyright (c) 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 "grid_row_model_ng.h"
17
18 #include "grid_row_layout_pattern.h"
19
20 #include "base/memory/referenced.h"
21 #include "core/components_ng/base/frame_node.h"
22 #include "core/components_ng/base/view_stack_processor.h"
23 #include "core/components_v2/inspector/inspector_constants.h"
24
25 namespace OHOS::Ace::NG {
Create()26 void GridRowModelNG::Create()
27 {
28 auto col = Referenced::MakeRefPtr<V2::GridContainerSize>();
29 auto gutter = Referenced::MakeRefPtr<V2::Gutter>();
30 auto breakpoints = Referenced::MakeRefPtr<V2::BreakPoints>();
31 auto direction = V2::GridRowDirection::Row;
32 Create(col, gutter, breakpoints, direction);
33 }
34
Create(const RefPtr<V2::GridContainerSize> & col,const RefPtr<V2::Gutter> & gutter,const RefPtr<V2::BreakPoints> & breakpoints,V2::GridRowDirection direction)35 void GridRowModelNG::Create(const RefPtr<V2::GridContainerSize>& col, const RefPtr<V2::Gutter>& gutter,
36 const RefPtr<V2::BreakPoints>& breakpoints, V2::GridRowDirection direction)
37 {
38 auto* stack = ViewStackProcessor::GetInstance();
39 auto nodeId = stack->ClaimNodeId();
40 ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", V2::GRID_ROW_ETS_TAG, nodeId);
41 auto frameNode = FrameNode::GetOrCreateFrameNode(
42 V2::GRID_ROW_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<GridRowLayoutPattern>(); });
43 stack->Push(frameNode);
44 ACE_UPDATE_LAYOUT_PROPERTY(GridRowLayoutProperty, Columns, *col);
45 ACE_UPDATE_LAYOUT_PROPERTY(GridRowLayoutProperty, Gutter, *gutter);
46 ACE_UPDATE_LAYOUT_PROPERTY(GridRowLayoutProperty, BreakPoints, *breakpoints);
47 ACE_UPDATE_LAYOUT_PROPERTY(GridRowLayoutProperty, Direction, direction);
48 }
49
CreateFrameNode(int32_t nodeId)50 RefPtr<FrameNode> GridRowModelNG::CreateFrameNode(int32_t nodeId)
51 {
52 auto frameNode = FrameNode::GetOrCreateFrameNode(
53 V2::GRID_ROW_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<GridRowLayoutPattern>(); });
54 return frameNode;
55 }
56
SetOnBreakPointChange(std::function<void (const std::string)> && onChange)57 void GridRowModelNG::SetOnBreakPointChange(std::function<void(const std::string)>&& onChange)
58 {
59 auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
60 CHECK_NULL_VOID(frameNode);
61 auto eventHub = frameNode->GetEventHub<GridRowEventHub>();
62 CHECK_NULL_VOID(eventHub);
63 eventHub->SetOnBreakpointChange(std::move(onChange));
64 }
65
SetAlignItems(FlexAlign alignItem)66 void GridRowModelNG::SetAlignItems(FlexAlign alignItem)
67 {
68 auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
69 CHECK_NULL_VOID(frameNode);
70 auto layoutPriority = frameNode->GetLayoutProperty<GridRowLayoutProperty>();
71 CHECK_NULL_VOID(layoutPriority);
72 layoutPriority->UpdateAlignItems(alignItem);
73 ACE_UPDATE_LAYOUT_PROPERTY(GridRowLayoutProperty, AlignItems, alignItem);
74 }
75
SetAlignItems(FrameNode * frameNode,FlexAlign alignItem)76 void GridRowModelNG::SetAlignItems(FrameNode* frameNode, FlexAlign alignItem)
77 {
78 CHECK_NULL_VOID(frameNode);
79 auto layoutPriority = frameNode->GetLayoutProperty<GridRowLayoutProperty>();
80 CHECK_NULL_VOID(layoutPriority);
81 layoutPriority->UpdateAlignItems(alignItem);
82 ACE_UPDATE_NODE_LAYOUT_PROPERTY(GridRowLayoutProperty, AlignItems, alignItem, frameNode);
83 }
84
SetGutter(FrameNode * frameNode,const RefPtr<V2::Gutter> & gutter)85 void GridRowModelNG::SetGutter(FrameNode* frameNode, const RefPtr<V2::Gutter>& gutter)
86 {
87 ACE_UPDATE_NODE_LAYOUT_PROPERTY(GridRowLayoutProperty, Gutter, *gutter, frameNode);
88 }
89
SetColumns(FrameNode * frameNode,const RefPtr<V2::GridContainerSize> & col)90 void GridRowModelNG::SetColumns(FrameNode* frameNode, const RefPtr<V2::GridContainerSize>& col)
91 {
92 ACE_UPDATE_NODE_LAYOUT_PROPERTY(GridRowLayoutProperty, Columns, *col, frameNode);
93 }
94
SetBreakpoints(FrameNode * frameNode,const RefPtr<V2::BreakPoints> & breakpoints)95 void GridRowModelNG::SetBreakpoints(FrameNode* frameNode, const RefPtr<V2::BreakPoints>& breakpoints)
96 {
97 ACE_UPDATE_NODE_LAYOUT_PROPERTY(GridRowLayoutProperty, BreakPoints, *breakpoints, frameNode);
98 }
99
SetDirection(FrameNode * frameNode,V2::GridRowDirection direction)100 void GridRowModelNG::SetDirection(FrameNode* frameNode, V2::GridRowDirection direction)
101 {
102 ACE_UPDATE_NODE_LAYOUT_PROPERTY(GridRowLayoutProperty, Direction, direction, frameNode);
103 }
104
SetOnBreakPointChange(FrameNode * frameNode,std::function<void (const std::string)> && onBreakPointChange)105 void GridRowModelNG::SetOnBreakPointChange(FrameNode* frameNode,
106 std::function<void(const std::string)>&& onBreakPointChange)
107 {
108 auto eventHub = frameNode->GetEventHub<GridRowEventHub>();
109 CHECK_NULL_VOID(eventHub);
110 eventHub->SetOnBreakpointChange(std::move(onBreakPointChange));
111 }
112 } // namespace OHOS::Ace::NG