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(this);
     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 // static
     70 void KeyboardOverlayView::ShowDialog(
     71     content::BrowserContext* context,
     72     WebContentsHandler* handler,
     73     const GURL& url) {
     74   if (Shell::GetInstance()->overlay_filter()->IsActive())
     75     return;
     76 
     77   KeyboardOverlayDelegate* delegate = new KeyboardOverlayDelegate(
     78       l10n_util::GetStringUTF16(IDS_ASH_KEYBOARD_OVERLAY_TITLE), url);
     79   KeyboardOverlayView* view =
     80       new KeyboardOverlayView(context, delegate, handler);
     81   delegate->Show(view);
     82 
     83   Shell::GetInstance()->overlay_filter()->Activate(view);
     84 }
     85 
     86 void KeyboardOverlayView::WindowClosing() {
     87   Cancel();
     88 }
     89 
     90 // static
     91 void KeyboardOverlayView::GetCancelingKeysForTesting(
     92     std::vector<KeyboardOverlayView::KeyEventData>* canceling_keys) {
     93   CHECK(canceling_keys);
     94   canceling_keys->clear();
     95   for (size_t i = 0; i < arraysize(kCancelKeys); ++i)
     96     canceling_keys->push_back(kCancelKeys[i]);
     97 }
     98 
     99 }  // namespace ash
    100