| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h" | 5 #include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "chrome/browser/chrome_page_zoom.h" |
| 8 #include "base/message_loop.h" | 8 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 9 #include "chrome/browser/ui/zoom/zoom_controller.h" |
| 9 #include "grit/generated_resources.h" | 10 #include "grit/generated_resources.h" |
| 10 #include "ui/base/l10n/l10n_util.h" | 11 #include "ui/base/l10n/l10n_util.h" |
| 11 #include "ui/views/layout/fill_layout.h" | 12 #include "ui/views/controls/separator.h" |
| 13 #include "ui/views/layout/box_layout.h" |
| 14 #include "ui/views/layout/layout_constants.h" |
| 12 #include "ui/views/widget/widget.h" | 15 #include "ui/views/widget/widget.h" |
| 13 | 16 |
| 14 namespace { | 17 namespace { |
| 15 | 18 |
| 16 // The number of milliseconds the bubble should stay on the screen for if it | 19 // The number of milliseconds the bubble should stay on the screen if it will |
| 17 // will automatically close. | 20 // close automatically. |
| 18 const int kBubbleCloseDelay = 400; | 21 const int kBubbleCloseDelay = 1500; |
| 22 |
| 23 // The number of pixels between the separator and the button. |
| 24 const int kSeparatorButtonSpacing = 2; |
| 25 |
| 26 // How many pixels larger the percentage label font should be, compared to the |
| 27 // default font. |
| 28 const int kPercentageFontIncrease = 5; |
| 19 | 29 |
| 20 } // namespace | 30 } // namespace |
| 21 | 31 |
| 22 // static | 32 // static |
| 23 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = NULL; | 33 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = NULL; |
| 24 | 34 |
| 25 // static | 35 // static |
| 26 void ZoomBubbleView::ShowBubble(views::View* anchor_view, | 36 void ZoomBubbleView::ShowBubble(views::View* anchor_view, |
| 27 int zoom_percent, | 37 TabContents* tab_contents, |
| 28 bool auto_close) { | 38 bool auto_close) { |
| 29 // If the bubble is already showing in this window and its |auto_close_| value | 39 // If the bubble is already showing in this window and its |auto_close_| value |
| 30 // is equal to |auto_close|, the bubble can be reused and only the label text | 40 // is equal to |auto_close|, the bubble can be reused and only the label text |
| 31 // needs to be updated. | 41 // needs to be updated. |
| 32 if (zoom_bubble_ && | 42 if (zoom_bubble_ && |
| 33 zoom_bubble_->anchor_view() == anchor_view && | 43 zoom_bubble_->anchor_view() == anchor_view && |
| 34 zoom_bubble_->auto_close_ == auto_close) { | 44 zoom_bubble_->auto_close_ == auto_close) { |
| 35 zoom_bubble_->label_->SetText( | 45 zoom_bubble_->Refresh(); |
| 36 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, zoom_percent)); | |
| 37 | |
| 38 if (auto_close) { | |
| 39 // If the bubble should be closed automatically, reset the timer so that | |
| 40 // it will show for the full amount of time instead of only what remained | |
| 41 // from the previous time. | |
| 42 zoom_bubble_->timer_.Reset(); | |
| 43 } | |
| 44 } else { | 46 } else { |
| 45 // If the bubble is already showing but its |auto_close_| value is not equal | 47 // If the bubble is already showing but its |auto_close_| value is not equal |
| 46 // to |auto_close|, the bubble's focus properties must change, so the | 48 // to |auto_close|, the bubble's focus properties must change, so the |
| 47 // current bubble must be closed and a new one created. | 49 // current bubble must be closed and a new one created. |
| 48 if (zoom_bubble_) | 50 if (zoom_bubble_) |
| 49 zoom_bubble_->Close(); | 51 zoom_bubble_->Close(); |
| 50 | 52 |
| 51 zoom_bubble_ = new ZoomBubbleView(anchor_view, zoom_percent, auto_close); | 53 zoom_bubble_ = new ZoomBubbleView(anchor_view, tab_contents, auto_close); |
| 52 views::BubbleDelegateView::CreateBubble(zoom_bubble_); | 54 views::BubbleDelegateView::CreateBubble(zoom_bubble_); |
| 53 zoom_bubble_->Show(); | 55 zoom_bubble_->Show(); |
| 54 } | 56 } |
| 55 } | 57 } |
| 56 | 58 |
| 57 // static | 59 // static |
| 58 void ZoomBubbleView::CloseBubble() { | 60 void ZoomBubbleView::CloseBubble() { |
| 59 if (zoom_bubble_) | 61 if (zoom_bubble_) |
| 60 zoom_bubble_->Close(); | 62 zoom_bubble_->Close(); |
| 61 } | 63 } |
| 62 | 64 |
| 63 // static | 65 // static |
| 64 bool ZoomBubbleView::IsShowing() { | 66 bool ZoomBubbleView::IsShowing() { |
| 65 return zoom_bubble_ != NULL; | 67 return zoom_bubble_ != NULL; |
| 66 } | 68 } |
| 67 | 69 |
| 68 ZoomBubbleView::ZoomBubbleView(views::View* anchor_view, | 70 ZoomBubbleView::ZoomBubbleView(views::View* anchor_view, |
| 69 int zoom_percent, | 71 TabContents* tab_contents, |
| 70 bool auto_close) | 72 bool auto_close) |
| 71 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT), | 73 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT), |
| 72 label_(NULL), | 74 label_(NULL), |
| 73 zoom_percent_(zoom_percent), | 75 tab_contents_(tab_contents), |
| 74 auto_close_(auto_close) { | 76 auto_close_(auto_close) { |
| 75 set_use_focusless(auto_close); | 77 set_use_focusless(auto_close); |
| 78 set_notify_enter_exit_on_child(true); |
| 76 } | 79 } |
| 77 | 80 |
| 78 ZoomBubbleView::~ZoomBubbleView() { | 81 ZoomBubbleView::~ZoomBubbleView() { |
| 79 } | 82 } |
| 80 | 83 |
| 84 void ZoomBubbleView::Refresh() { |
| 85 int zoom_percent = tab_contents_->zoom_controller()->zoom_percent(); |
| 86 label_->SetText( |
| 87 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); |
| 88 |
| 89 if (auto_close_) { |
| 90 // If the bubble should be closed automatically, reset the timer so that |
| 91 // it will show for the full amount of time instead of only what remained |
| 92 // from the previous time. |
| 93 timer_.Reset(); |
| 94 } |
| 95 } |
| 96 |
| 81 void ZoomBubbleView::Close() { | 97 void ZoomBubbleView::Close() { |
| 82 GetWidget()->Close(); | 98 GetWidget()->Close(); |
| 83 } | 99 } |
| 84 | 100 |
| 85 void ZoomBubbleView::Init() { | 101 void ZoomBubbleView::StartTimerIfNecessary() { |
| 86 SetLayoutManager(new views::FillLayout()); | |
| 87 label_ = new views::Label( | |
| 88 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, zoom_percent_)); | |
| 89 AddChildView(label_); | |
| 90 | |
| 91 if (auto_close_) { | 102 if (auto_close_) { |
| 92 timer_.Start( | 103 timer_.Start( |
| 93 FROM_HERE, | 104 FROM_HERE, |
| 94 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), | 105 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), |
| 95 this, | 106 this, |
| 96 &ZoomBubbleView::Close); | 107 &ZoomBubbleView::Close); |
| 97 } | 108 } |
| 98 } | 109 } |
| 99 | 110 |
| 111 void ZoomBubbleView::StopTimer() { |
| 112 timer_.Stop(); |
| 113 } |
| 114 |
| 115 void ZoomBubbleView::OnMouseEntered(const views::MouseEvent& event) { |
| 116 StopTimer(); |
| 117 } |
| 118 |
| 119 void ZoomBubbleView::OnMouseExited(const views::MouseEvent& event) { |
| 120 StartTimerIfNecessary(); |
| 121 } |
| 122 |
| 123 void ZoomBubbleView::ButtonPressed(views::Button* sender, |
| 124 const views::Event& event) { |
| 125 chrome_page_zoom::Zoom(tab_contents_->web_contents(), |
| 126 content::PAGE_ZOOM_RESET); |
| 127 } |
| 128 |
| 129 void ZoomBubbleView::Init() { |
| 130 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, |
| 131 0, 0, views::kRelatedControlVerticalSpacing)); |
| 132 |
| 133 int zoom_percent = tab_contents_->zoom_controller()->zoom_percent(); |
| 134 label_ = new views::Label( |
| 135 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); |
| 136 gfx::Font font = label_->font().DeriveFont(kPercentageFontIncrease); |
| 137 label_->SetFont(font); |
| 138 AddChildView(label_); |
| 139 |
| 140 AddChildView(new views::Separator()); |
| 141 |
| 142 views::TextButton* set_default_button = new views::TextButton( |
| 143 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT)); |
| 144 set_default_button->set_alignment(views::TextButtonBase::ALIGN_CENTER); |
| 145 AddChildView(set_default_button); |
| 146 |
| 147 StartTimerIfNecessary(); |
| 148 } |
| 149 |
| 150 gfx::Rect ZoomBubbleView::GetAnchorRect() { |
| 151 // Compensate for some built-in padding in the zoom image. |
| 152 gfx::Rect rect(BubbleDelegateView::GetAnchorRect()); |
| 153 rect.Inset(0, anchor_view() ? 5 : 0); |
| 154 return rect; |
| 155 } |
| 156 |
| 100 void ZoomBubbleView::WindowClosing() { | 157 void ZoomBubbleView::WindowClosing() { |
| 101 DCHECK(zoom_bubble_ == this); | 158 DCHECK(zoom_bubble_ == this); |
| 102 zoom_bubble_ = NULL; | 159 zoom_bubble_ = NULL; |
| 103 } | 160 } |
| OLD | NEW |