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/magnifier/partial_magnification_controller.h"
8 #include "ash/shell_window_ids.h"
9 #include "ui/aura/window.h"
10 #include "ui/aura/window_event_dispatcher.h"
11 #include "ui/aura/window_property.h"
12 #include "ui/aura/window_tree_host.h"
13 #include "ui/gfx/screen.h"
14 #include "ui/compositor/layer.h"
15 #include "ui/views/layout/fill_layout.h"
16 #include "ui/views/widget/widget.h"
17 #include "ui/views/widget/widget_delegate.h"
18 #include "ui/wm/core/compound_event_filter.h"
22 const float kMinPartialMagnifiedScaleThreshold
= 1.1f
;
24 // Number of pixels to make the border of the magnified area.
25 const int kZoomInset
= 16;
27 // Width of the magnified area.
28 const int kMagnifierWidth
= 200;
30 // Height of the magnified area.
31 const int kMagnifierHeight
= 200;
33 // Name of the magnifier window.
34 const char kPartialMagniferWindowName
[] = "PartialMagnifierWindow";
40 PartialMagnificationController::PartialMagnificationController()
41 : is_on_zooming_(false),
43 scale_(kNonPartialMagnifiedScale
),
45 Shell::GetInstance()->AddPreTargetHandler(this);
48 PartialMagnificationController::~PartialMagnificationController() {
49 CloseMagnifierWindow();
51 Shell::GetInstance()->RemovePreTargetHandler(this);
54 void PartialMagnificationController::SetScale(float scale
) {
60 if (IsPartialMagnified()) {
61 CreateMagnifierWindow();
63 CloseMagnifierWindow();
67 void PartialMagnificationController::SetEnabled(bool enabled
) {
69 is_enabled_
= enabled
;
70 SetScale(kDefaultPartialMagnifiedScale
);
72 SetScale(kNonPartialMagnifiedScale
);
73 is_enabled_
= enabled
;
77 ////////////////////////////////////////////////////////////////////////////////
78 // PartialMagnificationController: ui::EventHandler implementation
80 void PartialMagnificationController::OnMouseEvent(ui::MouseEvent
* event
) {
81 if (IsPartialMagnified() && event
->type() == ui::ET_MOUSE_MOVED
) {
82 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
83 aura::Window
* current_root
= target
->GetRootWindow();
84 // TODO(zork): Handle the case where the event is captured on a different
85 // display, such as when a menu is opened.
86 gfx::Rect root_bounds
= current_root
->bounds();
88 if (root_bounds
.Contains(event
->root_location())) {
89 SwitchTargetRootWindow(current_root
);
91 OnMouseMove(event
->root_location());
96 ////////////////////////////////////////////////////////////////////////////////
97 // PartialMagnificationController: aura::WindowObserver implementation
99 void PartialMagnificationController::OnWindowDestroying(
100 aura::Window
* window
) {
101 CloseMagnifierWindow();
103 aura::Window
* new_root_window
= GetCurrentRootWindow();
104 if (new_root_window
!= window
)
105 SwitchTargetRootWindow(new_root_window
);
108 void PartialMagnificationController::OnWidgetDestroying(
109 views::Widget
* widget
) {
110 DCHECK_EQ(widget
, zoom_widget_
);
111 RemoveZoomWidgetObservers();
115 void PartialMagnificationController::OnMouseMove(
116 const gfx::Point
& location_in_root
) {
117 gfx::Point
origin(location_in_root
);
119 origin
.Offset(-kMagnifierWidth
/ 2, -kMagnifierHeight
/ 2);
122 zoom_widget_
->SetBounds(gfx::Rect(origin
.x(), origin
.y(),
123 kMagnifierWidth
, kMagnifierHeight
));
127 bool PartialMagnificationController::IsPartialMagnified() const {
128 return scale_
>= kMinPartialMagnifiedScaleThreshold
;
131 void PartialMagnificationController::CreateMagnifierWindow() {
135 aura::Window
* root_window
= GetCurrentRootWindow();
139 root_window
->AddObserver(this);
142 root_window
->GetHost()->dispatcher()->GetLastMouseLocationInRoot());
144 zoom_widget_
= new views::Widget
;
145 views::Widget::InitParams
params(
146 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS
);
147 params
.activatable
= views::Widget::InitParams::ACTIVATABLE_NO
;
148 params
.accept_events
= false;
149 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
150 params
.parent
= root_window
;
151 zoom_widget_
->Init(params
);
152 zoom_widget_
->SetBounds(gfx::Rect(mouse
.x() - kMagnifierWidth
/ 2,
153 mouse
.y() - kMagnifierHeight
/ 2,
154 kMagnifierWidth
, kMagnifierHeight
));
155 zoom_widget_
->set_focus_on_creation(false);
156 zoom_widget_
->Show();
158 aura::Window
* window
= zoom_widget_
->GetNativeView();
159 window
->SetName(kPartialMagniferWindowName
);
161 zoom_widget_
->GetNativeView()->layer()->SetBounds(
165 zoom_widget_
->GetNativeView()->layer()->SetBackgroundZoom(
169 zoom_widget_
->AddObserver(this);
172 void PartialMagnificationController::CloseMagnifierWindow() {
174 RemoveZoomWidgetObservers();
175 zoom_widget_
->Close();
180 void PartialMagnificationController::RemoveZoomWidgetObservers() {
181 DCHECK(zoom_widget_
);
182 zoom_widget_
->RemoveObserver(this);
183 aura::Window
* root_window
=
184 zoom_widget_
->GetNativeView()->GetRootWindow();
186 root_window
->RemoveObserver(this);
189 void PartialMagnificationController::SwitchTargetRootWindow(
190 aura::Window
* new_root_window
) {
192 new_root_window
== zoom_widget_
->GetNativeView()->GetRootWindow())
195 CloseMagnifierWindow();
197 // Recreate the magnifier window by updating the scale factor.
198 SetScale(GetScale());
201 aura::Window
* PartialMagnificationController::GetCurrentRootWindow() {
202 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
203 for (aura::Window::Windows::const_iterator iter
= root_windows
.begin();
204 iter
!= root_windows
.end(); ++iter
) {
205 aura::Window
* root_window
= *iter
;
206 if (root_window
->ContainsPointInRoot(
207 root_window
->GetHost()->dispatcher()->GetLastMouseLocationInRoot()))