6
6
#include " brave/browser/ui/views/brave_ads/bounds_util.h"
7
7
8
8
#include " ui/display/screen.h"
9
+ #include " ui/gfx/geometry/point.h"
9
10
#include " ui/gfx/geometry/rect.h"
10
11
11
12
namespace brave_ads {
12
13
13
- void AdjustBoundsToFitWorkAreaForNativeView (gfx::Rect * bounds,
14
- gfx::NativeView native_view) {
14
+ namespace {
15
+
16
+ enum class EdgeGravity { kTop , kBottom , kLeft , kRight };
17
+
18
+ gfx::Rect GetDisplayScreenWorkArea (gfx::Rect * bounds,
19
+ gfx::NativeView native_view) {
15
20
DCHECK (bounds);
16
21
17
22
gfx::Rect work_area =
@@ -25,7 +30,65 @@ void AdjustBoundsToFitWorkAreaForNativeView(gfx::Rect* bounds,
25
30
.work_area ();
26
31
}
27
32
33
+ return work_area;
34
+ }
35
+
36
+ void AdjustBoundsToFitWorkArea (const gfx::Rect & work_area, gfx::Rect * bounds) {
37
+ DCHECK (bounds);
38
+
28
39
bounds->AdjustToFit (work_area);
29
40
}
30
41
42
+ void SnapBoundsToEdgeOfWorkArea (const gfx::Rect & work_area, gfx::Rect * bounds) {
43
+ DCHECK (bounds);
44
+
45
+ EdgeGravity gravity = EdgeGravity::kTop ;
46
+ int min_dist = bounds->y () - work_area.y ();
47
+
48
+ int dist =
49
+ work_area.y () + work_area.height () - bounds->y () - bounds->height ();
50
+ if (min_dist > dist) {
51
+ min_dist = dist;
52
+ gravity = EdgeGravity::kBottom ;
53
+ }
54
+
55
+ dist = bounds->x () - work_area.x ();
56
+ if (min_dist > dist) {
57
+ min_dist = dist;
58
+ gravity = EdgeGravity::kLeft ;
59
+ }
60
+
61
+ dist = work_area.x () + work_area.width () - bounds->x () - bounds->width ();
62
+ if (min_dist > dist) {
63
+ min_dist = dist;
64
+ gravity = EdgeGravity::kRight ;
65
+ }
66
+
67
+ switch (gravity) {
68
+ case EdgeGravity::kTop :
69
+ bounds->set_y (work_area.y ());
70
+ break ;
71
+ case EdgeGravity::kBottom :
72
+ bounds->set_y (work_area.y () + work_area.height () - bounds->height ());
73
+ break ;
74
+ case EdgeGravity::kLeft :
75
+ bounds->set_x (work_area.x ());
76
+ break ;
77
+ case EdgeGravity::kRight :
78
+ bounds->set_x (work_area.x () + work_area.width () - bounds->width ());
79
+ break ;
80
+ }
81
+ }
82
+
83
+ } // namespace
84
+
85
+ void AdjustBoundsAndSnapToFitWorkAreaForNativeView (gfx::NativeView native_view,
86
+ gfx::Rect * bounds) {
87
+ DCHECK (bounds);
88
+
89
+ const gfx::Rect work_area = GetDisplayScreenWorkArea (bounds, native_view);
90
+ AdjustBoundsToFitWorkArea (work_area, bounds);
91
+ SnapBoundsToEdgeOfWorkArea (work_area, bounds);
92
+ }
93
+
31
94
} // namespace brave_ads
0 commit comments