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"
36 #include "ui/base/l10n/l10n_util_win.h"
39 // ExclusiveAccessView ---------------------------------------------------------
43 // Space between the site info label and the buttons / link.
44 const int kMiddlePaddingPx
= 30;
46 class ButtonView
: public views::View
{
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_
; }
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();
89 class ExclusiveAccessBubbleViews::ExclusiveAccessView
91 public views::ButtonListener
,
92 public views::LinkListener
{
94 ExclusiveAccessView(ExclusiveAccessBubbleViews
* bubble
,
95 const base::string16
& accelerator
,
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
);
109 ExclusiveAccessBubbleViews
* bubble_
;
111 // Clickable hint text for exiting fullscreen mode.
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
,
127 ExclusiveAccessBubbleType bubble_type
)
130 mouse_lock_exit_instruction_(nullptr),
131 message_label_(nullptr),
132 button_view_(nullptr),
133 browser_fullscreen_exit_accelerator_(accelerator
) {
134 scoped_ptr
<views::BubbleBorder
> bubble_border(
135 new views::BubbleBorder(views::BubbleBorder::NONE
,
136 views::BubbleBorder::BIG_SHADOW
, SK_ColorWHITE
));
137 set_background(new views::BubbleBackground(bubble_border
.get()));
138 SetBorder(bubble_border
.Pass());
141 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
142 const gfx::FontList
& medium_font_list
=
143 rb
.GetFontList(ui::ResourceBundle::MediumFont
);
144 message_label_
= new views::Label(base::string16(), medium_font_list
);
146 mouse_lock_exit_instruction_
=
147 new views::Label(bubble_
->GetInstructionText(), medium_font_list
);
148 mouse_lock_exit_instruction_
->set_collapse_when_hidden(true);
150 link_
= new views::Link();
151 link_
->set_collapse_when_hidden(true);
152 link_
->SetFocusable(false);
153 #if defined(OS_CHROMEOS)
154 // On CrOS, the link text doesn't change, since it doesn't show the shortcut.
155 link_
->SetText(l10n_util::GetStringUTF16(IDS_EXIT_FULLSCREEN_MODE
));
157 link_
->set_listener(this);
158 link_
->SetFontList(medium_font_list
);
159 link_
->SetPressedColor(message_label_
->enabled_color());
160 link_
->SetEnabledColor(message_label_
->enabled_color());
161 link_
->SetVisible(false);
163 link_
->SetBackgroundColor(background()->get_color());
164 message_label_
->SetBackgroundColor(background()->get_color());
165 mouse_lock_exit_instruction_
->SetBackgroundColor(background()->get_color());
167 button_view_
= new ButtonView(this, kPaddingPx
);
169 views::GridLayout
* layout
= new views::GridLayout(this);
170 views::ColumnSet
* columns
= layout
->AddColumnSet(0);
171 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::CENTER
, 0,
172 views::GridLayout::USE_PREF
, 0, 0);
173 columns
->AddPaddingColumn(1, kMiddlePaddingPx
);
174 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::CENTER
, 0,
175 views::GridLayout::USE_PREF
, 0, 0);
176 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::CENTER
, 0,
177 views::GridLayout::USE_PREF
, 0, 0);
178 columns
->AddColumn(views::GridLayout::LEADING
, views::GridLayout::CENTER
, 0,
179 views::GridLayout::USE_PREF
, 0, 0);
181 layout
->StartRow(0, 0);
182 layout
->AddView(message_label_
);
183 layout
->AddView(button_view_
);
184 layout
->AddView(mouse_lock_exit_instruction_
);
185 layout
->AddView(link_
);
187 gfx::Insets
padding(kPaddingPx
, kPaddingPx
, kPaddingPx
, kPaddingPx
);
188 padding
+= GetInsets();
189 layout
->SetInsets(padding
);
190 SetLayoutManager(layout
);
192 UpdateContent(url
, bubble_type
);
195 ExclusiveAccessBubbleViews::ExclusiveAccessView::~ExclusiveAccessView() {
198 void ExclusiveAccessBubbleViews::ExclusiveAccessView::ButtonPressed(
199 views::Button
* sender
,
200 const ui::Event
& event
) {
201 if (sender
== button_view_
->accept_button())
207 void ExclusiveAccessBubbleViews::ExclusiveAccessView::LinkClicked(
210 bubble_
->ExitExclusiveAccess();
213 void ExclusiveAccessBubbleViews::ExclusiveAccessView::UpdateContent(
215 ExclusiveAccessBubbleType bubble_type
) {
216 DCHECK_NE(EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE
, bubble_type
);
218 message_label_
->SetText(bubble_
->GetCurrentMessageText());
219 if (exclusive_access_bubble::ShowButtonsForType(bubble_type
)) {
220 link_
->SetVisible(false);
221 mouse_lock_exit_instruction_
->SetVisible(false);
222 button_view_
->SetVisible(true);
223 button_view_
->deny_button()->SetText(bubble_
->GetCurrentDenyButtonText());
224 button_view_
->deny_button()->SetMinSize(gfx::Size());
225 button_view_
->accept_button()->SetText(
226 bubble_
->GetCurrentAllowButtonText());
227 button_view_
->accept_button()->SetMinSize(gfx::Size());
229 bool link_visible
= true;
230 base::string16 accelerator
;
232 EXCLUSIVE_ACCESS_BUBBLE_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION
||
234 EXCLUSIVE_ACCESS_BUBBLE_TYPE_EXTENSION_FULLSCREEN_EXIT_INSTRUCTION
) {
235 accelerator
= browser_fullscreen_exit_accelerator_
;
236 } else if (bubble_type
==
237 EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION
) {
238 accelerator
= l10n_util::GetStringUTF16(IDS_APP_ESC_KEY
);
240 link_visible
= false;
242 #if !defined(OS_CHROMEOS)
244 link_
->SetText(l10n_util::GetStringUTF16(IDS_EXIT_FULLSCREEN_MODE
) +
245 base::UTF8ToUTF16(" ") +
246 l10n_util::GetStringFUTF16(
247 IDS_EXIT_FULLSCREEN_MODE_ACCELERATOR
, accelerator
));
250 link_
->SetVisible(link_visible
);
251 mouse_lock_exit_instruction_
->SetVisible(!link_visible
);
252 button_view_
->SetVisible(false);
256 // ExclusiveAccessBubbleViews --------------------------------------------------
258 ExclusiveAccessBubbleViews::ExclusiveAccessBubbleViews(
259 ExclusiveAccessBubbleViewsContext
* context
,
261 ExclusiveAccessBubbleType bubble_type
)
262 : ExclusiveAccessBubble(context
->GetExclusiveAccessManager(),
265 bubble_view_context_(context
),
267 animation_(new gfx::SlideAnimation(this)),
268 animated_attribute_(ANIMATED_ATTRIBUTE_BOUNDS
) {
269 animation_
->Reset(1);
271 // Create the contents view.
272 ui::Accelerator
accelerator(ui::VKEY_UNKNOWN
, ui::EF_NONE
);
273 bool got_accelerator
=
274 bubble_view_context_
->GetBubbleAssociatedWidget()->GetAccelerator(
275 IDC_FULLSCREEN
, &accelerator
);
276 DCHECK(got_accelerator
);
277 view_
= new ExclusiveAccessView(this, accelerator
.GetShortcutText(), url
,
280 // TODO(yzshen): Change to use the new views bubble, BubbleDelegateView.
281 // TODO(pkotwicz): When this becomes a views bubble, make sure that this
282 // bubble is ignored by ImmersiveModeControllerAsh::BubbleManager.
283 // Initialize the popup.
284 popup_
= new views::Widget
;
285 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_POPUP
);
286 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
287 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
289 bubble_view_context_
->GetBubbleAssociatedWidget()->GetNativeView();
290 params
.bounds
= GetPopupRect(false);
291 popup_
->Init(params
);
292 gfx::Size size
= GetPopupRect(true).size();
293 popup_
->SetContentsView(view_
);
294 // We set layout manager to nullptr to prevent the widget from sizing its
295 // contents to the same size as itself. This prevents the widget contents from
296 // shrinking while we animate the height of the popup to give the impression
297 // that it is sliding off the top of the screen.
298 popup_
->GetRootView()->SetLayoutManager(nullptr);
299 view_
->SetBounds(0, 0, size
.width(), size
.height());
300 popup_
->ShowInactive(); // This does not activate the popup.
302 popup_
->AddObserver(this);
304 registrar_
.Add(this, chrome::NOTIFICATION_FULLSCREEN_CHANGED
,
305 content::Source
<FullscreenController
>(
306 bubble_view_context_
->GetExclusiveAccessManager()
307 ->fullscreen_controller()));
309 UpdateForImmersiveState();
312 ExclusiveAccessBubbleViews::~ExclusiveAccessBubbleViews() {
313 popup_
->RemoveObserver(this);
315 // This is tricky. We may be in an ATL message handler stack, in which case
316 // the popup cannot be deleted yet. We also can't set the popup's ownership
317 // model to NATIVE_WIDGET_OWNS_WIDGET because if the user closed the last tab
318 // while in fullscreen mode, Windows has already destroyed the popup HWND by
319 // the time we get here, and thus either the popup will already have been
320 // deleted (if we set this in our constructor) or the popup will never get
321 // another OnFinalMessage() call (if not, as currently). So instead, we tell
322 // the popup to synchronously hide, and then asynchronously close and delete
325 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, popup_
);
328 void ExclusiveAccessBubbleViews::UpdateContent(
330 ExclusiveAccessBubbleType bubble_type
) {
331 DCHECK_NE(EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE
, bubble_type
);
332 if (bubble_type_
== bubble_type
&& url_
== url
)
336 bubble_type_
= bubble_type
;
337 view_
->UpdateContent(url_
, bubble_type_
);
339 gfx::Size size
= GetPopupRect(true).size();
340 view_
->SetSize(size
);
341 popup_
->SetBounds(GetPopupRect(false));
344 // Stop watching the mouse even if UpdateMouseWatcher() will start watching
345 // it again so that the popup with the new content is visible for at least
346 // |kInitialDelayMs|.
349 UpdateMouseWatcher();
352 void ExclusiveAccessBubbleViews::RepositionIfVisible() {
353 if (popup_
->IsVisible())
357 views::View
* ExclusiveAccessBubbleViews::GetView() {
361 void ExclusiveAccessBubbleViews::UpdateMouseWatcher() {
362 bool should_watch_mouse
= false;
363 if (popup_
->IsVisible())
365 !exclusive_access_bubble::ShowButtonsForType(bubble_type_
);
367 should_watch_mouse
= CanMouseTriggerSlideIn();
369 if (should_watch_mouse
== IsWatchingMouse())
372 if (should_watch_mouse
)
373 StartWatchingMouse();
378 void ExclusiveAccessBubbleViews::UpdateForImmersiveState() {
379 AnimatedAttribute expected_animated_attribute
=
380 bubble_view_context_
->IsImmersiveModeEnabled()
381 ? ANIMATED_ATTRIBUTE_OPACITY
382 : ANIMATED_ATTRIBUTE_BOUNDS
;
383 if (animated_attribute_
!= expected_animated_attribute
) {
384 // If an animation is currently in progress, skip to the end because
385 // switching the animated attribute midway through the animation looks
389 animated_attribute_
= expected_animated_attribute
;
391 // We may have finished hiding |popup_|. However, the bounds animation
392 // assumes |popup_| has the opacity when it is fully shown and the opacity
393 // animation assumes |popup_| has the bounds when |popup_| is fully shown.
394 if (animated_attribute_
== ANIMATED_ATTRIBUTE_BOUNDS
)
395 popup_
->SetOpacity(255);
400 UpdateMouseWatcher();
403 void ExclusiveAccessBubbleViews::UpdateBounds() {
404 gfx::Rect
popup_rect(GetPopupRect(false));
405 if (!popup_rect
.IsEmpty()) {
406 popup_
->SetBounds(popup_rect
);
407 view_
->SetY(popup_rect
.height() - view_
->height());
411 views::View
* ExclusiveAccessBubbleViews::GetBrowserRootView() const {
412 return bubble_view_context_
->GetBubbleAssociatedWidget()->GetRootView();
415 void ExclusiveAccessBubbleViews::AnimationProgressed(
416 const gfx::Animation
* animation
) {
417 if (animated_attribute_
== ANIMATED_ATTRIBUTE_OPACITY
) {
418 int opacity
= animation_
->CurrentValueBetween(0, 255);
423 popup_
->SetOpacity(opacity
);
426 if (GetPopupRect(false).IsEmpty()) {
435 void ExclusiveAccessBubbleViews::AnimationEnded(
436 const gfx::Animation
* animation
) {
437 AnimationProgressed(animation
);
440 gfx::Rect
ExclusiveAccessBubbleViews::GetPopupRect(
441 bool ignore_animation_state
) const {
442 gfx::Size
size(view_
->GetPreferredSize());
443 // NOTE: don't use the bounds of the root_view_. On linux GTK changing window
444 // size is async. Instead we use the size of the screen.
445 gfx::Screen
* screen
= gfx::Screen::GetScreenFor(
446 bubble_view_context_
->GetBubbleAssociatedWidget()->GetNativeView());
447 gfx::Rect screen_bounds
=
448 screen
->GetDisplayNearestWindow(
449 bubble_view_context_
->GetBubbleAssociatedWidget()
450 ->GetNativeView()).bounds();
451 int x
= screen_bounds
.x() + (screen_bounds
.width() - size
.width()) / 2;
453 int top_container_bottom
= screen_bounds
.y();
454 if (bubble_view_context_
->IsImmersiveModeEnabled()) {
455 // Skip querying the top container height in non-immersive fullscreen
457 // - The top container height is always zero in non-immersive fullscreen.
458 // - Querying the top container height may return the height before entering
459 // fullscreen because layout is disabled while entering fullscreen.
460 // A visual glitch due to the delayed layout is avoided in immersive
461 // fullscreen because entering fullscreen starts with the top container
462 // revealed. When revealed, the top container has the same height as before
463 // entering fullscreen.
464 top_container_bottom
=
465 bubble_view_context_
->GetTopContainerBoundsInScreen().bottom();
467 int y
= top_container_bottom
+ kPopupTopPx
;
469 if (!ignore_animation_state
&&
470 animated_attribute_
== ANIMATED_ATTRIBUTE_BOUNDS
) {
471 int total_height
= size
.height() + kPopupTopPx
;
472 int popup_bottom
= animation_
->CurrentValueBetween(total_height
, 0);
473 int y_offset
= std::min(popup_bottom
, kPopupTopPx
);
474 size
.set_height(size
.height() - popup_bottom
+ y_offset
);
477 return gfx::Rect(gfx::Point(x
, y
), size
);
480 gfx::Point
ExclusiveAccessBubbleViews::GetCursorScreenPoint() {
481 gfx::Point cursor_pos
=
482 gfx::Screen::GetScreenFor(
483 bubble_view_context_
->GetBubbleAssociatedWidget()->GetNativeView())
484 ->GetCursorScreenPoint();
485 views::View::ConvertPointFromScreen(GetBrowserRootView(), &cursor_pos
);
489 bool ExclusiveAccessBubbleViews::WindowContainsPoint(gfx::Point pos
) {
490 return GetBrowserRootView()->HitTestPoint(pos
);
493 bool ExclusiveAccessBubbleViews::IsWindowActive() {
494 return bubble_view_context_
->GetBubbleAssociatedWidget()->IsActive();
497 void ExclusiveAccessBubbleViews::Hide() {
498 animation_
->SetSlideDuration(kSlideOutDurationMs
);
502 void ExclusiveAccessBubbleViews::Show() {
503 animation_
->SetSlideDuration(kSlideInDurationMs
);
507 bool ExclusiveAccessBubbleViews::IsAnimating() {
508 return animation_
->is_animating();
511 bool ExclusiveAccessBubbleViews::CanMouseTriggerSlideIn() const {
512 return !bubble_view_context_
->IsImmersiveModeEnabled();
515 void ExclusiveAccessBubbleViews::Observe(
517 const content::NotificationSource
& source
,
518 const content::NotificationDetails
& details
) {
519 DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED
, type
);
520 UpdateForImmersiveState();
523 void ExclusiveAccessBubbleViews::OnWidgetVisibilityChanged(
524 views::Widget
* widget
,
526 UpdateMouseWatcher();