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::Window* 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::OnWidgetDestroying(
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.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
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(
167 zoom_widget_->AddObserver(this);
170 void PartialMagnificationController::CloseMagnifierWindow() {
172 RemoveZoomWidgetObservers();
173 zoom_widget_->Close();
178 void PartialMagnificationController::RemoveZoomWidgetObservers() {
179 DCHECK(zoom_widget_);
180 zoom_widget_->RemoveObserver(this);
181 aura::Window* root_window =
182 zoom_widget_->GetNativeView()->GetRootWindow();
184 root_window->RemoveObserver(this);
187 void PartialMagnificationController::SwitchTargetRootWindow(
188 aura::Window* new_root_window) {
190 new_root_window == zoom_widget_->GetNativeView()->GetRootWindow())
193 CloseMagnifierWindow();
195 // Recreate the magnifier window by updating the scale factor.
196 SetScale(GetScale());
199 aura::RootWindow* PartialMagnificationController::GetCurrentRootWindow() {
200 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
201 for (Shell::RootWindowList::const_iterator iter = root_windows.begin();
202 iter != root_windows.end(); ++iter) {
203 aura::RootWindow* root_window = *iter;
204 if (root_window->ContainsPointInRoot(
205 root_window->GetLastMouseLocationInRoot()))