Home | History | Annotate | Download | only in core
      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 "ui/wm/core/accelerator_filter.h"
      6 
      7 #include "ui/base/accelerators/accelerator.h"
      8 #include "ui/events/event.h"
      9 #include "ui/wm/core/accelerator_delegate.h"
     10 
     11 namespace wm {
     12 namespace {
     13 
     14 // Returns true if |key_code| is a key usually handled directly by the shell.
     15 bool IsSystemKey(ui::KeyboardCode key_code) {
     16 #if defined(OS_CHROMEOS)
     17   switch (key_code) {
     18     case ui::VKEY_MEDIA_LAUNCH_APP2:  // Fullscreen button.
     19     case ui::VKEY_MEDIA_LAUNCH_APP1:  // Overview button.
     20     case ui::VKEY_BRIGHTNESS_DOWN:
     21     case ui::VKEY_BRIGHTNESS_UP:
     22     case ui::VKEY_KBD_BRIGHTNESS_DOWN:
     23     case ui::VKEY_KBD_BRIGHTNESS_UP:
     24     case ui::VKEY_VOLUME_MUTE:
     25     case ui::VKEY_VOLUME_DOWN:
     26     case ui::VKEY_VOLUME_UP:
     27       return true;
     28     default:
     29       return false;
     30   }
     31 #endif  // defined(OS_CHROMEOS)
     32   return false;
     33 }
     34 
     35 }  // namespace
     36 
     37 ////////////////////////////////////////////////////////////////////////////////
     38 // AcceleratorFilter, public:
     39 
     40 AcceleratorFilter::AcceleratorFilter(scoped_ptr<AcceleratorDelegate> delegate)
     41     : delegate_(delegate.Pass()) {
     42 }
     43 
     44 AcceleratorFilter::~AcceleratorFilter() {
     45 }
     46 
     47 ////////////////////////////////////////////////////////////////////////////////
     48 // AcceleratorFilter, EventFilter implementation:
     49 
     50 void AcceleratorFilter::OnKeyEvent(ui::KeyEvent* event) {
     51   const ui::EventType type = event->type();
     52   DCHECK(event->target());
     53   if ((type != ui::ET_KEY_PRESSED && type != ui::ET_KEY_RELEASED) ||
     54       event->is_char() || !event->target()) {
     55     return;
     56   }
     57 
     58   ui::Accelerator accelerator = CreateAcceleratorFromKeyEvent(*event);
     59 
     60   AcceleratorDelegate::KeyType key_type =
     61       IsSystemKey(event->key_code()) ? AcceleratorDelegate::KEY_TYPE_SYSTEM
     62                                      : AcceleratorDelegate::KEY_TYPE_OTHER;
     63 
     64   if (delegate_->ProcessAccelerator(*event, accelerator, key_type))
     65     event->StopPropagation();
     66 }
     67 
     68 ui::Accelerator CreateAcceleratorFromKeyEvent(const ui::KeyEvent& key_event) {
     69   const int kModifierFlagMask =
     70       (ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN);
     71 
     72   ui::Accelerator accelerator(key_event.key_code(),
     73                               key_event.flags() & kModifierFlagMask);
     74   if (key_event.type() == ui::ET_KEY_RELEASED)
     75     accelerator.set_type(ui::ET_KEY_RELEASED);
     76   accelerator.set_is_repeat(key_event.IsRepeat());
     77   return accelerator;
     78 }
     79 
     80 }  // namespace wm
     81