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.
4
5 #include "ash/touch/touch_observer_hud.h"
6
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/root_window.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"
17
18 namespace ash {
19 namespace internal {
20
TouchObserverHUD(aura::Window * initial_root)21 TouchObserverHUD::TouchObserverHUD(aura::Window* initial_root)
22 : display_id_(GetRootWindowSettings(initial_root)->display_id),
23 root_window_(initial_root),
24 widget_(NULL) {
25 const gfx::Display& display =
26 Shell::GetInstance()->display_manager()->GetDisplayForId(display_id_);
27
28 views::View* content = new views::View;
29
30 const gfx::Size& display_size = display.size();
31 content->SetSize(display_size);
32
33 widget_ = new views::Widget();
34 views::Widget::InitParams
35 params(views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
36 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
37 params.can_activate = false;
38 params.accept_events = false;
39 params.bounds = display.bounds();
40 params.parent = Shell::GetContainer(
41 root_window_,
42 internal::kShellWindowId_OverlayContainer);
43 widget_->Init(params);
44 widget_->SetContentsView(content);
45 widget_->StackAtTop();
46 widget_->Show();
47
48 widget_->AddObserver(this);
49
50 // Observe changes in display size and mode to update touch HUD.
51 Shell::GetScreen()->AddObserver(this);
52 #if defined(OS_CHROMEOS)
53 Shell::GetInstance()->output_configurator()->AddObserver(this);
54 #endif // defined(OS_CHROMEOS)
55
56 Shell::GetInstance()->display_controller()->AddObserver(this);
57 root_window_->AddPreTargetHandler(this);
58 }
59
~TouchObserverHUD()60 TouchObserverHUD::~TouchObserverHUD() {
61 Shell::GetInstance()->display_controller()->RemoveObserver(this);
62
63 #if defined(OS_CHROMEOS)
64 Shell::GetInstance()->output_configurator()->RemoveObserver(this);
65 #endif // defined(OS_CHROMEOS)
66 Shell::GetScreen()->RemoveObserver(this);
67
68 widget_->RemoveObserver(this);
69 }
70
Clear()71 void TouchObserverHUD::Clear() {
72 }
73
Remove()74 void TouchObserverHUD::Remove() {
75 root_window_->RemovePreTargetHandler(this);
76
77 RootWindowController* controller = GetRootWindowController(root_window_);
78 UnsetHudForRootWindowController(controller);
79
80 widget_->CloseNow();
81 }
82
OnTouchEvent(ui::TouchEvent *)83 void TouchObserverHUD::OnTouchEvent(ui::TouchEvent* /*event*/) {
84 }
85
OnWidgetDestroying(views::Widget * widget)86 void TouchObserverHUD::OnWidgetDestroying(views::Widget* widget) {
87 DCHECK_EQ(widget, widget_);
88 delete this;
89 }
90
OnDisplayBoundsChanged(const gfx::Display & display)91 void TouchObserverHUD::OnDisplayBoundsChanged(const gfx::Display& display) {
92 if (display.id() != display_id_)
93 return;
94 widget_->SetSize(display.size());
95 }
96
OnDisplayAdded(const gfx::Display & new_display)97 void TouchObserverHUD::OnDisplayAdded(const gfx::Display& new_display) {}
98
OnDisplayRemoved(const gfx::Display & old_display)99 void TouchObserverHUD::OnDisplayRemoved(const gfx::Display& old_display) {
100 if (old_display.id() != display_id_)
101 return;
102 widget_->CloseNow();
103 }
104
105 #if defined(OS_CHROMEOS)
OnDisplayModeChanged(const std::vector<chromeos::OutputConfigurator::OutputSnapshot> & outputs)106 void TouchObserverHUD::OnDisplayModeChanged(
107 const std::vector<chromeos::OutputConfigurator::OutputSnapshot>& outputs) {
108 // Clear touch HUD for any change in display mode (single, dual extended, dual
109 // mirrored, ...).
110 Clear();
111 }
112 #endif // defined(OS_CHROMEOS)
113
OnDisplayConfigurationChanging()114 void TouchObserverHUD::OnDisplayConfigurationChanging() {
115 if (!root_window_)
116 return;
117
118 root_window_->RemovePreTargetHandler(this);
119
120 RootWindowController* controller = GetRootWindowController(root_window_);
121 UnsetHudForRootWindowController(controller);
122
123 views::Widget::ReparentNativeView(
124 widget_->GetNativeView(),
125 Shell::GetContainer(root_window_,
126 internal::kShellWindowId_UnparentedControlContainer));
127
128 root_window_ = NULL;
129 }
130
OnDisplayConfigurationChanged()131 void TouchObserverHUD::OnDisplayConfigurationChanged() {
132 if (root_window_)
133 return;
134
135 root_window_ = Shell::GetInstance()->display_controller()->
136 GetRootWindowForDisplayId(display_id_);
137
138 views::Widget::ReparentNativeView(
139 widget_->GetNativeView(),
140 Shell::GetContainer(root_window_,
141 internal::kShellWindowId_OverlayContainer));
142
143 RootWindowController* controller = GetRootWindowController(root_window_);
144 SetHudForRootWindowController(controller);
145
146 root_window_->AddPreTargetHandler(this);
147 }
148
149 } // namespace internal
150 } // namespace ash
151