Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: ash/magnifier/partial_magnification_controller.cc

Issue 10915140: Add the partial screen magnifier to Chrome OS. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Remove pure virtual interface Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "ash/magnifier/partial_magnification_controller.h"
6
7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h"
9 #include "ui/aura/root_window.h"
10 #include "ui/aura/shared/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"
18
19 namespace {
20
21 const float kMinPartialMagnifiedScaleThreshold = 1.1f;
22
23 // Number of pixels to make the border of the magnified area.
24 const int kZoomInset = 16;
25
26 // Width of the magnified area.
27 const int kMagnifierWidth = 200;
28
29 // Height of the magnified area.
30 const int kMagnifierHeight = 200;
31
32 } // namespace
33
34 namespace ash {
35 namespace internal {
36
37 PartialMagnificationController::PartialMagnificationController()
38 : is_on_zooming_(false),
39 is_enabled_(false),
40 scale_(kNonPartialMagnifiedScale),
41 zoom_widget_(NULL) {
42 Shell::GetInstance()->AddEnvEventFilter(this);
43 }
44
45 PartialMagnificationController::~PartialMagnificationController() {
46 CloseMagnifierWindow();
47
48 Shell::GetInstance()->RemoveEnvEventFilter(this);
49 }
50
51 void PartialMagnificationController::SetScale(float scale) {
52 if (!is_enabled_)
53 return;
54
55 scale_ = scale;
56
57 if (IsPartialMagnified()) {
58 CreateMagnifierWindow();
59 } else {
60 CloseMagnifierWindow();
61 }
62 }
63
64 float PartialMagnificationController::GetScale() const {
65 return scale_;
oshima 2012/10/16 23:43:24 this should be accessor in header.
66 }
67
68 void PartialMagnificationController::SetEnabled(bool enabled) {
69 if (enabled) {
70 is_enabled_ = enabled;
71 SetScale(kDefaultPartialMagnifiedScale);
72 } else {
73 SetScale(kNonPartialMagnifiedScale);
74 is_enabled_ = enabled;
75 }
76 }
77
78 bool PartialMagnificationController::IsEnabled() const {
79 return is_enabled_;
80 }
81
82
83 ////////////////////////////////////////////////////////////////////////////////
84 // PartialMagnificationController: aura::EventFilter implementation
85
86 bool PartialMagnificationController::PreHandleKeyEvent(
87 aura::Window* target,
88 ui::KeyEvent* event) {
89 return false;
90 }
91
92 bool PartialMagnificationController::PreHandleMouseEvent(
93 aura::Window* target,
94 ui::MouseEvent* event) {
95 if (IsPartialMagnified() && event->type() == ui::ET_MOUSE_MOVED) {
96 aura::RootWindow* current_root = target->GetRootWindow();
97 gfx::Rect root_bounds = current_root->bounds();
oshima 2012/10/16 23:43:24 This probably doesn't work if event is captured by
Zachary Kuznia 2012/11/12 08:43:46 Added TODO On 2012/10/16 23:43:24, oshima wrote:
98
99 if (root_bounds.Contains(event->root_location())) {
100 SwitchTargetRootWindow(current_root);
101
102 OnMouseMove(event->root_location());
103 }
104 }
105
106 return false;
107 }
108
109 ui::TouchStatus PartialMagnificationController::PreHandleTouchEvent(
110 aura::Window* target,
111 ui::TouchEvent* event) {
112 return ui::TOUCH_STATUS_UNKNOWN;
113 }
114
115 ui::EventResult PartialMagnificationController::PreHandleGestureEvent(
116 aura::Window* target,
117 ui::GestureEvent* event) {
118 return ui::ER_UNHANDLED;
119 }
120
121 ////////////////////////////////////////////////////////////////////////////////
122 // PartialMagnificationController: aura::WindowObserver implementation
123
124 void PartialMagnificationController::OnWindowDestroying(
125 aura::Window* window) {
126 CloseMagnifierWindow();
127
128 aura::RootWindow* new_root_window = GetCurrentRootWindow();
sky 2012/10/16 16:29:22 This makes me nervous. In particular this assumes
oshima 2012/10/16 23:43:24 I actually wonder if we ever need this. When displ
Zachary Kuznia 2012/11/12 08:43:46 That is correct, it will get recreated when the mo
129 if (new_root_window != window)
130 SwitchTargetRootWindow(new_root_window);
131 }
132
133 void PartialMagnificationController::OnWidgetClosing(
134 views::Widget* widget) {
135 widget->RemoveObserver(this);
136
137 aura::RootWindow* root_window =
138 zoom_widget_->GetNativeView()->GetRootWindow();
139 if (root_window)
sky 2012/10/16 16:29:22 This should be a DCHECK. And refactor CloseMagnifi
Zachary Kuznia 2012/11/12 08:43:46 Done.
140 root_window->RemoveObserver(this);
141
142 zoom_widget_ = NULL;
143 }
144
145 void PartialMagnificationController::OnMouseMove(
146 const gfx::Point& location_in_root) {
147 gfx::Point origin(location_in_root);
148
149 origin.Offset(-kMagnifierWidth / 2, -kMagnifierHeight / 2);
150
151 if (zoom_widget_) {
152 zoom_widget_->SetBounds(gfx::Rect(origin.x(), origin.y(),
153 kMagnifierWidth, kMagnifierHeight));
154 }
155 }
156
157 bool PartialMagnificationController::IsPartialMagnified() const {
158 return scale_ >= kMinPartialMagnifiedScaleThreshold;
159 }
160
161 void PartialMagnificationController::CreateMagnifierWindow() {
162 if (zoom_widget_)
163 return;
164
165 aura::RootWindow* root_window = GetCurrentRootWindow();
166 if (!root_window)
167 return;
168
169 root_window->AddObserver(this);
170
171 gfx::Point mouse(root_window->GetLastMouseLocationInRoot());
172
173 zoom_widget_ = new views::Widget;
174 views::Widget::InitParams params(
175 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
176 params.can_activate = false;
177 params.accept_events = false;
178 params.transparent = true;
179 params.parent = root_window;
180 zoom_widget_->Init(params);
181 zoom_widget_->SetBounds(gfx::Rect(mouse.x() - kMagnifierWidth / 2,
182 mouse.y() - kMagnifierHeight / 2,
183 kMagnifierWidth, kMagnifierHeight));
184 zoom_widget_->set_focus_on_creation(false);
185 zoom_widget_->Show();
186
187 zoom_widget_->GetNativeView()->layer()->SetBounds(
188 gfx::Rect(0, 0,
189 kMagnifierWidth,
190 kMagnifierHeight));
191 zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom(
192 (kMagnifierWidth - (kMagnifierWidth / scale_)) / 2,
193 (kMagnifierHeight - (kMagnifierHeight / scale_)) / 2,
194 scale_,
195 kZoomInset);
196
197 zoom_widget_->AddObserver(this);
198 }
199
200 void PartialMagnificationController::CloseMagnifierWindow() {
201 if (zoom_widget_) {
202 zoom_widget_->RemoveObserver(this);
203 aura::RootWindow* root_window =
204 zoom_widget_->GetNativeView()->GetRootWindow();
205 if (root_window)
206 root_window->RemoveObserver(this);
207
208 zoom_widget_->Close();
209 zoom_widget_ = NULL;
210 }
211 }
212
213 void PartialMagnificationController::SwitchTargetRootWindow(
214 aura::RootWindow* new_root_window) {
215 if (zoom_widget_ &&
216 new_root_window == zoom_widget_->GetNativeView()->GetRootWindow())
217 return;
218
219 float scale = GetScale();
220
221 CloseMagnifierWindow();
222 SetScale(scale);
sky 2012/10/16 16:29:22 Why the SetScale here? And if you really need it,
Zachary Kuznia 2012/11/12 08:43:46 Added a comment to clarify what's going on. On 20
223 }
224
225 aura::RootWindow* PartialMagnificationController::GetCurrentRootWindow() {
226 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
227 for (Shell::RootWindowList::const_iterator iter = root_windows.begin();
228 iter != root_windows.end(); ++iter) {
229 aura::RootWindow* root_window = *iter;
230 if (root_window->ContainsPointInRoot(
231 root_window->GetLastMouseLocationInRoot()))
232 return root_window;
233 }
234 return NULL;
235 }
236
237 } // namespace internal
238 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698