Home | History | Annotate | Download | only in views
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "ui/message_center/views/message_bubble_base.h"
      6 
      7 #include "base/bind.h"
      8 #include "ui/message_center/message_center_style.h"
      9 #include "ui/message_center/message_center_util.h"
     10 #include "ui/views/widget/widget.h"
     11 #include "ui/views/widget/widget_observer.h"
     12 
     13 namespace {
     14 // Delay laying out the MessageBubbleBase until all notifications have been
     15 // added and icons have had a chance to load.
     16 const int kUpdateDelayMs = 50;
     17 const int kMessageBubbleBaseDefaultMaxHeight = 400;
     18 }
     19 
     20 namespace message_center {
     21 
     22 const SkColor MessageBubbleBase::kBackgroundColor =
     23     SkColorSetRGB(0xfe, 0xfe, 0xfe);
     24 
     25 MessageBubbleBase::MessageBubbleBase(MessageCenter* message_center,
     26                                      MessageCenterTray* tray)
     27     : message_center_(message_center),
     28       tray_(tray),
     29       bubble_view_(NULL),
     30       weak_ptr_factory_(this),
     31       max_height_(kMessageBubbleBaseDefaultMaxHeight) {
     32 }
     33 
     34 MessageBubbleBase::~MessageBubbleBase() {
     35   if (bubble_view_)
     36     bubble_view_->reset_delegate();
     37 }
     38 
     39 void MessageBubbleBase::BubbleViewDestroyed() {
     40   bubble_view_ = NULL;
     41   OnBubbleViewDestroyed();
     42 }
     43 
     44 void MessageBubbleBase::ScheduleUpdate() {
     45   weak_ptr_factory_.InvalidateWeakPtrs();  // Cancel any pending update.
     46   base::MessageLoop::current()->PostDelayedTask(
     47       FROM_HERE,
     48       base::Bind(&MessageBubbleBase::UpdateBubbleView,
     49                  weak_ptr_factory_.GetWeakPtr()),
     50       base::TimeDelta::FromMilliseconds(kUpdateDelayMs));
     51 }
     52 
     53 bool MessageBubbleBase::IsVisible() const {
     54   return bubble_view() && bubble_view()->GetWidget()->IsVisible();
     55 }
     56 
     57 void MessageBubbleBase::SetMaxHeight(int height) {
     58   // Maximum height makes sense only for the new design.
     59   if (height == 0)
     60     height = kMessageBubbleBaseDefaultMaxHeight;
     61   if (height == max_height_)
     62     return;
     63 
     64   max_height_ = height;
     65   if (bubble_view_)
     66     bubble_view_->SetMaxHeight(max_height_);
     67 }
     68 
     69 views::TrayBubbleView::InitParams MessageBubbleBase::GetDefaultInitParams(
     70     views::TrayBubbleView::AnchorAlignment anchor_alignment) {
     71   views::TrayBubbleView::InitParams init_params(
     72       views::TrayBubbleView::ANCHOR_TYPE_TRAY,
     73       anchor_alignment,
     74       kNotificationWidth,
     75       kNotificationWidth);
     76   init_params.arrow_color = kBackgroundDarkColor;
     77   return init_params;
     78 }
     79 
     80 }  // namespace message_center
     81