Skip to content

Commit

Permalink
Snap custom ad notification to the screen edge.
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren committed Sep 17, 2021
1 parent 6e2c6b5 commit 1a6cb0e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 20 deletions.
25 changes: 11 additions & 14 deletions browser/ui/views/brave_ads/ad_notification_popup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ SkColor GetDarkModeBackgroundColor() {
return bg_color;
}

void AdjustBoundsAndSnapToFitWorkAreaForWidget(views::Widget* widget) {
DCHECK(widget);

gfx::Rect bounds = widget->GetWindowBoundsInScreen();
const gfx::NativeView native_view = widget->GetNativeView();
AdjustBoundsAndSnapToFitWorkAreaForNativeView(native_view, &bounds);
widget->SetBounds(bounds);
}

} // namespace

AdNotificationPopup::PopupInstanceFactory::~PopupInstanceFactory() = default;
Expand Down Expand Up @@ -284,16 +293,6 @@ void AdNotificationPopup::OnThemeChanged() {
SchedulePaint();
}

void AdNotificationPopup::OnWidgetCreated(views::Widget* widget) {
DCHECK(widget);

gfx::Rect bounds = widget->GetWindowBoundsInScreen();
const gfx::NativeView native_view = widget->GetNativeView();
AdjustBoundsToFitWorkAreaForNativeView(&bounds, native_view);

widget->SetBounds(bounds);
}

void AdNotificationPopup::OnWidgetDestroyed(views::Widget* widget) {
DCHECK(widget);

Expand Down Expand Up @@ -448,10 +447,7 @@ void AdNotificationPopup::RecomputeAlignment() {
return;
}

gfx::Rect bounds = GetWidget()->GetWindowBoundsInScreen();
const gfx::NativeView native_view = GetWidget()->GetNativeView();
AdjustBoundsToFitWorkAreaForNativeView(&bounds, native_view);
GetWidget()->SetBounds(bounds);
AdjustBoundsAndSnapToFitWorkAreaForWidget(GetWidget());
}

const gfx::ShadowDetails& AdNotificationPopup::GetShadowDetails() const {
Expand All @@ -475,6 +471,7 @@ void AdNotificationPopup::CreateWidgetView() {
if (!g_disable_fade_in_animation_for_testing) {
widget->SetOpacity(0.0);
}
AdjustBoundsAndSnapToFitWorkAreaForWidget(widget);
widget->ShowInactive();
}

Expand Down
1 change: 0 additions & 1 deletion browser/ui/views/brave_ads/ad_notification_popup.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ class AdNotificationPopup : public views::WidgetDelegateView,
void OnThemeChanged() override;

// views::WidgetObserver:
void OnWidgetCreated(views::Widget* widget) override;
void OnWidgetDestroyed(views::Widget* widget) override;
void OnWidgetBoundsChanged(views::Widget* widget,
const gfx::Rect& new_bounds) override;
Expand Down
2 changes: 1 addition & 1 deletion browser/ui/views/brave_ads/ad_notification_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ bool AdNotificationView::OnMouseDragged(const ui::MouseEvent& event) {
const std::string id = ad_notification_.id();
gfx::Rect bounds = AdNotificationPopup::GetBounds(id) + movement;
const gfx::NativeView native_view = GetWidget()->GetNativeView();
AdjustBoundsToFitWorkAreaForNativeView(&bounds, native_view);
AdjustBoundsAndSnapToFitWorkAreaForNativeView(native_view, &bounds);
GetWidget()->SetBounds(bounds);

return true;
Expand Down
67 changes: 65 additions & 2 deletions browser/ui/views/brave_ads/bounds_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
#include "brave/browser/ui/views/brave_ads/bounds_util.h"

#include "ui/display/screen.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"

namespace brave_ads {

void AdjustBoundsToFitWorkAreaForNativeView(gfx::Rect* bounds,
gfx::NativeView native_view) {
namespace {

enum class EdgeGravity { kTop, kBottom, kLeft, kRight };

gfx::Rect GetDisplayScreenWorkArea(gfx::Rect* bounds,
gfx::NativeView native_view) {
DCHECK(bounds);

gfx::Rect work_area =
Expand All @@ -25,7 +30,65 @@ void AdjustBoundsToFitWorkAreaForNativeView(gfx::Rect* bounds,
.work_area();
}

return work_area;
}

void AdjustBoundsToFitWorkArea(const gfx::Rect& work_area, gfx::Rect* bounds) {
DCHECK(bounds);

bounds->AdjustToFit(work_area);
}

void SnapBoundsToEdgeOfWorkArea(const gfx::Rect& work_area, gfx::Rect* bounds) {
DCHECK(bounds);

EdgeGravity gravity = EdgeGravity::kTop;
int min_dist = bounds->y() - work_area.y();

int dist =
work_area.y() + work_area.height() - bounds->y() - bounds->height();
if (min_dist > dist) {
min_dist = dist;
gravity = EdgeGravity::kBottom;
}

dist = bounds->x() - work_area.x();
if (min_dist > dist) {
min_dist = dist;
gravity = EdgeGravity::kLeft;
}

dist = work_area.x() + work_area.width() - bounds->x() - bounds->width();
if (min_dist > dist) {
min_dist = dist;
gravity = EdgeGravity::kRight;
}

switch (gravity) {
case EdgeGravity::kTop:
bounds->set_y(work_area.y());
break;
case EdgeGravity::kBottom:
bounds->set_y(work_area.y() + work_area.height() - bounds->height());
break;
case EdgeGravity::kLeft:
bounds->set_x(work_area.x());
break;
case EdgeGravity::kRight:
bounds->set_x(work_area.x() + work_area.width() - bounds->width());
break;
}
}

} // namespace

void AdjustBoundsAndSnapToFitWorkAreaForNativeView(gfx::NativeView native_view,
gfx::Rect* bounds) {
DCHECK(bounds);

const gfx::Rect work_area = GetDisplayScreenWorkArea(bounds, native_view);
AdjustBoundsToFitWorkArea(work_area, bounds);
SnapBoundsToEdgeOfWorkArea(work_area, bounds);
}

} // namespace brave_ads
4 changes: 2 additions & 2 deletions browser/ui/views/brave_ads/bounds_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Rect;

namespace brave_ads {

void AdjustBoundsToFitWorkAreaForNativeView(gfx::Rect* bounds,
gfx::NativeView native_view);
void AdjustBoundsAndSnapToFitWorkAreaForNativeView(gfx::NativeView native_view,
gfx::Rect* bounds);

} // namespace brave_ads

Expand Down

0 comments on commit 1a6cb0e

Please sign in to comment.