1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/fullscreen_control/fullscreen_control_host.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/metrics/user_metrics.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/app_mode/app_mode_utils.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
#include "chrome/browser/ui/views/exclusive_access_bubble_views.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/chrome_switches.h"
#include "components/fullscreen_control/fullscreen_control_view.h"
#include "components/version_info/channel.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/event_monitor.h"
#include "ui/views/view.h"
namespace {
// +----------------------------+
// | +-------+ |
// | |Control| |
// | +-------+ |
// | | <-- Control.bottom * kExitHeightScaleFactor
// | Screen | Buffer for mouse moves or pointer events
// | | before closing the fullscreen exit
// | | control.
// +----------------------------+
//
// The same value is also used for timeout cooldown.
// This is a common scenario where people play video or present slides and they
// just want to keep their cursor on the top. In this case we timeout the exit
// control so that it doesn't show permanently. The user will then need to move
// the cursor out of the cooldown area and move it back to the top to re-trigger
// the exit UI.
constexpr float kExitHeightScaleFactor = 1.5f;
// +----------------------------+
// | |
// | |
// | | <-- kShowFullscreenExitControlHeight
// | Screen | If a mouse move or pointer event is
// | | above this line, show the fullscreen
// | | exit control.
// | |
// +----------------------------+
constexpr float kShowFullscreenExitControlHeight = 3.f;
// Time to wait to hide the popup after it is triggered.
constexpr base::TimeDelta kMousePopupTimeout = base::Seconds(3);
constexpr base::TimeDelta kTouchPopupTimeout = base::Seconds(10);
// Time to wait before showing the popup when the escape key is held.
constexpr base::TimeDelta kKeyPressPopupDelay = base::Seconds(1);
bool IsExitUiEnabled() {
#if BUILDFLAG(IS_MAC)
// Exit UI is unnecessary, since Mac uses the OS fullscreen such that window
// menu and controls reveal when the cursor is moved to the top.
return false;
#else
// Kiosk mode is a fullscreen experience, which makes the exit UI
// inappropriate.
return !IsRunningInAppMode();
#endif
}
} // namespace
FullscreenControlHost::FullscreenControlHost(BrowserView* browser_view)
: browser_view_(browser_view) {
if (IsFullscreenExitUIEnabled()) {
event_monitor_ = views::EventMonitor::CreateWindowMonitor(
this, browser_view->GetNativeWindow(),
{ui::EventType::kMouseMoved, ui::EventType::kKeyPressed,
ui::EventType::kKeyReleased, ui::EventType::kTouchPressed,
ui::EventType::kGestureLongPress});
}
}
FullscreenControlHost::~FullscreenControlHost() = default;
// static
bool FullscreenControlHost::IsFullscreenExitUIEnabled() {
// TODO(joedow): Remove this function and all uses of it. The fullscreen exit
// UI is now always enabled because the keyboard lock UI is always enabled.
return true;
}
void FullscreenControlHost::OnEvent(const ui::Event& event) {
if (event.IsKeyEvent()) {
OnKeyEvent(*event.AsKeyEvent());
} else if (event.IsMouseEvent()) {
OnMouseEvent(*event.AsMouseEvent());
} else if (event.IsTouchEvent()) {
OnTouchEvent(*event.AsTouchEvent());
} else if (event.IsGestureEvent()) {
OnGestureEvent(*event.AsGestureEvent());
}
}
void FullscreenControlHost::OnKeyEvent(const ui::KeyEvent& event) {
if (event.key_code() != ui::VKEY_ESCAPE ||
(input_entry_method_ != InputEntryMethod::NOT_ACTIVE &&
input_entry_method_ != InputEntryMethod::KEYBOARD)) {
return;
}
ExclusiveAccessManager* const exclusive_access_manager =
browser_view_->browser()->exclusive_access_manager();
// FullscreenControlHost UI is not needed for the keyboard input method in any
// fullscreen mode except for tab-initiated fullscreen (and only when the user
// is required to press and hold the escape key to exit).
// If we are not in tab-initiated fullscreen, then we want to make sure the
// UI exit bubble is not displayed. This can occur when:
// 1.) The user enters browser fullscreen (F11)
// 2.) The website then enters tab-initiated fullscreen
// 3.) User performs a press and hold gesture on escape
//
// In this case, the fullscreen controller will revert back to browser
// fullscreen mode but there won't be a fullscreen exit message to trigger
// the UI cleanup for the exit bubble. To handle this case, we need to check
// to make sure the UI is in the right fullscreen mode before proceeding.
if (!exclusive_access_manager->fullscreen_controller()
->IsWindowFullscreenForTabOrPending()) {
key_press_delay_timer_.Stop();
if (IsVisible() && input_entry_method_ == InputEntryMethod::KEYBOARD) {
Hide(true);
}
return;
}
// Note: This logic handles the UI feedback element used when holding down the
// esc key, however the logic for exiting fullscreen is handled by the
// KeyboardLockController class.
if (event.type() == ui::EventType::kKeyPressed &&
!key_press_delay_timer_.IsRunning() &&
exclusive_access_manager->keyboard_lock_controller()
->RequiresPressAndHoldEscToExit()) {
key_press_delay_timer_.Start(
FROM_HERE, kKeyPressPopupDelay,
base::BindOnce(&FullscreenControlHost::ShowForInputEntryMethod,
base::Unretained(this), InputEntryMethod::KEYBOARD));
} else if (event.type() == ui::EventType::kKeyReleased) {
key_press_delay_timer_.Stop();
if (IsVisible() && input_entry_method_ == InputEntryMethod::KEYBOARD) {
Hide(true);
}
}
}
void FullscreenControlHost::OnMouseEvent(const ui::MouseEvent& event) {
if (!IsExitUiEnabled()) {
return;
}
if (event.type() != ui::EventType::kMouseMoved || IsAnimating() ||
(input_entry_method_ != InputEntryMethod::NOT_ACTIVE &&
input_entry_method_ != InputEntryMethod::MOUSE)) {
return;
}
// TODO(crbug.com/957455) Do not show fullscreen exit button while in pointer
// lock mode. This is only necessary because the current implementation of
// pointer lock doesn't constrain the mouse cursor position, so the exit
// button may still appear even though the mouse cursor is invisible and its
// position is technically undefined. This mitigation will become unnecessary
// when pointer lock is re-implemented using relative motion events.
if (IsPointerLocked()) {
return;
}
if (IsExitUiNeeded()) {
if (IsVisible()) {
if (event.y() >= CalculateCursorBufferHeight()) {
Hide(true);
}
} else {
DCHECK_EQ(InputEntryMethod::NOT_ACTIVE, input_entry_method_);
if (!in_mouse_cooldown_mode_ &&
event.y() <= kShowFullscreenExitControlHeight) {
// If the exit fullscreen prompt is being shown (say user just pressed
// F11 with the cursor on the top of the screen) then we suppress the
// fullscreen control host and just put it in cooldown mode.
const auto* bubble = browser_view_->exclusive_access_bubble();
if (bubble && bubble->IsShowing()) {
in_mouse_cooldown_mode_ = true;
} else {
ShowForInputEntryMethod(InputEntryMethod::MOUSE);
}
} else if (in_mouse_cooldown_mode_ &&
event.y() >= CalculateCursorBufferHeight()) {
in_mouse_cooldown_mode_ = false;
}
}
} else if (IsVisible()) {
Hide(true);
}
}
void FullscreenControlHost::OnTouchEvent(const ui::TouchEvent& event) {
if (input_entry_method_ != InputEntryMethod::TOUCH) {
return;
}
DCHECK(IsVisible());
// Hide the popup if it is showing and the user touches outside of the popup.
if (event.type() == ui::EventType::kTouchPressed && !IsAnimating()) {
Hide(true);
}
}
void FullscreenControlHost::OnGestureEvent(const ui::GestureEvent& event) {
if (!IsExitUiEnabled()) {
return;
}
if (event.type() == ui::EventType::kGestureLongPress && IsExitUiNeeded() &&
!IsVisible()) {
ShowForInputEntryMethod(InputEntryMethod::TOUCH);
}
}
void FullscreenControlHost::Hide(bool animate) {
if (IsPopupCreated()) {
GetPopup()->Hide(animate);
}
}
bool FullscreenControlHost::IsVisible() const {
return IsPopupCreated() && fullscreen_control_popup_->IsVisible();
}
FullscreenControlPopup* FullscreenControlHost::GetPopup() {
if (!IsPopupCreated()) {
fullscreen_control_popup_ = std::make_unique<FullscreenControlPopup>(
browser_view_->GetBubbleParentView(),
base::BindRepeating(
&FullscreenControlHost::OnExitFullscreenPopupClicked,
base::Unretained(this)),
base::BindRepeating(&FullscreenControlHost::OnVisibilityChanged,
base::Unretained(this)));
}
return fullscreen_control_popup_.get();
}
bool FullscreenControlHost::IsPopupCreated() const {
return fullscreen_control_popup_.get() != nullptr;
}
bool FullscreenControlHost::IsAnimating() const {
return IsPopupCreated() && fullscreen_control_popup_->IsAnimating();
}
void FullscreenControlHost::ShowForInputEntryMethod(
InputEntryMethod input_entry_method) {
input_entry_method_ = input_entry_method;
auto* bubble = browser_view_->exclusive_access_bubble();
if (bubble) {
bubble->HideImmediately();
}
GetPopup()->Show(browser_view_->GetClientAreaBoundsInScreen());
// Exit cooldown mode in case the exit UI is triggered by a different method.
in_mouse_cooldown_mode_ = false;
}
void FullscreenControlHost::OnVisibilityChanged() {
if (!IsVisible()) {
input_entry_method_ = InputEntryMethod::NOT_ACTIVE;
key_press_delay_timer_.Stop();
} else if (input_entry_method_ == InputEntryMethod::MOUSE) {
StartPopupTimeout(InputEntryMethod::MOUSE, kMousePopupTimeout);
} else if (input_entry_method_ == InputEntryMethod::TOUCH) {
StartPopupTimeout(InputEntryMethod::TOUCH, kTouchPopupTimeout);
}
if (on_popup_visibility_changed_) {
std::move(on_popup_visibility_changed_).Run();
}
}
void FullscreenControlHost::StartPopupTimeout(
InputEntryMethod expected_input_method,
base::TimeDelta timeout) {
popup_timeout_timer_.Start(
FROM_HERE, timeout,
base::BindOnce(&FullscreenControlHost::OnPopupTimeout,
base::Unretained(this), expected_input_method));
}
void FullscreenControlHost::OnPopupTimeout(
InputEntryMethod expected_input_method) {
if (IsVisible() && !IsAnimating() &&
input_entry_method_ == expected_input_method) {
if (input_entry_method_ == InputEntryMethod::MOUSE) {
in_mouse_cooldown_mode_ = true;
}
Hide(true);
}
}
bool FullscreenControlHost::IsExitUiNeeded() {
return browser_view_->IsFullscreen() &&
browser_view_->CanUserExitFullscreen() &&
browser_view_->ShouldHideUIForFullscreen();
}
bool FullscreenControlHost::IsPointerLocked() {
if (!browser_view_) {
return false;
}
auto* web_contents = browser_view_->GetActiveWebContents();
if (!web_contents) {
return false;
}
auto* rwhv = web_contents->GetRenderWidgetHostView();
if (!rwhv) {
return false;
}
return rwhv->IsPointerLocked();
}
float FullscreenControlHost::CalculateCursorBufferHeight() const {
float control_bottom = FullscreenControlPopup::GetButtonBottomOffset();
return control_bottom * kExitHeightScaleFactor;
}
void FullscreenControlHost::OnExitFullscreenPopupClicked() {
base::RecordAction(
base::UserMetricsAction("ExitFullscreen_PopupCloseButton"));
browser_view_->ExitFullscreen();
}
|