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/root_window.h"
10 #include "ui/views/corewm/compound_event_filter.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura/window_property.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"
21 const float kMinPartialMagnifiedScaleThreshold
= 1.1f
;
23 // Number of pixels to make the border of the magnified area.
24 const int kZoomInset
= 16;
26 // Width of the magnified area.
27 const int kMagnifierWidth
= 200;
29 // Height of the magnified area.
30 const int kMagnifierHeight
= 200;
32 // Name of the magnifier window.
33 const char kPartialMagniferWindowName
[] = "PartialMagnifierWindow";
39 PartialMagnificationController::PartialMagnificationController()
40 : is_on_zooming_(false),
42 scale_(kNonPartialMagnifiedScale
),
44 Shell::GetInstance()->AddPreTargetHandler(this);
47 PartialMagnificationController::~PartialMagnificationController() {
48 CloseMagnifierWindow();
50 Shell::GetInstance()->RemovePreTargetHandler(this);
53 void PartialMagnificationController::SetScale(float scale
) {
59 if (IsPartialMagnified()) {
60 CreateMagnifierWindow();
62 CloseMagnifierWindow();
66 void PartialMagnificationController::SetEnabled(bool enabled
) {
68 is_enabled_
= enabled
;
69 SetScale(kDefaultPartialMagnifiedScale
);
71 SetScale(kNonPartialMagnifiedScale
);
72 is_enabled_
= enabled
;
76 ////////////////////////////////////////////////////////////////////////////////
77 // PartialMagnificationController: ui::EventHandler implementation
79 void PartialMagnificationController::OnMouseEvent(ui::MouseEvent
* event
) {
80 if (IsPartialMagnified() && event
->type() == ui::ET_MOUSE_MOVED
) {
81 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
82 aura::RootWindow
* current_root
= target
->GetRootWindow();
83 // TODO(zork): Handle the case where the event is captured on a different
84 // display, such as when a menu is opened.
85 gfx::Rect root_bounds
= current_root
->bounds();
87 if (root_bounds
.Contains(event
->root_location())) {
88 SwitchTargetRootWindow(current_root
);
90 OnMouseMove(event
->root_location());
95 ////////////////////////////////////////////////////////////////////////////////
96 // PartialMagnificationController: aura::WindowObserver implementation
98 void PartialMagnificationController::OnWindowDestroying(
99 aura::Window
* window
) {
100 CloseMagnifierWindow();
102 aura::RootWindow
* new_root_window
= GetCurrentRootWindow();
103 if (new_root_window
!= window
)
104 SwitchTargetRootWindow(new_root_window
);
107 void PartialMagnificationController::OnWidgetClosing(
108 views::Widget
* widget
) {
109 DCHECK_EQ(widget
, zoom_widget_
);
110 RemoveZoomWidgetObservers();
114 void PartialMagnificationController::OnMouseMove(
115 const gfx::Point
& location_in_root
) {
116 gfx::Point
origin(location_in_root
);
118 origin
.Offset(-kMagnifierWidth
/ 2, -kMagnifierHeight
/ 2);
121 zoom_widget_
->SetBounds(gfx::Rect(origin
.x(), origin
.y(),
122 kMagnifierWidth
, kMagnifierHeight
));
126 bool PartialMagnificationController::IsPartialMagnified() const {
127 return scale_
>= kMinPartialMagnifiedScaleThreshold
;
130 void PartialMagnificationController::CreateMagnifierWindow() {
134 aura::RootWindow
* root_window
= GetCurrentRootWindow();
138 root_window
->AddObserver(this);
140 gfx::Point
mouse(root_window
->GetLastMouseLocationInRoot());
142 zoom_widget_
= new views::Widget
;
143 views::Widget::InitParams
params(
144 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS
);
145 params
.can_activate
= false;
146 params
.accept_events
= false;
147 params
.transparent
= true;
148 params
.parent
= root_window
;
149 zoom_widget_
->Init(params
);
150 zoom_widget_
->SetBounds(gfx::Rect(mouse
.x() - kMagnifierWidth
/ 2,
151 mouse
.y() - kMagnifierHeight
/ 2,
152 kMagnifierWidth
, kMagnifierHeight
));
153 zoom_widget_
->set_focus_on_creation(false);
154 zoom_widget_
->Show();
156 aura::Window
* window
= zoom_widget_
->GetNativeView();
157 window
->SetName(kPartialMagniferWindowName
);
159 zoom_widget_
->GetNativeView()->layer()->SetBounds(
163 zoom_widget_
->GetNativeView()->layer()->SetBackgroundZoom(
164 (kMagnifierWidth
- (kMagnifierWidth
/ scale_
)) / 2,
165 (kMagnifierHeight
- (kMagnifierHeight
/ scale_
)) / 2,
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::RootWindow
* root_window
=
184 zoom_widget_
->GetNativeView()->GetRootWindow();
186 root_window
->RemoveObserver(this);
189 void PartialMagnificationController::SwitchTargetRootWindow(
190 aura::RootWindow
* 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::RootWindow
* PartialMagnificationController::GetCurrentRootWindow() {
202 Shell::RootWindowList root_windows
= Shell::GetAllRootWindows();
203 for (Shell::RootWindowList::const_iterator iter
= root_windows
.begin();
204 iter
!= root_windows
.end(); ++iter
) {
205 aura::RootWindow
* root_window
= *iter
;
206 if (root_window
->ContainsPointInRoot(
207 root_window
->GetLastMouseLocationInRoot()))