Skip to content

Commit 1a6cb0e

Browse files
committed
Snap custom ad notification to the screen edge.
fix brave/brave-browser#17527
1 parent 6e2c6b5 commit 1a6cb0e

File tree

5 files changed

+79
-20
lines changed

5 files changed

+79
-20
lines changed

browser/ui/views/brave_ads/ad_notification_popup.cc

+11-14
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ SkColor GetDarkModeBackgroundColor() {
9797
return bg_color;
9898
}
9999

100+
void AdjustBoundsAndSnapToFitWorkAreaForWidget(views::Widget* widget) {
101+
DCHECK(widget);
102+
103+
gfx::Rect bounds = widget->GetWindowBoundsInScreen();
104+
const gfx::NativeView native_view = widget->GetNativeView();
105+
AdjustBoundsAndSnapToFitWorkAreaForNativeView(native_view, &bounds);
106+
widget->SetBounds(bounds);
107+
}
108+
100109
} // namespace
101110

102111
AdNotificationPopup::PopupInstanceFactory::~PopupInstanceFactory() = default;
@@ -284,16 +293,6 @@ void AdNotificationPopup::OnThemeChanged() {
284293
SchedulePaint();
285294
}
286295

287-
void AdNotificationPopup::OnWidgetCreated(views::Widget* widget) {
288-
DCHECK(widget);
289-
290-
gfx::Rect bounds = widget->GetWindowBoundsInScreen();
291-
const gfx::NativeView native_view = widget->GetNativeView();
292-
AdjustBoundsToFitWorkAreaForNativeView(&bounds, native_view);
293-
294-
widget->SetBounds(bounds);
295-
}
296-
297296
void AdNotificationPopup::OnWidgetDestroyed(views::Widget* widget) {
298297
DCHECK(widget);
299298

@@ -448,10 +447,7 @@ void AdNotificationPopup::RecomputeAlignment() {
448447
return;
449448
}
450449

451-
gfx::Rect bounds = GetWidget()->GetWindowBoundsInScreen();
452-
const gfx::NativeView native_view = GetWidget()->GetNativeView();
453-
AdjustBoundsToFitWorkAreaForNativeView(&bounds, native_view);
454-
GetWidget()->SetBounds(bounds);
450+
AdjustBoundsAndSnapToFitWorkAreaForWidget(GetWidget());
455451
}
456452

457453
const gfx::ShadowDetails& AdNotificationPopup::GetShadowDetails() const {
@@ -475,6 +471,7 @@ void AdNotificationPopup::CreateWidgetView() {
475471
if (!g_disable_fade_in_animation_for_testing) {
476472
widget->SetOpacity(0.0);
477473
}
474+
AdjustBoundsAndSnapToFitWorkAreaForWidget(widget);
478475
widget->ShowInactive();
479476
}
480477

browser/ui/views/brave_ads/ad_notification_popup.h

-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ class AdNotificationPopup : public views::WidgetDelegateView,
102102
void OnThemeChanged() override;
103103

104104
// views::WidgetObserver:
105-
void OnWidgetCreated(views::Widget* widget) override;
106105
void OnWidgetDestroyed(views::Widget* widget) override;
107106
void OnWidgetBoundsChanged(views::Widget* widget,
108107
const gfx::Rect& new_bounds) override;

browser/ui/views/brave_ads/ad_notification_view.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ bool AdNotificationView::OnMouseDragged(const ui::MouseEvent& event) {
8181
const std::string id = ad_notification_.id();
8282
gfx::Rect bounds = AdNotificationPopup::GetBounds(id) + movement;
8383
const gfx::NativeView native_view = GetWidget()->GetNativeView();
84-
AdjustBoundsToFitWorkAreaForNativeView(&bounds, native_view);
84+
AdjustBoundsAndSnapToFitWorkAreaForNativeView(native_view, &bounds);
8585
GetWidget()->SetBounds(bounds);
8686

8787
return true;

browser/ui/views/brave_ads/bounds_util.cc

+65-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
#include "brave/browser/ui/views/brave_ads/bounds_util.h"
77

88
#include "ui/display/screen.h"
9+
#include "ui/gfx/geometry/point.h"
910
#include "ui/gfx/geometry/rect.h"
1011

1112
namespace brave_ads {
1213

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) {
1520
DCHECK(bounds);
1621

1722
gfx::Rect work_area =
@@ -25,7 +30,65 @@ void AdjustBoundsToFitWorkAreaForNativeView(gfx::Rect* bounds,
2530
.work_area();
2631
}
2732

33+
return work_area;
34+
}
35+
36+
void AdjustBoundsToFitWorkArea(const gfx::Rect& work_area, gfx::Rect* bounds) {
37+
DCHECK(bounds);
38+
2839
bounds->AdjustToFit(work_area);
2940
}
3041

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+
3194
} // namespace brave_ads

browser/ui/views/brave_ads/bounds_util.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Rect;
1414

1515
namespace brave_ads {
1616

17-
void AdjustBoundsToFitWorkAreaForNativeView(gfx::Rect* bounds,
18-
gfx::NativeView native_view);
17+
void AdjustBoundsAndSnapToFitWorkAreaForNativeView(gfx::NativeView native_view,
18+
gfx::Rect* bounds);
1919

2020
} // namespace brave_ads
2121

0 commit comments

Comments
 (0)