Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ash / keyboard_overlay / keyboard_overlay_view.cc
blobe5a71f9a2453fd17f7132c876ac56a7f3519bdaa
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.
5 #include "ash/keyboard_overlay/keyboard_overlay_view.h"
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"
18 using ui::WebDialogDelegate;
20 namespace {
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 },
33 namespace ash {
35 KeyboardOverlayView::KeyboardOverlayView(
36 content::BrowserContext* context,
37 WebDialogDelegate* delegate,
38 WebContentsHandler* handler)
39 : views::WebDialogView(context, delegate, handler) {
42 KeyboardOverlayView::~KeyboardOverlayView() {
45 void KeyboardOverlayView::Cancel() {
46 Shell::GetInstance()->overlay_filter()->Deactivate(this);
47 views::Widget* widget = GetWidget();
48 if (widget)
49 widget->Close();
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;
62 return false;
65 aura::Window* KeyboardOverlayView::GetWindow() {
66 return GetWidget()->GetNativeWindow();
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;
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);
83 Shell::GetInstance()->overlay_filter()->Activate(view);
86 void KeyboardOverlayView::WindowClosing() {
87 Cancel();
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]);
99 } // namespace ash