#include "ui/message_center/views/desktop_message_popup_collection.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/message_center/public/cpp/message_center_constants.h"
namespace message_center {
DesktopMessagePopupCollection::DesktopMessagePopupCollection() = default;
DesktopMessagePopupCollection::~DesktopMessagePopupCollection() = default;
void DesktopMessagePopupCollection::StartObserving() {
auto* screen = display::Screen::Get();
if (screen_ || !screen)
return;
screen_ = screen;
display_observer_.emplace(this);
display::Display display = screen_->GetPrimaryDisplay();
primary_display_id_ = display.id();
RecomputeAlignment(display);
}
int DesktopMessagePopupCollection::GetPopupOriginX(
const gfx::Rect& popup_bounds) const {
if (IsFromLeft())
return work_area_.x() + kMarginBetweenPopups;
return work_area_.right() - kMarginBetweenPopups - popup_bounds.width();
}
int DesktopMessagePopupCollection::GetBaseline() const {
return IsTopDown() ? work_area_.y() + kMarginBetweenPopups
: work_area_.bottom() - kMarginBetweenPopups;
}
gfx::Rect DesktopMessagePopupCollection::GetWorkArea() const {
return work_area_;
}
bool DesktopMessagePopupCollection::IsTopDown() const {
return (alignment_ & POPUP_ALIGNMENT_TOP) != 0;
}
bool DesktopMessagePopupCollection::IsFromLeft() const {
return (alignment_ & POPUP_ALIGNMENT_LEFT) != 0;
}
bool DesktopMessagePopupCollection::RecomputeAlignment(
const display::Display& display) {
if (work_area_ == display.work_area())
return false;
work_area_ = display.work_area();
alignment_ = work_area_.y() > display.bounds().y() ? POPUP_ALIGNMENT_TOP
: POPUP_ALIGNMENT_BOTTOM;
alignment_ |= (work_area_.x() > display.bounds().x() &&
work_area_.y() == display.bounds().y())
? POPUP_ALIGNMENT_LEFT
: POPUP_ALIGNMENT_RIGHT;
return true;
}
void DesktopMessagePopupCollection::ConfigureWidgetInitParamsForContainer(
views::Widget* widget,
views::Widget::InitParams* init_params) {
}
bool DesktopMessagePopupCollection::IsPrimaryDisplayForNotification() const {
return true;
}
bool DesktopMessagePopupCollection::BlockForMixedFullscreen(
const Notification& notification) const {
return false;
}
bool DesktopMessagePopupCollection::CanUseTransformForBoundsAnimation() const {
return false;
}
void DesktopMessagePopupCollection::UpdatePrimaryDisplay() {
display::Display primary_display = screen_->GetPrimaryDisplay();
if (primary_display.id() != primary_display_id_) {
primary_display_id_ = primary_display.id();
if (RecomputeAlignment(primary_display))
ResetBounds();
}
}
void DesktopMessagePopupCollection::OnDisplayAdded(
const display::Display& added_display) {
UpdatePrimaryDisplay();
}
void DesktopMessagePopupCollection::OnDisplaysRemoved(
const display::Displays& removed_displays) {
UpdatePrimaryDisplay();
}
void DesktopMessagePopupCollection::OnDisplayMetricsChanged(
const display::Display& display,
uint32_t metrics) {
primary_display_id_ = display::kInvalidDisplayId;
UpdatePrimaryDisplay();
}
}