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

Unified 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, 3 months 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 side-by-side diff with in-line comments
Download patch
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..32f6ed70755b2cd26268f968d31cd121eb26ec13
--- /dev/null
+++ b/ash/magnifier/partial_magnification_controller.cc
@@ -0,0 +1,345 @@
+// 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_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;
+const int kZoomInset = 16;
+const int kMagnifierWidth = 200;
+const int kMagnifierHeight = 200;
oshima 2012/09/13 16:35:31 doument
Zachary Kuznia 2012/10/09 09:02:36 Done.
+
+} // namespace
+
+namespace ash {
+namespace internal {
+
+class ZoomDelegateView : public views::WidgetDelegateView {
+ public:
+ explicit ZoomDelegateView();
oshima 2012/09/13 16:35:31 remove explicit
Zachary Kuznia 2012/10/09 09:02:36 Done.
+ 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 aura::WindowObserver {
+ 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);
+
+ // Removes self as observer of old root window, sets the root window, and adds
+ // self as observer of the new window.
+ void SetRootWindow(aura::RootWindow* root_window);
+
+ // 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;
+
+ aura::RootWindow* root_window_;
+
+ // 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_(NULL),
+ 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) {
+ int x = location.x();
+ int y = location.y();
oshima 2012/09/13 16:35:31 or you can use gfx::Point::Offset. up to you.
Zachary Kuznia 2012/10/09 09:02:36 Done.
+
+ if (zoom_window_)
+ zoom_widget_->SetBounds(gfx::Rect(x - kMagnifierWidth / 2,
+ y - kMagnifierHeight / 2,
+ kMagnifierWidth, kMagnifierHeight));
+}
+
+bool PartialMagnificationControllerImpl::IsPartialMagnified() const {
+ return scale_ >= kMinPartialMagnifiedScaleThreshold;
+}
+
+void PartialMagnificationControllerImpl::CloseMagnifierWindow() {
+ if (zoom_window_) {
+ zoom_widget_->Close();
+ delete zoom_window_;
+ zoom_window_ = NULL;
+ zoom_widget_ = NULL;
+ }
+}
+
+void PartialMagnificationControllerImpl::SwitchTargetRootWindow(
+ aura::RootWindow* new_root_window) {
+ if (new_root_window == root_window_)
+ return;
+
+ float scale = GetScale();
+
+ SetScale(1.0f);
+ SetRootWindow(new_root_window);
+ SetScale(scale);
+}
+
+void PartialMagnificationControllerImpl::SetRootWindow(
+ aura::RootWindow* root_window) {
+ if (root_window_)
+ root_window_->RemoveObserver(this);
+
+ root_window_ = root_window;
+ if (root_window_)
+ root_window_->AddObserver(this);
+}
+
+
+////////////////////////////////////////////////////////////////////////////////
+// PartialMagnificationControllerImpl:
+// PartialMagnificationController implementation
+
+void PartialMagnificationControllerImpl::SetScale(float scale) {
+ if (!is_enabled_)
+ return;
+
+ scale_ = scale;
+
+ if (IsPartialMagnified()) {
+ if (!zoom_window_) {
+ if (!root_window_)
+ SetRootWindow(ash::Shell::GetActiveRootWindow());
oshima 2012/09/13 16:35:31 Don't you want to use root window where the mouse
Zachary Kuznia 2012/10/09 09:02:36 What is the proper way to get that window? Check
+
+ gfx::Point mouse(root_window_->GetLastMouseLocationInRoot());
+
+ zoom_window_ = new aura::Window(NULL);
+ zoom_window_->Init(ui::LAYER_NOT_DRAWN);
+ root_window_->AddChild(zoom_window_);
oshima 2012/09/13 16:35:31 to which container this will be added? Will this s
Zachary Kuznia 2012/10/09 09:02:36 It's added after the containers, so that it should
+
+ 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);
+ }
+ } else {
+ CloseMagnifierWindow();
+ }
+}
+
+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();
oshima 2012/09/13 16:35:31 IIRC, target can be on different root when input i
Zachary Kuznia 2012/10/09 09:02:36 It seems to work properly with menus.
+ gfx::Rect root_bounds = current_root->bounds();
+
+ if (root_bounds.Contains(event->root_location())) {
+ 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;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// PartialMagnificationControllerImpl: aura::WindowObserver implementation
+
+void PartialMagnificationControllerImpl::OnWindowDestroying(
+ aura::Window* window) {
+ aura::RootWindow* new_root_window = ash::Shell::GetActiveRootWindow();
+
+ if (new_root_window == root_window_)
+ new_root_window = ash::Shell::GetPrimaryRootWindow();
+
+ if (new_root_window == root_window_)
+ new_root_window = NULL;
+
+ SetRootWindow(new_root_window);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// PartialMagnificationController:
+
+// static
+PartialMagnificationController*
+PartialMagnificationController::CreateInstance() {
+ return new PartialMagnificationControllerImpl();
+}
+
+} // namespace internal
+} // namespace ash

Powered by Google App Engine
This is Rietveld 408576698