Commit 60d677dd authored by oshima@chromium.org's avatar oshima@chromium.org

Move common accelerator code from ash to ui/wm/core

accelerator_filter_unittest stays in ash because it requires ash.

BUG=375534

Review URL: https://codereview.chromium.org/307533007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@273811 0039d316-1c4b-4281-b951-d872f2087c98
parent 85e57d4d
// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "ash/accelerators/accelerator_filter.h" #include "ash/accelerators/accelerator_delegate.h"
#include "ash/accelerators/accelerator_controller.h" #include "ash/accelerators/accelerator_controller.h"
#include "ash/shell.h" #include "ash/shell.h"
#include "ash/wm/window_state.h" #include "ash/wm/window_state.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/base/accelerators/accelerator.h" #include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/accelerator_manager.h"
#include "ui/events/event.h" #include "ui/events/event.h"
#include "ui/wm/core/window_util.h" #include "ui/wm/core/window_util.h"
namespace ash { namespace ash {
namespace { namespace {} // namespace
const int kModifierFlagMask = (ui::EF_SHIFT_DOWN | AcceleratorDelegate::AcceleratorDelegate() {
ui::EF_CONTROL_DOWN | }
ui::EF_ALT_DOWN); AcceleratorDelegate::~AcceleratorDelegate() {
}
// Returns true if |key_code| is a key usually handled directly by the shell. void AcceleratorDelegate::PreProcessAccelerator(
bool IsSystemKey(ui::KeyboardCode key_code) { const ui::Accelerator& accelerator) {
#if defined(OS_CHROMEOS) // Fill out context object so AcceleratorController will know what
switch (key_code) { // was the previous accelerator or if the current accelerator is repeated.
case ui::VKEY_MEDIA_LAUNCH_APP2: // Fullscreen button. Shell::GetInstance()->accelerator_controller()->context()->UpdateContext(
case ui::VKEY_MEDIA_LAUNCH_APP1: // Overview button. accelerator);
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:
return true;
default:
return false;
}
#endif // defined(OS_CHROMEOS)
return false;
} }
// Returns true if the window should be allowed a chance to handle system keys.
// Uses the top level window so if the target is a web contents window the // Uses the top level window so if the target is a web contents window the
// containing parent window will be checked for the property. // containing parent window will be checked for the property.
bool CanConsumeSystemKeys(aura::Window* target) { bool AcceleratorDelegate::CanConsumeSystemKeys(const ui::KeyEvent& event) {
aura::Window* target = static_cast<aura::Window*>(event.target());
if (!target) // Can be NULL in tests. if (!target) // Can be NULL in tests.
return false; return false;
aura::Window* top_level = ::wm::GetToplevelWindow(target); aura::Window* top_level = ::wm::GetToplevelWindow(target);
...@@ -53,8 +39,10 @@ bool CanConsumeSystemKeys(aura::Window* target) { ...@@ -53,8 +39,10 @@ bool CanConsumeSystemKeys(aura::Window* target) {
// Returns true if the |accelerator| should be processed now, inside Ash's env // Returns true if the |accelerator| should be processed now, inside Ash's env
// event filter. // event filter.
bool ShouldProcessAcceleratorsNow(const ui::Accelerator& accelerator, bool AcceleratorDelegate::ShouldProcessAcceleratorNow(
aura::Window* target) { const ui::KeyEvent& event,
const ui::Accelerator& accelerator) {
aura::Window* target = static_cast<aura::Window*>(event.target());
if (!target) if (!target)
return true; return true;
...@@ -83,52 +71,9 @@ bool ShouldProcessAcceleratorsNow(const ui::Accelerator& accelerator, ...@@ -83,52 +71,9 @@ bool ShouldProcessAcceleratorsNow(const ui::Accelerator& accelerator,
accelerator); accelerator);
} }
} // namespace bool AcceleratorDelegate::ProcessAccelerator(
const ui::Accelerator& accelerator) {
//////////////////////////////////////////////////////////////////////////////// return Shell::GetInstance()->accelerator_controller()->Process(accelerator);
// AcceleratorFilter, public:
AcceleratorFilter::AcceleratorFilter() {
}
AcceleratorFilter::~AcceleratorFilter() {
}
////////////////////////////////////////////////////////////////////////////////
// AcceleratorFilter, EventFilter implementation:
void AcceleratorFilter::OnKeyEvent(ui::KeyEvent* event) {
const ui::EventType type = event->type();
if (type != ui::ET_KEY_PRESSED && type != ui::ET_KEY_RELEASED)
return;
if (event->is_char())
return;
ui::Accelerator accelerator(event->key_code(),
event->flags() & kModifierFlagMask);
accelerator.set_type(type);
// Fill out context object so AcceleratorController will know what
// was the previous accelerator or if the current accelerator is repeated.
AcceleratorController* accelerator_controller =
Shell::GetInstance()->accelerator_controller();
accelerator_controller->context()->UpdateContext(accelerator);
aura::Window* target = static_cast<aura::Window*>(event->target());
// Handle special hardware keys like brightness and volume. 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(event->key_code()) && !CanConsumeSystemKeys(target)) {
accelerator_controller->Process(accelerator);
// These keys are always consumed regardless of whether they trigger an
// accelerator to prevent windows from seeing unexpected key up events.
event->StopPropagation();
return;
}
if (!ShouldProcessAcceleratorsNow(accelerator, target))
return;
if (accelerator_controller->Process(accelerator))
event->StopPropagation();
} }
} // namespace ash } // namespace ash
// 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.
#ifndef ASH_ACCELERATORS_ACCELERATOR_DELEGATE_H_
#define ASH_ACCELERATORS_ACCELERATOR_DELEGATE_H_
#include "ash/ash_export.h"
#include "base/macros.h"
#include "ui/wm/core/accelerator_delegate.h"
namespace ash {
class ASH_EXPORT AcceleratorDelegate
: NON_EXPORTED_BASE(public wm::AcceleratorDelegate) {
public:
AcceleratorDelegate();
virtual ~AcceleratorDelegate();
// wm::AcceleratorDelegate:
virtual void PreProcessAccelerator(
const ui::Accelerator& accelerator) OVERRIDE;
virtual bool CanConsumeSystemKeys(const ui::KeyEvent& event) OVERRIDE;
virtual bool ShouldProcessAcceleratorNow(
const ui::KeyEvent& key_event,
const ui::Accelerator& accelerator) OVERRIDE;
virtual bool ProcessAccelerator(const ui::Accelerator& accelerator) OVERRIDE;
private:
DISALLOW_COPY_AND_ASSIGN(AcceleratorDelegate);
};
} // namespace ash
#endif // ASH_ACCELERATORS_ACCELERATOR_DELEGATE_H_
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "ash/accelerators/accelerator_filter.h" #include "ui/wm/core/accelerator_filter.h"
#include "ash/accelerators/accelerator_controller.h" #include "ash/accelerators/accelerator_controller.h"
#include "ash/accelerators/accelerator_delegate.h"
#include "ash/shell.h" #include "ash/shell.h"
#include "ash/shell_window_ids.h" #include "ash/shell_window_ids.h"
#include "ash/test/ash_test_base.h" #include "ash/test/ash_test_base.h"
...@@ -86,7 +87,8 @@ TEST_F(AcceleratorFilterTest, TestCapsLockMask) { ...@@ -86,7 +87,8 @@ TEST_F(AcceleratorFilterTest, TestCapsLockMask) {
// Tests if special hardware keys like brightness and volume are consumed as // Tests if special hardware keys like brightness and volume are consumed as
// expected by the shell. // expected by the shell.
TEST_F(AcceleratorFilterTest, CanConsumeSystemKeys) { TEST_F(AcceleratorFilterTest, CanConsumeSystemKeys) {
AcceleratorFilter filter; ::wm::AcceleratorFilter filter(
scoped_ptr< ::wm::AcceleratorDelegate>(new AcceleratorDelegate).Pass());
// Normal keys are not consumed. // Normal keys are not consumed.
ui::KeyEvent press_a(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE, false); ui::KeyEvent press_a(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE, false);
......
...@@ -57,8 +57,8 @@ ...@@ -57,8 +57,8 @@
'accelerators/accelerator_controller.h', 'accelerators/accelerator_controller.h',
'accelerators/nested_accelerator_delegate.cc', 'accelerators/nested_accelerator_delegate.cc',
'accelerators/nested_accelerator_delegate.h', 'accelerators/nested_accelerator_delegate.h',
'accelerators/accelerator_filter.cc', 'accelerators/accelerator_delegate.cc',
'accelerators/accelerator_filter.h', 'accelerators/accelerator_delegate.h',
'accelerators/accelerator_table.cc', 'accelerators/accelerator_table.cc',
'accelerators/accelerator_table.h', 'accelerators/accelerator_table.h',
'accelerators/debug_commands.cc', 'accelerators/debug_commands.cc',
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
#include <string> #include <string>
#include "ash/accelerators/accelerator_controller.h" #include "ash/accelerators/accelerator_controller.h"
#include "ash/accelerators/accelerator_filter.h" #include "ash/accelerators/accelerator_delegate.h"
#include "ash/accelerators/focus_manager_factory.h" #include "ash/accelerators/focus_manager_factory.h"
#include "ash/accelerators/nested_accelerator_delegate.h" #include "ash/accelerators/nested_accelerator_delegate.h"
#include "ash/accelerometer/accelerometer_controller.h" #include "ash/accelerometer/accelerometer_controller.h"
...@@ -105,6 +105,7 @@ ...@@ -105,6 +105,7 @@
#include "ui/views/focus/focus_manager_factory.h" #include "ui/views/focus/focus_manager_factory.h"
#include "ui/views/widget/native_widget_aura.h" #include "ui/views/widget/native_widget_aura.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
#include "ui/wm/core/accelerator_filter.h"
#include "ui/wm/core/compound_event_filter.h" #include "ui/wm/core/compound_event_filter.h"
#include "ui/wm/core/focus_controller.h" #include "ui/wm/core/focus_controller.h"
#include "ui/wm/core/input_method_event_filter.h" #include "ui/wm/core/input_method_event_filter.h"
...@@ -922,7 +923,8 @@ void Shell::Init(const ShellInitParams& init_params) { ...@@ -922,7 +923,8 @@ void Shell::Init(const ShellInitParams& init_params) {
root_window->GetHost()->GetAcceleratedWidget())); root_window->GetHost()->GetAcceleratedWidget()));
AddPreTargetHandler(input_method_filter_.get()); AddPreTargetHandler(input_method_filter_.get());
accelerator_filter_.reset(new AcceleratorFilter); accelerator_filter_.reset(new ::wm::AcceleratorFilter(
scoped_ptr< ::wm::AcceleratorDelegate>(new AcceleratorDelegate).Pass()));
AddPreTargetHandler(accelerator_filter_.get()); AddPreTargetHandler(accelerator_filter_.get());
event_transformation_handler_.reset(new EventTransformationHandler); event_transformation_handler_.reset(new EventTransformationHandler);
......
...@@ -62,6 +62,7 @@ class TooltipController; ...@@ -62,6 +62,7 @@ class TooltipController;
} }
namespace wm { namespace wm {
class AcceleratorFilter;
class CompoundEventFilter; class CompoundEventFilter;
class InputMethodEventFilter; class InputMethodEventFilter;
class NestedAcceleratorController; class NestedAcceleratorController;
...@@ -75,7 +76,6 @@ namespace ash { ...@@ -75,7 +76,6 @@ namespace ash {
class AcceleratorController; class AcceleratorController;
class AccelerometerController; class AccelerometerController;
class AcceleratorFilter;
class AccessibilityDelegate; class AccessibilityDelegate;
class AppListController; class AppListController;
class AshNativeCursorManager; class AshNativeCursorManager;
...@@ -693,7 +693,7 @@ class ASH_EXPORT Shell : public SystemModalContainerEventFilterDelegate, ...@@ -693,7 +693,7 @@ class ASH_EXPORT Shell : public SystemModalContainerEventFilterDelegate,
scoped_ptr<SystemGestureEventFilter> system_gesture_filter_; scoped_ptr<SystemGestureEventFilter> system_gesture_filter_;
// An event filter that pre-handles global accelerators. // An event filter that pre-handles global accelerators.
scoped_ptr<AcceleratorFilter> accelerator_filter_; scoped_ptr< ::wm::AcceleratorFilter> accelerator_filter_;
// An event filter that pre-handles all key events to send them to an IME. // An event filter that pre-handles all key events to send them to an IME.
scoped_ptr< ::wm::InputMethodEventFilter> input_method_filter_; scoped_ptr< ::wm::InputMethodEventFilter> input_method_filter_;
......
// 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.
#ifndef UI_WM_CORE_ACCELERATOR_DELEGATE_H_
#define UI_WM_CORE_ACCELERATOR_DELEGATE_H_
namespace ui {
class Accelerator;
class KeyEvent;
}
namespace wm {
class AcceleratorDelegate {
public:
virtual ~AcceleratorDelegate() {}
// TODO(oshima): Move the repeat detection to AcceleratorFilter.
virtual void PreProcessAccelerator(const ui::Accelerator& accelerator) = 0;
// Returns true if the window should be allowed a chance to handle
// system keys.
virtual bool CanConsumeSystemKeys(const ui::KeyEvent& event) = 0;
// Returns true if the |accelerator| should be processed.
virtual bool ShouldProcessAcceleratorNow(
const ui::KeyEvent& key_event,
const ui::Accelerator& accelerator) = 0;
// Return true if the |accelerator| has been processed.
virtual bool ProcessAccelerator(const ui::Accelerator& accelerator) = 0;
};
} // namespace wm
#endif // UI_WM_CORE_ACCELERATOR_DELEGATE_H_
// 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 "ui/wm/core/accelerator_filter.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/event.h"
#include "ui/wm/core/accelerator_delegate.h"
namespace wm {
namespace {
const int kModifierFlagMask =
(ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN);
// 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:
return true;
default:
return false;
}
#endif // defined(OS_CHROMEOS)
return false;
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// AcceleratorFilter, public:
AcceleratorFilter::AcceleratorFilter(scoped_ptr<AcceleratorDelegate> delegate)
: delegate_(delegate.Pass()) {
}
AcceleratorFilter::~AcceleratorFilter() {
}
////////////////////////////////////////////////////////////////////////////////
// AcceleratorFilter, EventFilter implementation:
void AcceleratorFilter::OnKeyEvent(ui::KeyEvent* event) {
const ui::EventType type = event->type();
if (type != ui::ET_KEY_PRESSED && type != ui::ET_KEY_RELEASED)
return;
if (event->is_char())
return;
ui::Accelerator accelerator(event->key_code(),
event->flags() & kModifierFlagMask);
accelerator.set_type(event->type());
delegate_->PreProcessAccelerator(accelerator);
// Handle special hardware keys like brightness and volume. 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(event->key_code()) &&
!delegate_->CanConsumeSystemKeys(*event)) {
delegate_->ProcessAccelerator(accelerator);
// These keys are always consumed regardless of whether they trigger an
// accelerator to prevent windows from seeing unexpected key up events.
event->StopPropagation();
return;
}
if (!delegate_->ShouldProcessAcceleratorNow(*event, accelerator))
return;
if (delegate_->ProcessAccelerator(accelerator))
event->StopPropagation();
}
} // namespace wm
// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef ASH_ACCELERATORS_ACCELERATOR_FILTER_H_ #ifndef UI_WM_CORE_ACCELERATOR_FILTER_H_
#define ASH_ACCELERATORS_ACCELERATOR_FILTER_H_ #define UI_WM_CORE_ACCELERATOR_FILTER_H_
#include "ash/ash_export.h" #include "base/macros.h"
#include "base/basictypes.h" #include "base/memory/scoped_ptr.h"
#include "base/compiler_specific.h"
#include "ui/events/event_handler.h" #include "ui/events/event_handler.h"
#include "ui/wm/wm_export.h"
namespace ash { namespace wm {
class AcceleratorDelegate;
// AcceleratorFilter filters key events for AcceleratorControler handling global // AcceleratorFilter filters key events for AcceleratorControler handling global
// keyboard accelerators. // keyboard accelerators.
class ASH_EXPORT AcceleratorFilter : public ui::EventHandler { class WM_EXPORT AcceleratorFilter : public ui::EventHandler {
public: public:
AcceleratorFilter(); AcceleratorFilter(scoped_ptr<AcceleratorDelegate> delegate);
virtual ~AcceleratorFilter(); virtual ~AcceleratorFilter();
// Overridden from ui::EventHandler: // Overridden from ui::EventHandler:
virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE; virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE;
private: private:
scoped_ptr<AcceleratorDelegate> delegate_;
DISALLOW_COPY_AND_ASSIGN(AcceleratorFilter); DISALLOW_COPY_AND_ASSIGN(AcceleratorFilter);
}; };
} // namespace ash } // namespace wm
#endif // ASH_ACCELERATORS_ACCELERATOR_FILTER_H_ #endif // UI_WM_CORE_ACCELERATOR_FILTER_H_
...@@ -26,13 +26,9 @@ ...@@ -26,13 +26,9 @@
'WM_IMPLEMENTATION', 'WM_IMPLEMENTATION',
], ],
'sources': [ 'sources': [
'core/nested_accelerator_dispatcher_linux.cc', 'core/accelerator_delegate.h',
'core/nested_accelerator_dispatcher_win.cc', 'core/accelerator_filter.cc',
'core/nested_accelerator_dispatcher.cc', 'core/accelerator_filter.h',
'core/nested_accelerator_dispatcher.h',
'core/nested_accelerator_delegate.h',
'core/nested_accelerator_controller.cc',
'core/nested_accelerator_controller.h',
'core/base_focus_rules.h', 'core/base_focus_rules.h',
'core/base_focus_rules.cc', 'core/base_focus_rules.cc',
'core/base_focus_rules.h', 'core/base_focus_rules.h',
...@@ -57,6 +53,13 @@ ...@@ -57,6 +53,13 @@
'core/masked_window_targeter.h', 'core/masked_window_targeter.h',
'core/native_cursor_manager.h', 'core/native_cursor_manager.h',
'core/native_cursor_manager_delegate.h', 'core/native_cursor_manager_delegate.h',
'core/nested_accelerator_dispatcher_linux.cc',
'core/nested_accelerator_dispatcher_win.cc',
'core/nested_accelerator_dispatcher.cc',
'core/nested_accelerator_dispatcher.h',
'core/nested_accelerator_delegate.h',
'core/nested_accelerator_controller.cc',
'core/nested_accelerator_controller.h',
'core/shadow.cc', 'core/shadow.cc',
'core/shadow.h', 'core/shadow.h',
'core/shadow_controller.cc', 'core/shadow_controller.cc',
......
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