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

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: Code review fixes Created 8 years, 1 month 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
36 PartialMagnificationController::PartialMagnificationController()
37 : is_on_zooming_(false),
38 is_enabled_(false),
39 scale_(kNonPartialMagnifiedScale),
40 zoom_widget_(NULL) {
41 Shell::GetInstance()->AddEnvEventFilter(this);
42 }
43
44 PartialMagnificationController::~PartialMagnificationController() {
45 CloseMagnifierWindow();
46
47 Shell::GetInstance()->RemoveEnvEventFilter(this);
48 }
49
50 void PartialMagnificationController::SetScale(float scale) {
51 if (!is_enabled_)
52 return;
53
54 scale_ = scale;
55
56 if (IsPartialMagnified()) {
57 CreateMagnifierWindow();
58 } else {
59 CloseMagnifierWindow();
60 }
61 }
62
63 void PartialMagnificationController::SetEnabled(bool enabled) {
64 if (enabled) {
65 is_enabled_ = enabled;
66 SetScale(kDefaultPartialMagnifiedScale);
67 } else {
68 SetScale(kNonPartialMagnifiedScale);
69 is_enabled_ = enabled;
70 }
71 }
72
73 bool PartialMagnificationController::IsEnabled() const {
74 return is_enabled_;
75 }
76
77
78 ////////////////////////////////////////////////////////////////////////////////
79 // PartialMagnificationController: aura::EventFilter implementation
80
81 bool PartialMagnificationController::PreHandleKeyEvent(
82 aura::Window* target,
83 ui::KeyEvent* event) {
84 return false;
85 }
86
87 bool PartialMagnificationController::PreHandleMouseEvent(
88 aura::Window* target,
89 ui::MouseEvent* event) {
90 if (IsPartialMagnified() && event->type() == ui::ET_MOUSE_MOVED) {
91 aura::RootWindow* current_root = target->GetRootWindow();
92 // TODO(zork): Handle the case where the event is captured on a different
93 // display, such as when a menu is opened.
94 gfx::Rect root_bounds = current_root->bounds();
95
96 if (root_bounds.Contains(event->root_location())) {
97 SwitchTargetRootWindow(current_root);
98
99 OnMouseMove(event->root_location());
100 }
101 }
102
103 return false;
104 }
105
106 ui::EventResult PartialMagnificationController::PreHandleTouchEvent(
107 aura::Window* target,
108 ui::TouchEvent* event) {
109 return ui::ER_UNHANDLED;
110 }
111
112 ui::EventResult PartialMagnificationController::PreHandleGestureEvent(
113 aura::Window* target,
114 ui::GestureEvent* event) {
115 return ui::ER_UNHANDLED;
116 }
117
118 ////////////////////////////////////////////////////////////////////////////////
119 // PartialMagnificationController: aura::WindowObserver implementation
120
121 void PartialMagnificationController::OnWindowDestroying(
122 aura::Window* window) {
123 CloseMagnifierWindow();
124
125 aura::RootWindow* new_root_window = GetCurrentRootWindow();
126 if (new_root_window != window)
127 SwitchTargetRootWindow(new_root_window);
128 }
129
130 void PartialMagnificationController::OnWidgetClosing(
131 views::Widget* widget) {
132 DCHECK(widget == zoom_widget_);
sky 2012/11/12 15:23:46 DCHECK_EQ
Zachary Kuznia 2012/11/13 08:16:15 Done.
133 RemoveZoomWidgetObservers();
134 zoom_widget_ = NULL;
135 }
136
137 void PartialMagnificationController::OnMouseMove(
138 const gfx::Point& location_in_root) {
139 gfx::Point origin(location_in_root);
140
141 origin.Offset(-kMagnifierWidth / 2, -kMagnifierHeight / 2);
142
143 if (zoom_widget_) {
144 zoom_widget_->SetBounds(gfx::Rect(origin.x(), origin.y(),
145 kMagnifierWidth, kMagnifierHeight));
146 }
147 }
148
149 bool PartialMagnificationController::IsPartialMagnified() const {
150 return scale_ >= kMinPartialMagnifiedScaleThreshold;
151 }
152
153 void PartialMagnificationController::CreateMagnifierWindow() {
154 if (zoom_widget_)
155 return;
156
157 aura::RootWindow* root_window = GetCurrentRootWindow();
158 if (!root_window)
159 return;
160
161 root_window->AddObserver(this);
162
163 gfx::Point mouse(root_window->GetLastMouseLocationInRoot());
164
165 zoom_widget_ = new views::Widget;
166 views::Widget::InitParams params(
167 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
168 params.can_activate = false;
169 params.accept_events = false;
170 params.transparent = true;
171 params.parent = root_window;
172 zoom_widget_->Init(params);
173 zoom_widget_->SetBounds(gfx::Rect(mouse.x() - kMagnifierWidth / 2,
174 mouse.y() - kMagnifierHeight / 2,
175 kMagnifierWidth, kMagnifierHeight));
176 zoom_widget_->set_focus_on_creation(false);
177 zoom_widget_->Show();
sky 2012/11/12 15:23:46 Set a name on the window associated with the widge
Zachary Kuznia 2012/11/13 08:16:15 Done.
178
179 zoom_widget_->GetNativeView()->layer()->SetBounds(
180 gfx::Rect(0, 0,
181 kMagnifierWidth,
182 kMagnifierHeight));
183 zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom(
184 (kMagnifierWidth - (kMagnifierWidth / scale_)) / 2,
185 (kMagnifierHeight - (kMagnifierHeight / scale_)) / 2,
186 scale_,
187 kZoomInset);
188
189 zoom_widget_->AddObserver(this);
190 }
191
192 void PartialMagnificationController::CloseMagnifierWindow() {
193 if (zoom_widget_) {
194 RemoveZoomWidgetObservers();
195 zoom_widget_->Close();
196 zoom_widget_ = NULL;
197 }
198 }
199
200 void PartialMagnificationController::RemoveZoomWidgetObservers() {
201 DCHECK(zoom_widget_);
202 zoom_widget_->RemoveObserver(this);
203 aura::RootWindow* root_window =
204 zoom_widget_->GetNativeView()->GetRootWindow();
205 DCHECK(root_window);
206 root_window->RemoveObserver(this);
207 }
208
209 void PartialMagnificationController::SwitchTargetRootWindow(
210 aura::RootWindow* new_root_window) {
211 if (zoom_widget_ &&
212 new_root_window == zoom_widget_->GetNativeView()->GetRootWindow())
213 return;
214
215 CloseMagnifierWindow();
216
217 // Recreate the magnifier window by updating the scale factor.
218 SetScale(GetScale());
219 }
220
221 aura::RootWindow* PartialMagnificationController::GetCurrentRootWindow() {
222 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
223 for (Shell::RootWindowList::const_iterator iter = root_windows.begin();
224 iter != root_windows.end(); ++iter) {
225 aura::RootWindow* root_window = *iter;
226 if (root_window->ContainsPointInRoot(
227 root_window->GetLastMouseLocationInRoot()))
228 return root_window;
229 }
230 return NULL;
231 }
232
233 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698