Chromium Code Reviews| Index: ash/magnifier/partial_magnification_controller.cc |
| diff --git a/ash/magnifier/partial_magnification_controller.cc b/ash/magnifier/partial_magnification_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b0df86a92f8482cf5b37abdd8dd5ef43af7b2b81 |
| --- /dev/null |
| +++ b/ash/magnifier/partial_magnification_controller.cc |
| @@ -0,0 +1,311 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/magnifier/partial_magnification_controller.h" |
| + |
| +#include "ash/shell.h" |
| +#include "ui/aura/event_filter.h" |
| +#include "ui/aura/root_window.h" |
| +#include "ui/aura/shared/compound_event_filter.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/aura/window_property.h" |
| +#include "ui/gfx/point3.h" |
| +#include "ui/gfx/screen.h" |
| +#include "ui/compositor/layer.h" |
| +#include "ui/views/widget/widget.h" |
| +#include "ui/views/widget/widget_delegate.h" |
| + |
| +namespace { |
| + |
| +const float kMaxPartialMagnifiedScale = 4.0f; |
| +const float kMaxPartialMagnifiedScaleThreshold = 4.0f; |
| +const float kMinPartialMagnifiedScaleThreshold = 1.1f; |
| + |
| +} // namespace |
| + |
| +namespace ash { |
| +namespace internal { |
| + |
| +class ZoomDelegateView : public views::WidgetDelegateView { |
| + public: |
| + explicit ZoomDelegateView(); |
| + virtual ~ZoomDelegateView(); |
| + |
| + // views::View overrides |
| + virtual gfx::Size GetPreferredSize() OVERRIDE; |
| + virtual void Layout() OVERRIDE; |
| + |
| + // views::WidgetZoomDelegateView overrides: |
| + virtual bool CanActivate() const OVERRIDE { |
| + return false; |
| + } |
| + |
| + private: |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ZoomDelegateView); |
| +}; |
| + |
| +ZoomDelegateView::ZoomDelegateView() { |
| +} |
| + |
| +ZoomDelegateView::~ZoomDelegateView() { |
| +} |
| + |
| +gfx::Size ZoomDelegateView::GetPreferredSize() { |
| + return child_count() > 0 ? child_at(0)->GetPreferredSize() : gfx::Size(); |
| +} |
| + |
| +void ZoomDelegateView::Layout() { |
| + if (child_count() == 0) |
| + return; |
| + child_at(0)->SetBounds(0, 0, width(), height()); |
| +} |
| + |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// PartialMagnificationControllerImpl: |
| + |
| +class PartialMagnificationControllerImpl |
| + : public PartialMagnificationController, |
| + public aura::EventFilter { |
| + public: |
| + PartialMagnificationControllerImpl(); |
| + virtual ~PartialMagnificationControllerImpl(); |
| + |
| + // PartialMagnificationController overrides: |
| + virtual void SetEnabled(bool enabled) OVERRIDE; |
| + virtual bool IsEnabled() OVERRIDE; |
| + virtual void SetScale(float scale) OVERRIDE; |
| + virtual float GetScale() const OVERRIDE { return scale_; } |
| + |
| + private: |
| + void OnMouseMove(const gfx::Point& location); |
| + |
| + // Switch PartialMagnified RootWindow to |new_root_window|. This does |
| + // following: |
| + // - Remove the magnifier from the current root window. |
| + // - Create a magnifier in the new root_window |new_root_window|. |
| + // - Switch the target window from current window to |new_root_window|. |
| + void SwitchTargetRootWindow(aura::RootWindow* new_root_window); |
| + |
| + // Returns if the magnification scale is 1.0 or not (larger then 1.0). |
|
oshima
2012/09/07 22:30:55
s/then/than
but this description is not clear if i
Zachary Kuznia
2012/09/13 11:49:02
Done.
|
| + bool IsPartialMagnified() const; |
| + |
| + // Correct the givin scale value if nessesary. |
| + void ValidateScale(float* scale); |
|
oshima
2012/09/07 22:30:55
CorrectScale, EnsureScale ?
validate sounds like
Zachary Kuznia
2012/09/13 11:49:02
Removed.
|
| + |
| + // aura::EventFilter overrides: |
| + virtual bool PreHandleKeyEvent(aura::Window* target, |
| + ui::KeyEvent* event) OVERRIDE; |
| + virtual bool PreHandleMouseEvent(aura::Window* target, |
| + ui::MouseEvent* event) OVERRIDE; |
| + virtual ui::TouchStatus PreHandleTouchEvent( |
| + aura::Window* target, |
| + ui::TouchEvent* event) OVERRIDE; |
| + virtual ui::EventResult PreHandleGestureEvent( |
| + aura::Window* target, |
| + ui::GestureEvent* event) OVERRIDE; |
| + |
| + aura::RootWindow* root_window_; |
|
sky
2012/09/07 16:06:59
What happens if root_window_ is destroyed?
Zachary Kuznia
2012/09/13 11:49:02
Added self as an observer.
|
| + |
| + // True if the magnified window is in motion of zooming or un-zooming effect. |
| + // Otherwise, false. |
| + bool is_on_zooming_; |
| + |
| + bool is_enabled_; |
| + |
| + // Current scale, origin (left-top) position of the magnification window. |
| + float scale_; |
| + gfx::Point origin_; |
| + |
| + aura::Window* zoom_window_; |
| + views::Widget* zoom_widget_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PartialMagnificationControllerImpl); |
| +}; |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// PartialMagnificationControllerImpl: |
| + |
| +PartialMagnificationControllerImpl::PartialMagnificationControllerImpl() |
| + : root_window_(ash::Shell::GetPrimaryRootWindow()), |
|
oshima
2012/09/07 22:30:55
Do you need to set this to primary first? Or can w
Zachary Kuznia
2012/09/13 11:49:02
Deferred to using GetActiveRootWindow() when enabl
|
| + is_on_zooming_(false), |
| + is_enabled_(false), |
| + scale_(kNonPartialMagnifiedScale), |
| + zoom_window_(NULL), |
| + zoom_widget_(NULL) { |
| + Shell::GetInstance()->AddEnvEventFilter(this); |
| +} |
| + |
| +PartialMagnificationControllerImpl::~PartialMagnificationControllerImpl() { |
| + Shell::GetInstance()->RemoveEnvEventFilter(this); |
|
sky
2012/09/07 16:06:59
What happens if zoom_window_/zoom_widget_ are non-
Zachary Kuznia
2012/09/13 11:49:02
Fixed.
|
| +} |
| + |
| +void PartialMagnificationControllerImpl::OnMouseMove( |
| + const gfx::Point& location) { |
| + int x = location.x(); |
| + int y = location.y(); |
| + |
| + if (zoom_window_) { |
| + zoom_widget_->SetBounds(gfx::Rect(x - 100, y - 100, 200, 200)); |
| + } |
|
oshima
2012/09/07 22:30:55
nuke {} and use const for size.
Zachary Kuznia
2012/09/13 11:49:02
Done.
|
| +} |
| + |
| +bool PartialMagnificationControllerImpl::IsPartialMagnified() const { |
| + return scale_ >= kMinPartialMagnifiedScaleThreshold; |
| +} |
| + |
| +void PartialMagnificationControllerImpl::ValidateScale(float* scale) { |
| + // Adjust the scale to adjust |kNonPartialMagnifiedScale| if scale is |
| + // smaller than |kMinPartialMagnifiedScaleThreshold|; |
| + if (*scale < kMinPartialMagnifiedScaleThreshold) |
| + *scale = kNonPartialMagnifiedScale; |
|
sky
2012/09/07 16:06:59
How come you have Threshold and Scale? And the com
Zachary Kuznia
2012/09/13 11:49:02
Removed this function for now, since magnification
|
| + |
| + // Adjust the scale to adjust |kMinPartialMagnifiedScale| if scale is bigger |
| + // than |kMinPartialMagnifiedScaleThreshold|; |
| + if (*scale > kMaxPartialMagnifiedScaleThreshold) |
|
oshima
2012/09/07 22:30:55
else if
Zachary Kuznia
2012/09/13 11:49:02
Removed this function for now, since magnification
|
| + *scale = kMaxPartialMagnifiedScale; |
| +} |
| + |
| +void PartialMagnificationControllerImpl::SwitchTargetRootWindow( |
| + aura::RootWindow* new_root_window) { |
| + if (new_root_window == root_window_) |
| + return; |
| + |
| + float scale = GetScale(); |
| + |
| + SetScale(1.0f); |
| + root_window_ = new_root_window; |
| + SetScale(scale); |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// PartialMagnificationControllerImpl: |
| +// PartialMagnificationController implementation |
| + |
| +void PartialMagnificationControllerImpl::SetScale(float scale) { |
| + if (!is_enabled_) |
| + return; |
| + |
| + ValidateScale(&scale); |
| + |
| + scale_ = scale; |
| + |
| + const int width = 200; |
| + const int height = 200; |
| + if (IsPartialMagnified()) { |
| + if (!zoom_window_) { |
| + gfx::Point mouse(root_window_->GetLastMouseLocationInRoot()); |
| + |
| + zoom_window_ = new aura::Window(NULL); |
|
sky
2012/09/07 16:06:59
Who deletes this?
Zachary Kuznia
2012/09/13 11:49:02
Added the delete after close.
|
| + zoom_window_->Init(ui::LAYER_NOT_DRAWN); |
| + root_window_->AddChild(zoom_window_); |
| + |
| + zoom_widget_ = new views::Widget; |
| + views::Widget::InitParams params( |
| + views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| + params.transparent = true; |
| + params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
|
sky
2012/09/07 16:06:59
You never delete zoom_widget_, which means because
oshima
2012/09/07 22:30:55
To add a bit more explanation, widget with WIDGET_
Zachary Kuznia
2012/09/13 11:49:02
Fixed.
|
| + params.parent = zoom_window_; |
| + ZoomDelegateView* delegate_view = new ZoomDelegateView; |
| + params.delegate = delegate_view; |
| + zoom_widget_->Init(params); |
| + zoom_widget_->SetBounds(gfx::Rect(mouse.x() - width / 2, |
|
sky
2012/09/07 16:06:59
You should make sure this does what you want when
Zachary Kuznia
2012/09/13 11:49:02
It seems to. What strange behavior might happen?
|
| + mouse.y() - height / 2, |
| + width, height)); |
| + zoom_widget_->set_focus_on_creation(false); |
| + zoom_widget_->SetContentsView(delegate_view); |
| + zoom_widget_->Show(); |
| + zoom_window_->layer()->SetBounds(gfx::Rect(0, 0, width, height)); |
| + zoom_window_->Show(); |
| + |
| + zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom( |
| + (width - (width / scale_)) / 2, |
| + (height - (height / scale_)) / 2, |
| + scale_); |
| + } |
| + } else { |
| + if (zoom_window_) { |
| + zoom_widget_->Close(); |
| + zoom_window_ = NULL; |
| + zoom_widget_ = NULL; |
| + } |
| + } |
| +} |
| + |
| +void PartialMagnificationControllerImpl::SetEnabled(bool enabled) { |
| + if (enabled) { |
| + is_enabled_ = enabled; |
| + SetScale(kDefaultPartialMagnifiedScale); |
| + } else { |
| + SetScale(kNonPartialMagnifiedScale); |
| + is_enabled_ = enabled; |
| + } |
| +} |
| + |
| +bool PartialMagnificationControllerImpl::IsEnabled() { |
| + return is_enabled_; |
| +} |
| + |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// PartialMagnificationControllerImpl: aura::EventFilter implementation |
| + |
| +bool PartialMagnificationControllerImpl::PreHandleKeyEvent( |
| + aura::Window* target, |
| + ui::KeyEvent* event) { |
| + return false; |
| +} |
| + |
| +bool PartialMagnificationControllerImpl::PreHandleMouseEvent( |
| + aura::Window* target, |
| + ui::MouseEvent* event) { |
| + if (event->type() == ui::ET_SCROLL && event->IsAltDown()) { |
| + ui::ScrollEvent* scroll_event = static_cast<ui::ScrollEvent*>(event); |
| + if (scroll_event->y_offset() > 0) |
| + SetScale(kDefaultPartialMagnifiedScale); |
| + else |
| + SetScale(1); |
| + |
| + return true; |
| + } |
| + |
| + if (IsPartialMagnified() && event->type() == ui::ET_MOUSE_MOVED) { |
| + aura::RootWindow* current_root = target->GetRootWindow(); |
| + gfx::Rect root_bounds = current_root->bounds(); |
| + |
| + if (root_bounds.Contains(event->root_location())) { |
|
oshima
2012/09/07 22:30:55
I believe this root location is native X's, and no
Zachary Kuznia
2012/09/13 11:49:02
This seems to be aura's root location. When I con
|
| + if (current_root != root_window_) |
| + SwitchTargetRootWindow(current_root); |
| + |
| + OnMouseMove(event->root_location()); |
| + } |
| + } |
| + |
| + return false; |
| +} |
| + |
| +ui::TouchStatus PartialMagnificationControllerImpl::PreHandleTouchEvent( |
| + aura::Window* target, |
| + ui::TouchEvent* event) { |
| + return ui::TOUCH_STATUS_UNKNOWN; |
| +} |
| + |
| +ui::EventResult PartialMagnificationControllerImpl::PreHandleGestureEvent( |
| + aura::Window* target, |
| + ui::GestureEvent* event) { |
| + return ui::ER_UNHANDLED; |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// PartialMagnificationController: |
| + |
| +// static |
| +PartialMagnificationController* |
| +PartialMagnificationController::CreateInstance() { |
| + return new PartialMagnificationControllerImpl(); |
| +} |
| + |
| +} // namespace internal |
| +} // namespace ash |