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 #ifndef OHOS_ROSEN_WINDOW_PAIR_H
17 #define OHOS_ROSEN_WINDOW_PAIR_H
18 
19 #include <refbase.h>
20 #include "class_var_definition.h"
21 #include "window_node.h"
22 #include "window_layout_policy.h"
23 #include "wm_common_inner.h"
24 #include "wm_common.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 /**
29  * @brief Enumerates the status of window pair.
30  */
31 enum class WindowPairStatus : uint32_t {
32     EMPTY,
33     SINGLE_PRIMARY,
34     SINGLE_SECONDARY,
35     SINGLE_SPLIT,
36     PRIMARY_AND_SECONDARY,
37     PRIMARY_AND_DIVIDER,
38     SECONDARY_AND_DIVIDER,
39     PAIRED_DONE
40 };
41 /**
42  * @brief Enumerates the message of split event.
43  */
44 enum class SplitEventMsgType : uint32_t {
45     MSG_SHOW_PRIMARY,
46     MSG_SHOW_SECONDARY,
47     MSG_SHOW_DIVIDER,
48     MSG_DESTROY_DIVIDER,
49 };
50 
51 class WindowPair : public RefBase {
52 public:
53     /**
54      * @brief Constructor used to create an empty WindowPair instance.
55      *
56      * @param displayId the display of window pair
57      */
WindowPair(const DisplayId& displayId)58     explicit WindowPair(const DisplayId& displayId) : displayId_(displayId) {};
59 
60     /**
61      * @brief Deconstructor used to deconstruct.
62      *
63      */
64     ~WindowPair();
65 
66     /**
67      * @brief Clear window pair.
68      *
69      */
70     void Clear();
71 
72     /**
73      * @brief Set split ratio.
74      *
75      * @param ratio split ratio
76      */
77     void SetSplitRatio(float ratio);
78 
79     /**
80      * @brief Get split ratio.
81      *
82      * @return split ratio
83      */
84     float GetSplitRatio() const;
85 
86     /**
87      * @brief Get whether the window pair is paired.
88      *
89      * @return the pair state of window pair
90      */
91     bool IsPaired() const;
92 
93     /**
94      * @brief Get whether the window status_ is valid.
95      */
96     bool IsAbnormalStatus() const;
97 
98     /**
99      * @brief Check pair status support recent update
100      *
101      * @param node trigger window node
102      */
103     bool StatusSupprtedWhenRecentUpdate(sptr<WindowNode>& node);
104     /**
105      * @brief Handle changes in the state of the window pair
106      *
107      * @param node trigger window node
108      */
109     void UpdateIfSplitRelated(sptr<WindowNode>& node);
110 
111     /**
112      * @brief Handle remove window from pair.
113      *
114      * @param node target node
115      */
116     void HandleRemoveWindow(sptr<WindowNode>& node);
117 
118     /**
119      * @brief Find window node from window pair.
120      *
121      * @param node target window node
122      * @return window node
123      */
124     sptr<WindowNode> Find(sptr<WindowNode>& node);
125 
126     /**
127      * @brief Get divider window node.
128      *
129      * @return divider window node
130      */
131     sptr<WindowNode> GetDividerWindow() const;
132 
133     /**
134      * @brief Get pair status.
135      *
136      * @return the pair status of window pair
137      */
138     WindowPairStatus GetPairStatus() const;
139 
140     /**
141      * @brief Get all window node form pair in Z order.
142      *
143      * @return the list window form pair
144      */
145     std::vector<sptr<WindowNode>> GetOrderedPair(sptr<WindowNode>& node);
146 
147     /**
148      * @brief Get all window node form pair.
149      *
150      * @return the list window form pair
151      */
152     std::vector<sptr<WindowNode>> GetPairedWindows();
153 
154     /**
155      * @brief Get whether dock slice is forbidden to move.
156      *
157      * @return whether dock slice is forbidden to move
158      */
159     bool IsForbidDockSliceMove() const;
160 
161     /**
162      * @brief Exit split screen mode when dock slice in exit split screen mode area.
163      */
164     void ExitSplitMode();
165 
166     /**
167      * @brief whether dock slice in exit split screen mode area
168      */
169     bool IsDockSliceInExitSplitModeArea(const std::vector<int32_t>& exitSplitPoints);
170 
171     /**
172      * @brief Set the initial rect of divider window.
173      *
174      * @param rect divider window rect
175      */
176     void SetDividerRect(const Rect& rect);
177 
178     /**
179      * @brief Update divider window rect when display orientation changed.
180      *
181      * @param rect default divider rect
182      */
183     void RotateDividerWindow(const Rect& rect);
184 
185     /**
186      * @brief Take window pair node snapshot.
187      */
188     bool TakePairSnapshot();
189 
190     /**
191      * @brief Clear window pair node snapshot.
192      */
193     void ClearPairSnapshot();
194 
195     /**
196      * @brief Set split ratio config.
197      */
198     void SetSplitRatioConfig(const SplitRatioConfig& splitRatioConfig);
199 
200     /**
201      * @brief Calculate split ratio points.
202      */
203     void CalculateSplitRatioPoints(const Rect& displayRect);
204 
205     /**
206      * @brief Get exit split points.
207      */
208     std::vector<int32_t> GetExitSplitPoints();
209 
210     /**
211      * @brief Get split ratio points.
212      */
213     std::vector<int32_t> GetSplitRatioPoints();
214 
215     /**
216      * @brief is during split.
217      * @return true means existing recent or status is not PARI_DONE.
218      */
219     bool IsDuringSplit();
220 
221 private:
222     /**
223      * @brief Gets whether the window is related to split window.
224      *
225      * @param node target node
226      * @return Whether target node is related to the split window
227      */
228     bool IsSplitRelated(sptr<WindowNode>& node) const;
229 
230     /**
231      * @brief Check Z order for window node form pair.
232      *
233      * @return the Z order of window form pair
234      */
235     void CheckOrderedPairZorder(
236         sptr<WindowNode>& node, bool& hasPrimaryDialog, bool& hasSecondaryDialog, bool& isPrimaryAbove);
237 
238     /**
239      * @brief Create all window node form pair.
240      *
241      * @return the list window form pair
242      */
243     std::vector<sptr<WindowNode>> CreateOrderedPair(
244         sptr<WindowNode>& bottom, sptr<WindowNode>& mid, sptr<WindowNode>& top);
245 
246     /**
247      * @brief Replace paired window.
248      *
249      * @param node current node
250      */
251     void Insert(sptr<WindowNode>& node);
252 
253     /**
254      * @brief Update paired window node
255      *
256      */
257     void HandlePairedNodesChange();
258 
259     /**
260      * @brief Update pair status
261      *
262      */
263     void UpdateWindowPairStatus();
264 
265     /**
266      * @brief Switch the position of two paired window.
267      *
268      */
269     void SwitchPosition();
270 
271     /**
272      * @brief Dump the info of pair.
273      *
274      */
275     void DumpPairInfo();
276 
277     /**
278      * @brief Send split screen event.
279      *
280      * @param msgType split event message type
281      * @param missionId mission id
282      */
283     void SendSplitScreenCommonEvent(SplitEventMsgType msgType, int32_t missionId);
284 
285     /**
286      * @brief Send split screen event to notify create recent view.
287      *
288      * @param node split node
289      */
290     void NotifyShowRecent(sptr<WindowNode> node);
291 
292     /**
293      * @brief Send split screen event to notify create or destroy divider window.
294      *
295      * @param node split node
296      * @param isDestroy destroy or create divider window flag
297      */
298     void NotifyCreateOrDestroyDivider(sptr<WindowNode> node, bool isDestroy);
299 
300     /**
301      * @brief Calculate and Get split ratio point
302      */
303     int32_t GetSplitRatioPoint(float ratio, const Rect& displayRect);
304 
305 private:
306     float ratio_ = DEFAULT_SPLIT_RATIO;
307     DisplayId displayId_;
308     sptr<WindowNode> primary_;
309     sptr<WindowNode> secondary_;
310     sptr<WindowNode> divider_;
311     WindowPairStatus status_ = {WindowPairStatus::EMPTY};
312     Rect dividerRect_ {0, 0, 0, 0};
313     std::vector<int32_t> exitSplitPoints_; // 2 element, first element < second element
314     std::vector<int32_t> splitRatioPoints_;
315     SplitRatioConfig splitRatioConfig_;
316     DEFINE_VAR_DEFAULT_FUNC_SET(bool, AllSplitAppWindowsRestoring, isAllSplitAppWindowsRestoring, false)
317 };
318 } // namespace Rosen
319 } // namespace OHOS
320 #endif // OHOS_ROSEN_WINDOW_PAIR_H