1 // Copyright 2013 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 "ash/display/virtual_keyboard_window_controller.h" 6 7 #include "ash/display/display_controller.h" 8 #include "ash/display/display_info.h" 9 #include "ash/display/display_manager.h" 10 #include "ash/display/root_window_transformers.h" 11 #include "ash/host/ash_window_tree_host.h" 12 #include "ash/host/ash_window_tree_host_init_params.h" 13 #include "ash/host/root_window_transformer.h" 14 #include "ash/root_window_controller.h" 15 #include "ash/root_window_settings.h" 16 #include "ash/shell.h" 17 #include "ash/shell_window_ids.h" 18 #include "base/strings/stringprintf.h" 19 #include "base/strings/utf_string_conversions.h" 20 #include "ui/aura/env.h" 21 #include "ui/aura/window_event_dispatcher.h" 22 #include "ui/keyboard/keyboard_controller.h" 23 #include "ui/keyboard/keyboard_util.h" 24 25 namespace ash { 26 27 VirtualKeyboardWindowController::VirtualKeyboardWindowController() { 28 Shell::GetInstance()->AddShellObserver(this); 29 } 30 31 VirtualKeyboardWindowController::~VirtualKeyboardWindowController() { 32 Shell::GetInstance()->RemoveShellObserver(this); 33 // Make sure the root window gets deleted before cursor_window_delegate. 34 Close(); 35 } 36 37 void VirtualKeyboardWindowController::ActivateKeyboard( 38 keyboard::KeyboardController* keyboard_controller) { 39 root_window_controller_->ActivateKeyboard(keyboard_controller); 40 } 41 42 void VirtualKeyboardWindowController::UpdateWindow( 43 const DisplayInfo& display_info) { 44 static int virtual_keyboard_host_count = 0; 45 if (!root_window_controller_.get()) { 46 AshWindowTreeHostInitParams init_params; 47 init_params.initial_bounds = display_info.bounds_in_native(); 48 AshWindowTreeHost* ash_host = AshWindowTreeHost::Create(init_params); 49 aura::WindowTreeHost* host = ash_host->AsWindowTreeHost(); 50 51 host->window()->SetName(base::StringPrintf("VirtualKeyboardRootWindow-%d", 52 virtual_keyboard_host_count++)); 53 54 // No need to remove WindowTreeHostObserver because the DisplayController 55 // outlives the host. 56 host->AddObserver(Shell::GetInstance()->display_controller()); 57 InitRootWindowSettings(host->window())->display_id = display_info.id(); 58 host->InitHost(); 59 RootWindowController::CreateForVirtualKeyboardDisplay(ash_host); 60 root_window_controller_.reset(GetRootWindowController(host->window())); 61 root_window_controller_->GetHost()->Show(); 62 root_window_controller_->ActivateKeyboard( 63 keyboard::KeyboardController::GetInstance()); 64 FlipDisplay(); 65 } else { 66 aura::WindowTreeHost* host = root_window_controller_->GetHost(); 67 GetRootWindowSettings(host->window())->display_id = display_info.id(); 68 host->SetBounds(display_info.bounds_in_native()); 69 } 70 } 71 72 void VirtualKeyboardWindowController::Close() { 73 if (root_window_controller_.get()) { 74 root_window_controller_->GetHost()->RemoveObserver( 75 Shell::GetInstance()->display_controller()); 76 root_window_controller_->Shutdown(); 77 root_window_controller_.reset(); 78 } 79 } 80 81 void VirtualKeyboardWindowController::FlipDisplay() { 82 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); 83 if (!display_manager->virtual_keyboard_root_window_enabled()) { 84 NOTREACHED() << "Attempting to flip virtual keyboard root window when it " 85 << "is not enabled."; 86 return; 87 } 88 display_manager->SetDisplayRotation( 89 display_manager->non_desktop_display().id(), gfx::Display::ROTATE_180); 90 91 aura::WindowTreeHost* host = root_window_controller_->GetHost(); 92 scoped_ptr<RootWindowTransformer> transformer( 93 CreateRootWindowTransformerForDisplay( 94 host->window(), display_manager->non_desktop_display())); 95 root_window_controller_->ash_host()->SetRootWindowTransformer( 96 transformer.Pass()); 97 } 98 99 void VirtualKeyboardWindowController::OnMaximizeModeStarted() { 100 keyboard::SetTouchKeyboardEnabled(true); 101 Shell::GetInstance()->CreateKeyboard(); 102 } 103 104 void VirtualKeyboardWindowController::OnMaximizeModeEnded() { 105 keyboard::SetTouchKeyboardEnabled(false); 106 if (!keyboard::IsKeyboardEnabled()) 107 Shell::GetInstance()->DeactivateKeyboard(); 108 } 109 110 } // namespace ash 111