Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/magnifier/magnification_controller.h" | 5 #include "ash/magnifier/magnification_controller.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "ui/aura/event.h" | |
| 8 #include "ui/aura/root_window.h" | 9 #include "ui/aura/root_window.h" |
| 10 #include "ui/aura/shared/root_window_event_filter.h" | |
| 9 #include "ui/aura/window.h" | 11 #include "ui/aura/window.h" |
| 10 #include "ui/aura/window_property.h" | 12 #include "ui/aura/window_property.h" |
| 13 #include "ui/gfx/point3.h" | |
| 14 #include "ui/compositor/dip_util.h" | |
| 11 #include "ui/compositor/layer.h" | 15 #include "ui/compositor/layer.h" |
| 16 #include "ui/compositor/layer_animation_observer.h" | |
| 12 #include "ui/compositor/scoped_layer_animation_settings.h" | 17 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 13 | 18 |
| 14 namespace { | 19 namespace { |
| 15 | 20 |
| 16 const float kMaximumMagnifiScale = 4.0f; | 21 const float kMaximumMagnifiScale = 4.0f; |
| 17 const float kMaximumMagnifiScaleThreshold = 4.0f; | 22 const float kMaximumMagnifiScaleThreshold = 4.0f; |
| 18 const float kMinimumMagnifiScale = 1.0f; | 23 const float kMinimumMagnifiScale = 1.0f; |
| 19 const float kMinimumMagnifiScaleThreshold = 1.1f; | 24 const float kMinimumMagnifiScaleThreshold = 1.1f; |
| 20 | 25 |
| 21 } // namespace | 26 } // namespace |
| 22 | 27 |
| 23 namespace ash { | 28 namespace ash { |
| 24 namespace internal { | 29 namespace internal { |
| 25 | 30 |
| 26 MagnificationController::MagnificationController() | 31 //////////////////////////////////////////////////////////////////////////////// |
| 32 // MagnificationControllerImpl: | |
| 33 | |
| 34 class MagnificationControllerImpl : virtual public MagnificationController, | |
| 35 public ui::ImplicitAnimationObserver { | |
| 36 public: | |
| 37 MagnificationControllerImpl(); | |
| 38 virtual ~MagnificationControllerImpl() {} | |
| 39 | |
| 40 // MagnificationController overrides: | |
| 41 void SetScale(float scale, bool animation); | |
|
sky
2012/06/07 18:05:50
virtual and OVERRIDE
yoshiki
2012/06/08 22:27:45
Done.
| |
| 42 float GetScale() const { return scale_; } | |
| 43 void MoveWindow(int x, int y, bool animation); | |
| 44 void MoveWindow(const gfx::Point& point, bool animation); | |
| 45 gfx::Point GetWindowPosition() const { return gfx::Point(x_, y_); } | |
| 46 void EnsureShowRect(const gfx::Rect& rect, bool animation); | |
| 47 void EnsureShowPoint(const gfx::Point& point, bool animation) OVERRIDE; | |
| 48 | |
| 49 private: | |
| 50 aura::RootWindow* root_window_; | |
| 51 bool is_on_zooming_; | |
| 52 | |
| 53 // ui::ImplicitAnimationObserver overrides: | |
| 54 void OnImplicitAnimationsCompleted() OVERRIDE; | |
|
sky
2012/06/07 18:05:50
methods before fields.
yoshiki
2012/06/08 22:27:45
Done.
| |
| 55 | |
| 56 // Current scale, position of the magnification window. | |
| 57 float scale_; | |
| 58 int x_; | |
| 59 int y_; | |
| 60 | |
| 61 // should be called internally just after the scale and/or the position are | |
| 62 // changed. | |
| 63 bool Redraw(const gfx::Point& position, float scale, bool animation); | |
| 64 bool RedrawDIP(const gfx::Point& position, float scale, bool animation); | |
| 65 | |
| 66 // Ensures that the given point, rect or last mouse location is inside | |
| 67 // magnification window. If not, the controller moves the window to contain | |
| 68 // the given point/rect. | |
| 69 void EnsureShowRectWithScale( | |
| 70 const gfx::Rect& target_rect, float scale, bool animation); | |
| 71 void EnsureShowRectDIP( | |
| 72 const gfx::Rect& target_rect_in_dip, float scale, bool animation); | |
| 73 void EnsureShowPointWithScale( | |
| 74 const gfx::Point& point, float scale, bool animation); | |
| 75 void OnMouseMove(); | |
| 76 | |
| 77 // Returns if the magnification scale is 1.0 or not (larger then 1.0). | |
| 78 bool IsMagnified() const; | |
| 79 | |
| 80 // Returns the rect of the magnification window. | |
| 81 gfx::Rect GetWindowRectDIP(float scale) const; | |
| 82 // Returns the size of the root window. | |
| 83 gfx::Size GetHostSizeDIP() const; | |
| 84 | |
| 85 // Correct the givin scale value if nessesary. | |
| 86 void ValidateScale(float* scale); | |
| 87 | |
| 88 // aura::EventFilter overrides: | |
| 89 virtual bool PreHandleKeyEvent(aura::Window* target, | |
| 90 aura::KeyEvent* event) OVERRIDE; | |
| 91 virtual bool PreHandleMouseEvent(aura::Window* target, | |
| 92 aura::MouseEvent* event) OVERRIDE; | |
| 93 virtual ui::TouchStatus PreHandleTouchEvent(aura::Window* target, | |
| 94 aura::TouchEvent* event) OVERRIDE; | |
| 95 virtual ui::GestureStatus PreHandleGestureEvent( | |
| 96 aura::Window* target, | |
| 97 aura::GestureEvent* event) OVERRIDE; | |
| 98 | |
| 99 DISALLOW_COPY_AND_ASSIGN(MagnificationControllerImpl); | |
| 100 }; | |
| 101 | |
| 102 //////////////////////////////////////////////////////////////////////////////// | |
| 103 // MagnificationControllerImpl: | |
| 104 | |
| 105 MagnificationControllerImpl::MagnificationControllerImpl() | |
| 27 : scale_(1.0f), x_(0), y_(0) { | 106 : scale_(1.0f), x_(0), y_(0) { |
| 28 root_window_ = ash::Shell::GetRootWindow(); | 107 root_window_ = ash::Shell::GetRootWindow(); |
| 29 } | 108 } |
| 30 | 109 |
| 31 void MagnificationController::SetScale(float scale) { | 110 bool MagnificationControllerImpl::Redraw( |
| 111 const gfx::Point& position, float scale, bool animation) { | |
| 112 const gfx::Point position_in_dip = | |
| 113 ui::ConvertPointToDIP(root_window_->layer(), position); | |
| 114 return RedrawDIP(position_in_dip, scale, animation); | |
| 115 } | |
| 116 | |
| 117 bool MagnificationControllerImpl::RedrawDIP( | |
| 118 const gfx::Point& position_in_dip, float scale, bool animation) { | |
| 119 int x = position_in_dip.x(); | |
| 120 int y = position_in_dip.y(); | |
| 121 | |
| 122 ValidateScale(&scale); | |
| 123 | |
| 124 if (x < 0) | |
| 125 x = 0; | |
| 126 if (y < 0) | |
| 127 y = 0; | |
| 128 | |
| 129 const gfx::Size host_size_in_dip = GetHostSizeDIP(); | |
| 130 const gfx::Size window_size_in_dip = GetWindowRectDIP(scale).size(); | |
| 131 int max_x = host_size_in_dip.width() - window_size_in_dip.width(); | |
| 132 int max_y = host_size_in_dip.height() - window_size_in_dip.height(); | |
| 133 if (x > max_x) | |
| 134 x = max_x; | |
| 135 if (y > max_y) | |
| 136 y = max_y; | |
| 137 | |
| 138 // Ignores 1 px diffirence because it may be error on calculation. | |
| 139 if (std::abs(x_ - x) <= 1 && std::abs(y_ - y) <= 1 && scale == scale_) | |
| 140 return false; | |
| 141 | |
| 142 x_ = x; | |
| 143 y_ = y; | |
| 32 scale_ = scale; | 144 scale_ = scale; |
| 33 RedrawScreen(true); | |
| 34 } | |
| 35 | |
| 36 void MagnificationController::MoveWindow(int x, int y) { | |
| 37 y_ = y; | |
| 38 x_ = x; | |
| 39 RedrawScreen(true); | |
| 40 } | |
| 41 | |
| 42 void MagnificationController::MoveWindow(const gfx::Point& point) { | |
| 43 MoveWindow(point.x(), point.y()); | |
| 44 } | |
| 45 | |
| 46 void MagnificationController::EnsureShowRect(const gfx::Rect& target_rect) { | |
| 47 gfx::Rect rect = GetWindowRect().AdjustToFit(target_rect); | |
| 48 MoveWindow(rect.x(), rect.y()); | |
| 49 } | |
| 50 | |
| 51 void MagnificationController::EnsureShowPoint(const gfx::Point& point, | |
| 52 bool animation) { | |
| 53 gfx::Rect rect = GetWindowRect(); | |
| 54 | |
| 55 if (rect.Contains(point)) | |
| 56 return; | |
| 57 | |
| 58 if (point.x() < rect.x()) | |
| 59 x_ = point.x(); | |
| 60 else if(rect.right() < point.x()) | |
| 61 x_ = point.x() - rect.width(); | |
| 62 | |
| 63 if (point.y() < rect.y()) | |
| 64 y_ = point.y(); | |
| 65 else if(rect.bottom() < point.y()) | |
| 66 y_ = point.y() - rect.height(); | |
| 67 | |
| 68 RedrawScreen(animation); | |
| 69 } | |
| 70 | |
| 71 void MagnificationController::RedrawScreen(bool animation) { | |
| 72 | |
| 73 // Adjust the scale to just |kMinimumMagnifiScale| if scale is smaller than | |
| 74 // |kMinimumMagnifiScaleThreshold|; | |
| 75 if (scale_ < kMinimumMagnifiScaleThreshold) | |
| 76 scale_ = kMinimumMagnifiScale; | |
| 77 // Adjust the scale to just |kMinimumMagnifiScale| if scale is bigger than | |
| 78 // |kMinimumMagnifiScaleThreshold|; | |
| 79 if (scale_ > kMaximumMagnifiScaleThreshold) | |
| 80 scale_ = kMaximumMagnifiScale; | |
| 81 | |
| 82 if (x_ < 0) | |
| 83 x_ = 0; | |
| 84 if (y_ < 0) | |
| 85 y_ = 0; | |
| 86 | |
| 87 gfx::Size host_size = root_window_->GetHostSize(); | |
| 88 gfx::Size window_size = GetWindowRect().size(); | |
| 89 int max_x = host_size.width() - window_size.width(); | |
| 90 int max_y = host_size.height() - window_size.height(); | |
| 91 if (x_ > max_x) | |
| 92 x_ = max_x; | |
| 93 if (y_ > max_y) | |
| 94 y_ = max_y; | |
| 95 | |
| 96 | |
| 97 float scale = scale_; | |
| 98 int x = x_; | |
| 99 int y = y_; | |
| 100 | 145 |
| 101 // Creates transform matrix. | 146 // Creates transform matrix. |
| 102 ui::Transform transform; | 147 ui::Transform transform; |
| 103 // Flips the signs intentionally to convert them from the position of the | 148 // Flips the signs intentionally to convert them from the position of the |
| 104 // magnification window. | 149 // magnification window. |
| 105 transform.ConcatTranslate(-x, -y); | 150 transform.ConcatTranslate(-x_, -y_); |
| 106 transform.ConcatScale(scale, scale); | 151 transform.ConcatScale(scale_, scale_); |
| 107 | 152 |
| 108 if (animation) { | 153 ui::ScopedLayerAnimationSettings settings( |
| 109 ui::ScopedLayerAnimationSettings settings( | 154 root_window_->layer()->GetAnimator()); |
| 110 root_window_->layer()->GetAnimator()); | 155 settings.AddObserver(this); |
| 111 settings.SetPreemptionStrategy( | 156 settings.SetPreemptionStrategy( |
| 112 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); | 157 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 113 settings.SetTweenType(ui::Tween::EASE_IN_OUT); | 158 settings.SetTweenType(ui::Tween::EASE_OUT); |
| 114 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds(100)); | 159 settings.SetTransitionDuration( |
| 160 base::TimeDelta::FromMilliseconds(animation ? 100 : 0)); | |
| 161 | |
| 162 root_window_->layer()->SetTransform(transform); | |
| 163 | |
| 164 return true; | |
| 165 } | |
| 166 | |
| 167 void MagnificationControllerImpl::EnsureShowRectWithScale( | |
| 168 const gfx::Rect& target_rect, float scale, bool animation) { | |
| 169 const gfx::Rect target_rect_in_dip = | |
| 170 ui::ConvertRectToDIP(root_window_->layer(), target_rect); | |
| 171 EnsureShowRectDIP(target_rect_in_dip, scale, animation); | |
| 172 } | |
| 173 | |
| 174 void MagnificationControllerImpl::EnsureShowRectDIP( | |
| 175 const gfx::Rect& target_rect, float scale, bool animation) { | |
| 176 ValidateScale(&scale); | |
| 177 | |
| 178 const gfx::Rect window_rect = GetWindowRectDIP(scale); | |
| 179 if (scale == scale_ && window_rect.Contains(target_rect)) | |
| 180 return; | |
| 181 | |
| 182 gfx::Rect rect = window_rect; | |
| 183 if (target_rect.width() > rect.width()) | |
| 184 rect.set_x(target_rect.CenterPoint().x() - rect.x() / 2); | |
| 185 else if (target_rect.right() < rect.x()) | |
| 186 rect.set_x(target_rect.right()); | |
| 187 else if (rect.right() < target_rect.x()) | |
| 188 rect.set_x(target_rect.x() - rect.width()); | |
| 189 | |
| 190 if (rect.height() > window_rect.height()) | |
| 191 rect.set_y(target_rect.CenterPoint().y() - rect.y() / 2); | |
| 192 else if (target_rect.bottom() < rect.y()) | |
| 193 rect.set_y(target_rect.bottom()); | |
| 194 else if (rect.bottom() < target_rect.y()) | |
| 195 rect.set_y(target_rect.y() - rect.height()); | |
| 196 | |
| 197 RedrawDIP(rect.origin(), scale, animation); | |
| 198 } | |
| 199 | |
| 200 void MagnificationControllerImpl::EnsureShowPointWithScale( | |
| 201 const gfx::Point& point, float scale, bool animation) { | |
| 202 EnsureShowRectWithScale(gfx::Rect(point, gfx::Size(0, 0)), scale, animation); | |
| 203 } | |
| 204 | |
| 205 void MagnificationControllerImpl::OnMouseMove() { | |
| 206 gfx::Point mouse = root_window_->last_mouse_location(); | |
| 207 | |
| 208 int x = x_; | |
| 209 int y = y_; | |
| 210 bool start_zoom = false; | |
| 211 | |
| 212 const gfx::Rect window_rect = GetWindowRectDIP(scale_); | |
| 213 const int left = window_rect.x(); | |
| 214 const int right = window_rect.right(); | |
| 215 const int width_margin = static_cast<int>(0.1f * window_rect.width()); | |
| 216 const int width_offset = static_cast<int>(0.5f * window_rect.width()); | |
| 217 | |
| 218 if (mouse.x() < left + width_margin) { | |
| 219 x -= width_offset; | |
| 220 start_zoom = true; | |
| 221 } else if (right - width_margin < mouse.x()) { | |
| 222 x += width_offset; | |
| 223 start_zoom = true; | |
| 115 } | 224 } |
| 116 | 225 |
| 117 root_window_->layer()->SetTransform(transform); | 226 const int top = window_rect.y(); |
| 118 } | 227 const int bottom = window_rect.bottom(); |
| 119 | 228 // Uses same margin with x-axis's one. |
| 120 gfx::Rect MagnificationController::GetWindowRect() { | 229 const int height_margin = width_margin; |
| 121 gfx::Size size = root_window_->GetHostSize(); | 230 const int height_offset = static_cast<int>(0.5f * window_rect.height()); |
| 122 int width = size.width() / scale_; | 231 |
| 123 int height = size.height() / scale_; | 232 if (mouse.y() < top + height_margin) { |
| 233 y -= height_offset; | |
| 234 start_zoom = true; | |
| 235 } else if (bottom - height_margin < mouse.y()) { | |
| 236 y += height_offset; | |
| 237 start_zoom = true; | |
| 238 } | |
| 239 | |
| 240 if (start_zoom && !is_on_zooming_) { | |
| 241 bool ret = Redraw(gfx::Point(x, y), scale_, true); | |
| 242 | |
| 243 if (ret) { | |
| 244 is_on_zooming_ = true; | |
| 245 | |
| 246 int x_diff = x_ - window_rect.x(); | |
| 247 int y_diff = y_ - window_rect.y(); | |
| 248 // If the magnified region is moved, hides the mouse cursor and moves it. | |
| 249 if (x_diff != 0 || y_diff != 0) { | |
| 250 ash::Shell::GetInstance()-> | |
| 251 root_filter()->set_update_cursor_visibility(false); | |
| 252 root_window_->ShowCursor(false); | |
| 253 mouse.set_x(mouse.x() - (x_ - window_rect.x())); | |
| 254 mouse.set_y(mouse.y() - (y_ - window_rect.y())); | |
| 255 root_window_->MoveCursorTo(mouse); | |
| 256 } | |
| 257 } | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 gfx::Size MagnificationControllerImpl::GetHostSizeDIP() const { | |
| 262 return ui::ConvertSizeToDIP(root_window_->layer(), | |
| 263 root_window_->GetHostSize()); | |
| 264 } | |
| 265 | |
| 266 gfx::Rect MagnificationControllerImpl::GetWindowRectDIP(float scale) const { | |
| 267 const gfx::Size size_in_dip = | |
| 268 ui::ConvertSizeToDIP(root_window_->layer(), | |
| 269 root_window_->GetHostSize()); | |
| 270 const int width = size_in_dip.width() / scale; | |
| 271 const int height = size_in_dip.height() / scale; | |
| 124 | 272 |
| 125 return gfx::Rect(x_, y_, width, height); | 273 return gfx::Rect(x_, y_, width, height); |
| 126 } | 274 } |
| 127 | 275 |
| 276 bool MagnificationControllerImpl::IsMagnified() const { | |
| 277 return scale_ >= kMinimumMagnifiScaleThreshold; | |
| 278 } | |
| 279 | |
| 280 void MagnificationControllerImpl::ValidateScale(float* scale) { | |
| 281 // Adjust the scale to just |kMinimumMagnifiScale| if scale is smaller than | |
| 282 // |kMinimumMagnifiScaleThreshold|; | |
| 283 if (*scale < kMinimumMagnifiScaleThreshold) | |
| 284 *scale = kMinimumMagnifiScale; | |
| 285 | |
| 286 // Adjust the scale to just |kMinimumMagnifiScale| if scale is bigger than | |
| 287 // |kMinimumMagnifiScaleThreshold|; | |
| 288 if (*scale > kMaximumMagnifiScaleThreshold) | |
| 289 *scale = kMaximumMagnifiScale; | |
| 290 } | |
| 291 | |
| 292 void MagnificationControllerImpl::OnImplicitAnimationsCompleted() { | |
| 293 root_window_->ShowCursor(true); | |
| 294 is_on_zooming_ = false; | |
| 295 } | |
| 296 | |
| 297 //////////////////////////////////////////////////////////////////////////////// | |
| 298 // MagnificationControllerImpl: MagnificationController implementation | |
| 299 | |
| 300 void MagnificationControllerImpl::SetScale(float scale, bool animation) { | |
| 301 ValidateScale(&scale); | |
| 302 | |
| 303 // Try not to change the point which the mouse cursor indicates to. | |
| 304 const gfx::Rect window_rect = GetWindowRectDIP(scale); | |
| 305 const gfx::Point mouse = root_window_->last_mouse_location(); | |
| 306 const gfx::Point origin = gfx::Point(mouse.x() * (1.0f - 1.0f / scale), | |
| 307 mouse.y() * (1.0f - 1.0f / scale)); | |
| 308 Redraw(origin, scale, animation); | |
| 309 } | |
| 310 | |
| 311 void MagnificationControllerImpl::MoveWindow(int x, int y, bool animation) { | |
| 312 Redraw(gfx::Point(x, y), scale_, animation); | |
| 313 } | |
| 314 | |
| 315 void MagnificationControllerImpl::MoveWindow( | |
| 316 const gfx::Point& point, bool animation) { | |
| 317 Redraw(point, scale_, animation); | |
| 318 } | |
| 319 | |
| 320 void MagnificationControllerImpl::EnsureShowRect( | |
| 321 const gfx::Rect& target_rect, bool animation) { | |
| 322 EnsureShowRectWithScale(target_rect, scale_, animation); | |
| 323 } | |
| 324 | |
| 325 void MagnificationControllerImpl::EnsureShowPoint( | |
| 326 const gfx::Point& point, bool animation) { | |
| 327 EnsureShowPointWithScale(point, scale_, animation); | |
| 328 } | |
| 329 | |
| 330 //////////////////////////////////////////////////////////////////////////////// | |
| 331 // MagnificationControllerImpl: aura::EventFilter implementation | |
| 332 | |
| 333 bool MagnificationControllerImpl::PreHandleKeyEvent( | |
| 334 aura::Window* target, aura::KeyEvent* event) { | |
| 335 return false; | |
| 336 } | |
| 337 | |
| 338 bool MagnificationControllerImpl::PreHandleMouseEvent( | |
| 339 aura::Window* target, aura::MouseEvent* event) { | |
| 340 if (IsMagnified()) | |
| 341 OnMouseMove(); | |
| 342 return false; | |
| 343 } | |
| 344 | |
| 345 ui::TouchStatus MagnificationControllerImpl::PreHandleTouchEvent( | |
| 346 aura::Window* target, aura::TouchEvent* event) { | |
| 347 return ui::TOUCH_STATUS_UNKNOWN; | |
| 348 } | |
| 349 | |
| 350 ui::GestureStatus MagnificationControllerImpl::PreHandleGestureEvent( | |
| 351 aura::Window* target, | |
| 352 aura::GestureEvent* event) { | |
| 353 return ui::GESTURE_STATUS_UNKNOWN; | |
| 354 } | |
| 355 | |
| 356 //////////////////////////////////////////////////////////////////////////////// | |
| 357 // MagnificationController: | |
| 358 | |
| 359 // static | |
| 360 MagnificationController* MagnificationController::CreateInstance() { | |
| 361 return new MagnificationControllerImpl(); | |
| 362 } | |
| 363 | |
| 128 } // namespace internal | 364 } // namespace internal |
| 129 } // namespace ash | 365 } // namespace ash |
| OLD | NEW |