Chromium Code Reviews| 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 "base/bind.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "chrome/common/pref_names.h" | |
|
Peter Kasting
2012/07/26 01:36:13
Nit: Alphabetical order
Kyle Horimoto
2012/08/03 03:33:29
Done.
| |
| 10 #include "chrome/browser/prefs/pref_service.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
| 13 #include "chrome/browser/ui/zoom/zoom_controller.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 #include "content/public/browser/render_view_host.h" | |
| 9 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 10 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 11 #include "ui/views/layout/fill_layout.h" | 18 #include "ui/views/controls/separator.h" |
| 19 #include "ui/views/layout/box_layout.h" | |
| 20 #include "ui/views/layout/layout_constants.h" | |
| 12 | 21 |
| 13 namespace { | 22 namespace { |
| 14 | 23 |
| 15 // The number of milliseconds the bubble should stay on the screen for if it | 24 // The number of milliseconds the bubble should stay on the screen for if it |
|
Peter Kasting
2012/07/26 01:36:13
Nit: Removing "for" makes this sentence read bette
Kyle Horimoto
2012/08/03 03:33:29
Done.
| |
| 16 // will automatically close. | 25 // will automatically close. |
| 17 const int kBubbleCloseDelay = 400; | 26 const int kBubbleCloseDelay = 1500; |
| 27 | |
| 28 // The number of pixels between the separator and the button. | |
| 29 const int kSeparatorButtonSpacing = 2; | |
| 30 | |
| 31 // The font size of the percentage label. | |
|
Peter Kasting
2012/07/26 01:36:13
Nit: How about: "How many pixels larger the percen
Kyle Horimoto
2012/08/03 03:33:29
Done.
| |
| 32 const int kPercentageFontIncrease = 5; | |
| 18 | 33 |
| 19 } | 34 } |
| 20 | 35 |
| 21 // static | 36 // static |
| 22 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = NULL; | 37 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = NULL; |
| 23 | 38 |
| 24 // static | 39 // static |
| 25 void ZoomBubbleView::ShowBubble(views::View* anchor_view, | 40 void ZoomBubbleView::ShowBubble(views::View* anchor_view, |
| 26 int zoom_percent, | 41 TabContents* tab_contents, |
| 27 bool auto_close) { | 42 bool auto_close) { |
| 28 // If the bubble is already showing in this window and its |auto_close_| value | 43 // 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 | 44 // is equal to |auto_close|, the bubble can be reused and only the label text |
| 30 // needs to be updated. | 45 // needs to be updated. |
| 31 if (zoom_bubble_ && | 46 if (zoom_bubble_ && |
| 32 zoom_bubble_->anchor_view() == anchor_view && | 47 zoom_bubble_->anchor_view() == anchor_view && |
| 33 zoom_bubble_->auto_close_ == auto_close) { | 48 zoom_bubble_->auto_close_ == auto_close) { |
| 34 zoom_bubble_->label_->SetText( | 49 zoom_bubble_->Refresh(); |
| 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 { | 50 } else { |
| 44 // If the bubble is already showing but its |auto_close_| value is not equal | 51 // 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 | 52 // to |auto_close|, the bubble's focus properties must change, so the |
| 46 // current bubble must be closed and a new one created. | 53 // current bubble must be closed and a new one created. |
| 47 if (zoom_bubble_) | 54 if (zoom_bubble_) |
| 48 zoom_bubble_->Close(); | 55 zoom_bubble_->Close(); |
| 49 | 56 |
| 50 zoom_bubble_ = new ZoomBubbleView(anchor_view, zoom_percent, auto_close); | 57 zoom_bubble_ = new ZoomBubbleView(anchor_view, tab_contents, auto_close); |
| 51 views::BubbleDelegateView::CreateBubble(zoom_bubble_); | 58 views::BubbleDelegateView::CreateBubble(zoom_bubble_); |
| 52 zoom_bubble_->Show(); | 59 zoom_bubble_->Show(); |
| 53 } | 60 } |
| 54 } | 61 } |
| 55 | 62 |
| 56 // static | 63 // static |
| 57 void ZoomBubbleView::CloseBubble() { | 64 void ZoomBubbleView::CloseBubble() { |
| 58 if (zoom_bubble_) | 65 if (zoom_bubble_) |
| 59 zoom_bubble_->Close(); | 66 zoom_bubble_->Close(); |
| 60 } | 67 } |
| 61 | 68 |
| 62 // static | 69 // static |
| 63 bool ZoomBubbleView::IsShowing() { | 70 bool ZoomBubbleView::IsShowing() { |
| 64 return zoom_bubble_ != NULL; | 71 return zoom_bubble_ != NULL; |
| 65 } | 72 } |
| 66 | 73 |
| 67 ZoomBubbleView::ZoomBubbleView(views::View* anchor_view, | 74 ZoomBubbleView::ZoomBubbleView(views::View* anchor_view, |
| 68 int zoom_percent, | 75 TabContents* tab_contents, |
| 69 bool auto_close) | 76 bool auto_close) |
| 70 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT), | 77 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT), |
| 71 label_(NULL), | 78 label_(NULL), |
| 72 zoom_percent_(zoom_percent), | 79 tab_contents_(tab_contents), |
| 73 auto_close_(auto_close) { | 80 auto_close_(auto_close) { |
| 74 set_use_focusless(auto_close); | 81 set_use_focusless(auto_close); |
| 75 } | 82 } |
| 76 | 83 |
| 77 ZoomBubbleView::~ZoomBubbleView() { | 84 ZoomBubbleView::~ZoomBubbleView() { |
| 78 } | 85 } |
| 79 | 86 |
| 87 void ZoomBubbleView::Refresh() { | |
| 88 int zoom_percent = tab_contents_->zoom_controller()->zoom_percent(); | |
| 89 label_->SetText( | |
| 90 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); | |
| 91 | |
| 92 if (auto_close_) { | |
| 93 // If the bubble should be closed automatically, reset the timer so that | |
| 94 // it will show for the full amount of time instead of only what remained | |
| 95 // from the previous time. | |
| 96 timer_.Reset(); | |
| 97 } | |
| 98 } | |
| 99 | |
| 80 void ZoomBubbleView::Close() { | 100 void ZoomBubbleView::Close() { |
| 81 GetWidget()->Close(); | 101 GetWidget()->Close(); |
| 82 } | 102 } |
| 83 | 103 |
| 84 void ZoomBubbleView::Init() { | 104 void ZoomBubbleView::StartTimerIfNecessary() { |
| 85 SetLayoutManager(new views::FillLayout()); | |
| 86 label_ = new views::Label( | |
| 87 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, zoom_percent_)); | |
| 88 AddChildView(label_); | |
| 89 | |
| 90 if (auto_close_) { | 105 if (auto_close_) { |
| 91 timer_.Start( | 106 timer_.Start( |
| 92 FROM_HERE, | 107 FROM_HERE, |
| 93 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), | 108 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), |
| 94 this, | 109 this, |
| 95 &ZoomBubbleView::Close); | 110 &ZoomBubbleView::Close); |
| 96 } | 111 } |
| 97 } | 112 } |
| 98 | 113 |
| 114 void ZoomBubbleView::StopTimer() { | |
| 115 timer_.Stop(); | |
| 116 } | |
| 117 | |
| 118 void ZoomBubbleView::OnMouseMoved(const views::MouseEvent& event) { | |
| 119 StopTimer(); | |
| 120 } | |
| 121 | |
| 122 void ZoomBubbleView::OnMouseExited(const views::MouseEvent& event) { | |
| 123 StartTimerIfNecessary(); | |
| 124 } | |
| 125 | |
| 126 void ZoomBubbleView::ButtonPressed(views::Button* sender, | |
| 127 const views::Event& event) { | |
| 128 double default_zoom_level = | |
|
Peter Kasting
2012/07/26 01:36:14
It seems like this whole function call can be repl
Kyle Horimoto
2012/08/03 03:33:29
Done. Fix for that function in an upcoming CL.
| |
| 129 Profile::FromBrowserContext(tab_contents_->profile())-> | |
| 130 GetPrefs()->GetDouble(prefs::kDefaultZoomLevel); | |
| 131 tab_contents_->web_contents()->GetRenderViewHost()-> | |
| 132 SetZoomLevel(default_zoom_level); | |
| 133 } | |
| 134 | |
| 135 void ZoomBubbleView::Init() { | |
| 136 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, | |
| 137 0, 0, views::kRelatedControlVerticalSpacing)); | |
| 138 | |
| 139 int zoom_percent = tab_contents_->zoom_controller()->zoom_percent(); | |
| 140 label_ = new views::Label( | |
| 141 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); | |
| 142 gfx::Font font = label_->font().DeriveFont(kPercentageFontIncrease); | |
| 143 label_->SetFont(font); | |
| 144 AddChildView(label_); | |
| 145 | |
| 146 AddChildView(new views::Separator()); | |
| 147 | |
| 148 ZoomBubbleViewTextButton* set_default_button = new ZoomBubbleViewTextButton( | |
| 149 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT)); | |
| 150 set_default_button->set_alignment(views::TextButtonBase::ALIGN_CENTER); | |
| 151 AddChildView(set_default_button); | |
| 152 | |
| 153 StartTimerIfNecessary(); | |
| 154 } | |
| 155 | |
| 156 gfx::Rect ZoomBubbleView::GetAnchorRect() { | |
| 157 // Compensate for some built-in padding in the zoom image. | |
| 158 gfx::Rect rect(BubbleDelegateView::GetAnchorRect()); | |
| 159 rect.Inset(0, anchor_view() ? 5 : 0); | |
| 160 return rect; | |
| 161 } | |
| 162 | |
| 99 void ZoomBubbleView::WindowClosing() { | 163 void ZoomBubbleView::WindowClosing() { |
| 100 DCHECK(zoom_bubble_ == this); | 164 DCHECK(zoom_bubble_ == this); |
| 101 zoom_bubble_ = NULL; | 165 zoom_bubble_ = NULL; |
| 102 } | 166 } |
| 167 | |
| 168 ZoomBubbleView::ZoomBubbleViewTextButton::ZoomBubbleViewTextButton( | |
| 169 ZoomBubbleView* listener, const string16& text) : | |
|
Peter Kasting
2012/07/26 01:36:14
Nit: One arg per line; ':' goes on next line (befo
Kyle Horimoto
2012/08/03 03:33:29
Done.
| |
| 170 views::TextButton(listener, text), | |
| 171 delegate_(listener) {} | |
| 172 | |
| 173 void ZoomBubbleView::ZoomBubbleViewTextButton::OnMouseMoved( | |
| 174 const views::MouseEvent& event) { | |
| 175 views::TextButton::OnMouseMoved(event); | |
| 176 delegate_->StopTimer(); | |
| 177 } | |
| 178 | |
| 179 void ZoomBubbleView::ZoomBubbleViewTextButton::OnMouseExited( | |
| 180 const views::MouseEvent& event) { | |
| 181 views::TextButton::OnMouseExited(event); | |
|
Peter Kasting
2012/07/26 01:36:14
This doesn't seem right? If we exit from the butt
Kyle Horimoto
2012/08/03 03:33:29
Done.
| |
| 182 delegate_->StartTimerIfNecessary(); | |
| 183 } | |
| OLD | NEW |