• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 "athena/screen/screen_accelerator_handler.h"
6 
7 #include "athena/input/public/accelerator_manager.h"
8 #include "ui/aura/window.h"
9 #include "ui/aura/window_event_dispatcher.h"
10 #include "ui/aura/window_tree_host.h"
11 #include "ui/compositor/debug_utils.h"
12 #include "ui/wm/public/activation_client.h"
13 
14 namespace athena {
15 namespace {
16 
17 enum Command {
18   CMD_PRINT_LAYER_HIERARCHY,
19   CMD_PRINT_WINDOW_HIERARCHY,
20 };
21 
22 const int EF_ALL_DOWN =
23     ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN;
24 
25 const AcceleratorData accelerator_data[] = {
26     {TRIGGER_ON_PRESS, ui::VKEY_L, EF_ALL_DOWN, CMD_PRINT_LAYER_HIERARCHY,
27      AF_DEBUG},
28     {TRIGGER_ON_PRESS, ui::VKEY_W, EF_ALL_DOWN, CMD_PRINT_WINDOW_HIERARCHY,
29      AF_DEBUG},
30 };
31 
PrintLayerHierarchy(aura::Window * root_window)32 void PrintLayerHierarchy(aura::Window* root_window) {
33   ui::PrintLayerHierarchy(
34       root_window->layer(),
35       root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot());
36 }
37 
PrintWindowHierarchy(aura::Window * window,aura::Window * active,int indent,std::ostringstream * out)38 void PrintWindowHierarchy(aura::Window* window,
39                           aura::Window* active,
40                           int indent,
41                           std::ostringstream* out) {
42   std::string indent_str(indent, ' ');
43   std::string name(window->name());
44   if (name.empty())
45     name = "\"\"";
46   *out << indent_str << name << " (" << window << ")"
47        << " type=" << window->type()
48        << ((window == active) ? " [active] " : " ")
49        << (window->IsVisible() ? " visible " : " ")
50        << window->bounds().ToString() << '\n';
51 
52   for (size_t i = 0; i < window->children().size(); ++i)
53     PrintWindowHierarchy(window->children()[i], active, indent + 3, out);
54 }
55 
HandlePrintWindowHierarchy(aura::Window * root_window)56 void HandlePrintWindowHierarchy(aura::Window* root_window) {
57   aura::Window* active =
58       aura::client::GetActivationClient(root_window)->GetActiveWindow();
59   std::ostringstream out;
60   out << "RootWindow :\n";
61   PrintWindowHierarchy(root_window, active, 0, &out);
62   // Error so logs can be collected from end-users.
63   LOG(ERROR) << out.str();
64 }
65 
66 }  // namespace
67 
68 // static
ScreenAcceleratorHandler(aura::Window * root_window)69 ScreenAcceleratorHandler::ScreenAcceleratorHandler(aura::Window* root_window)
70     : root_window_(root_window) {
71   AcceleratorManager::Get()->RegisterAccelerators(
72       accelerator_data, arraysize(accelerator_data), this);
73 }
74 
~ScreenAcceleratorHandler()75 ScreenAcceleratorHandler::~ScreenAcceleratorHandler() {
76 }
77 
IsCommandEnabled(int command_id) const78 bool ScreenAcceleratorHandler::IsCommandEnabled(int command_id) const {
79   return true;
80 }
81 
OnAcceleratorFired(int command_id,const ui::Accelerator & accelerator)82 bool ScreenAcceleratorHandler::OnAcceleratorFired(
83     int command_id,
84     const ui::Accelerator& accelerator) {
85   switch (command_id) {
86     case CMD_PRINT_LAYER_HIERARCHY:
87       PrintLayerHierarchy(root_window_);
88       return true;
89     case CMD_PRINT_WINDOW_HIERARCHY:
90       HandlePrintWindowHierarchy(root_window_);
91       return true;
92   }
93   return false;
94 }
95 
96 }  // namesapce athena
97