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" |
| 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/button/text_button.h" |
| 19 #include "ui/views/controls/separator.h" |
| 20 #include "ui/views/layout/box_layout.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 |
16 // will automatically close. | 25 // will automatically close. |
17 const int kBubbleCloseDelay = 400; | 26 const int kBubbleCloseDelay = 400; |
18 | 27 |
| 28 // The number of pixels between the label and the separator and the separator |
| 29 // and the button. |
| 30 const int kVerticalSpacing = 5; |
| 31 |
| 32 // The font size of the percentage label. |
| 33 const int kPercentageFontSize = 20; |
| 34 |
19 } | 35 } |
20 | 36 |
21 // static | 37 // static |
22 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = NULL; | 38 ZoomBubbleView* ZoomBubbleView::zoom_bubble_ = NULL; |
23 | 39 |
24 // static | 40 // static |
25 void ZoomBubbleView::ShowBubble(views::View* anchor_view, | 41 void ZoomBubbleView::ShowBubble(views::View* anchor_view, |
26 int zoom_percent, | 42 TabContents* tab_contents, |
27 bool auto_close) { | 43 bool auto_close) { |
28 // If the bubble is already showing in this window and its |auto_close_| value | 44 // 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 | 45 // is equal to |auto_close|, the bubble can be reused and only the label text |
30 // needs to be updated. | 46 // needs to be updated. |
31 if (zoom_bubble_ && | 47 if (zoom_bubble_ && |
32 zoom_bubble_->anchor_view() == anchor_view && | 48 zoom_bubble_->anchor_view() == anchor_view && |
33 zoom_bubble_->auto_close_ == auto_close) { | 49 zoom_bubble_->auto_close_ == auto_close) { |
34 zoom_bubble_->label_->SetText( | 50 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 { | 51 } else { |
44 // If the bubble is already showing but its |auto_close_| value is not equal | 52 // 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 | 53 // to |auto_close|, the bubble's focus properties must change, so the |
46 // current bubble must be closed and a new one created. | 54 // current bubble must be closed and a new one created. |
47 if (zoom_bubble_) | 55 if (zoom_bubble_) |
48 zoom_bubble_->Close(); | 56 zoom_bubble_->Close(); |
49 | 57 |
50 zoom_bubble_ = new ZoomBubbleView(anchor_view, zoom_percent, auto_close); | 58 zoom_bubble_ = new ZoomBubbleView(anchor_view, tab_contents, auto_close); |
51 views::BubbleDelegateView::CreateBubble(zoom_bubble_); | 59 views::BubbleDelegateView::CreateBubble(zoom_bubble_); |
52 zoom_bubble_->Show(); | 60 zoom_bubble_->Show(); |
53 } | 61 } |
54 } | 62 } |
55 | 63 |
56 // static | 64 // static |
57 void ZoomBubbleView::CloseBubble() { | 65 void ZoomBubbleView::CloseBubble() { |
58 if (zoom_bubble_) | 66 if (zoom_bubble_) |
59 zoom_bubble_->Close(); | 67 zoom_bubble_->Close(); |
60 } | 68 } |
61 | 69 |
62 // static | 70 // static |
63 bool ZoomBubbleView::IsShowing() { | 71 bool ZoomBubbleView::IsShowing() { |
64 return zoom_bubble_ != NULL; | 72 return zoom_bubble_ != NULL; |
65 } | 73 } |
66 | 74 |
67 ZoomBubbleView::ZoomBubbleView(views::View* anchor_view, | 75 ZoomBubbleView::ZoomBubbleView(views::View* anchor_view, |
68 int zoom_percent, | 76 TabContents* tab_contents, |
69 bool auto_close) | 77 bool auto_close) |
70 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT), | 78 : BubbleDelegateView(anchor_view, views::BubbleBorder::TOP_RIGHT), |
71 label_(NULL), | 79 label_(NULL), |
72 zoom_percent_(zoom_percent), | 80 tab_contents_(tab_contents), |
73 auto_close_(auto_close) { | 81 auto_close_(auto_close) { |
74 set_use_focusless(auto_close); | 82 set_use_focusless(auto_close); |
75 } | 83 } |
76 | 84 |
77 ZoomBubbleView::~ZoomBubbleView() { | 85 ZoomBubbleView::~ZoomBubbleView() { |
78 } | 86 } |
79 | 87 |
| 88 void ZoomBubbleView::Refresh() { |
| 89 int zoom_percent = tab_contents_->zoom_controller()->zoom_percent(); |
| 90 label_->SetText( |
| 91 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, zoom_percent)); |
| 92 |
| 93 if (auto_close_) { |
| 94 // If the bubble should be closed automatically, reset the timer so that |
| 95 // it will show for the full amount of time instead of only what remained |
| 96 // from the previous time. |
| 97 timer_.Reset(); |
| 98 } |
| 99 } |
| 100 |
| 101 void ZoomBubbleView::ButtonPressed(views::Button* sender, |
| 102 const views::Event& event) { |
| 103 double default_zoom_level = Profile::FromBrowserContext( |
| 104 tab_contents_->web_contents()->GetBrowserContext())-> |
| 105 GetPrefs()->GetDouble(prefs::kDefaultZoomLevel); |
| 106 tab_contents_->web_contents()->GetRenderViewHost()-> |
| 107 SetZoomLevel(default_zoom_level); |
| 108 } |
| 109 |
80 void ZoomBubbleView::Close() { | 110 void ZoomBubbleView::Close() { |
81 GetWidget()->Close(); | 111 GetWidget()->Close(); |
82 } | 112 } |
83 | 113 |
84 void ZoomBubbleView::Init() { | 114 void ZoomBubbleView::Init() { |
85 SetLayoutManager(new views::FillLayout()); | 115 SetLayoutManager(new views::BoxLayout( |
| 116 views::BoxLayout::kVertical, 0, 0, kVerticalSpacing)); |
| 117 |
| 118 int zoom_percent = tab_contents_->zoom_controller()->zoom_percent(); |
86 label_ = new views::Label( | 119 label_ = new views::Label( |
87 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, zoom_percent_)); | 120 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, zoom_percent)); |
| 121 gfx::Font font(label_->font().GetFontName(), kPercentageFontSize); |
| 122 label_->SetFont(font); |
88 AddChildView(label_); | 123 AddChildView(label_); |
89 | 124 |
90 if (auto_close_) { | 125 if (auto_close_) { |
91 timer_.Start( | 126 timer_.Start( |
92 FROM_HERE, | 127 FROM_HERE, |
93 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), | 128 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), |
94 this, | 129 this, |
95 &ZoomBubbleView::Close); | 130 &ZoomBubbleView::Close); |
| 131 } else { |
| 132 AddChildView(new views::Separator()); |
| 133 |
| 134 AddChildView(new views::TextButton( |
| 135 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT))); |
96 } | 136 } |
97 } | 137 } |
98 | 138 |
99 void ZoomBubbleView::WindowClosing() { | 139 void ZoomBubbleView::WindowClosing() { |
100 DCHECK(zoom_bubble_ == this); | 140 DCHECK(zoom_bubble_ == this); |
101 zoom_bubble_ = NULL; | 141 zoom_bubble_ = NULL; |
102 } | 142 } |
OLD | NEW |