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..b309757333735c4d3b60b9a94026bf141a97257c |
| --- /dev/null |
| +++ b/ash/magnifier/partial_magnification_controller.cc |
| @@ -0,0 +1,351 @@ |
| +// 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 "ash/shell_window_ids.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_observer.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 kMinPartialMagnifiedScaleThreshold = 1.1f; |
| + |
| +// Number of pixels to make the border of the magnified area. |
| +const int kZoomInset = 16; |
| + |
| +// Width of the magnified area. |
| +const int kMagnifierWidth = 200; |
| + |
| +// Height of the magnified area. |
| +const int kMagnifierHeight = 200; |
| + |
| +} // namespace |
| + |
| +namespace ash { |
| +namespace internal { |
| + |
| +class ZoomDelegateView : public views::WidgetDelegateView { |
| + public: |
| + 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()); |
|
oshima
2012/10/10 19:55:52
if (child_count())
child_at(0)->SetBounds(0, 0,
Zachary Kuznia
2012/10/11 08:57:24
Done.
|
| +} |
| + |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// PartialMagnificationControllerImpl: |
| + |
| +class PartialMagnificationControllerImpl |
| + : public PartialMagnificationController, |
| + public aura::EventFilter, |
| + public aura::WindowObserver { |
| + public: |
| + PartialMagnificationControllerImpl(); |
| + virtual ~PartialMagnificationControllerImpl(); |
| + |
| + // PartialMagnificationController overrides: |
| + virtual void SetEnabled(bool enabled) OVERRIDE; |
| + virtual bool IsEnabled() const OVERRIDE; |
| + virtual void SetScale(float scale) OVERRIDE; |
| + virtual float GetScale() const OVERRIDE { return scale_; } |
| + |
| + private: |
| + void OnMouseMove(const gfx::Point& location_in_root); |
| + |
| + // 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 the root window that contains the mouse cursor. |
| + aura::RootWindow* GetCurrentRootWindow(); |
| + |
| + // Return true if the magnification scale > kMinPartialMagnifiedScaleThreshold |
| + bool IsPartialMagnified() const; |
| + |
| + // Cleans up the window if needed. |
| + void CloseMagnifierWindow(); |
| + |
| + // 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; |
| + |
| + // Overridden from WindowObserver: |
| + virtual void OnWindowDestroying(aura::Window* window) OVERRIDE; |
| + |
| + // 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() |
| + : is_on_zooming_(false), |
| + is_enabled_(false), |
| + scale_(kNonPartialMagnifiedScale), |
| + zoom_window_(NULL), |
| + zoom_widget_(NULL) { |
| + Shell::GetInstance()->AddEnvEventFilter(this); |
| +} |
| + |
| +PartialMagnificationControllerImpl::~PartialMagnificationControllerImpl() { |
| + CloseMagnifierWindow(); |
| + |
| + Shell::GetInstance()->RemoveEnvEventFilter(this); |
| +} |
| + |
| +void PartialMagnificationControllerImpl::OnMouseMove( |
| + const gfx::Point& location_in_root) { |
| + gfx::Point origin(location_in_root); |
| + |
| + origin.Offset(-kMagnifierWidth / 2, -kMagnifierHeight / 2); |
| + |
| + if (zoom_window_) |
| + zoom_widget_->SetBounds(gfx::Rect(origin.x(), origin.y(), |
| + kMagnifierWidth, kMagnifierHeight)); |
|
oshima
2012/10/10 19:55:52
you need {} in this case.
Zachary Kuznia
2012/10/11 08:57:24
Done.
|
| +} |
| + |
| +bool PartialMagnificationControllerImpl::IsPartialMagnified() const { |
| + return scale_ >= kMinPartialMagnifiedScaleThreshold; |
| +} |
| + |
| +void PartialMagnificationControllerImpl::CloseMagnifierWindow() { |
| + if (zoom_window_) { |
| + zoom_window_->RemoveObserver(this); |
| + aura::RootWindow* root_window = zoom_window_->GetRootWindow(); |
| + if (root_window) |
| + root_window->RemoveObserver(this); |
| + |
| + zoom_widget_->Close(); |
| + delete zoom_window_; |
| + zoom_window_ = NULL; |
| + zoom_widget_ = NULL; |
| + } |
| +} |
| + |
| +void PartialMagnificationControllerImpl::SwitchTargetRootWindow( |
| + aura::RootWindow* new_root_window) { |
| + if (zoom_window_ && new_root_window == zoom_window_->GetRootWindow()) |
| + return; |
| + |
| + float scale = GetScale(); |
| + |
| + CloseMagnifierWindow(); |
| + SetScale(scale); |
| +} |
| + |
| +aura::RootWindow* PartialMagnificationControllerImpl::GetCurrentRootWindow() { |
| + Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); |
| + for (Shell::RootWindowList::const_iterator iter = root_windows.begin(); |
| + iter != root_windows.end(); ++iter) { |
| + aura::RootWindow* root_window = *iter; |
| + if (root_window->ContainsPointInRoot( |
| + root_window->GetLastMouseLocationInRoot())) |
|
oshima
2012/10/10 19:55:52
indent
Zachary Kuznia
2012/10/11 08:57:24
Done.
|
| + return root_window; |
| + } |
| + return NULL; |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// PartialMagnificationControllerImpl: |
| +// PartialMagnificationController implementation |
| + |
| +void PartialMagnificationControllerImpl::SetScale(float scale) { |
| + if (!is_enabled_) |
| + return; |
| + |
| + scale_ = scale; |
| + |
| + if (IsPartialMagnified()) { |
| + if (!zoom_window_) { |
| + aura::RootWindow* root_window = GetCurrentRootWindow(); |
| + if (!root_window) |
| + return; |
| + |
| + root_window->AddObserver(this); |
| + |
| + gfx::Point mouse(root_window->GetLastMouseLocationInRoot()); |
| + |
| + zoom_window_ = new aura::Window(NULL); |
| + 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.parent = zoom_window_; |
| + ZoomDelegateView* delegate_view = new ZoomDelegateView; |
| + params.delegate = delegate_view; |
| + zoom_widget_->Init(params); |
| + zoom_widget_->SetBounds(gfx::Rect(mouse.x() - kMagnifierWidth / 2, |
| + mouse.y() - kMagnifierHeight / 2, |
| + kMagnifierWidth, kMagnifierHeight)); |
| + zoom_widget_->set_focus_on_creation(false); |
| + zoom_widget_->SetContentsView(delegate_view); |
| + zoom_widget_->Show(); |
| + zoom_window_->layer()->SetBounds(gfx::Rect(0, 0, |
| + kMagnifierWidth, |
| + kMagnifierHeight)); |
| + zoom_window_->Show(); |
| + |
| + zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom( |
| + (kMagnifierWidth - (kMagnifierWidth / scale_)) / 2, |
| + (kMagnifierHeight - (kMagnifierHeight / scale_)) / 2, |
| + scale_, |
| + kZoomInset); |
| + |
| + zoom_window_->AddObserver(this); |
| + } |
| + } else { |
| + CloseMagnifierWindow(); |
| + } |
| +} |
| + |
| +void PartialMagnificationControllerImpl::SetEnabled(bool enabled) { |
| + if (enabled) { |
| + is_enabled_ = enabled; |
| + SetScale(kDefaultPartialMagnifiedScale); |
| + } else { |
| + SetScale(kNonPartialMagnifiedScale); |
| + is_enabled_ = enabled; |
| + } |
| +} |
| + |
| +bool PartialMagnificationControllerImpl::IsEnabled() const { |
| + 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 (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())) { |
| + 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; |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// PartialMagnificationControllerImpl: aura::WindowObserver implementation |
| + |
| +void PartialMagnificationControllerImpl::OnWindowDestroying( |
| + aura::Window* window) { |
| + if (window == zoom_window_) { |
| + aura::RootWindow* root_window = zoom_window_->GetRootWindow(); |
| + if (root_window) |
| + root_window->RemoveObserver(this); |
| + zoom_window_ = NULL; |
| + zoom_widget_ = NULL; |
| + } else { |
| + CloseMagnifierWindow(); |
| + |
| + aura::RootWindow* new_root_window = GetCurrentRootWindow(); |
| + if (new_root_window != window) |
| + SwitchTargetRootWindow(new_root_window); |
| + } |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// PartialMagnificationController: |
| + |
| +// static |
| +PartialMagnificationController* |
| +PartialMagnificationController::CreateInstance() { |
| + return new PartialMagnificationControllerImpl(); |
| +} |
| + |
| +} // namespace internal |
| +} // namespace ash |