1 /*
2  * Copyright (c) 2022-2023 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_GRID_GRID_ITEM_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_GRID_GRID_ITEM_PATTERN_H
18 
19 #include "core/components_ng/base/inspector_filter.h"
20 #include "core/components_ng/pattern/grid/grid_item_accessibility_property.h"
21 #include "core/components_ng/pattern/grid/grid_item_event_hub.h"
22 #include "core/components_ng/pattern/grid/grid_item_layout_algorithm.h"
23 #include "core/components_ng/pattern/grid/grid_item_layout_property.h"
24 #include "core/components_ng/pattern/grid/grid_item_model.h"
25 #include "core/components_ng/pattern/grid/grid_item_theme.h"
26 #include "core/components_ng/pattern/grid/grid_pattern.h"
27 #include "core/components_ng/pattern/pattern.h"
28 #include "core/components_ng/syntax/shallow_builder.h"
29 
30 namespace OHOS::Ace::NG {
31 class ACE_EXPORT GridItemPattern : public Pattern {
32     DECLARE_ACE_TYPE(GridItemPattern, Pattern);
33 
34 public:
GridItemPattern(const RefPtr<ShallowBuilder> & shallowBuilder)35     explicit GridItemPattern(const RefPtr<ShallowBuilder>& shallowBuilder) : shallowBuilder_(shallowBuilder) {}
GridItemPattern(const RefPtr<ShallowBuilder> & shallowBuilder,GridItemStyle gridItemStyle)36     explicit GridItemPattern(const RefPtr<ShallowBuilder>& shallowBuilder, GridItemStyle gridItemStyle)
37         : shallowBuilder_(shallowBuilder), gridItemStyle_(gridItemStyle)
38     {}
39     ~GridItemPattern() override = default;
40 
CreateLayoutAlgorithm()41     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
42     {
43         return MakeRefPtr<GridItemLayoutAlgorithm>();
44     }
45 
IsAtomicNode()46     bool IsAtomicNode() const override
47     {
48         return false;
49     }
50 
CreateLayoutProperty()51     RefPtr<LayoutProperty> CreateLayoutProperty() override
52     {
53         return MakeRefPtr<GridItemLayoutProperty>();
54     }
55 
CreateEventHub()56     RefPtr<EventHub> CreateEventHub() override
57     {
58         return MakeRefPtr<GridItemEventHub>();
59     }
60 
CreateAccessibilityProperty()61     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
62     {
63         return MakeRefPtr<GridItemAccessibilityProperty>();
64     }
65 
66 
67     void BeforeCreateLayoutWrapper() override;
68 
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,const DirtySwapConfig & config)69     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override
70     {
71         return false;
72     }
73 
SetForceRebuild(bool forceRebuild)74     void SetForceRebuild(bool forceRebuild)
75     {
76         forceRebuild_ = forceRebuild;
77     }
78 
79     void SetSelectable(bool selectable);
80 
ForceRebuild()81     bool ForceRebuild() const
82     {
83         return forceRebuild_;
84     }
85 
Selectable()86     bool Selectable() const
87     {
88         return selectable_;
89     }
90 
GetFocusPattern()91     FocusPattern GetFocusPattern() const override
92     {
93         auto pipeline = PipelineBase::GetCurrentContext();
94         CHECK_NULL_RETURN(pipeline, FocusPattern());
95         auto theme = pipeline->GetTheme<GridItemTheme>();
96         CHECK_NULL_RETURN(theme, FocusPattern());
97         auto focusColor = theme->GetGridItemFocusColor();
98         FocusPaintParam focusPaintParam;
99         focusPaintParam.SetPaintColor(focusColor);
100         return { FocusType::SCOPE, true, FocusStyleType::CUSTOM_REGION, focusPaintParam };
101     }
102 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter)103     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override
104     {
105         Pattern::ToJsonValue(json, filter);
106         if (filter.IsFastFilter()) {
107             json->PutFixedAttr("selectable", selectable_ ? "true" : "false", filter, FIXED_ATTR_SELECTABLE);
108             return;
109         }
110         json->PutExtAttr("forceRebuild", forceRebuild_ ? "true" : "false", filter);
111         json->PutFixedAttr("selectable", selectable_ ? "true" : "false", filter, FIXED_ATTR_SELECTABLE);
112         json->PutExtAttr("selected", isSelected_ ? "true" : "false", filter);
113     }
114 
115     void MarkIsSelected(bool isSelected);
116 
IsSelected()117     bool IsSelected() const
118     {
119         return isSelected_;
120     }
121 
SetSelected(bool selected)122     void SetSelected(bool selected)
123     {
124         isSelected_ = selected;
125     }
126 
SetIrregularItemInfo(GridItemIndexInfo info)127     void SetIrregularItemInfo(GridItemIndexInfo info)
128     {
129         irregularItemInfo_ = info;
130     }
GetIrregularItemInfo()131     std::optional<GridItemIndexInfo> GetIrregularItemInfo() const
132     {
133         return irregularItemInfo_;
134     }
135 
ResetGridItemInfo()136     void ResetGridItemInfo()
137     {
138         irregularItemInfo_.reset();
139     }
140 
141     void DumpAdvanceInfo() override;
142     void UpdateGridItemStyle(GridItemStyle gridItemStyle);
143 
144 protected:
145     void OnModifyDone() override;
146 
IsNeedInitClickEventRecorder()147     bool IsNeedInitClickEventRecorder() const override
148     {
149         return true;
150     }
151 
152 private:
153     void SetAccessibilityAction();
154     void OnAttachToFrameNode() override;
155     void InitFocusPaintRect(const RefPtr<FocusHub>& focusHub);
156     void GetInnerFocusPaintRect(RoundRect& paintRect);
157     Color GetBlendGgColor();
158     void InitHoverEvent();
159     void HandleHoverEvent(bool isHover);
160     void InitPressEvent();
161     void HandlePressEvent(bool isPressed);
162     void InitDisableStyle();
163 
164     RefPtr<ShallowBuilder> shallowBuilder_;
165     bool forceRebuild_ = false;
166     bool selectable_ = true;
167     bool isSelected_ = false;
168 
169     RefPtr<InputEvent> hoverEvent_;
170     RefPtr<TouchEventImpl> touchListener_;
171     bool isHover_ = false;
172     bool isPressed_ = false;
173     GridItemStyle gridItemStyle_ = GridItemStyle::NONE;
174     std::optional<GridItemIndexInfo> irregularItemInfo_;
175 
176     ACE_DISALLOW_COPY_AND_MOVE(GridItemPattern);
177 };
178 } // namespace OHOS::Ace::NG
179 
180 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_GRID_GRID_ITEM_PATTERN_H
181