Blink roll 174125:174137
[chromium-blink-merge.git] / ash / touch / touch_observer_hud.cc
blob87d6d4ee50d5c0f3c17807de8f0b9fc5de3d70e0
1 // Copyright (c) 2012 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.
5 #include "ash/touch/touch_observer_hud.h"
7 #include "ash/root_window_controller.h"
8 #include "ash/root_window_settings.h"
9 #include "ash/shell.h"
10 #include "ash/shell_window_ids.h"
11 #include "ui/aura/window_event_dispatcher.h"
12 #include "ui/gfx/display.h"
13 #include "ui/gfx/rect.h"
14 #include "ui/gfx/screen.h"
15 #include "ui/gfx/size.h"
16 #include "ui/views/widget/widget.h"
18 namespace ash {
20 TouchObserverHUD::TouchObserverHUD(aura::Window* initial_root)
21 : display_id_(GetRootWindowSettings(initial_root)->display_id),
22 root_window_(initial_root),
23 widget_(NULL) {
24 const gfx::Display& display =
25 Shell::GetInstance()->display_manager()->GetDisplayForId(display_id_);
27 views::View* content = new views::View;
29 const gfx::Size& display_size = display.size();
30 content->SetSize(display_size);
32 widget_ = new views::Widget();
33 views::Widget::InitParams
34 params(views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
35 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
36 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
37 params.accept_events = false;
38 params.bounds = display.bounds();
39 params.parent =
40 Shell::GetContainer(root_window_, kShellWindowId_OverlayContainer);
41 widget_->Init(params);
42 widget_->SetContentsView(content);
43 widget_->StackAtTop();
44 widget_->Show();
46 widget_->AddObserver(this);
48 // Observe changes in display size and mode to update touch HUD.
49 Shell::GetScreen()->AddObserver(this);
50 #if defined(OS_CHROMEOS)
51 Shell::GetInstance()->display_configurator()->AddObserver(this);
52 #endif // defined(OS_CHROMEOS)
54 Shell::GetInstance()->display_controller()->AddObserver(this);
55 root_window_->AddPreTargetHandler(this);
58 TouchObserverHUD::~TouchObserverHUD() {
59 Shell::GetInstance()->display_controller()->RemoveObserver(this);
61 #if defined(OS_CHROMEOS)
62 Shell::GetInstance()->display_configurator()->RemoveObserver(this);
63 #endif // defined(OS_CHROMEOS)
64 Shell::GetScreen()->RemoveObserver(this);
66 widget_->RemoveObserver(this);
69 void TouchObserverHUD::Clear() {
72 void TouchObserverHUD::Remove() {
73 root_window_->RemovePreTargetHandler(this);
75 RootWindowController* controller = GetRootWindowController(root_window_);
76 UnsetHudForRootWindowController(controller);
78 widget_->CloseNow();
81 void TouchObserverHUD::OnTouchEvent(ui::TouchEvent* /*event*/) {
84 void TouchObserverHUD::OnWidgetDestroying(views::Widget* widget) {
85 DCHECK_EQ(widget, widget_);
86 delete this;
89 void TouchObserverHUD::OnDisplayBoundsChanged(const gfx::Display& display) {
90 if (display.id() != display_id_)
91 return;
92 widget_->SetSize(display.size());
95 void TouchObserverHUD::OnDisplayAdded(const gfx::Display& new_display) {}
97 void TouchObserverHUD::OnDisplayRemoved(const gfx::Display& old_display) {
98 if (old_display.id() != display_id_)
99 return;
100 widget_->CloseNow();
103 #if defined(OS_CHROMEOS)
104 void TouchObserverHUD::OnDisplayModeChanged(
105 const ui::DisplayConfigurator::DisplayStateList& outputs) {
106 // Clear touch HUD for any change in display mode (single, dual extended, dual
107 // mirrored, ...).
108 Clear();
110 #endif // defined(OS_CHROMEOS)
112 void TouchObserverHUD::OnDisplaysInitialized() {
113 OnDisplayConfigurationChanged();
116 void TouchObserverHUD::OnDisplayConfigurationChanging() {
117 if (!root_window_)
118 return;
120 root_window_->RemovePreTargetHandler(this);
122 RootWindowController* controller = GetRootWindowController(root_window_);
123 UnsetHudForRootWindowController(controller);
125 views::Widget::ReparentNativeView(
126 widget_->GetNativeView(),
127 Shell::GetContainer(root_window_,
128 kShellWindowId_UnparentedControlContainer));
130 root_window_ = NULL;
133 void TouchObserverHUD::OnDisplayConfigurationChanged() {
134 if (root_window_)
135 return;
137 root_window_ = Shell::GetInstance()->display_controller()->
138 GetRootWindowForDisplayId(display_id_);
140 views::Widget::ReparentNativeView(
141 widget_->GetNativeView(),
142 Shell::GetContainer(root_window_, kShellWindowId_OverlayContainer));
144 RootWindowController* controller = GetRootWindowController(root_window_);
145 SetHudForRootWindowController(controller);
147 root_window_->AddPreTargetHandler(this);
150 } // namespace ash