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