Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: chrome/browser/ui/views/fullscreen_exit_bubble_views.cc

Issue 7740044: Implement fullscreen info bubble on Win and Mac (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: respond to commens Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/fullscreen_exit_bubble_views.h" 5 #include "chrome/browser/ui/views/fullscreen_exit_bubble_views.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h" 9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/ui/views/bubble/bubble.h"
10 #include "grit/generated_resources.h" 11 #include "grit/generated_resources.h"
11 #include "ui/base/animation/slide_animation.h" 12 #include "ui/base/animation/slide_animation.h"
12 #include "ui/base/keycodes/keyboard_codes.h" 13 #include "ui/base/keycodes/keyboard_codes.h"
13 #include "ui/base/l10n/l10n_util.h" 14 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/resource/resource_bundle.h" 15 #include "ui/base/resource/resource_bundle.h"
15 #include "ui/gfx/canvas_skia.h" 16 #include "ui/gfx/canvas_skia.h"
16 #include "ui/gfx/screen.h" 17 #include "ui/gfx/screen.h"
18 #include "views/bubble/bubble_border.h"
19 #include "views/controls/button/text_button.h"
17 #include "views/controls/link.h" 20 #include "views/controls/link.h"
18 #include "views/widget/widget.h" 21 #include "views/widget/widget.h"
19 22
20 #if defined(OS_WIN) 23 #if defined(OS_WIN)
21 #include "ui/base/l10n/l10n_util_win.h" 24 #include "ui/base/l10n/l10n_util_win.h"
22 #endif 25 #endif
23 26
24 // FullscreenExitView ---------------------------------------------------------- 27 // FullscreenExitView ----------------------------------------------------------
25 28
26 class FullscreenExitBubbleViews::FullscreenExitView : public views::View { 29 class FullscreenExitBubbleViews::FullscreenExitView
30 : public views::View,
31 public views::ButtonListener {
27 public: 32 public:
28 FullscreenExitView(FullscreenExitBubbleViews* bubble, 33 FullscreenExitView(FullscreenExitBubbleViews* bubble,
29 const std::wstring& accelerator); 34 const std::wstring& accelerator,
35 const GURL& url,
36 bool ask_permission);
30 virtual ~FullscreenExitView(); 37 virtual ~FullscreenExitView();
31 38
32 // views::View 39 // views::View
33 virtual gfx::Size GetPreferredSize(); 40 virtual gfx::Size GetPreferredSize();
34 41
42 // views::ButtonListener
43 virtual void ButtonPressed(views::Button* sender, const views::Event& event);
44
45 // Hide the accept and deny buttons, exposing the exit link.
46 void HideButtons();
47
35 private: 48 private:
49 string16 GetMessage(const GURL& url);
50
51 // Space between the site info label and the buttons / link.
52 static const int kMiddlePaddingPx = 30;
53
36 // views::View 54 // views::View
37 virtual void Layout(); 55 virtual void Layout();
38 virtual void OnPaint(gfx::Canvas* canvas); 56
57 FullscreenExitBubbleViews* bubble_;
39 58
40 // Clickable hint text to show in the bubble. 59 // Clickable hint text to show in the bubble.
41 views::Link link_; 60 views::Link link_;
61 views::Label message_label_;
62 views::NativeTextButton* accept_button_;
63 views::NativeTextButton* deny_button_;
64
65 bool show_buttons_;
42 }; 66 };
43 67
44 FullscreenExitBubbleViews::FullscreenExitView::FullscreenExitView( 68 FullscreenExitBubbleViews::FullscreenExitView::FullscreenExitView(
45 FullscreenExitBubbleViews* bubble, 69 FullscreenExitBubbleViews* bubble,
46 const std::wstring& accelerator) { 70 const std::wstring& accelerator,
71 const GURL& url,
72 bool ask_permission)
73 : bubble_(bubble),
74 accept_button_(NULL),
75 deny_button_(NULL),
76 show_buttons_(ask_permission) {
77 set_focusable(false);
47 link_.set_parent_owned(false); 78 link_.set_parent_owned(false);
79 link_.set_collapse_when_hidden(false);
80 link_.set_focusable(false);
48 #if !defined(OS_CHROMEOS) 81 #if !defined(OS_CHROMEOS)
49 link_.SetText( 82 link_.SetText(
50 l10n_util::GetStringFUTF16(IDS_EXIT_FULLSCREEN_MODE, 83 l10n_util::GetStringFUTF16(IDS_EXIT_FULLSCREEN_MODE,
51 WideToUTF16(accelerator))); 84 WideToUTF16(accelerator)));
52 #else 85 #else
53 link_.SetText(l10n_util::GetStringUTF16(IDS_EXIT_FULLSCREEN_MODE)); 86 link_.SetText(l10n_util::GetStringUTF16(IDS_EXIT_FULLSCREEN_MODE));
54 #endif 87 #endif
55 link_.set_listener(bubble); 88 link_.set_listener(bubble);
56 link_.SetFont(ResourceBundle::GetSharedInstance().GetFont( 89 link_.SetFont(ResourceBundle::GetSharedInstance().GetFont(
57 ResourceBundle::LargeFont)); 90 ResourceBundle::MediumFont));
58 link_.SetNormalColor(SK_ColorWHITE); 91 link_.SetNormalColor(SK_ColorBLACK);
59 link_.SetHighlightedColor(SK_ColorWHITE); 92 link_.SetHighlightedColor(SK_ColorBLACK);
93
94 message_label_.set_parent_owned(false);
95 message_label_.SetText(
96 GetMessage(url));
97 message_label_.SetFont(ResourceBundle::GetSharedInstance().GetFont(
98 ResourceBundle::MediumFont));
99 AddChildView(&message_label_);
60 AddChildView(&link_); 100 AddChildView(&link_);
101
102 views::BubbleBorder* bubble_border =
103 new views::BubbleBorder(views::BubbleBorder::NONE);
104 bubble_border->set_background_color(Bubble::kBackgroundColor);
105 set_background(new views::BubbleBackground(bubble_border));
106 set_border(bubble_border);
107
108 accept_button_ = new views::NativeTextButton(this,
109 UTF16ToWide(l10n_util::GetStringUTF16(IDS_FULLSCREEN_INFOBAR_ALLOW)));
110 AddChildView(accept_button_);
111
112 deny_button_ = new views::NativeTextButton(this,
113 UTF16ToWide(l10n_util::GetStringUTF16(IDS_FULLSCREEN_INFOBAR_DENY)));
114 AddChildView(deny_button_);
115 accept_button_->set_focusable(false);
116 deny_button_->set_focusable(false);
117 link_.SetVisible(false);
118 if (!show_buttons_)
119 HideButtons();
120 }
121
122 string16 FullscreenExitBubbleViews::FullscreenExitView::GetMessage(
123 const GURL& url) {
124 if (url.empty()) {
125 return l10n_util::GetStringUTF16(
126 IDS_FULLSCREEN_INFOBAR_USER_ENTERED_FULLSCREEN);
127 }
128 if (url.SchemeIsFile())
129 return l10n_util::GetStringUTF16(IDS_FULLSCREEN_INFOBAR_FILE_PAGE_NAME);
130 return l10n_util::GetStringFUTF16(IDS_FULLSCREEN_INFOBAR_REQUEST_PERMISSION,
131 UTF8ToUTF16(url.host()));
61 } 132 }
62 133
63 FullscreenExitBubbleViews::FullscreenExitView::~FullscreenExitView() { 134 FullscreenExitBubbleViews::FullscreenExitView::~FullscreenExitView() {
64 } 135 }
65 136
137 void FullscreenExitBubbleViews::FullscreenExitView::ButtonPressed(
138 views::Button* sender, const views::Event& event) {
139 if (sender == accept_button_)
140 bubble_->OnAcceptFullscreen();
141 else
142 bubble_->OnCancelFullscreen();
143 }
144
145 void FullscreenExitBubbleViews::FullscreenExitView::HideButtons() {
146 show_buttons_ = false;
147 accept_button_->SetVisible(false);
148 deny_button_->SetVisible(false);
149 link_.SetVisible(true);
150 }
151
66 gfx::Size FullscreenExitBubbleViews::FullscreenExitView::GetPreferredSize() { 152 gfx::Size FullscreenExitBubbleViews::FullscreenExitView::GetPreferredSize() {
67 gfx::Size preferred_size(link_.GetPreferredSize()); 153 gfx::Size link_preferred_size(link_.GetPreferredSize());
68 preferred_size.Enlarge(kPaddingPx * 2, kPaddingPx * 2); 154 gfx::Size site_info_preferred_size(message_label_.GetPreferredSize());
69 return preferred_size; 155 gfx::Size accept_preferred_size(accept_button_->GetPreferredSize());
156 gfx::Size deny_preferred_size(deny_button_->GetPreferredSize());
157 gfx::Insets insets(GetInsets());
158
159 int buttons_width = accept_preferred_size.width() + kPaddingPx +
160 deny_preferred_size.width();
161 int button_box_width = std::max(buttons_width, link_preferred_size.width());
162 int width = kPaddingPx + site_info_preferred_size.width() + kMiddlePaddingPx +
163 button_box_width + kPaddingPx;
164
165 gfx::Size result(width + insets.width(),
166 kPaddingPx * 2 + accept_preferred_size.height() + insets.height());
167 return result;
70 } 168 }
71 169
72 void FullscreenExitBubbleViews::FullscreenExitView::Layout() { 170 void FullscreenExitBubbleViews::FullscreenExitView::Layout() {
73 gfx::Size link_preferred_size(link_.GetPreferredSize()); 171 gfx::Size link_preferred_size(link_.GetPreferredSize());
74 link_.SetBounds(kPaddingPx, 172 gfx::Size site_info_preferred_size(message_label_.GetPreferredSize());
75 height() - kPaddingPx - link_preferred_size.height(), 173 gfx::Size accept_preferred_size(accept_button_->GetPreferredSize());
76 link_preferred_size.width(), link_preferred_size.height()); 174 gfx::Size deny_preferred_size(deny_button_->GetPreferredSize());
77 } 175 gfx::Insets insets(GetInsets());
78 176
79 void FullscreenExitBubbleViews::FullscreenExitView::OnPaint( 177 int inner_height = height() - insets.height();
80 gfx::Canvas* canvas) { 178 int button_box_x = insets.left() + kPaddingPx +
81 // Create a round-bottomed rect to fill the whole View. 179 site_info_preferred_size.width() + kMiddlePaddingPx;
82 SkRect rect; 180 int site_info_y = insets.top() +
83 SkScalar padding = SkIntToScalar(kPaddingPx); 181 (inner_height - site_info_preferred_size.height()) / 2;
84 // The "-padding" top coordinate ensures that the rect is always tall enough 182 int link_x = width() - insets.right() - kPaddingPx -
85 // to contain the complete rounded corner radius. If we set this to 0, as the 183 link_preferred_size.width();
86 // popup slides offscreen (in reality, squishes to 0 height), the corners will 184 int link_y = insets.top() + (inner_height - link_preferred_size.height()) / 2;
87 // flatten out as the height becomes less than the corner radius.
88 rect.set(0, -padding, SkIntToScalar(width()), SkIntToScalar(height()));
89 SkScalar rad[8] = { 0, 0, 0, 0, padding, padding, padding, padding };
90 SkPath path;
91 path.addRoundRect(rect, rad, SkPath::kCW_Direction);
92 185
93 // Fill it black. 186 message_label_.SetBounds(insets.left() + kPaddingPx,
94 SkPaint paint; 187 site_info_y,
95 paint.setStyle(SkPaint::kFill_Style); 188 site_info_preferred_size.width(),
96 paint.setFlags(SkPaint::kAntiAlias_Flag); 189 site_info_preferred_size.height());
97 paint.setColor(SK_ColorBLACK); 190 link_.SetBounds(link_x,
98 canvas->GetSkCanvas()->drawPath(path, paint); 191 link_y,
192 link_preferred_size.width(),
193 link_preferred_size.height());
194 if (show_buttons_) {
195 accept_button_->SetBounds(button_box_x,
196 insets.top() + kPaddingPx,
197 accept_preferred_size.width(),
198 accept_preferred_size.height());
199 deny_button_->SetBounds(
200 button_box_x + accept_preferred_size.width() + kPaddingPx,
201 insets.top() + kPaddingPx,
202 deny_preferred_size.width(),
203 deny_preferred_size.height());
204 }
99 } 205 }
100 206
101 // FullscreenExitBubbleViews --------------------------------------------------- 207 // FullscreenExitBubbleViews ---------------------------------------------------
102 208
103 FullscreenExitBubbleViews::FullscreenExitBubbleViews( 209 FullscreenExitBubbleViews::FullscreenExitBubbleViews(views::Widget* frame,
104 views::Widget* frame, 210 Browser* browser,
105 CommandUpdater::CommandUpdaterDelegate* delegate) 211 const GURL& url,
106 : FullscreenExitBubble(delegate), 212 bool ask_permission)
213 : FullscreenExitBubble(browser),
107 root_view_(frame->GetRootView()), 214 root_view_(frame->GetRootView()),
108 popup_(NULL), 215 popup_(NULL),
109 size_animation_(new ui::SlideAnimation(this)) { 216 size_animation_(new ui::SlideAnimation(this)),
217 url_(url) {
110 size_animation_->Reset(1); 218 size_animation_->Reset(1);
111 219
112 // Create the contents view. 220 // Create the contents view.
113 views::Accelerator accelerator(ui::VKEY_UNKNOWN, false, false, false); 221 views::Accelerator accelerator(ui::VKEY_UNKNOWN, false, false, false);
114 bool got_accelerator = frame->GetAccelerator(IDC_FULLSCREEN, &accelerator); 222 bool got_accelerator = frame->GetAccelerator(IDC_FULLSCREEN, &accelerator);
115 DCHECK(got_accelerator); 223 DCHECK(got_accelerator);
116 view_ = new FullscreenExitView( 224 view_ = new FullscreenExitView(this,
117 this, UTF16ToWideHack(accelerator.GetShortcutText())); 225 UTF16ToWideHack(accelerator.GetShortcutText()), url, ask_permission);
118 226
119 // Initialize the popup. 227 // Initialize the popup.
120 popup_ = new views::Widget; 228 popup_ = new views::Widget;
121 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); 229 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
122 params.transparent = true; 230 params.transparent = true;
123 params.can_activate = false; 231 params.can_activate = false;
124 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 232 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
125 params.parent = frame->GetNativeView(); 233 params.parent = frame->GetNativeView();
126 params.bounds = GetPopupRect(false); 234 params.bounds = GetPopupRect(false);
127 popup_->Init(params); 235 popup_->Init(params);
236 gfx::Size size = GetPopupRect(true).size();
128 popup_->SetContentsView(view_); 237 popup_->SetContentsView(view_);
129 popup_->SetOpacity(static_cast<unsigned char>(0xff * kOpacity)); 238 // We set layout manager to NULL to prevent the widget from sizing its
239 // contents to the same size as itself. This prevents the widget contents from
240 // shrinking while we animate the height of the popup to give the impression
241 // that it is sliding off the top of the screen.
242 popup_->GetRootView()->SetLayoutManager(NULL);
243 view_->SetBounds(0, 0, size.width(), size.height());
130 popup_->Show(); // This does not activate the popup. 244 popup_->Show(); // This does not activate the popup.
131 245
132 StartWatchingMouse(); 246 if (!ask_permission)
247 StartWatchingMouse();
133 } 248 }
134 249
135 FullscreenExitBubbleViews::~FullscreenExitBubbleViews() { 250 FullscreenExitBubbleViews::~FullscreenExitBubbleViews() {
136 // This is tricky. We may be in an ATL message handler stack, in which case 251 // This is tricky. We may be in an ATL message handler stack, in which case
137 // the popup cannot be deleted yet. We also can't set the popup's ownership 252 // the popup cannot be deleted yet. We also can't set the popup's ownership
138 // model to NATIVE_WIDGET_OWNS_WIDGET because if the user closed the last tab 253 // model to NATIVE_WIDGET_OWNS_WIDGET because if the user closed the last tab
139 // while in fullscreen mode, Windows has already destroyed the popup HWND by 254 // while in fullscreen mode, Windows has already destroyed the popup HWND by
140 // the time we get here, and thus either the popup will already have been 255 // the time we get here, and thus either the popup will already have been
141 // deleted (if we set this in our constructor) or the popup will never get 256 // deleted (if we set this in our constructor) or the popup will never get
142 // another OnFinalMessage() call (if not, as currently). So instead, we tell 257 // another OnFinalMessage() call (if not, as currently). So instead, we tell
143 // the popup to synchronously hide, and then asynchronously close and delete 258 // the popup to synchronously hide, and then asynchronously close and delete
144 // itself. 259 // itself.
145 popup_->Close(); 260 popup_->Close();
146 MessageLoop::current()->DeleteSoon(FROM_HERE, popup_); 261 MessageLoop::current()->DeleteSoon(FROM_HERE, popup_);
147 } 262 }
148 263
149 void FullscreenExitBubbleViews::LinkClicked( 264 void FullscreenExitBubbleViews::LinkClicked(
150 views::Link* source, int event_flags) { 265 views::Link* source, int event_flags) {
151 ToggleFullscreen(); 266 ToggleFullscreen();
152 } 267 }
153 268
269 void FullscreenExitBubbleViews::OnAcceptFullscreen() {
270 AcceptFullscreen(url_);
271 view_->HideButtons();
272 StartWatchingMouse();
273 }
274
275 void FullscreenExitBubbleViews::OnCancelFullscreen() {
276 CancelFullscreen();
277 }
278
154 void FullscreenExitBubbleViews::AnimationProgressed( 279 void FullscreenExitBubbleViews::AnimationProgressed(
155 const ui::Animation* animation) { 280 const ui::Animation* animation) {
156 gfx::Rect popup_rect(GetPopupRect(false)); 281 gfx::Rect popup_rect(GetPopupRect(false));
157 if (popup_rect.IsEmpty()) { 282 if (popup_rect.IsEmpty()) {
158 popup_->Hide(); 283 popup_->Hide();
159 } else { 284 } else {
160 popup_->SetBounds(popup_rect); 285 popup_->SetBounds(popup_rect);
286 view_->SetY(popup_rect.height() - view_->height());
161 popup_->Show(); 287 popup_->Show();
162 } 288 }
163 } 289 }
290
164 void FullscreenExitBubbleViews::AnimationEnded( 291 void FullscreenExitBubbleViews::AnimationEnded(
165 const ui::Animation* animation) { 292 const ui::Animation* animation) {
166 AnimationProgressed(animation); 293 AnimationProgressed(animation);
167 } 294 }
168 295
169 void FullscreenExitBubbleViews::Hide() { 296 void FullscreenExitBubbleViews::Hide() {
170 size_animation_->SetSlideDuration(kSlideOutDurationMs); 297 size_animation_->SetSlideDuration(kSlideOutDurationMs);
171 size_animation_->Hide(); 298 size_animation_->Hide();
172 } 299 }
173 300
(...skipping 17 matching lines...) Expand all
191 gfx::Point FullscreenExitBubbleViews::GetCursorScreenPoint() { 318 gfx::Point FullscreenExitBubbleViews::GetCursorScreenPoint() {
192 gfx::Point cursor_pos = gfx::Screen::GetCursorScreenPoint(); 319 gfx::Point cursor_pos = gfx::Screen::GetCursorScreenPoint();
193 gfx::Point transformed_pos(cursor_pos); 320 gfx::Point transformed_pos(cursor_pos);
194 views::View::ConvertPointToView(NULL, root_view_, &transformed_pos); 321 views::View::ConvertPointToView(NULL, root_view_, &transformed_pos);
195 return transformed_pos; 322 return transformed_pos;
196 } 323 }
197 324
198 gfx::Rect FullscreenExitBubbleViews::GetPopupRect( 325 gfx::Rect FullscreenExitBubbleViews::GetPopupRect(
199 bool ignore_animation_state) const { 326 bool ignore_animation_state) const {
200 gfx::Size size(view_->GetPreferredSize()); 327 gfx::Size size(view_->GetPreferredSize());
201 if (!ignore_animation_state) {
202 size.set_height(static_cast<int>(static_cast<double>(size.height()) *
203 size_animation_->GetCurrentValue()));
204 }
205 // NOTE: don't use the bounds of the root_view_. On linux changing window 328 // NOTE: don't use the bounds of the root_view_. On linux changing window
206 // size is async. Instead we use the size of the screen. 329 // size is async. Instead we use the size of the screen.
207 gfx::Rect screen_bounds = gfx::Screen::GetMonitorAreaNearestWindow( 330 gfx::Rect screen_bounds = gfx::Screen::GetMonitorAreaNearestWindow(
208 root_view_->GetWidget()->GetNativeView()); 331 root_view_->GetWidget()->GetNativeView());
209 gfx::Point origin(screen_bounds.x() + 332 gfx::Point origin(screen_bounds.x() +
210 (screen_bounds.width() - size.width()) / 2, 333 (screen_bounds.width() - size.width()) / 2,
211 screen_bounds.y()); 334 kPopupTopPx + screen_bounds.y());
335 if (!ignore_animation_state) {
336 int total_height = size.height() + kPopupTopPx;
337 int animation_height =
338 size_animation_->CurrentValueBetween(static_cast<double>(total_height),
339 0f);
340 int y_offset = std::min(animation_height, kPopupTopPx);
341 size.set_height(size.height() - animation_height + y_offset);
342 origin.set_y(origin.y() - y_offset);
343 }
212 return gfx::Rect(origin, size); 344 return gfx::Rect(origin, size);
213 } 345 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698