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 OHOS_ROSEN_WINDOW_LAYOUT_POLICY_CASCADE_H
17 #define OHOS_ROSEN_WINDOW_LAYOUT_POLICY_CASCADE_H
18 
19 #include <map>
20 #include <refbase.h>
21 #include <set>
22 
23 #include "window_layout_policy.h"
24 #include "window_node.h"
25 #include "wm_common.h"
26 
27 namespace OHOS {
28 namespace Rosen {
29 class WindowLayoutPolicyCascade : public WindowLayoutPolicy {
30 public:
31     WindowLayoutPolicyCascade() = delete;
32     WindowLayoutPolicyCascade(DisplayGroupWindowTree& displayGroupWindowTree);
33     ~WindowLayoutPolicyCascade() = default;
34     void Launch() override;
35     void Reorder() override;
36     Rect GetDividerRect(DisplayId displayId) const override;
37     void SetSplitDividerWindowRects(std::map<DisplayId, Rect> dividerWindowRects) override;
38     void PerformWindowLayout(const sptr<WindowNode>& node, WindowUpdateType updateType) override;
39     void GetMaximizeRect(const sptr<WindowNode>& node, Rect& maxRect) override;
40 
41 private:
42     /*
43      * methods for calculate cascadeRect and splitRect
44      */
45     void InitAllRects();
46     void InitSplitRects(DisplayId displayId);
47     void SetSplitRectByDivider(const Rect& divRect, DisplayId displayId);
48     void SetInitialDividerRect(const sptr<WindowNode>& node, DisplayId displayId);
49     void InitCascadeRect(DisplayId displayId);
50     void SetDefaultCascadeRect(const sptr<WindowNode>& node);
51     Rect StepCascadeRect(Rect rect, DisplayId displayId) const;
52     Rect GetCurCascadeRect(const sptr<WindowNode>& node) const;
53 
54     // methods for limit divider position by display and split ratio
55     void UpdateDividerPosition(const sptr<WindowNode>& node) const;
56     void LimitDividerInDisplayRegion(Rect& rect, DisplayId displayId) const;
57     void LimitDividerPositionBySplitRatio(DisplayId displayId, Rect& winRect) const;
58 
59     /*
60      * methods for calculate window rect
61      */
62     void LayoutDivider(const sptr<WindowNode>& node, WindowUpdateType type);
63     void LayoutSplitNodes(DisplayId displayId, WindowUpdateType type, bool layoutByDivider = false);
64     void UpdateLayoutRect(const sptr<WindowNode>& node) override;
65     void ComputeDecoratedRequestRect(const sptr<WindowNode>& node) const;
66     void ApplyWindowRectConstraints(const sptr<WindowNode>& node, Rect& winRect) const;
67     void ComputeRectByAspectRatio(const sptr<WindowNode>& node) const;
68     bool CheckAspectRatioBySizeLimits(const sptr<WindowNode>& node, WindowLimits& newLimits) const;
69 
70     /*
71      * methods for floating window limitSize and position
72      */
73     DockWindowShowState GetDockWindowShowState(DisplayId displayId, Rect& dockWinRect) const;
74     void LimitFloatingWindowSize(const sptr<WindowNode>& node, Rect& winRect) const;
75     void LimitMainFloatingWindowPosition(const sptr<WindowNode>& node, Rect& winRect) const;
76     void UpdateFloatingWindowSizeForStretchableWindow(const sptr<WindowNode>& node,
77         const Rect& displayRect, Rect& winRect) const;
78     void UpdateFloatingWindowSizeBySizeLimits(const sptr<WindowNode>& node,
79         const Rect& displayRect, Rect& winRect) const;
80     void LimitWindowPositionWhenInitRectOrMove(const sptr<WindowNode>& node, Rect& winRect) const;
81     void LimitWindowPositionWhenDrag(const sptr<WindowNode>& node, Rect& winRect) const;
82     void FixWindowSizeByRatioIfDragBeyondLimitRegion(const sptr<WindowNode>& node, Rect& winRect) const;
83     void FixWindowRectWhenDrag(const sptr<WindowNode>& node, const Rect& oriWinRect, Rect& winRect) const;
84 
85     /*
86      * Layout preprocess:
87      * 1) Set default cascade rect if rect is empty
88      * 2) Get aspect ratio form persistent storage
89      * 3) Fix rect within display region
90      */
91     void LayoutPreProcess(const sptr<WindowNode>& node, WindowUpdateType updateType);
92 
93     struct CascadeRects {
94         Rect primaryRect_;
95         Rect secondaryRect_;
96         Rect dividerRect_;
97         Rect defaultCascadeRect_;
98     };
99     mutable std::map<DisplayId, CascadeRects> cascadeRectsMap_;
100     std::map<DisplayId, Rect> restoringDividerWindowRects_;
101 };
102 }
103 }
104 #endif // OHOS_ROSEN_WINDOW_LAYOUT_POLICY_CASCADE_H
105