Commit eed46f89 authored by sky's avatar sky Committed by Commit bot

Move processing out of AcceleratorDelegate

AcceleratorDelegate now trivially forwards to AcceleratorRouter, which
is now in terms of ash/common types.

BUG=612331
TEST=none
R=jamescook@chromium.org

Review-Url: https://codereview.chromium.org/2171983002
Cr-Commit-Position: refs/heads/master@{#407198}
parent 80155b30
......@@ -4,114 +4,23 @@
#include "ash/accelerators/accelerator_delegate.h"
#include "ash/common/accelerators/accelerator_controller.h"
#include "ash/common/wm/window_state.h"
#include "ash/common/wm_shell.h"
#include "ash/shell.h"
#include "ash/wm/window_state_aura.h"
#include "ui/base/accelerators/accelerator.h"
#include "ash/aura/wm_window_aura.h"
#include "ash/common/accelerators/accelerator_router.h"
#include "ui/aura/window.h"
#include "ui/events/event.h"
#include "ui/wm/core/window_util.h"
namespace ash {
namespace {
AcceleratorDelegate::AcceleratorDelegate() : router_(new AcceleratorRouter) {}
// Returns true if |key_code| is a key usually handled directly by the shell.
bool IsSystemKey(ui::KeyboardCode key_code) {
#if defined(OS_CHROMEOS)
switch (key_code) {
case ui::VKEY_MEDIA_LAUNCH_APP2: // Fullscreen button.
case ui::VKEY_MEDIA_LAUNCH_APP1: // Overview button.
case ui::VKEY_BRIGHTNESS_DOWN:
case ui::VKEY_BRIGHTNESS_UP:
case ui::VKEY_KBD_BRIGHTNESS_DOWN:
case ui::VKEY_KBD_BRIGHTNESS_UP:
case ui::VKEY_VOLUME_MUTE:
case ui::VKEY_VOLUME_DOWN:
case ui::VKEY_VOLUME_UP:
case ui::VKEY_POWER:
return true;
default:
return false;
}
#endif // defined(OS_CHROMEOS)
return false;
}
} // namespace
AcceleratorDelegate::AcceleratorDelegate() {}
AcceleratorDelegate::~AcceleratorDelegate() {}
bool AcceleratorDelegate::ProcessAccelerator(
const ui::KeyEvent& key_event,
const ui::Accelerator& accelerator) {
// Special hardware keys like brightness and volume are handled in
// special way. However, some windows can override this behavior
// (e.g. Chrome v1 apps by default and Chrome v2 apps with
// permission) by setting a window property.
if (IsSystemKey(key_event.key_code()) && !CanConsumeSystemKeys(key_event)) {
// System keys are always consumed regardless of whether they trigger an
// accelerator to prevent windows from seeing unexpected key up events.
WmShell::Get()->accelerator_controller()->Process(accelerator);
return true;
}
if (!ShouldProcessAcceleratorNow(key_event, accelerator))
return false;
return WmShell::Get()->accelerator_controller()->Process(accelerator);
}
// Uses the top level window so if the target is a web contents window the
// containing parent window will be checked for the property.
bool AcceleratorDelegate::CanConsumeSystemKeys(const ui::KeyEvent& event) {
aura::Window* target = static_cast<aura::Window*>(event.target());
DCHECK(target);
aura::Window* top_level = ::wm::GetToplevelWindow(target);
return top_level && wm::GetWindowState(top_level)->can_consume_system_keys();
}
// Returns true if the |accelerator| should be processed now, inside Ash's env
// event filter.
bool AcceleratorDelegate::ShouldProcessAcceleratorNow(
const ui::KeyEvent& event,
const ui::Accelerator& accelerator) {
// On ChromeOS, If the accelerator is Search+<key(s)> then it must never be
// intercepted by apps or windows.
if (accelerator.IsCmdDown())
return true;
aura::Window* target = static_cast<aura::Window*>(event.target());
DCHECK(target);
aura::Window::Windows root_windows = Shell::GetAllRootWindows();
if (std::find(root_windows.begin(), root_windows.end(), target) !=
root_windows.end())
return true;
aura::Window* top_level = ::wm::GetToplevelWindow(target);
AcceleratorController* accelerator_controller =
WmShell::Get()->accelerator_controller();
// Reserved accelerators (such as Power button) always have a prority.
if (accelerator_controller->IsReserved(accelerator))
return true;
// A full screen window has a right to handle all key events including the
// reserved ones.
if (top_level && wm::GetWindowState(top_level)->IsFullscreen()) {
// On ChromeOS, fullscreen windows are either browser or apps, which
// send key events to a web content first, then will process keys
// if the web content didn't consume them.
return false;
}
// Handle preferred accelerators (such as ALT-TAB) before sending
// to the target.
if (accelerator_controller->IsPreferred(accelerator))
return true;
return WmShell::Get()->GetAppListTargetVisibility();
return router_->ProcessAccelerator(
WmWindowAura::Get(static_cast<aura::Window*>(key_event.target())),
key_event, accelerator);
}
} // namespace ash
......@@ -5,6 +5,8 @@
#ifndef ASH_ACCELERATORS_ACCELERATOR_DELEGATE_H_
#define ASH_ACCELERATORS_ACCELERATOR_DELEGATE_H_
#include <memory>
#include "ash/ash_export.h"
#include "base/compiler_specific.h"
#include "base/macros.h"
......@@ -12,6 +14,8 @@
namespace ash {
class AcceleratorRouter;
class ASH_EXPORT AcceleratorDelegate
: NON_EXPORTED_BASE(public ::wm::AcceleratorDelegate) {
public:
......@@ -23,12 +27,7 @@ class ASH_EXPORT AcceleratorDelegate
const ui::Accelerator& accelerator) override;
private:
// Returns true if the window should be allowed a chance to handle
// system keys.
bool CanConsumeSystemKeys(const ui::KeyEvent& event);
bool ShouldProcessAcceleratorNow(const ui::KeyEvent& event,
const ui::Accelerator& accelerator);
std::unique_ptr<AcceleratorRouter> router_;
DISALLOW_COPY_AND_ASSIGN(AcceleratorDelegate);
};
......
......@@ -49,6 +49,8 @@
'common/accelerators/accelerator_controller.cc',
'common/accelerators/accelerator_controller.h',
'common/accelerators/accelerator_controller_delegate.h',
'common/accelerators/accelerator_router.cc',
'common/accelerators/accelerator_router.h',
'common/accelerators/accelerator_table.cc',
'common/accelerators/accelerator_table.h',
'common/accelerators/debug_commands.cc',
......
......@@ -286,6 +286,10 @@ WmWindow* WmWindowAura::GetToplevelWindow() {
return Get(window_->GetToplevelWindow());
}
WmWindow* WmWindowAura::GetToplevelWindowForFocus() {
return Get(::wm::GetToplevelWindow(window_));
}
void WmWindowAura::SetParentUsingContext(WmWindow* context,
const gfx::Rect& screen_bounds) {
aura::client::ParentWindowWithContext(window_, GetAuraWindow(context),
......
......@@ -76,6 +76,7 @@ class ASH_EXPORT WmWindowAura : public WmWindow, public aura::WindowObserver {
int GetIntProperty(WmWindowProperty key) override;
const wm::WindowState* GetWindowState() const override;
WmWindow* GetToplevelWindow() override;
WmWindow* GetToplevelWindowForFocus() override;
void SetParentUsingContext(WmWindow* context,
const gfx::Rect& screen_bounds) override;
void AddChild(WmWindow* window) override;
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/common/accelerators/accelerator_router.h"
#include "ash/common/accelerators/accelerator_controller.h"
#include "ash/common/wm/window_state.h"
#include "ash/common/wm_shell.h"
#include "ash/common/wm_window.h"
#include "base/stl_util.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/event.h"
namespace ash {
namespace {
// Returns true if |key_code| is a key usually handled directly by the shell.
bool IsSystemKey(ui::KeyboardCode key_code) {
#if defined(OS_CHROMEOS)
switch (key_code) {
case ui::VKEY_MEDIA_LAUNCH_APP2: // Fullscreen button.
case ui::VKEY_MEDIA_LAUNCH_APP1: // Overview button.
case ui::VKEY_BRIGHTNESS_DOWN:
case ui::VKEY_BRIGHTNESS_UP:
case ui::VKEY_KBD_BRIGHTNESS_DOWN:
case ui::VKEY_KBD_BRIGHTNESS_UP:
case ui::VKEY_VOLUME_MUTE:
case ui::VKEY_VOLUME_DOWN:
case ui::VKEY_VOLUME_UP:
case ui::VKEY_POWER:
return true;
default:
return false;
}
#endif // defined(OS_CHROMEOS)
return false;
}
} // namespace
AcceleratorRouter::AcceleratorRouter() {}
AcceleratorRouter::~AcceleratorRouter() {}
bool AcceleratorRouter::ProcessAccelerator(WmWindow* target,
const ui::KeyEvent& key_event,
const ui::Accelerator& accelerator) {
// Callers should never supply null.
DCHECK(target);
// Special hardware keys like brightness and volume are handled in
// special way. However, some windows can override this behavior
// (e.g. Chrome v1 apps by default and Chrome v2 apps with
// permission) by setting a window property.
if (IsSystemKey(key_event.key_code()) &&
!CanConsumeSystemKeys(target, key_event)) {
// System keys are always consumed regardless of whether they trigger an
// accelerator to prevent windows from seeing unexpected key up events.
WmShell::Get()->accelerator_controller()->Process(accelerator);
return true;
}
if (!ShouldProcessAcceleratorNow(target, key_event, accelerator))
return false;
return WmShell::Get()->accelerator_controller()->Process(accelerator);
}
bool AcceleratorRouter::CanConsumeSystemKeys(WmWindow* target,
const ui::KeyEvent& event) {
// Uses the top level window so if the target is a web contents window the
// containing parent window will be checked for the property.
WmWindow* top_level = target->GetToplevelWindowForFocus();
return top_level && top_level->GetWindowState()->can_consume_system_keys();
}
bool AcceleratorRouter::ShouldProcessAcceleratorNow(
WmWindow* target,
const ui::KeyEvent& event,
const ui::Accelerator& accelerator) {
// Callers should never supply null.
DCHECK(target);
// On ChromeOS, If the accelerator is Search+<key(s)> then it must never be
// intercepted by apps or windows.
if (accelerator.IsCmdDown())
return true;
if (ContainsValue(WmShell::Get()->GetAllRootWindows(), target))
return true;
AcceleratorController* accelerator_controller =
WmShell::Get()->accelerator_controller();
// Reserved accelerators (such as Power button) always have a prority.
if (accelerator_controller->IsReserved(accelerator))
return true;
// A full screen window has a right to handle all key events including the
// reserved ones.
WmWindow* top_level = target->GetToplevelWindowForFocus();
if (top_level && top_level->GetWindowState()->IsFullscreen()) {
// On ChromeOS, fullscreen windows are either browser or apps, which
// send key events to a web content first, then will process keys
// if the web content didn't consume them.
return false;
}
// Handle preferred accelerators (such as ALT-TAB) before sending
// to the target.
if (accelerator_controller->IsPreferred(accelerator))
return true;
return WmShell::Get()->GetAppListTargetVisibility();
}
} // namespace ash
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ASH_ACCELERATORS_ACCELERATOR_ROUTER_H_
#define ASH_ACCELERATORS_ACCELERATOR_ROUTER_H_
#include "ash/ash_export.h"
#include "base/macros.h"
namespace ui {
class Accelerator;
class KeyEvent;
}
namespace ash {
class WmWindow;
// AcceleratorRouter does a minimal amount of processing before routing the
// accelerator to the AcceleratorController. AcceleratorRouter may also decide
// not to process certain accelerators.
class ASH_EXPORT AcceleratorRouter {
public:
AcceleratorRouter();
~AcceleratorRouter();
// Returns true if event should be consumed. |target| is the target of the
// event.
bool ProcessAccelerator(WmWindow* target,
const ui::KeyEvent& event,
const ui::Accelerator& accelerator);
private:
// Returns true if the window should be allowed a chance to handle
// system keys.
bool CanConsumeSystemKeys(WmWindow* target, const ui::KeyEvent& event);
// Returns true if the |accelerator| should be processed now.
bool ShouldProcessAcceleratorNow(WmWindow* target,
const ui::KeyEvent& event,
const ui::Accelerator& accelerator);
DISALLOW_COPY_AND_ASSIGN(AcceleratorRouter);
};
} // namespace ash
#endif // ASH_ACCELERATORS_ACCELERATOR_ROUTER_H_
......@@ -126,7 +126,11 @@ class ASH_EXPORT WmWindow {
}
virtual const wm::WindowState* GetWindowState() const = 0;
// The implementation of this matches aura::Window::GetToplevelWindow().
virtual WmWindow* GetToplevelWindow() = 0;
// The implementation of this matches
// aura::client::ActivationClient::GetToplevelWindow().
virtual WmWindow* GetToplevelWindowForFocus() = 0;
// See aura::client::ParentWindowWithContext() for details of what this does.
virtual void SetParentUsingContext(WmWindow* context,
......
......@@ -352,6 +352,12 @@ WmWindow* WmWindowMus::GetToplevelWindow() {
return WmShellMus::GetToplevelAncestor(window_);
}
WmWindow* WmWindowMus::GetToplevelWindowForFocus() {
// TODO(sky): resolve if we really need two notions of top-level. In the mus
// world they are the same.
return WmShellMus::GetToplevelAncestor(window_);
}
void WmWindowMus::SetParentUsingContext(WmWindow* context,
const gfx::Rect& screen_bounds) {
wm::GetDefaultParent(context, this, screen_bounds)->AddChild(this);
......
......@@ -134,6 +134,7 @@ class WmWindowMus : public WmWindow, public ::ui::WindowObserver {
int GetIntProperty(WmWindowProperty key) override;
const wm::WindowState* GetWindowState() const override;
WmWindow* GetToplevelWindow() override;
WmWindow* GetToplevelWindowForFocus() override;
void SetParentUsingContext(WmWindow* context,
const gfx::Rect& screen_bounds) override;
void AddChild(WmWindow* window) override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment