Home | History | Annotate | Download | only in keyboard_overlay
      1 // Copyright (c) 2012 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/keyboard_overlay/keyboard_overlay_view.h"
      6 
      7 #include "ash/keyboard_overlay/keyboard_overlay_delegate.h"
      8 #include "ash/shell.h"
      9 #include "base/strings/utf_string_conversions.h"
     10 #include "content/public/browser/browser_context.h"
     11 #include "grit/ash_strings.h"
     12 #include "ui/base/l10n/l10n_util.h"
     13 #include "ui/events/event.h"
     14 #include "ui/gfx/screen.h"
     15 #include "ui/views/widget/widget.h"
     16 #include "ui/web_dialogs/web_dialog_delegate.h"
     17 
     18 using ui::WebDialogDelegate;
     19 
     20 namespace {
     21 
     22 // Keys to invoke Cancel (Escape, Ctrl+Alt+/, or Shift+Ctrl+Alt+/, Help, F14).
     23 const ash::KeyboardOverlayView::KeyEventData kCancelKeys[] = {
     24   { ui::VKEY_ESCAPE, ui::EF_NONE},
     25   { ui::VKEY_OEM_2, ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN },
     26   { ui::VKEY_OEM_2, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN },
     27   { ui::VKEY_HELP, ui::EF_NONE },
     28   { ui::VKEY_F14, ui::EF_NONE },
     29 };
     30 
     31 }
     32 
     33 namespace ash {
     34 
     35 KeyboardOverlayView::KeyboardOverlayView(
     36     content::BrowserContext* context,
     37     WebDialogDelegate* delegate,
     38     WebContentsHandler* handler)
     39     : views::WebDialogView(context, delegate, handler) {
     40 }
     41 
     42 KeyboardOverlayView::~KeyboardOverlayView() {
     43 }
     44 
     45 void KeyboardOverlayView::Cancel() {
     46   Shell::GetInstance()->overlay_filter()->Deactivate();
     47   views::Widget* widget = GetWidget();
     48   if (widget)
     49     widget->Close();
     50 }
     51 
     52 bool KeyboardOverlayView::IsCancelingKeyEvent(ui::KeyEvent* event) {
     53   if (event->type() != ui::ET_KEY_PRESSED)
     54     return false;
     55   // Ignore the caps lock state.
     56   const int flags = (event->flags() & ~ui::EF_CAPS_LOCK_DOWN);
     57   for (size_t i = 0; i < arraysize(kCancelKeys); ++i) {
     58     if ((kCancelKeys[i].key_code == event->key_code()) &&
     59         (kCancelKeys[i].flags == flags))
     60       return true;
     61   }
     62   return false;
     63 }
     64 
     65 aura::Window* KeyboardOverlayView::GetWindow() {
     66   return GetWidget()->GetNativeWindow();
     67 }
     68 
     69 void KeyboardOverlayView::ShowDialog(
     70     content::BrowserContext* context,
     71     WebContentsHandler* handler,
     72     const GURL& url) {
     73   KeyboardOverlayDelegate* delegate = new KeyboardOverlayDelegate(
     74       l10n_util::GetStringUTF16(IDS_ASH_KEYBOARD_OVERLAY_TITLE), url);
     75   KeyboardOverlayView* view =
     76       new KeyboardOverlayView(context, delegate, handler);
     77   delegate->Show(view);
     78 
     79   Shell::GetInstance()->overlay_filter()->Activate(view);
     80 }
     81 
     82 void KeyboardOverlayView::WindowClosing() {
     83   Cancel();
     84 }
     85 
     86 // static
     87 void KeyboardOverlayView::GetCancelingKeysForTesting(
     88     std::vector<KeyboardOverlayView::KeyEventData>* canceling_keys) {
     89   CHECK(canceling_keys);
     90   canceling_keys->clear();
     91   for (size_t i = 0; i < arraysize(kCancelKeys); ++i)
     92     canceling_keys->push_back(kCancelKeys[i]);
     93 }
     94 
     95 }  // namespace ash
     96