Commit 2b1535ca authored by wutao's avatar wutao Committed by Commit Bot

cros: Introduce main view of KSV - KSV part 2.

Introduce the main view for Keyboard Shortcut Viewer (KSV).

Changes:
1. //ui/chromeos/ksv/views/*: the main view and test.
2. Populate the KeyboardShortcutItem's |shortcut_key_codes|.

Bug: 768932
Test: KeyboardShortcutViewTest and open the view and load metadata.
Change-Id: Ibc90872fc38600c1d81a63a49ab223f902a49afa
Reviewed-on: https://chromium-review.googlesource.com/880161
Commit-Queue: Tao Wu <wutao@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Reviewed-by: default avatarAhmed Fakhry <afakhry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532205}
parent 9877275d
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "chrome/browser/ui/ash/chrome_new_window_client.h" #include "chrome/browser/ui/ash/chrome_new_window_client.h"
#include "ash/content/keyboard_overlay/keyboard_overlay_view.h" #include "ash/content/keyboard_overlay/keyboard_overlay_view.h"
#include "ash/public/cpp/ash_switches.h"
#include "ash/public/interfaces/constants.mojom.h" #include "ash/public/interfaces/constants.mojom.h"
#include "base/macros.h" #include "base/macros.h"
#include "chrome/browser/chromeos/file_manager/app_id.h" #include "chrome/browser/chromeos/file_manager/app_id.h"
...@@ -215,6 +216,14 @@ void ChromeNewWindowClient::RestoreTab() { ...@@ -215,6 +216,14 @@ void ChromeNewWindowClient::RestoreTab() {
// TODO(crbug.com/755448): Remove this when the new shortcut viewer is enabled. // TODO(crbug.com/755448): Remove this when the new shortcut viewer is enabled.
void ChromeNewWindowClient::ShowKeyboardOverlay() { void ChromeNewWindowClient::ShowKeyboardOverlay() {
// TODO(wutao): It is only for cl reviewers to test the new shortcut viewer
// and will be removed when the new shortcut viewer is enabled.
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
ash::switches::kAshEnableKeyboardShortcutViewer)) {
keyboard_shortcut_viewer_util::ShowKeyboardShortcutViewer();
return;
}
// TODO(mazda): Move the show logic to ash (http://crbug.com/124222). // TODO(mazda): Move the show logic to ash (http://crbug.com/124222).
Profile* profile = ProfileManager::GetActiveUserProfile(); Profile* profile = ProfileManager::GetActiveUserProfile();
std::string url(chrome::kChromeUIKeyboardOverlayURL); std::string url(chrome::kChromeUIKeyboardOverlayURL);
......
...@@ -4,6 +4,7 @@ include_rules = [ ...@@ -4,6 +4,7 @@ include_rules = [
# completely independent mojo app under mustash. # completely independent mojo app under mustash.
"-ash", "-ash",
"-chrome", "-chrome",
"+ash/wm/window_util.h",
"+chrome/browser/ui/ash/ksv", "+chrome/browser/ui/ash/ksv",
"+ui/chromeos/ksv", "+ui/chromeos/ksv",
] ]
......
...@@ -4,10 +4,14 @@ ...@@ -4,10 +4,14 @@
#include "chrome/browser/ui/ash/ksv/keyboard_shortcut_viewer_util.h" #include "chrome/browser/ui/ash/ksv/keyboard_shortcut_viewer_util.h"
#include "ash/wm/window_util.h"
#include "ui/chromeos/ksv/views/keyboard_shortcut_view.h"
namespace keyboard_shortcut_viewer_util { namespace keyboard_shortcut_viewer_util {
void ShowKeyboardShortcutViewer() { void ShowKeyboardShortcutViewer() {
// TODO(wutao): create KSV window. keyboard_shortcut_viewer::KeyboardShortcutView::Show(
ash::wm::GetActiveWindow());
} }
} // namespace keyboard_shortcut_viewer_util } // namespace keyboard_shortcut_viewer_util
...@@ -81,6 +81,7 @@ test("ui_chromeos_unittests") { ...@@ -81,6 +81,7 @@ test("ui_chromeos_unittests") {
"//skia", "//skia",
"//testing/gtest", "//testing/gtest",
"//ui/aura:test_support", "//ui/aura:test_support",
"//ui/chromeos/ksv:ksv_unittests",
"//ui/compositor", "//ui/compositor",
"//ui/events:test_support", "//ui/events:test_support",
"//ui/gl:test_support", "//ui/gl:test_support",
......
...@@ -11,6 +11,8 @@ source_set("ksv") { ...@@ -11,6 +11,8 @@ source_set("ksv") {
"keyboard_shortcut_viewer_metadata.cc", "keyboard_shortcut_viewer_metadata.cc",
"keyboard_shortcut_viewer_metadata.h", "keyboard_shortcut_viewer_metadata.h",
"ksv_export.h", "ksv_export.h",
"views/keyboard_shortcut_view.cc",
"views/keyboard_shortcut_view.h",
] ]
defines = [ "KSV_IMPLEMENTATION" ] defines = [ "KSV_IMPLEMENTATION" ]
...@@ -18,5 +20,19 @@ source_set("ksv") { ...@@ -18,5 +20,19 @@ source_set("ksv") {
deps = [ deps = [
"//ui/chromeos/strings", "//ui/chromeos/strings",
"//ui/events:events_base", "//ui/events:events_base",
"//ui/views",
]
}
source_set("ksv_unittests") {
testonly = true
sources = [
"views/keyboard_shortcut_view_unittest.cc",
]
deps = [
":ksv",
"//testing/gtest",
"//ui/views",
"//ui/views:test_support",
] ]
} }
afakhry@chromium.org
wutao@chromium.org
...@@ -4,13 +4,36 @@ ...@@ -4,13 +4,36 @@
#include "ui/chromeos/ksv/keyboard_shortcut_viewer_metadata.h" #include "ui/chromeos/ksv/keyboard_shortcut_viewer_metadata.h"
#include "base/logging.h"
#include "base/macros.h" #include "base/macros.h"
#include "ui/chromeos/ksv/keyboard_shortcut_item.h" #include "ui/chromeos/ksv/keyboard_shortcut_item.h"
#include "ui/chromeos/strings/grit/ui_chromeos_strings.h" #include "ui/chromeos/strings/grit/ui_chromeos_strings.h"
#include "ui/events/event_constants.h" #include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes.h"
namespace keyboard_shortcut_viewer { namespace keyboard_shortcut_viewer {
namespace {
// Get the keyboard codes for modifiers.
ui::KeyboardCode GetModifierKeyCode(ui::EventFlags modifier) {
switch (modifier) {
case ui::EF_CONTROL_DOWN:
return ui::VKEY_CONTROL;
case ui::EF_ALT_DOWN:
return ui::VKEY_LMENU;
case ui::EF_SHIFT_DOWN:
return ui::VKEY_SHIFT;
case ui::EF_COMMAND_DOWN:
return ui::VKEY_COMMAND;
default:
NOTREACHED();
return ui::VKEY_UNKNOWN;
}
}
} // namespace
const std::vector<KeyboardShortcutItem>& GetKeyboardShortcutItemList() { const std::vector<KeyboardShortcutItem>& GetKeyboardShortcutItemList() {
CR_DEFINE_STATIC_LOCAL( CR_DEFINE_STATIC_LOCAL(
std::vector<KeyboardShortcutItem>, item_list, std::vector<KeyboardShortcutItem>, item_list,
...@@ -34,6 +57,34 @@ const std::vector<KeyboardShortcutItem>& GetKeyboardShortcutItemList() { ...@@ -34,6 +57,34 @@ const std::vector<KeyboardShortcutItem>& GetKeyboardShortcutItemList() {
// |accelerator_ids| // |accelerator_ids|
{{ui::VKEY_OEM_MINUS, ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN}, {{ui::VKEY_OEM_MINUS, ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN},
{ui::VKEY_OEM_PLUS, ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN}}}})); {ui::VKEY_OEM_PLUS, ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN}}}}));
CR_DEFINE_STATIC_LOCAL(bool, is_initialized, (false));
// If the item's |shortcut_key_codes| is empty, we need to dynamically
// populate the keycodes with |accelerator_ids| to construct the shortcut
// string.
if (!is_initialized) {
is_initialized = true;
for (auto& item : item_list) {
if (item.shortcut_key_codes.empty()) {
DCHECK(!item.accelerator_ids.empty());
// Only use the first |accelerator_id| because the modifiers are the
// same even if it is a grouped accelerators.
const AcceleratorId& accelerator_id = item.accelerator_ids[0];
// Insert |shortcut_key_codes| by the order of CTLR, ALT, SHIFT, SEARCH,
// and then key, to be consistent with how we describe it in the
// |shortcut_message_id| associated string template.
for (auto modifier : {ui::EF_CONTROL_DOWN, ui::EF_ALT_DOWN,
ui::EF_SHIFT_DOWN, ui::EF_COMMAND_DOWN}) {
if (accelerator_id.modifiers & modifier)
item.shortcut_key_codes.emplace_back(GetModifierKeyCode(modifier));
}
// For non grouped accelerators, we need to populate the key as well.
if (item.accelerator_ids.size() == 1)
item.shortcut_key_codes.emplace_back(accelerator_id.keycode);
}
}
}
return item_list; return item_list;
} }
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/chromeos/ksv/views/keyboard_shortcut_view.h"
#include "ui/views/background.h"
#include "ui/views/widget/widget.h"
namespace keyboard_shortcut_viewer {
namespace {
KeyboardShortcutView* g_ksv_view = nullptr;
} // namespace
KeyboardShortcutView::~KeyboardShortcutView() {
DCHECK_EQ(g_ksv_view, this);
g_ksv_view = nullptr;
}
// static
views::Widget* KeyboardShortcutView::Show(gfx::NativeWindow context) {
if (g_ksv_view) {
// If there is a KeyboardShortcutView window open already, just activate it.
g_ksv_view->GetWidget()->Activate();
} else {
// TODO(wutao): change the bounds to UX specs when they are available.
views::Widget::CreateWindowWithContextAndBounds(
new KeyboardShortcutView(), context, gfx::Rect(0, 0, 660, 560));
g_ksv_view->GetWidget()->Show();
}
return g_ksv_view->GetWidget();
}
KeyboardShortcutView::KeyboardShortcutView() {
DCHECK_EQ(g_ksv_view, nullptr);
g_ksv_view = this;
// Default background is transparent.
SetBackground(views::CreateSolidBackground(SK_ColorWHITE));
InitViews();
}
void KeyboardShortcutView::InitViews() {
// TODO(wutao): implement convertion from KeyboardShortcutItem to
// KeyboardShortcutItemView, including generating strings and icons for VKEY.
// Call GetKeyboardShortcutItemList() to constuct the item views.
NOTIMPLEMENTED();
}
bool KeyboardShortcutView::CanMaximize() const {
return false;
}
bool KeyboardShortcutView::CanMinimize() const {
return true;
}
bool KeyboardShortcutView::CanResize() const {
return false;
}
views::ClientView* KeyboardShortcutView::CreateClientView(
views::Widget* widget) {
return new views::ClientView(widget, this);
}
} // namespace keyboard_shortcut_viewer
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_CHROMEOS_KSV_VIEWS_KEYBOARD_SHORTCUT_VIEW_H_
#define UI_CHROMEOS_KSV_VIEWS_KEYBOARD_SHORTCUT_VIEW_H_
#include "base/macros.h"
#include "ui/views/widget/widget_delegate.h"
namespace views {
class Widget;
} // namespace views
namespace keyboard_shortcut_viewer {
// The UI container for Ash and Chrome keyboard shortcuts.
class KeyboardShortcutView : public views::WidgetDelegateView {
public:
~KeyboardShortcutView() override;
// Shows the Keyboard Shortcut Viewer window, or re-activates an existing one.
static views::Widget* Show(gfx::NativeWindow context);
private:
KeyboardShortcutView();
void InitViews();
// views::WidgetDelegate:
bool CanMaximize() const override;
bool CanMinimize() const override;
bool CanResize() const override;
// TODO(wutao): need to customize the frame view header based on UX specs.
views::ClientView* CreateClientView(views::Widget* widget) override;
DISALLOW_COPY_AND_ASSIGN(KeyboardShortcutView);
};
} // namespace keyboard_shortcut_viewer
#endif // UI_CHROMEOS_KSV_VIEWS_KEYBOARD_SHORTCUT_VIEW_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/chromeos/ksv/views/keyboard_shortcut_view.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/widget.h"
namespace keyboard_shortcut_viewer {
using KeyboardShortcutViewTest = views::ViewsTestBase;
// Shows and closes the widget for KeyboardShortcutViewer.
TEST_F(KeyboardShortcutViewTest, ShowAndClose) {
// Showing the widget.
views::Widget* widget = KeyboardShortcutView::Show(GetContext());
EXPECT_TRUE(widget);
// Cleaning up.
widget->CloseNow();
}
} // namespace keyboard_shortcut_viewer
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment