Add HSTS preload entries submitted as of 2014-11-18.
[chromium-blink-merge.git] / ash / magnifier / partial_magnification_controller.cc
blob7f1d92e91af29a503c974a404ea82dafb4eb8646
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_on_zooming_(false),
42 is_enabled_(false),
43 scale_(kNonPartialMagnifiedScale),
44 zoom_widget_(NULL) {
45 Shell::GetInstance()->AddPreTargetHandler(this);
48 PartialMagnificationController::~PartialMagnificationController() {
49 CloseMagnifierWindow();
51 Shell::GetInstance()->RemovePreTargetHandler(this);
54 void PartialMagnificationController::SetScale(float scale) {
55 if (!is_enabled_)
56 return;
58 scale_ = scale;
60 if (IsPartialMagnified()) {
61 CreateMagnifierWindow();
62 } else {
63 CloseMagnifierWindow();
67 void PartialMagnificationController::SetEnabled(bool enabled) {
68 if (enabled) {
69 is_enabled_ = enabled;
70 SetScale(kDefaultPartialMagnifiedScale);
71 } else {
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();
112 zoom_widget_ = NULL;
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);
121 if (zoom_widget_) {
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() {
132 if (zoom_widget_)
133 return;
135 aura::Window* root_window = GetCurrentRootWindow();
136 if (!root_window)
137 return;
139 root_window->AddObserver(this);
141 gfx::Point mouse(
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(
162 gfx::Rect(0, 0,
163 kMagnifierWidth,
164 kMagnifierHeight));
165 zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom(
166 scale_,
167 kZoomInset);
169 zoom_widget_->AddObserver(this);
172 void PartialMagnificationController::CloseMagnifierWindow() {
173 if (zoom_widget_) {
174 RemoveZoomWidgetObservers();
175 zoom_widget_->Close();
176 zoom_widget_ = NULL;
180 void PartialMagnificationController::RemoveZoomWidgetObservers() {
181 DCHECK(zoom_widget_);
182 zoom_widget_->RemoveObserver(this);
183 aura::Window* root_window =
184 zoom_widget_->GetNativeView()->GetRootWindow();
185 DCHECK(root_window);
186 root_window->RemoveObserver(this);
189 void PartialMagnificationController::SwitchTargetRootWindow(
190 aura::Window* new_root_window) {
191 if (zoom_widget_ &&
192 new_root_window == zoom_widget_->GetNativeView()->GetRootWindow())
193 return;
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()))
208 return root_window;
210 return NULL;
213 } // namespace ash