OLD | NEW |
(Empty) | |
| 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 "chrome/browser/ui/views/location_bar/zoom_bubble_view.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "grit/generated_resources.h" |
| 10 #include "ui/base/l10n/l10n_util.h" |
| 11 #include "ui/views/layout/fill_layout.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // The number of milliseconds the bubble should stay on the screen for if it |
| 16 // will automatically close. |
| 17 const int kBubbleCloseDelay = 400; |
| 18 |
| 19 } |
| 20 |
| 21 // static |
| 22 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = NULL; |
| 23 |
| 24 // static |
| 25 void ZoomBubbleView::ShowBubble(views::View* anchor_view, |
| 26 int zoom_percent, |
| 27 bool auto_close) { |
| 28 // If the bubble is already showing in this window and its |auto_close_| value |
| 29 // is equal to |auto_close|, the bubble can be reused and only the label text |
| 30 // needs to be updated. |
| 31 if (zoom_bubble_ && |
| 32 zoom_bubble_->anchor_view() == anchor_view && |
| 33 zoom_bubble_->auto_close_ == auto_close) { |
| 34 zoom_bubble_->label_->SetText( |
| 35 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, zoom_percent)); |
| 36 |
| 37 if (auto_close) { |
| 38 // If the bubble should be closed automatically, reset the timer so that |
| 39 // it will show for the full amount of time instead of only what remained |
| 40 // from the previous time. |
| 41 zoom_bubble_->timer_.Reset(); |
| 42 } |
| 43 } else { |
| 44 // If the bubble is already showing but its |auto_close_| value is not equal |
| 45 // to |auto_close|, the bubble's focus properties must change, so the |
| 46 // current bubble must be closed and a new one created. |
| 47 if (zoom_bubble_) |
| 48 zoom_bubble_->Close(); |
| 49 |
| 50 zoom_bubble_ = new ZoomBubbleView(anchor_view, zoom_percent, auto_close); |
| 51 views::BubbleDelegateView::CreateBubble(zoom_bubble_); |
| 52 zoom_bubble_->Show(); |
| 53 } |
| 54 } |
| 55 |
| 56 // static |
| 57 void ZoomBubbleView::CloseBubble() { |
| 58 if (zoom_bubble_) |
| 59 zoom_bubble_->Close(); |
| 60 } |
| 61 |
| 62 // static |
| 63 bool ZoomBubbleView::IsShowing() { |
| 64 return zoom_bubble_ != NULL; |
| 65 } |
| 66 |
| 67 void ZoomBubbleView::WindowClosing() { |
| 68 DCHECK(zoom_bubble_ == this); |
| 69 zoom_bubble_ = NULL; |
| 70 } |
| 71 |
| 72 void ZoomBubbleView::Init() { |
| 73 SetLayoutManager(new views::FillLayout()); |
| 74 label_ = new views::Label( |
| 75 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, zoom_percent_)); |
| 76 AddChildView(label_); |
| 77 |
| 78 if (auto_close_) { |
| 79 timer_.Start( |
| 80 FROM_HERE, |
| 81 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), |
| 82 this, |
| 83 &ZoomBubbleView::Close); |
| 84 } |
| 85 } |
| 86 |
| 87 ZoomBubbleView::ZoomBubbleView(views::View* anchor_view, |
| 88 int zoom_percent, |
| 89 bool auto_close) |
| 90 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT), |
| 91 zoom_percent_(zoom_percent), |
| 92 auto_close_(auto_close) { |
| 93 set_use_focusless(auto_close); |
| 94 } |
| 95 |
| 96 ZoomBubbleView::~ZoomBubbleView() { |
| 97 } |
| 98 |
| 99 void ZoomBubbleView::Close() { |
| 100 GetWidget()->Close(); |
| 101 } |
OLD | NEW |