[OriginChip] Show the default search provider in the Omnibox hint text.
[chromium-blink-merge.git] / ash / magnifier / partial_magnification_controller.cc
bloba24ba51d4e95e50b7489dda343f12007af598da8
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/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"
19 namespace {
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";
35 } // namespace
37 namespace ash {
39 PartialMagnificationController::PartialMagnificationController()
40 : is_on_zooming_(false),
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(root_window->GetDispatcher()->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(
160 gfx::Rect(0, 0,
161 kMagnifierWidth,
162 kMagnifierHeight));
163 zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom(
164 scale_,
165 kZoomInset);
167 zoom_widget_->AddObserver(this);
170 void PartialMagnificationController::CloseMagnifierWindow() {
171 if (zoom_widget_) {
172 RemoveZoomWidgetObservers();
173 zoom_widget_->Close();
174 zoom_widget_ = NULL;
178 void PartialMagnificationController::RemoveZoomWidgetObservers() {
179 DCHECK(zoom_widget_);
180 zoom_widget_->RemoveObserver(this);
181 aura::Window* root_window =
182 zoom_widget_->GetNativeView()->GetRootWindow();
183 DCHECK(root_window);
184 root_window->RemoveObserver(this);
187 void PartialMagnificationController::SwitchTargetRootWindow(
188 aura::Window* new_root_window) {
189 if (zoom_widget_ &&
190 new_root_window == zoom_widget_->GetNativeView()->GetRootWindow())
191 return;
193 CloseMagnifierWindow();
195 // Recreate the magnifier window by updating the scale factor.
196 SetScale(GetScale());
199 aura::Window* PartialMagnificationController::GetCurrentRootWindow() {
200 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
201 for (aura::Window::Windows::const_iterator iter = root_windows.begin();
202 iter != root_windows.end(); ++iter) {
203 aura::Window* root_window = *iter;
204 if (root_window->ContainsPointInRoot(
205 root_window->GetDispatcher()->GetLastMouseLocationInRoot()))
206 return root_window;
208 return NULL;
211 } // namespace ash