1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "shell_tab_handler.h"
7 #include "ash/capture_mode/capture_mode_util.h"
8 #include "ash/focus_cycler.h"
9 #include "ash/shelf/shelf.h"
10 #include "ash/shelf/shelf_navigation_widget.h"
11 #include "ash/shell.h"
12 #include "ash/system/status_area_widget.h"
13 #include "ash/wm/tablet_mode/tablet_mode_controller.h"
14 #include "ash/wm/window_util.h"
15 #include "ui/events/event.h"
16 #include "ui/wm/public/activation_client.h"
20 void ShellTabHandler::OnKeyEvent(ui::KeyEvent* key_event) {
21 // Only focus the shelf if the device is in clamshell mode, and the user
23 if (key_event->key_code() != ui::KeyboardCode::VKEY_TAB ||
24 key_event->type() != ui::EventType::ET_KEY_PRESSED ||
25 key_event->IsAltDown() || key_event->IsControlDown() ||
26 key_event->IsCommandDown() ||
27 shell_->tablet_mode_controller()->InTabletMode()) {
31 // Capture session will process their own tab events.
32 if (capture_mode_util::IsCaptureModeActive())
35 aura::Window* root_window_for_new_windows =
36 Shell::GetRootWindowForNewWindows();
38 if (!root_window_for_new_windows || window_util::GetActiveWindow())
41 // If there is no active window, focus the HomeButton or StatusWidget,
42 // depending on whether this is Tab or Shift + Tab. This will allow the
43 // users focus to traverse the shelf.
44 auto* shelf = Shelf::ForWindow(root_window_for_new_windows);
45 views::Widget* status_area_widget = shelf->status_area_widget();
46 views::Widget* navigation_widget = shelf->navigation_widget();
47 shell_->focus_cycler()->FocusWidget(
48 key_event->IsShiftDown() ? status_area_widget : navigation_widget);
49 key_event->SetHandled();
50 key_event->StopPropagation();