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.
4
5#include "chrome/browser/ui/fullscreen/fullscreen_exit_bubble.h"
6
7#include "base/strings/utf_string_conversions.h"
8#include "chrome/app/chrome_command_ids.h"
9#include "chrome/browser/profiles/profile.h"
10#include "chrome/browser/ui/browser.h"
11#include "chrome/browser/ui/browser_commands.h"
12#include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
13#include "chrome/grit/generated_resources.h"
14#include "extensions/browser/extension_system.h"
15#include "ui/base/l10n/l10n_util.h"
16#include "ui/gfx/rect.h"
17#include "ui/strings/grit/ui_strings.h"
18
19// NOTE(koz): Linux doesn't use the thick shadowed border, so we add padding
20// here.
21#if defined(OS_LINUX)
22const int FullscreenExitBubble::kPaddingPx = 8;
23#else
24const int FullscreenExitBubble::kPaddingPx = 15;
25#endif
26const int FullscreenExitBubble::kInitialDelayMs = 3800;
27const int FullscreenExitBubble::kIdleTimeMs = 2300;
28const int FullscreenExitBubble::kPositionCheckHz = 10;
29const int FullscreenExitBubble::kSlideInRegionHeightPx = 4;
30const int FullscreenExitBubble::kSlideInDurationMs = 350;
31const int FullscreenExitBubble::kSlideOutDurationMs = 700;
32const int FullscreenExitBubble::kPopupTopPx = 15;
33
34FullscreenExitBubble::FullscreenExitBubble(Browser* browser,
35                                           const GURL& url,
36                                           FullscreenExitBubbleType bubble_type)
37    : browser_(browser),
38      url_(url),
39      bubble_type_(bubble_type) {
40  DCHECK_NE(FEB_TYPE_NONE, bubble_type_);
41}
42
43FullscreenExitBubble::~FullscreenExitBubble() {
44}
45
46void FullscreenExitBubble::StartWatchingMouse() {
47  // Start the initial delay timer and begin watching the mouse.
48  initial_delay_.Start(FROM_HERE,
49                       base::TimeDelta::FromMilliseconds(kInitialDelayMs), this,
50                       &FullscreenExitBubble::CheckMousePosition);
51  gfx::Point cursor_pos = GetCursorScreenPoint();
52  last_mouse_pos_ = cursor_pos;
53  mouse_position_checker_.Start(FROM_HERE,
54      base::TimeDelta::FromMilliseconds(1000 / kPositionCheckHz), this,
55      &FullscreenExitBubble::CheckMousePosition);
56}
57
58void FullscreenExitBubble::StopWatchingMouse() {
59  initial_delay_.Stop();
60  idle_timeout_.Stop();
61  mouse_position_checker_.Stop();
62}
63
64bool FullscreenExitBubble::IsWatchingMouse() const {
65  return mouse_position_checker_.IsRunning();
66}
67
68void FullscreenExitBubble::CheckMousePosition() {
69  // Desired behavior:
70  //
71  // +------------+-----------------------------+------------+
72  // | _  _  _  _ | Exit full screen mode (F11) | _  _  _  _ |  Slide-in region
73  // | _  _  _  _ \_____________________________/ _  _  _  _ |  Neutral region
74  // |                                                       |  Slide-out region
75  // :                                                       :
76  //
77  // * If app is not active, we hide the popup.
78  // * If the mouse is offscreen or in the slide-out region, we hide the popup.
79  // * If the mouse goes idle, we hide the popup.
80  // * If the mouse is in the slide-in-region and not idle, we show the popup.
81  // * If the mouse is in the neutral region and not idle, and the popup is
82  //   currently sliding out, we show it again.  This facilitates users
83  //   correcting us if they try to mouse horizontally towards the popup and
84  //   unintentionally drop too low.
85  // * Otherwise, we do nothing, because the mouse is in the neutral region and
86  //   either the popup is hidden or the mouse is not idle, so we don't want to
87  //   change anything's state.
88
89  gfx::Point cursor_pos = GetCursorScreenPoint();
90
91  // Check to see whether the mouse is idle.
92  if (cursor_pos != last_mouse_pos_) {
93    // The mouse moved; reset the idle timer.
94    idle_timeout_.Stop();  // If the timer isn't running, this is a no-op.
95    idle_timeout_.Start(FROM_HERE,
96                        base::TimeDelta::FromMilliseconds(kIdleTimeMs), this,
97                        &FullscreenExitBubble::CheckMousePosition);
98  }
99  last_mouse_pos_ = cursor_pos;
100
101  if (!IsWindowActive() ||
102      !WindowContainsPoint(cursor_pos) ||
103      (cursor_pos.y() >= GetPopupRect(true).bottom()) ||
104      !idle_timeout_.IsRunning()) {
105    // The cursor is offscreen, in the slide-out region, or idle.
106    if (!initial_delay_.IsRunning()) {
107      Hide();
108    }
109  } else if (cursor_pos.y() < kSlideInRegionHeightPx &&
110             CanMouseTriggerSlideIn()) {
111    Show();
112  } else if (IsAnimating()) {
113    // The cursor is not idle and either it's in the slide-in region or it's in
114    // the neutral region and we're sliding in or out.
115    Show();
116  }
117}
118
119void FullscreenExitBubble::ToggleFullscreen() {
120  browser_->fullscreen_controller()->
121      ExitTabOrBrowserFullscreenToPreviousState();
122}
123
124void FullscreenExitBubble::Accept() {
125  browser_->fullscreen_controller()->OnAcceptFullscreenPermission();
126}
127
128void FullscreenExitBubble::Cancel() {
129  browser_->fullscreen_controller()->OnDenyFullscreenPermission();
130}
131
132base::string16 FullscreenExitBubble::GetCurrentMessageText() const {
133  return fullscreen_bubble::GetLabelTextForType(
134      bubble_type_, url_,
135      extensions::ExtensionSystem::Get(
136          browser_->profile())->extension_service());
137}
138
139base::string16 FullscreenExitBubble::GetCurrentDenyButtonText() const {
140  return fullscreen_bubble::GetDenyButtonTextForType(bubble_type_);
141}
142
143base::string16 FullscreenExitBubble::GetAllowButtonText() const {
144  return l10n_util::GetStringUTF16(IDS_FULLSCREEN_ALLOW);
145}
146
147base::string16 FullscreenExitBubble::GetInstructionText() const {
148  return l10n_util::GetStringFUTF16(IDS_FULLSCREEN_PRESS_ESC_TO_EXIT,
149      l10n_util::GetStringUTF16(IDS_APP_ESC_KEY));
150}
151