Remove LSan options/suppressions from test_env.py.
[chromium-blink-merge.git] / ash / magnifier / partial_magnification_controller.cc
blobe152ed9d115afa96c8afdfedd73411469c95a54f
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"
7 #include "ash/shell.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"
20 namespace {
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";
36 } // namespace
38 namespace ash {
40 PartialMagnificationController::PartialMagnificationController()
41 : is_enabled_(false),
42 scale_(kNonPartialMagnifiedScale),
43 zoom_widget_(NULL) {
44 Shell::GetInstance()->AddPreTargetHandler(this);
47 PartialMagnificationController::~PartialMagnificationController() {
48 CloseMagnifierWindow();
50 Shell::GetInstance()->RemovePreTargetHandler(this);
53 void PartialMagnificationController::SetScale(float scale) {
54 if (!is_enabled_)
55 return;
57 scale_ = scale;
59 if (IsPartialMagnified()) {
60 CreateMagnifierWindow();
61 } else {
62 CloseMagnifierWindow();
66 void PartialMagnificationController::SetEnabled(bool enabled) {
67 if (enabled) {
68 is_enabled_ = enabled;
69 SetScale(kDefaultPartialMagnifiedScale);
70 } else {
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::Window* 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();
111 zoom_widget_ = NULL;
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);
120 if (zoom_widget_) {
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() {
131 if (zoom_widget_)
132 return;
134 aura::Window* root_window = GetCurrentRootWindow();
135 if (!root_window)
136 return;
138 root_window->AddObserver(this);
140 gfx::Point mouse(
141 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot());
143 zoom_widget_ = new views::Widget;
144 views::Widget::InitParams params(
145 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
146 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
147 params.accept_events = false;
148 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
149 params.parent = root_window;
150 zoom_widget_->Init(params);
151 zoom_widget_->SetBounds(gfx::Rect(mouse.x() - kMagnifierWidth / 2,
152 mouse.y() - kMagnifierHeight / 2,
153 kMagnifierWidth, kMagnifierHeight));
154 zoom_widget_->set_focus_on_creation(false);
155 zoom_widget_->Show();
157 aura::Window* window = zoom_widget_->GetNativeView();
158 window->SetName(kPartialMagniferWindowName);
160 zoom_widget_->GetNativeView()->layer()->SetBounds(
161 gfx::Rect(0, 0,
162 kMagnifierWidth,
163 kMagnifierHeight));
164 zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom(
165 scale_,
166 kZoomInset);
168 zoom_widget_->AddObserver(this);
171 void PartialMagnificationController::CloseMagnifierWindow() {
172 if (zoom_widget_) {
173 RemoveZoomWidgetObservers();
174 zoom_widget_->Close();
175 zoom_widget_ = NULL;
179 void PartialMagnificationController::RemoveZoomWidgetObservers() {
180 DCHECK(zoom_widget_);
181 zoom_widget_->RemoveObserver(this);
182 aura::Window* root_window =
183 zoom_widget_->GetNativeView()->GetRootWindow();
184 DCHECK(root_window);
185 root_window->RemoveObserver(this);
188 void PartialMagnificationController::SwitchTargetRootWindow(
189 aura::Window* new_root_window) {
190 if (zoom_widget_ &&
191 new_root_window == zoom_widget_->GetNativeView()->GetRootWindow())
192 return;
194 CloseMagnifierWindow();
196 // Recreate the magnifier window by updating the scale factor.
197 SetScale(GetScale());
200 aura::Window* PartialMagnificationController::GetCurrentRootWindow() {
201 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
202 for (aura::Window::Windows::const_iterator iter = root_windows.begin();
203 iter != root_windows.end(); ++iter) {
204 aura::Window* root_window = *iter;
205 if (root_window->ContainsPointInRoot(
206 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot()))
207 return root_window;
209 return NULL;
212 } // namespace ash