Commit 6149ddf9 authored by Abhijeet Singh's avatar Abhijeet Singh Committed by Commit Bot

Allowing keyboard focus for Quick-Answers on Up key

This CL modifies the way users can gain keyboard focus to Quick-Answers
related views by pressing the Up key after a menu has been opened.
Currently, pressing the Up key selects the last menu-item in the active
menu, from which QuickAnswersView can be accessed by pressing the Down
key again.

Bug: b:152057976
Test: Tested on Chrome OS VM.
Change-Id: Ib435d867ca7b70017531d5b8a1b3f5aa6cd577b1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2271071
Commit-Queue: Abhijeet Singh <siabhijeet@google.com>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#783662}
parent ede59cfc
......@@ -169,24 +169,36 @@ void QuickAnswersPreTargetHandler::ProcessKeyEvent(ui::KeyEvent* key_event) {
// Get the selected item, if any, in the currently active menu.
auto* const active_menu = views::MenuController::GetActiveInstance();
auto* const selected_item = active_menu->GetSelectedMenuItem();
if (!selected_item || !selected_item->GetParentMenuItem())
if (!selected_item)
return;
// Check if the item is within the outer-most menu, since we do not want
// the selection to loop back to |view_| for submenus.
const auto* const parent = selected_item->GetParentMenuItem();
if (parent->GetParentMenuItem())
return;
// Check if the selected item is first or last within the menu.
bool is_first = selected_item == parent->GetSubmenu()->children().front();
bool is_last = selected_item == parent->GetSubmenu()->children().back();
if (!is_first && !is_last)
return;
bool view_should_gain_focus = false;
if (parent) {
// Check if the item is within the outer-most menu, since we do not want
// the selection to loop back to |view_| for submenus.
if (parent->GetParentMenuItem())
return;
// |view_| should gain focus only when the selected item is first or
// last within the menu.
bool first_item_selected =
selected_item == parent->GetSubmenu()->children().front();
bool last_item_selected =
selected_item == parent->GetSubmenu()->children().back();
view_should_gain_focus =
(first_item_selected || last_item_selected) &&
first_item_selected == (key_code == ui::VKEY_UP);
} else {
// Selected menu-item will have no parent only when there are no nested
// menus and no items are visibly selected, and |view_| should gain
// focus for Up-key press in such scenario.
view_should_gain_focus = key_code == ui::VKEY_UP;
}
// Focus |view_| if compatible key-event should transfer the selection to
// it from within the menu.
if (is_first == (key_code == ui::VKEY_UP)) {
if (view_should_gain_focus) {
// Track currently focused view to restore back to later and send focus
// to |view_|.
external_focus_tracker_->SetFocusManager(focus_manager);
......
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