Chromium Code Reviews| Index: chrome/browser/ui/views/message_center/message_center_widget_delegate.cc |
| diff --git a/chrome/browser/ui/views/message_center/message_center_widget_delegate.cc b/chrome/browser/ui/views/message_center/message_center_widget_delegate.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1ccb1c6ddcc5ddea79df5cfb66c2fccf29c36fc4 |
| --- /dev/null |
| +++ b/chrome/browser/ui/views/message_center/message_center_widget_delegate.cc |
| @@ -0,0 +1,251 @@ |
| +// Copyright 2013 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 "chrome/browser/ui/views/message_center/message_center_widget_delegate.h" |
| + |
| +#include <complex> |
| + |
| +#include "chrome/browser/ui/views/message_center/message_center_frame_view.h" |
| +#include "content/public/browser/user_metrics.h" |
| +#include "ui/base/accessibility/accessible_view_state.h" |
| +#include "ui/gfx/screen.h" |
| +#include "ui/message_center/message_center_style.h" |
| +#include "ui/message_center/message_center_util.h" |
| +#include "ui/message_center/views/message_center_view.h" |
| +#include "ui/native_theme/native_theme.h" |
| +#include "ui/views/border.h" |
| +#include "ui/views/layout/box_layout.h" |
| +#include "ui/views/widget/widget.h" |
| + |
| +namespace { |
| + |
| +const int kBorderWidth = 1; |
| + |
| +} // namespace |
| + |
| +namespace message_center { |
| + |
| +MessageCenterWidgetDelegate::MessageCenterWidgetDelegate( |
| + WebNotificationTray* tray, |
| + MessageCenterTray* mc_tray, |
| + bool initially_settings_visible, |
| + PositionInfo pos_info) |
| + : MessageCenterView(tray->message_center(), |
| + mc_tray, |
| + pos_info.max_height, |
| + initially_settings_visible, |
| + pos_info.bubble_alignment & ALIGNMENT_TOP /* Show buttons on top if |
|
dewittj
2013/07/02 18:43:32
I might just change this constructor to accept bub
sidharthms
2013/07/02 21:01:16
But MessageCenterView is also used in other places
|
| + message center is |
| + top aligned */), |
| + pos_info_(pos_info), |
| + tray_(tray) { |
| + preferred_width_ = kNotificationWidth; |
| + |
| + views::BoxLayout* layout = |
| + new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); |
| + layout->set_spread_blank_space(true); |
| + SetLayoutManager(layout); |
| + |
| + AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| + UpdateColorsFromTheme(GetNativeTheme()); |
|
dewittj
2013/07/02 18:43:32
Should this be called on Windows?
sidharthms
2013/07/02 21:01:16
Eh, wouldn't it be better to use native themes?
O
|
| + |
| + if (get_use_acceleration_when_possible()) { |
| + SetPaintToLayer(true); |
| + SetFillsBoundsOpaquely(true); |
| + } |
| + |
| + InitWidget(); |
| + UpdateNotifications(); |
|
dewittj
2013/07/02 18:43:32
This method is at least misnamed (since it also sh
sidharthms
2013/07/02 21:01:16
Done.
|
| +} |
| + |
| +MessageCenterWidgetDelegate::~MessageCenterWidgetDelegate() { |
| + views::Widget* widget = GetWidget(); |
| + if (widget) { |
| + widget->RemoveObserver(this); |
| + } |
| +} |
| + |
| +views::View* MessageCenterWidgetDelegate::GetContentsView() { |
| + return this; |
| +} |
| + |
| +views::NonClientFrameView* |
| +MessageCenterWidgetDelegate::CreateNonClientFrameView(views::Widget* widget) { |
| + return new MessageCenterFrameView(kBorderWidth); |
| +} |
| + |
| +views::Widget* MessageCenterWidgetDelegate::GetWidget() { |
| + return View::GetWidget(); |
| +} |
| + |
| +const views::Widget* MessageCenterWidgetDelegate::GetWidget() const { |
| + return View::GetWidget(); |
| +} |
| + |
| +void MessageCenterWidgetDelegate::OnWidgetActivationChanged( |
| + views::Widget* widget, |
| + bool active) { |
| + if (!active) |
| + tray_->HideBubble(); |
| +} |
| + |
| +void MessageCenterWidgetDelegate::PreferredSizeChanged() { |
| + GetWidget()->SetBounds(GetBubbleBounds()); |
| + views::View::PreferredSizeChanged(); |
| +} |
| + |
| +gfx::Size MessageCenterWidgetDelegate::GetPreferredSize() { |
| + gfx::Size size = |
| + gfx::Size(preferred_width_, GetHeightForWidth(preferred_width_)); |
| + |
| + // Make space for borders on sides. |
| + size.Enlarge(2 * kBorderWidth, 2 * kBorderWidth); |
| + return size; |
| +} |
| + |
| +gfx::Size MessageCenterWidgetDelegate::GetMaximumSize() { |
| + gfx::Size size = GetPreferredSize(); |
| + size.set_width(preferred_width_); |
|
dewittj
2013/07/02 18:43:32
This is smaller than the preferred size above. Sh
sidharthms
2013/07/02 21:01:16
Done.
|
| + return size; |
| +} |
| + |
| +int MessageCenterWidgetDelegate::GetHeightForWidth(int width) { |
| + int height = MessageCenterView::GetHeightForWidth(width); |
| + return (pos_info_.max_height != 0) ? std::min(height, pos_info_.max_height) |
| + : height; |
| +} |
| +bool MessageCenterWidgetDelegate::AcceleratorPressed( |
| + const ui::Accelerator& accelerator) { |
|
dewittj
2013/07/02 18:43:32
Not sure, do we need to pass these through to the
sidharthms
2013/07/02 21:01:16
MessageCenterView doesn't seem to handle these...
|
| + if (accelerator.key_code() != ui::VKEY_ESCAPE) |
| + return false; |
| + GetWidget()->Close(); |
| + return true; |
| +} |
| + |
| +void MessageCenterWidgetDelegate::OnNativeThemeChanged( |
| + const ui::NativeTheme* theme) { |
| + UpdateColorsFromTheme(theme); |
| +} |
| + |
| +void MessageCenterWidgetDelegate::InitWidget() { |
| + views::Widget* widget = new views::Widget(); |
| + views::Widget::InitParams params(views::Widget::InitParams::TYPE_BUBBLE); |
| + params.delegate = this; |
| + params.keep_on_top = true; |
| + params.top_level = true; |
| +#if defined(OS_WIN) && !defined(USE_AURA) |
| + params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
| +#endif |
| + widget->Init(params); |
| + |
| + widget->AddObserver(this); |
| + widget->StackAtTop(); |
| + |
| + // Popups should appear on top of everything, but not disturb the user's |
| + // focus since they could appear at any time. Message Center is always |
| + // shown as a result of user action so it can be activated here. |
| + widget->SetAlwaysOnTop(true); |
| + widget->Activate(); |
| +} |
| + |
| +void MessageCenterWidgetDelegate::CloseWidget() { |
| + GetWidget()->Close(); |
| +} |
| + |
| +void MessageCenterWidgetDelegate::UpdateColorsFromTheme( |
| + const ui::NativeTheme* theme) { |
| + color_ = GetNativeTheme()->GetSystemColor( |
| + ui::NativeTheme::kColorId_WindowBackground); |
| + set_background(views::Background::CreateSolidBackground(color_)); |
| +} |
| + |
| +void MessageCenterWidgetDelegate::UpdateNotifications() { |
| + const NotificationList::Notifications& notifications = |
| + tray_->message_center()->GetNotifications(); |
| + SetNotifications(notifications); |
| + GetWidget()->Show(); |
| + GetWidget()->SetBounds(GetBubbleBounds()); |
|
dewittj
2013/07/02 18:43:32
Seems like it would make more sense to set the bou
sidharthms
2013/07/02 21:01:16
Done.
|
| + GetWidget()->GetRootView()->SchedulePaint(); |
|
dewittj
2013/07/02 18:43:32
Does it not paint the widget when you show it?
sidharthms
2013/07/02 21:01:16
Done.
|
| +} |
| + |
| +MessageCenterFrameView* MessageCenterWidgetDelegate::GetBubbleFrameView() |
| + const { |
| + const views::Widget* widget = GetWidget(); |
| + const views::NonClientView* view = widget ? widget->non_client_view() : NULL; |
| + return view ? static_cast<MessageCenterFrameView*>(view->frame_view()) : NULL; |
| +} |
| + |
| +gfx::Rect MessageCenterWidgetDelegate::GetBubbleBounds() { |
| + const gfx::Size size = GetPreferredSize(); |
| + gfx::Rect bounds(size); |
| + |
| + gfx::Point corrected_anchor = GetCorrectedAnchor(size); |
| + |
| + if (pos_info_.bubble_alignment & ALIGNMENT_TOP) |
| + bounds.set_y(corrected_anchor.y()); |
| + if (pos_info_.bubble_alignment & ALIGNMENT_BOTTOM) |
| + bounds.set_y(corrected_anchor.y() - size.height()); |
| + if (pos_info_.bubble_alignment & ALIGNMENT_LEFT) |
| + bounds.set_x(corrected_anchor.x()); |
| + if (pos_info_.bubble_alignment & ALIGNMENT_RIGHT) |
| + bounds.set_x(corrected_anchor.x() - size.width()); |
| + |
| + return bounds; |
| +} |
| + |
| +gfx::Point MessageCenterWidgetDelegate::GetCorrectedAnchor( |
| + gfx::Size calculated_size) { |
| + gfx::Point corrected_anchor = pos_info_.inital_anchor_point; |
| + |
| + // Inset the width slightly so that the click point is not exactly on the edge |
| + // of the message center but somewhere within the middle 60 %. |
| + int insetted_width = (calculated_size.width() * 4) / 5; |
| + |
| + if (pos_info_.systray_alignment == ALIGNMENT_TOP || |
| + pos_info_.systray_alignment == ALIGNMENT_BOTTOM) { |
| + int click_point_x = tray_->mouse_click_point().x(); |
| + |
| + if (pos_info_.bubble_alignment & ALIGNMENT_RIGHT) { |
| + int opposite_x_corner = |
| + pos_info_.inital_anchor_point.x() - insetted_width; |
| + |
| + // If the click point is outside the x axis length of the message center, |
| + // push the message center towards the LEFT to align with the click point. |
| + if (opposite_x_corner > click_point_x) |
| + corrected_anchor.set_x(pos_info_.inital_anchor_point.x() - |
| + (opposite_x_corner - click_point_x)); |
| + } else { |
| + int opposite_x_corner = |
| + pos_info_.inital_anchor_point.x() + insetted_width; |
| + |
| + if (opposite_x_corner < click_point_x) |
| + corrected_anchor.set_x(pos_info_.inital_anchor_point.x() + |
| + (click_point_x - opposite_x_corner)); |
| + } |
| + } else if (pos_info_.systray_alignment == ALIGNMENT_LEFT || |
| + pos_info_.systray_alignment == ALIGNMENT_RIGHT) { |
| + int click_point_y = tray_->mouse_click_point().y(); |
| + |
| + if (pos_info_.bubble_alignment & ALIGNMENT_BOTTOM) { |
| + int opposite_y_corner = |
| + pos_info_.inital_anchor_point.y() - insetted_width; |
| + |
| + // If the click point is outside the y axis length of the message center, |
| + // push the message center UPWARDS to align with the click point. |
| + if (opposite_y_corner > click_point_y) |
| + corrected_anchor.set_y(pos_info_.inital_anchor_point.y() - |
| + (opposite_y_corner - click_point_y)); |
| + } else { |
| + int opposite_y_corner = |
| + pos_info_.inital_anchor_point.y() + insetted_width; |
| + |
| + if (opposite_y_corner < click_point_y) |
| + corrected_anchor.set_y(pos_info_.inital_anchor_point.y() + |
| + (click_point_y - opposite_y_corner)); |
| + } |
| + } |
| + return corrected_anchor; |
| +} |
| + |
| +} // namespace message_center |