1 // Copyright 2014 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 "ash/accelerators/accelerator_delegate.h" 6 7 #include "ash/accelerators/accelerator_controller.h" 8 #include "ash/shell.h" 9 #include "ash/wm/window_state.h" 10 #include "ui/base/accelerators/accelerator.h" 11 #include "ui/events/event.h" 12 #include "ui/wm/core/window_util.h" 13 14 namespace ash { 15 namespace {} // namespace 16 17 AcceleratorDelegate::AcceleratorDelegate() { 18 } 19 AcceleratorDelegate::~AcceleratorDelegate() { 20 } 21 22 bool AcceleratorDelegate::ProcessAccelerator(const ui::KeyEvent& key_event, 23 const ui::Accelerator& accelerator, 24 KeyType key_type) { 25 // Special hardware keys like brightness and volume are handled in 26 // special way. However, some windows can override this behavior 27 // (e.g. Chrome v1 apps by default and Chrome v2 apps with 28 // permission) by setting a window property. 29 if (key_type == KEY_TYPE_SYSTEM && !CanConsumeSystemKeys(key_event)) { 30 // System keys are always consumed regardless of whether they trigger an 31 // accelerator to prevent windows from seeing unexpected key up events. 32 Shell::GetInstance()->accelerator_controller()->Process(accelerator); 33 return true; 34 } 35 if (!ShouldProcessAcceleratorNow(key_event, accelerator)) 36 return false; 37 return Shell::GetInstance()->accelerator_controller()->Process(accelerator); 38 } 39 40 // Uses the top level window so if the target is a web contents window the 41 // containing parent window will be checked for the property. 42 bool AcceleratorDelegate::CanConsumeSystemKeys(const ui::KeyEvent& event) { 43 aura::Window* target = static_cast<aura::Window*>(event.target()); 44 DCHECK(target); 45 aura::Window* top_level = ::wm::GetToplevelWindow(target); 46 return top_level && wm::GetWindowState(top_level)->can_consume_system_keys(); 47 } 48 49 // Returns true if the |accelerator| should be processed now, inside Ash's env 50 // event filter. 51 bool AcceleratorDelegate::ShouldProcessAcceleratorNow( 52 const ui::KeyEvent& event, 53 const ui::Accelerator& accelerator) { 54 aura::Window* target = static_cast<aura::Window*>(event.target()); 55 DCHECK(target); 56 57 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); 58 if (std::find(root_windows.begin(), root_windows.end(), target) != 59 root_windows.end()) 60 return true; 61 62 // A full screen window should be able to handle all key events including the 63 // reserved ones. 64 aura::Window* top_level = ::wm::GetToplevelWindow(target); 65 66 if (top_level && wm::GetWindowState(top_level)->IsFullscreen()) { 67 // TODO(yusukes): On Chrome OS, only browser and flash windows can be full 68 // screen. Launching an app in "open full-screen" mode is not supported yet. 69 // That makes the IsWindowFullscreen() check above almost meaningless 70 // because a browser and flash window do handle Ash accelerators anyway 71 // before they're passed to a page or flash content. 72 return false; 73 } 74 75 if (Shell::GetInstance()->GetAppListTargetVisibility()) 76 return true; 77 78 // Unless |target| is in the full screen state, handle reserved accelerators 79 // such as Alt+Tab now. 80 return Shell::GetInstance()->accelerator_controller()->IsReservedAccelerator( 81 accelerator); 82 } 83 84 } // namespace ash 85