diff options
Diffstat (limited to 'ash/magnifier/magnification_controller.cc')
-rw-r--r-- | ash/magnifier/magnification_controller.cc | 99 |
1 files changed, 67 insertions, 32 deletions
diff --git a/ash/magnifier/magnification_controller.cc b/ash/magnifier/magnification_controller.cc index d132976..9a980d3 100644 --- a/ash/magnifier/magnification_controller.cc +++ b/ash/magnifier/magnification_controller.cc @@ -17,6 +17,9 @@ #include "ui/compositor/layer.h" #include "ui/compositor/layer_animation_observer.h" #include "ui/compositor/scoped_layer_animation_settings.h" +#include "ui/gfx/point_conversions.h" +#include "ui/gfx/point_f.h" +#include "ui/gfx/rect_conversions.h" #include "ui/gfx/screen.h" #include "ui/views/corewm/compound_event_filter.h" @@ -51,7 +54,9 @@ class MagnificationControllerImpl : virtual public MagnificationController, virtual float GetScale() const OVERRIDE { return scale_; } virtual void MoveWindow(int x, int y, bool animate) OVERRIDE; virtual void MoveWindow(const gfx::Point& point, bool animate) OVERRIDE; - virtual gfx::Point GetWindowPosition() const OVERRIDE { return origin_; } + virtual gfx::Point GetWindowPosition() const OVERRIDE { + return gfx::ToFlooredPoint(origin_); + } virtual void EnsureRectIsVisible(const gfx::Rect& rect, bool animate) OVERRIDE; virtual void EnsurePointIsVisible(const gfx::Point& point, @@ -65,8 +70,8 @@ class MagnificationControllerImpl : virtual public MagnificationController, // given scale. Returns true if the window is changed; otherwise, false. // These methods should be called internally just after the scale and/or // the position are changed to redraw the window. - bool Redraw(const gfx::Point& position, float scale, bool animate); - bool RedrawDIP(const gfx::Point& position, float scale, bool animate); + bool Redraw(const gfx::PointF& position, float scale, bool animate); + bool RedrawDIP(const gfx::PointF& position, float scale, bool animate); // Redraw with the given zoom scale keeping the mouse cursor location. In // other words, zoom (or unzoom) centering around the cursor. @@ -86,6 +91,11 @@ class MagnificationControllerImpl : virtual public MagnificationController, bool animate); void OnMouseMove(const gfx::Point& location); + // Move the mouse cursot to the given point. Actual move will be done when + // the animation is completed. This should be called after animation is + // started. + void AfterAnimationMoveCursorTo(const gfx::Point& location); + // Switch Magnified RootWindow to |new_root_window|. This does following: // - Unzoom the current root_window. // - Zoom the given new root_window |new_root_window|. @@ -109,7 +119,7 @@ class MagnificationControllerImpl : virtual public MagnificationController, } // Returns the rect of the magnification window. - gfx::Rect GetWindowRectDIP(float scale) const; + gfx::RectF GetWindowRectDIP(float scale) const; // Returns the size of the root window. gfx::Size GetHostSizeDIP() const; @@ -128,9 +138,15 @@ class MagnificationControllerImpl : virtual public MagnificationController, bool is_enabled_; + // True if the cursor needs to move the given position after the animation + // will be finished. When using this, set |position_after_animation_| as well. + bool move_cursor_after_animation_; + // Stores the position of cursor to be moved after animation. + gfx::Point position_after_animation_; + // Current scale, origin (left-top) position of the magnification window. float scale_; - gfx::Point origin_; + gfx::PointF origin_; DISALLOW_COPY_AND_ASSIGN(MagnificationControllerImpl); }; @@ -142,6 +158,7 @@ MagnificationControllerImpl::MagnificationControllerImpl() : root_window_(ash::Shell::GetPrimaryRootWindow()), is_on_zooming_(false), is_enabled_(false), + move_cursor_after_animation_(false), scale_(std::numeric_limits<double>::min()) { Shell::GetInstance()->AddPreTargetHandler(this); } @@ -153,25 +170,34 @@ MagnificationControllerImpl::~MagnificationControllerImpl() { void MagnificationControllerImpl::RedrawKeepingMousePosition( float scale, bool animate) { gfx::Point mouse_in_root = root_window_->GetLastMouseLocationInRoot(); - const gfx::Point origin = - gfx::Point(mouse_in_root.x() * (1.0f - 1.0f / scale), - mouse_in_root.y() * (1.0f - 1.0f / scale)); - Redraw(origin, scale, animate); + + // mouse_in_root is invalid value when the cursor is hidden. + if (!root_window_->bounds().Contains(mouse_in_root)) + return; + + const gfx::PointF origin = + gfx::PointF(mouse_in_root.x() - + (scale_ / scale) * (mouse_in_root.x() - origin_.x()), + mouse_in_root.y() - + (scale_ / scale) * (mouse_in_root.y() - origin_.y())); + bool changed = Redraw(origin, scale, animate); + if (changed) + AfterAnimationMoveCursorTo(mouse_in_root); } -bool MagnificationControllerImpl::Redraw(const gfx::Point& position, +bool MagnificationControllerImpl::Redraw(const gfx::PointF& position, float scale, bool animate) { - const gfx::Point position_in_dip = + const gfx::PointF position_in_dip = ui::ConvertPointToDIP(root_window_->layer(), position); return RedrawDIP(position_in_dip, scale, animate); } -bool MagnificationControllerImpl::RedrawDIP(const gfx::Point& position_in_dip, +bool MagnificationControllerImpl::RedrawDIP(const gfx::PointF& position_in_dip, float scale, bool animate) { - int x = position_in_dip.x(); - int y = position_in_dip.y(); + float x = position_in_dip.x(); + float y = position_in_dip.y(); ValidateScale(&scale); @@ -181,9 +207,9 @@ bool MagnificationControllerImpl::RedrawDIP(const gfx::Point& position_in_dip, y = 0; const gfx::Size host_size_in_dip = GetHostSizeDIP(); - const gfx::Size window_size_in_dip = GetWindowRectDIP(scale).size(); - int max_x = host_size_in_dip.width() - window_size_in_dip.width(); - int max_y = host_size_in_dip.height() - window_size_in_dip.height(); + const gfx::SizeF window_size_in_dip = GetWindowRectDIP(scale).size(); + float max_x = host_size_in_dip.width() - window_size_in_dip.width(); + float max_y = host_size_in_dip.height() - window_size_in_dip.height(); if (x > max_x) x = max_x; if (y > max_y) @@ -236,7 +262,7 @@ void MagnificationControllerImpl::EnsureRectIsVisibleDIP( bool animate) { ValidateScale(&scale); - const gfx::Rect window_rect = GetWindowRectDIP(scale); + const gfx::Rect window_rect = gfx::ToEnclosingRect(GetWindowRectDIP(scale)); if (scale == scale_ && window_rect.Contains(target_rect)) return; @@ -277,7 +303,7 @@ void MagnificationControllerImpl::OnMouseMove(const gfx::Point& location) { int y = origin_.y(); bool start_zoom = false; - const gfx::Rect window_rect = GetWindowRectDIP(scale_); + const gfx::Rect window_rect = gfx::ToEnclosingRect(GetWindowRectDIP(scale_)); const int left = window_rect.x(); const int right = window_rect.right(); const int width_margin = static_cast<int>(0.1f * window_rect.width()); @@ -314,32 +340,35 @@ void MagnificationControllerImpl::OnMouseMove(const gfx::Point& location) { int x_diff = origin_.x() - window_rect.x(); int y_diff = origin_.y() - window_rect.y(); // If the magnified region is moved, hides the mouse cursor and moves it. - if (x_diff != 0 || y_diff != 0) { - aura::client::CursorClient* cursor_client = - aura::client::GetCursorClient(root_window_); - if (cursor_client) - cursor_client->ShowCursor(false); - mouse.set_x(mouse.x() - (origin_.x() - window_rect.x())); - mouse.set_y(mouse.y() - (origin_.y() - window_rect.y())); - root_window_->MoveCursorTo(mouse); - } + if (x_diff != 0 || y_diff != 0) + AfterAnimationMoveCursorTo(mouse); } } } +void MagnificationControllerImpl::AfterAnimationMoveCursorTo( + const gfx::Point& location) { + aura::client::CursorClient* cursor_client = + aura::client::GetCursorClient(root_window_); + if (cursor_client) + cursor_client->ShowCursor(false); + move_cursor_after_animation_ = true; + position_after_animation_ = location; +} + gfx::Size MagnificationControllerImpl::GetHostSizeDIP() const { return ui::ConvertSizeToDIP(root_window_->layer(), root_window_->GetHostSize()); } -gfx::Rect MagnificationControllerImpl::GetWindowRectDIP(float scale) const { +gfx::RectF MagnificationControllerImpl::GetWindowRectDIP(float scale) const { const gfx::Size size_in_dip = ui::ConvertSizeToDIP(root_window_->layer(), root_window_->GetHostSize()); - const int width = size_in_dip.width() / scale; - const int height = size_in_dip.height() / scale; + const float width = size_in_dip.width() / scale; + const float height = size_in_dip.height() / scale; - return gfx::Rect(origin_.x(), origin_.y(), width, height); + return gfx::RectF(origin_.x(), origin_.y(), width, height); } bool MagnificationControllerImpl::IsMagnified() const { @@ -363,6 +392,12 @@ void MagnificationControllerImpl::ValidateScale(float* scale) { void MagnificationControllerImpl::OnImplicitAnimationsCompleted() { if (!is_on_zooming_) return; + + if (move_cursor_after_animation_) { + root_window_->MoveCursorTo(position_after_animation_); + move_cursor_after_animation_ = false; + } + aura::client::CursorClient* cursor_client = aura::client::GetCursorClient(root_window_); if (cursor_client) |