Chromium Code Reviews| Index: ash/system/web_notification/popup_alignment_delegate.cc |
| diff --git a/ash/system/web_notification/popup_alignment_delegate.cc b/ash/system/web_notification/popup_alignment_delegate.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9b279a50746d3f984b82337a08eb3d7496c7d332 |
| --- /dev/null |
| +++ b/ash/system/web_notification/popup_alignment_delegate.cc |
| @@ -0,0 +1,171 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/system/web_notification/popup_alignment_delegate.h" |
| + |
| +#include "ash/display/display_controller.h" |
| +#include "ash/shelf/shelf_constants.h" |
| +#include "ash/shelf/shelf_layout_manager.h" |
| +#include "ash/shelf/shelf_types.h" |
| +#include "ash/shelf/shelf_widget.h" |
| +#include "ash/shell.h" |
| +#include "base/i18n/rtl.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/gfx/display.h" |
| +#include "ui/gfx/geometry/rect.h" |
| +#include "ui/gfx/screen.h" |
| +#include "ui/message_center/message_center_style.h" |
| +#include "ui/message_center/views/message_popup_collection.h" |
| + |
| +namespace ash { |
| + |
| +namespace { |
| + |
| +const int kToastMarginX = 3; |
| + |
| +// If there should be no margin for the first item, this value needs to be |
| +// substracted to flush the message to the shelf (the width of the border + |
| +// shadow). |
| +const int kNoToastMarginBorderAndShadowOffset = 2; |
| + |
| +} |
| + |
| +PopupAlignmentDelegate::PopupAlignmentDelegate() |
| + : display_id_(gfx::Display::kInvalidDisplayID), |
| + screen_(NULL), |
|
stevenjb
2014/07/02 21:59:23
root_window_(NULL)
Jun Mukai
2014/07/07 18:12:35
Done.
|
| + shelf_(NULL), |
| + system_tray_height_(0) { |
| +} |
| + |
| +PopupAlignmentDelegate::~PopupAlignmentDelegate() { |
| + if (screen_) |
| + screen_->RemoveObserver(this); |
| + Shell::GetInstance()->RemoveShellObserver(this); |
| + if (shelf_) |
| + shelf_->RemoveObserver(this); |
| + shelf_ = NULL; |
|
stevenjb
2014/07/02 21:59:23
nit: unnecessary (and inconsistent with screen_)
Jun Mukai
2014/07/07 18:12:35
Done.
|
| +} |
| + |
| +void PopupAlignmentDelegate::StartObserving( |
| + gfx::Screen* screen, const gfx::Display& display) { |
|
stevenjb
2014/07/02 21:59:23
one line per arg
Jun Mukai
2014/07/07 18:12:35
Done.
|
| + screen_ = screen; |
| + display_id_ = display.id(); |
| + UpdateShelf(); |
| + screen->AddObserver(this); |
| + Shell::GetInstance()->AddShellObserver(this); |
| + if (system_tray_height_ > 0) |
| + OnAutoHideStateChanged(shelf_->auto_hide_state()); |
| +} |
| + |
| +void PopupAlignmentDelegate::SetSystemTrayHeight(int height) { |
| + system_tray_height_ = height; |
| + |
| + // If the shelf is shown during auto-hide state, the distance from the edge |
| + // should be reduced by the height of shelf's shown height. |
| + if (shelf_ && shelf_->visibility_state() == SHELF_AUTO_HIDE && |
| + shelf_->auto_hide_state() == SHELF_AUTO_HIDE_SHOWN) { |
| + system_tray_height_ -= kShelfSize - ShelfLayoutManager::kAutoHideSize; |
| + } |
| + |
| + if (system_tray_height_ > 0) |
| + system_tray_height_ += message_center::kMarginBetweenItems; |
| + else |
| + system_tray_height_ = 0; |
| + |
| + if (!shelf_) |
| + return; |
| + |
| + DoUpdateIfPossible(); |
| +} |
| + |
| +int PopupAlignmentDelegate::GetToastOriginX( |
| + const gfx::Rect& toast_bounds) const { |
| + // In Ash, RTL UI language mirrors the whole ash layout, so the toast |
| + // widgets should be at the bottom-left instead of bottom right. |
| + if (base::i18n::IsRTL()) |
| + return work_area_.x() + kToastMarginX; |
| + |
| + if (IsFromLeft()) |
| + return work_area_.x() + kToastMarginX; |
| + return work_area_.right() - kToastMarginX - toast_bounds.width(); |
| +} |
| + |
| +int PopupAlignmentDelegate::GetBaseLine() const { |
| + return IsTopDown() |
| + ? work_area_.y() + kNoToastMarginBorderAndShadowOffset + |
| + system_tray_height_ |
| + : work_area_.bottom() - kNoToastMarginBorderAndShadowOffset - |
| + system_tray_height_; |
| +} |
| + |
| +int PopupAlignmentDelegate::GetWorkAreaBottom() const { |
| + return work_area_.bottom() - system_tray_height_; |
| +} |
| + |
| +bool PopupAlignmentDelegate::IsTopDown() const { |
| + return GetAlignment() == SHELF_ALIGNMENT_TOP; |
| +} |
| + |
| +bool PopupAlignmentDelegate::IsFromLeft() const { |
| + return GetAlignment() == SHELF_ALIGNMENT_LEFT; |
| +} |
| + |
| +ShelfAlignment PopupAlignmentDelegate::GetAlignment() const { |
| + return shelf_ ? shelf_->GetAlignment() : SHELF_ALIGNMENT_BOTTOM; |
| +} |
| + |
| +void PopupAlignmentDelegate::UpdateShelf() { |
| + if (shelf_) |
| + return; |
| + |
| + aura::Window* root_window = ash::Shell::GetInstance()->display_controller()-> |
| + GetRootWindowForDisplayId(display_id_); |
| + shelf_ = ShelfLayoutManager::ForShelf(root_window); |
| + if (shelf_) |
| + shelf_->AddObserver(this); |
| +} |
| + |
| +void PopupAlignmentDelegate::OnDisplayWorkAreaInsetsChanged() { |
| + UpdateShelf(); |
| + |
| + work_area_ = Shell::GetScreen()->GetDisplayNearestWindow( |
| + shelf_->shelf_widget()->GetNativeView()).work_area(); |
| +} |
| + |
| +void PopupAlignmentDelegate::OnAutoHideStateChanged( |
| + ShelfAutoHideState new_state) { |
| + work_area_ = Shell::GetScreen()->GetDisplayNearestWindow( |
| + shelf_->shelf_widget()->GetNativeView()).work_area(); |
| + int width = 0; |
| + if ((shelf_->visibility_state() == SHELF_AUTO_HIDE) && |
| + new_state == SHELF_AUTO_HIDE_SHOWN) { |
| + // Since the work_area is already reduced by kAutoHideSize, the inset width |
| + // should be just the difference. |
| + width = kShelfSize - ShelfLayoutManager::kAutoHideSize; |
| + } |
| + work_area_.Inset(shelf_->SelectValueForShelfAlignment( |
| + gfx::Insets(0, 0, width, 0), |
| + gfx::Insets(0, width, 0, 0), |
| + gfx::Insets(0, 0, 0, width), |
| + gfx::Insets(width, 0, 0, 0))); |
| + |
| + DoUpdateIfPossible(); |
| +} |
| + |
| +void PopupAlignmentDelegate::OnDisplayAdded( |
| + const gfx::Display& new_display) { |
| +} |
| + |
| +void PopupAlignmentDelegate::OnDisplayRemoved( |
| + const gfx::Display& old_display) { |
| +} |
| + |
| +void PopupAlignmentDelegate::OnDisplayMetricsChanged( |
| + const gfx::Display& display, |
| + uint32_t metrics) { |
| + if (display.id() == display_id_ && shelf_) |
| + OnAutoHideStateChanged(shelf_->auto_hide_state()); |
| +} |
| + |
| +} // namespace ash |