xref: /aosp_15_r20/frameworks/native/libs/ui/include/ui/EdgeExtensionEffect.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 /**
20  * Stores the edge that will be extended
21  */
22 namespace android {
23 
24 enum CanonicalDirections { NONE = 0, LEFT = 1, RIGHT = 2, TOP = 4, BOTTOM = 8 };
25 
to_string(CanonicalDirections direction)26 inline std::string to_string(CanonicalDirections direction) {
27     switch (direction) {
28         case LEFT:
29             return "LEFT";
30         case RIGHT:
31             return "RIGHT";
32         case TOP:
33             return "TOP";
34         case BOTTOM:
35             return "BOTTOM";
36         case NONE:
37             return "NONE";
38     }
39 }
40 
41 struct EdgeExtensionEffect {
EdgeExtensionEffectEdgeExtensionEffect42     EdgeExtensionEffect(bool left, bool right, bool top, bool bottom) {
43         extensionEdges = NONE;
44         if (left) {
45             extensionEdges |= LEFT;
46         }
47         if (right) {
48             extensionEdges |= RIGHT;
49         }
50         if (top) {
51             extensionEdges |= TOP;
52         }
53         if (bottom) {
54             extensionEdges |= BOTTOM;
55         }
56     }
57 
EdgeExtensionEffectEdgeExtensionEffect58     EdgeExtensionEffect() { EdgeExtensionEffect(false, false, false, false); }
59 
extendsEdgeEdgeExtensionEffect60     bool extendsEdge(CanonicalDirections edge) const { return extensionEdges & edge; }
61 
hasEffectEdgeExtensionEffect62     bool hasEffect() const { return extensionEdges != NONE; };
63 
resetEdgeExtensionEffect64     void reset() { extensionEdges = NONE; }
65 
66     bool operator==(const EdgeExtensionEffect& other) const {
67         return extensionEdges == other.extensionEdges;
68     }
69 
70     bool operator!=(const EdgeExtensionEffect& other) const { return !(*this == other); }
71 
72 private:
73     int extensionEdges;
74 };
75 
to_string(const EdgeExtensionEffect & effect)76 inline std::string to_string(const EdgeExtensionEffect& effect) {
77     std::string s = "EdgeExtensionEffect={edges=[";
78     if (effect.hasEffect()) {
79         for (CanonicalDirections edge : {LEFT, RIGHT, TOP, BOTTOM}) {
80             if (effect.extendsEdge(edge)) {
81                 s += to_string(edge) + ", ";
82             }
83         }
84     } else {
85         s += to_string(NONE);
86     }
87     s += "]}";
88     return s;
89 }
90 
91 inline std::ostream& operator<<(std::ostream& os, const EdgeExtensionEffect effect) {
92     os << to_string(effect);
93     return os;
94 }
95 } // namespace android
96