Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / exclusive_access_bubble_views.cc
blob6a62028d5cdb2f90ee5a67964df171621e6bbf41
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/exclusive_access_bubble_views.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
12 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
13 #include "chrome/browser/ui/views/exclusive_access_bubble_views_context.h"
14 #include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
15 #include "chrome/browser/ui/views/frame/top_container_view.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "content/public/browser/notification_service.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/events/keycodes/keyboard_codes.h"
21 #include "ui/gfx/animation/slide_animation.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/screen.h"
24 #include "ui/strings/grit/ui_strings.h"
25 #include "ui/views/bubble/bubble_border.h"
26 #include "ui/views/controls/button/label_button.h"
27 #include "ui/views/controls/link.h"
28 #include "ui/views/controls/link_listener.h"
29 #include "ui/views/layout/box_layout.h"
30 #include "ui/views/layout/grid_layout.h"
31 #include "ui/views/view.h"
32 #include "ui/views/widget/widget.h"
33 #include "url/gurl.h"
35 #if defined(OS_WIN)
36 #include "ui/base/l10n/l10n_util_win.h"
37 #endif
39 // ExclusiveAccessView ---------------------------------------------------------
41 namespace {
43 // Space between the site info label and the buttons / link.
44 const int kMiddlePaddingPx = 30;
46 class ButtonView : public views::View {
47 public:
48 ButtonView(views::ButtonListener* listener, int between_button_spacing);
49 ~ButtonView() override;
51 // Returns an empty size when the view is not visible.
52 gfx::Size GetPreferredSize() const override;
54 views::LabelButton* accept_button() const { return accept_button_; }
55 views::LabelButton* deny_button() const { return deny_button_; }
57 private:
58 views::LabelButton* accept_button_;
59 views::LabelButton* deny_button_;
60 DISALLOW_COPY_AND_ASSIGN(ButtonView);
63 ButtonView::ButtonView(views::ButtonListener* listener,
64 int between_button_spacing)
65 : accept_button_(nullptr), deny_button_(nullptr) {
66 accept_button_ = new views::LabelButton(listener, base::string16());
67 accept_button_->SetStyle(views::Button::STYLE_BUTTON);
68 accept_button_->SetFocusable(false);
69 AddChildView(accept_button_);
71 deny_button_ = new views::LabelButton(listener, base::string16());
72 deny_button_->SetStyle(views::Button::STYLE_BUTTON);
73 deny_button_->SetFocusable(false);
74 AddChildView(deny_button_);
76 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0,
77 between_button_spacing));
80 ButtonView::~ButtonView() {
83 gfx::Size ButtonView::GetPreferredSize() const {
84 return visible() ? views::View::GetPreferredSize() : gfx::Size();
87 } // namespace
89 class ExclusiveAccessBubbleViews::ExclusiveAccessView
90 : public views::View,
91 public views::ButtonListener,
92 public views::LinkListener {
93 public:
94 ExclusiveAccessView(ExclusiveAccessBubbleViews* bubble,
95 const base::string16& accelerator,
96 const GURL& url,
97 ExclusiveAccessBubbleType bubble_type);
98 ~ExclusiveAccessView() override;
100 // views::ButtonListener
101 void ButtonPressed(views::Button* sender, const ui::Event& event) override;
103 // views::LinkListener
104 void LinkClicked(views::Link* source, int event_flags) override;
106 void UpdateContent(const GURL& url, ExclusiveAccessBubbleType bubble_type);
108 private:
109 ExclusiveAccessBubbleViews* bubble_;
111 // Clickable hint text for exiting fullscreen mode.
112 views::Link* link_;
113 // Instruction for exiting mouse lock.
114 views::Label* mouse_lock_exit_instruction_;
115 // Informational label: 'www.foo.com has gone fullscreen'.
116 views::Label* message_label_;
117 ButtonView* button_view_;
118 const base::string16 browser_fullscreen_exit_accelerator_;
120 DISALLOW_COPY_AND_ASSIGN(ExclusiveAccessView);
123 ExclusiveAccessBubbleViews::ExclusiveAccessView::ExclusiveAccessView(
124 ExclusiveAccessBubbleViews* bubble,
125 const base::string16& accelerator,
126 const GURL& url,
127 ExclusiveAccessBubbleType bubble_type)
128 : bubble_(bubble),
129 link_(nullptr),
130 mouse_lock_exit_instruction_(nullptr),
131 message_label_(nullptr),
132 button_view_(nullptr),
133 browser_fullscreen_exit_accelerator_(accelerator) {
134 views::BubbleBorder::Shadow shadow_type = views::BubbleBorder::BIG_SHADOW;
135 #if defined(OS_CHROMEOS)
136 // Use a smaller shadow on ChromeOS as the shadow assets can overlap
137 // each other in a fullscreen notification bubble. crbug.com/462983.
138 shadow_type = views::BubbleBorder::SMALL_SHADOW;
139 #endif
140 scoped_ptr<views::BubbleBorder> bubble_border(
141 new views::BubbleBorder(views::BubbleBorder::NONE,
142 shadow_type, SK_ColorWHITE));
143 set_background(new views::BubbleBackground(bubble_border.get()));
144 SetBorder(bubble_border.Pass());
145 SetFocusable(false);
147 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
148 const gfx::FontList& medium_font_list =
149 rb.GetFontList(ui::ResourceBundle::MediumFont);
150 message_label_ = new views::Label(base::string16(), medium_font_list);
152 mouse_lock_exit_instruction_ =
153 new views::Label(bubble_->GetInstructionText(), medium_font_list);
154 mouse_lock_exit_instruction_->set_collapse_when_hidden(true);
156 link_ = new views::Link();
157 link_->set_collapse_when_hidden(true);
158 link_->SetFocusable(false);
159 #if defined(OS_CHROMEOS)
160 // On CrOS, the link text doesn't change, since it doesn't show the shortcut.
161 link_->SetText(l10n_util::GetStringUTF16(IDS_EXIT_FULLSCREEN_MODE));
162 #endif
163 link_->set_listener(this);
164 link_->SetFontList(medium_font_list);
165 link_->SetPressedColor(message_label_->enabled_color());
166 link_->SetEnabledColor(message_label_->enabled_color());
167 link_->SetVisible(false);
169 link_->SetBackgroundColor(background()->get_color());
170 message_label_->SetBackgroundColor(background()->get_color());
171 mouse_lock_exit_instruction_->SetBackgroundColor(background()->get_color());
173 button_view_ = new ButtonView(this, kPaddingPx);
175 views::GridLayout* layout = new views::GridLayout(this);
176 views::ColumnSet* columns = layout->AddColumnSet(0);
177 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0,
178 views::GridLayout::USE_PREF, 0, 0);
179 columns->AddPaddingColumn(1, kMiddlePaddingPx);
180 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0,
181 views::GridLayout::USE_PREF, 0, 0);
182 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0,
183 views::GridLayout::USE_PREF, 0, 0);
184 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0,
185 views::GridLayout::USE_PREF, 0, 0);
187 layout->StartRow(0, 0);
188 layout->AddView(message_label_);
189 layout->AddView(button_view_);
190 layout->AddView(mouse_lock_exit_instruction_);
191 layout->AddView(link_);
193 gfx::Insets padding(kPaddingPx, kPaddingPx, kPaddingPx, kPaddingPx);
194 padding += GetInsets();
195 layout->SetInsets(padding);
196 SetLayoutManager(layout);
198 UpdateContent(url, bubble_type);
201 ExclusiveAccessBubbleViews::ExclusiveAccessView::~ExclusiveAccessView() {
204 void ExclusiveAccessBubbleViews::ExclusiveAccessView::ButtonPressed(
205 views::Button* sender,
206 const ui::Event& event) {
207 if (sender == button_view_->accept_button())
208 bubble_->Accept();
209 else
210 bubble_->Cancel();
213 void ExclusiveAccessBubbleViews::ExclusiveAccessView::LinkClicked(
214 views::Link* link,
215 int event_flags) {
216 bubble_->ExitExclusiveAccess();
219 void ExclusiveAccessBubbleViews::ExclusiveAccessView::UpdateContent(
220 const GURL& url,
221 ExclusiveAccessBubbleType bubble_type) {
222 DCHECK_NE(EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE, bubble_type);
224 message_label_->SetText(bubble_->GetCurrentMessageText());
225 if (exclusive_access_bubble::ShowButtonsForType(bubble_type)) {
226 link_->SetVisible(false);
227 mouse_lock_exit_instruction_->SetVisible(false);
228 button_view_->SetVisible(true);
229 button_view_->deny_button()->SetText(bubble_->GetCurrentDenyButtonText());
230 button_view_->deny_button()->SetMinSize(gfx::Size());
231 button_view_->accept_button()->SetText(
232 bubble_->GetCurrentAllowButtonText());
233 button_view_->accept_button()->SetMinSize(gfx::Size());
234 } else {
235 bool link_visible = true;
236 base::string16 accelerator;
237 if (bubble_type ==
238 EXCLUSIVE_ACCESS_BUBBLE_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION ||
239 bubble_type ==
240 EXCLUSIVE_ACCESS_BUBBLE_TYPE_EXTENSION_FULLSCREEN_EXIT_INSTRUCTION) {
241 accelerator = browser_fullscreen_exit_accelerator_;
242 } else if (bubble_type ==
243 EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION) {
244 accelerator = l10n_util::GetStringUTF16(IDS_APP_ESC_KEY);
245 } else {
246 link_visible = false;
248 #if !defined(OS_CHROMEOS)
249 if (link_visible) {
250 link_->SetText(l10n_util::GetStringUTF16(IDS_EXIT_FULLSCREEN_MODE) +
251 base::UTF8ToUTF16(" ") +
252 l10n_util::GetStringFUTF16(
253 IDS_EXIT_FULLSCREEN_MODE_ACCELERATOR, accelerator));
255 #endif
256 link_->SetVisible(link_visible);
257 mouse_lock_exit_instruction_->SetVisible(!link_visible);
258 button_view_->SetVisible(false);
262 // ExclusiveAccessBubbleViews --------------------------------------------------
264 ExclusiveAccessBubbleViews::ExclusiveAccessBubbleViews(
265 ExclusiveAccessBubbleViewsContext* context,
266 const GURL& url,
267 ExclusiveAccessBubbleType bubble_type)
268 : ExclusiveAccessBubble(context->GetExclusiveAccessManager(),
269 url,
270 bubble_type),
271 bubble_view_context_(context),
272 popup_(nullptr),
273 animation_(new gfx::SlideAnimation(this)),
274 animated_attribute_(ANIMATED_ATTRIBUTE_BOUNDS) {
275 // With the simplified fullscreen UI flag, initially hide the bubble;
276 // otherwise, initially show it.
277 double initial_value =
278 ExclusiveAccessManager::IsSimplifiedFullscreenUIEnabled() ? 0 : 1;
279 animation_->Reset(initial_value);
281 // Create the contents view.
282 ui::Accelerator accelerator(ui::VKEY_UNKNOWN, ui::EF_NONE);
283 bool got_accelerator =
284 bubble_view_context_->GetBubbleAssociatedWidget()->GetAccelerator(
285 IDC_FULLSCREEN, &accelerator);
286 DCHECK(got_accelerator);
287 view_ = new ExclusiveAccessView(this, accelerator.GetShortcutText(), url,
288 bubble_type_);
290 // TODO(yzshen): Change to use the new views bubble, BubbleDelegateView.
291 // TODO(pkotwicz): When this becomes a views bubble, make sure that this
292 // bubble is ignored by ImmersiveModeControllerAsh::BubbleManager.
293 // Initialize the popup.
294 popup_ = new views::Widget;
295 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
296 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
297 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
298 params.parent =
299 bubble_view_context_->GetBubbleAssociatedWidget()->GetNativeView();
300 popup_->Init(params);
301 popup_->SetContentsView(view_);
302 gfx::Size size = GetPopupRect(true).size();
303 popup_->SetBounds(GetPopupRect(false));
304 // We set layout manager to nullptr to prevent the widget from sizing its
305 // contents to the same size as itself. This prevents the widget contents from
306 // shrinking while we animate the height of the popup to give the impression
307 // that it is sliding off the top of the screen.
308 popup_->GetRootView()->SetLayoutManager(nullptr);
309 view_->SetBounds(0, 0, size.width(), size.height());
310 if (!ExclusiveAccessManager::IsSimplifiedFullscreenUIEnabled())
311 popup_->ShowInactive(); // This does not activate the popup.
313 popup_->AddObserver(this);
315 registrar_.Add(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED,
316 content::Source<FullscreenController>(
317 bubble_view_context_->GetExclusiveAccessManager()
318 ->fullscreen_controller()));
320 UpdateForImmersiveState();
323 ExclusiveAccessBubbleViews::~ExclusiveAccessBubbleViews() {
324 popup_->RemoveObserver(this);
326 // This is tricky. We may be in an ATL message handler stack, in which case
327 // the popup cannot be deleted yet. We also can't set the popup's ownership
328 // model to NATIVE_WIDGET_OWNS_WIDGET because if the user closed the last tab
329 // while in fullscreen mode, Windows has already destroyed the popup HWND by
330 // the time we get here, and thus either the popup will already have been
331 // deleted (if we set this in our constructor) or the popup will never get
332 // another OnFinalMessage() call (if not, as currently). So instead, we tell
333 // the popup to synchronously hide, and then asynchronously close and delete
334 // itself.
335 popup_->Close();
336 base::MessageLoop::current()->DeleteSoon(FROM_HERE, popup_);
339 void ExclusiveAccessBubbleViews::UpdateContent(
340 const GURL& url,
341 ExclusiveAccessBubbleType bubble_type) {
342 DCHECK_NE(EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE, bubble_type);
343 if (bubble_type_ == bubble_type && url_ == url)
344 return;
346 url_ = url;
347 bubble_type_ = bubble_type;
348 view_->UpdateContent(url_, bubble_type_);
350 gfx::Size size = GetPopupRect(true).size();
351 view_->SetSize(size);
352 popup_->SetBounds(GetPopupRect(false));
353 Show();
355 // Stop watching the mouse even if UpdateMouseWatcher() will start watching
356 // it again so that the popup with the new content is visible for at least
357 // |kInitialDelayMs|.
358 StopWatchingMouse();
360 UpdateMouseWatcher();
363 void ExclusiveAccessBubbleViews::RepositionIfVisible() {
364 if (popup_->IsVisible())
365 UpdateBounds();
368 views::View* ExclusiveAccessBubbleViews::GetView() {
369 return view_;
372 void ExclusiveAccessBubbleViews::UpdateMouseWatcher() {
373 bool should_watch_mouse = false;
374 if (popup_->IsVisible())
375 should_watch_mouse =
376 !exclusive_access_bubble::ShowButtonsForType(bubble_type_);
377 else
378 should_watch_mouse = CanMouseTriggerSlideIn();
380 if (should_watch_mouse == IsWatchingMouse())
381 return;
383 if (should_watch_mouse)
384 StartWatchingMouse();
385 else
386 StopWatchingMouse();
389 void ExclusiveAccessBubbleViews::UpdateForImmersiveState() {
390 AnimatedAttribute expected_animated_attribute =
391 bubble_view_context_->IsImmersiveModeEnabled()
392 ? ANIMATED_ATTRIBUTE_OPACITY
393 : ANIMATED_ATTRIBUTE_BOUNDS;
394 if (animated_attribute_ != expected_animated_attribute) {
395 // If an animation is currently in progress, skip to the end because
396 // switching the animated attribute midway through the animation looks
397 // weird.
398 animation_->End();
400 animated_attribute_ = expected_animated_attribute;
402 // We may have finished hiding |popup_|. However, the bounds animation
403 // assumes |popup_| has the opacity when it is fully shown and the opacity
404 // animation assumes |popup_| has the bounds when |popup_| is fully shown.
405 if (animated_attribute_ == ANIMATED_ATTRIBUTE_BOUNDS)
406 popup_->SetOpacity(255);
407 else
408 UpdateBounds();
411 UpdateMouseWatcher();
414 void ExclusiveAccessBubbleViews::UpdateBounds() {
415 gfx::Rect popup_rect(GetPopupRect(false));
416 if (!popup_rect.IsEmpty()) {
417 popup_->SetBounds(popup_rect);
418 view_->SetY(popup_rect.height() - view_->height());
422 views::View* ExclusiveAccessBubbleViews::GetBrowserRootView() const {
423 return bubble_view_context_->GetBubbleAssociatedWidget()->GetRootView();
426 void ExclusiveAccessBubbleViews::AnimationProgressed(
427 const gfx::Animation* animation) {
428 if (animated_attribute_ == ANIMATED_ATTRIBUTE_OPACITY) {
429 int opacity = animation_->CurrentValueBetween(0, 255);
430 if (opacity == 0) {
431 popup_->Hide();
432 } else {
433 popup_->Show();
434 popup_->SetOpacity(opacity);
436 } else {
437 if (GetPopupRect(false).IsEmpty()) {
438 popup_->Hide();
439 } else {
440 UpdateBounds();
441 popup_->Show();
446 void ExclusiveAccessBubbleViews::AnimationEnded(
447 const gfx::Animation* animation) {
448 AnimationProgressed(animation);
451 gfx::Rect ExclusiveAccessBubbleViews::GetPopupRect(
452 bool ignore_animation_state) const {
453 gfx::Size size(view_->GetPreferredSize());
454 // NOTE: don't use the bounds of the root_view_. On linux GTK changing window
455 // size is async. Instead we use the size of the screen.
456 gfx::Screen* screen = gfx::Screen::GetScreenFor(
457 bubble_view_context_->GetBubbleAssociatedWidget()->GetNativeView());
458 gfx::Rect screen_bounds =
459 screen->GetDisplayNearestWindow(
460 bubble_view_context_->GetBubbleAssociatedWidget()
461 ->GetNativeView()).bounds();
462 int x = screen_bounds.x() + (screen_bounds.width() - size.width()) / 2;
464 int top_container_bottom = screen_bounds.y();
465 if (bubble_view_context_->IsImmersiveModeEnabled()) {
466 // Skip querying the top container height in non-immersive fullscreen
467 // because:
468 // - The top container height is always zero in non-immersive fullscreen.
469 // - Querying the top container height may return the height before entering
470 // fullscreen because layout is disabled while entering fullscreen.
471 // A visual glitch due to the delayed layout is avoided in immersive
472 // fullscreen because entering fullscreen starts with the top container
473 // revealed. When revealed, the top container has the same height as before
474 // entering fullscreen.
475 top_container_bottom =
476 bubble_view_context_->GetTopContainerBoundsInScreen().bottom();
478 int y = top_container_bottom + kPopupTopPx;
480 if (!ignore_animation_state &&
481 animated_attribute_ == ANIMATED_ATTRIBUTE_BOUNDS) {
482 int total_height = size.height() + kPopupTopPx;
483 int popup_bottom = animation_->CurrentValueBetween(total_height, 0);
484 int y_offset = std::min(popup_bottom, kPopupTopPx);
485 size.set_height(size.height() - popup_bottom + y_offset);
486 y -= y_offset;
488 return gfx::Rect(gfx::Point(x, y), size);
491 gfx::Point ExclusiveAccessBubbleViews::GetCursorScreenPoint() {
492 gfx::Point cursor_pos =
493 gfx::Screen::GetScreenFor(
494 bubble_view_context_->GetBubbleAssociatedWidget()->GetNativeView())
495 ->GetCursorScreenPoint();
496 views::View::ConvertPointFromScreen(GetBrowserRootView(), &cursor_pos);
497 return cursor_pos;
500 bool ExclusiveAccessBubbleViews::WindowContainsPoint(gfx::Point pos) {
501 return GetBrowserRootView()->HitTestPoint(pos);
504 bool ExclusiveAccessBubbleViews::IsWindowActive() {
505 return bubble_view_context_->GetBubbleAssociatedWidget()->IsActive();
508 void ExclusiveAccessBubbleViews::Hide() {
509 animation_->SetSlideDuration(kSlideOutDurationMs);
510 animation_->Hide();
513 void ExclusiveAccessBubbleViews::Show() {
514 animation_->SetSlideDuration(kSlideInDurationMs);
515 animation_->Show();
518 bool ExclusiveAccessBubbleViews::IsAnimating() {
519 return animation_->is_animating();
522 bool ExclusiveAccessBubbleViews::CanMouseTriggerSlideIn() const {
523 return !bubble_view_context_->IsImmersiveModeEnabled();
526 void ExclusiveAccessBubbleViews::Observe(
527 int type,
528 const content::NotificationSource& source,
529 const content::NotificationDetails& details) {
530 DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED, type);
531 UpdateForImmersiveState();
534 void ExclusiveAccessBubbleViews::OnWidgetVisibilityChanged(
535 views::Widget* widget,
536 bool visible) {
537 UpdateMouseWatcher();