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

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, 2 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..327cae758d5c9c566fa00a0437ef83dc544a6f80
--- /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:
+
sky 2012/10/11 17:00:49 remove newline
Zachary Kuznia 2012/10/12 08:41:35 Done.
+ DISALLOW_COPY_AND_ASSIGN(ZoomDelegateView);
+};
+
+ZoomDelegateView::ZoomDelegateView() {
+}
+
+ZoomDelegateView::~ZoomDelegateView() {
+}
+
+gfx::Size ZoomDelegateView::GetPreferredSize() {
+ return child_count() > 0 ? child_at(0)->GetPreferredSize() : gfx::Size();
sky 2012/10/11 17:00:49 Set the layout manager to FillLayout and you can r
Zachary Kuznia 2012/10/12 08:41:35 Done.
+}
+
+void ZoomDelegateView::Layout() {
+ if (child_count())
+ 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() 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_;
sky 2012/10/11 17:00:49 scoped_ptr
sky 2012/10/11 17:00:49 Why do you need zoom_window_? AFAICT you create it
Zachary Kuznia 2012/10/12 08:41:35 Done.
+ 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(
sky 2012/10/11 17:00:49 What if someone is using touch input?
Zachary Kuznia 2012/10/12 08:41:35 Support for touch input will come in a future patc
+ 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));
+ }
+}
+
+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()))
+ 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();
sky 2012/10/11 17:00:49 Move all this into a function, maybe CreateZoomWin
Zachary Kuznia 2012/10/12 08:41:35 Done.
+ 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_;
sky 2012/10/11 17:00:49 Do you want to set can_activate and accept_events
Zachary Kuznia 2012/10/12 08:41:35 Done.
+ 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);
sky 2012/10/11 17:00:49 You should override GetContentsView() on ZoomDeleg
Zachary Kuznia 2012/10/12 08:41:35 Doing this causes it to crash in FillLayout(). Do
sky 2012/10/12 17:09:19 what is the crash. Maybe FillLayoutGetPreferredSiz
Zachary Kuznia 2012/10/15 08:28:40 Actually, it turns out that since I removed zoom_w
+ 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) {
sky 2012/10/11 17:00:49 Do you need both is_enabled_ and the scale? If you
Zachary Kuznia 2012/10/12 08:41:35 They are different. When it's enabled, you can to
+ 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

Powered by Google App Engine
This is Rietveld 408576698