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..3485b9da82367b7dfc9efa4d24e5cee40445f71f |
--- /dev/null |
+++ b/ash/magnifier/partial_magnification_controller.cc |
@@ -0,0 +1,238 @@ |
+// 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/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/screen.h" |
+#include "ui/compositor/layer.h" |
+#include "ui/views/layout/fill_layout.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 { |
+ |
+PartialMagnificationController::PartialMagnificationController() |
+ : is_on_zooming_(false), |
+ is_enabled_(false), |
+ scale_(kNonPartialMagnifiedScale), |
+ zoom_widget_(NULL) { |
+ Shell::GetInstance()->AddEnvEventFilter(this); |
+} |
+ |
+PartialMagnificationController::~PartialMagnificationController() { |
+ CloseMagnifierWindow(); |
+ |
+ Shell::GetInstance()->RemoveEnvEventFilter(this); |
+} |
+ |
+void PartialMagnificationController::SetScale(float scale) { |
+ if (!is_enabled_) |
+ return; |
+ |
+ scale_ = scale; |
+ |
+ if (IsPartialMagnified()) { |
+ CreateMagnifierWindow(); |
+ } else { |
+ CloseMagnifierWindow(); |
+ } |
+} |
+ |
+float PartialMagnificationController::GetScale() const { |
+ return scale_; |
oshima
2012/10/16 23:43:24
this should be accessor in header.
|
+} |
+ |
+void PartialMagnificationController::SetEnabled(bool enabled) { |
+ if (enabled) { |
+ is_enabled_ = enabled; |
+ SetScale(kDefaultPartialMagnifiedScale); |
+ } else { |
+ SetScale(kNonPartialMagnifiedScale); |
+ is_enabled_ = enabled; |
+ } |
+} |
+ |
+bool PartialMagnificationController::IsEnabled() const { |
+ return is_enabled_; |
+} |
+ |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// PartialMagnificationController: aura::EventFilter implementation |
+ |
+bool PartialMagnificationController::PreHandleKeyEvent( |
+ aura::Window* target, |
+ ui::KeyEvent* event) { |
+ return false; |
+} |
+ |
+bool PartialMagnificationController::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(); |
oshima
2012/10/16 23:43:24
This probably doesn't work if event is captured by
Zachary Kuznia
2012/11/12 08:43:46
Added TODO
On 2012/10/16 23:43:24, oshima wrote:
|
+ |
+ if (root_bounds.Contains(event->root_location())) { |
+ SwitchTargetRootWindow(current_root); |
+ |
+ OnMouseMove(event->root_location()); |
+ } |
+ } |
+ |
+ return false; |
+} |
+ |
+ui::TouchStatus PartialMagnificationController::PreHandleTouchEvent( |
+ aura::Window* target, |
+ ui::TouchEvent* event) { |
+ return ui::TOUCH_STATUS_UNKNOWN; |
+} |
+ |
+ui::EventResult PartialMagnificationController::PreHandleGestureEvent( |
+ aura::Window* target, |
+ ui::GestureEvent* event) { |
+ return ui::ER_UNHANDLED; |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// PartialMagnificationController: aura::WindowObserver implementation |
+ |
+void PartialMagnificationController::OnWindowDestroying( |
+ aura::Window* window) { |
+ CloseMagnifierWindow(); |
+ |
+ aura::RootWindow* new_root_window = GetCurrentRootWindow(); |
sky
2012/10/16 16:29:22
This makes me nervous. In particular this assumes
oshima
2012/10/16 23:43:24
I actually wonder if we ever need this. When displ
Zachary Kuznia
2012/11/12 08:43:46
That is correct, it will get recreated when the mo
|
+ if (new_root_window != window) |
+ SwitchTargetRootWindow(new_root_window); |
+} |
+ |
+void PartialMagnificationController::OnWidgetClosing( |
+ views::Widget* widget) { |
+ widget->RemoveObserver(this); |
+ |
+ aura::RootWindow* root_window = |
+ zoom_widget_->GetNativeView()->GetRootWindow(); |
+ if (root_window) |
sky
2012/10/16 16:29:22
This should be a DCHECK. And refactor CloseMagnifi
Zachary Kuznia
2012/11/12 08:43:46
Done.
|
+ root_window->RemoveObserver(this); |
+ |
+ zoom_widget_ = NULL; |
+} |
+ |
+void PartialMagnificationController::OnMouseMove( |
+ const gfx::Point& location_in_root) { |
+ gfx::Point origin(location_in_root); |
+ |
+ origin.Offset(-kMagnifierWidth / 2, -kMagnifierHeight / 2); |
+ |
+ if (zoom_widget_) { |
+ zoom_widget_->SetBounds(gfx::Rect(origin.x(), origin.y(), |
+ kMagnifierWidth, kMagnifierHeight)); |
+ } |
+} |
+ |
+bool PartialMagnificationController::IsPartialMagnified() const { |
+ return scale_ >= kMinPartialMagnifiedScaleThreshold; |
+} |
+ |
+void PartialMagnificationController::CreateMagnifierWindow() { |
+ if (zoom_widget_) |
+ return; |
+ |
+ aura::RootWindow* root_window = GetCurrentRootWindow(); |
+ if (!root_window) |
+ return; |
+ |
+ root_window->AddObserver(this); |
+ |
+ gfx::Point mouse(root_window->GetLastMouseLocationInRoot()); |
+ |
+ zoom_widget_ = new views::Widget; |
+ views::Widget::InitParams params( |
+ views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
+ params.can_activate = false; |
+ params.accept_events = false; |
+ params.transparent = true; |
+ params.parent = root_window; |
+ 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_->Show(); |
+ |
+ zoom_widget_->GetNativeView()->layer()->SetBounds( |
+ gfx::Rect(0, 0, |
+ kMagnifierWidth, |
+ kMagnifierHeight)); |
+ zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom( |
+ (kMagnifierWidth - (kMagnifierWidth / scale_)) / 2, |
+ (kMagnifierHeight - (kMagnifierHeight / scale_)) / 2, |
+ scale_, |
+ kZoomInset); |
+ |
+ zoom_widget_->AddObserver(this); |
+} |
+ |
+void PartialMagnificationController::CloseMagnifierWindow() { |
+ if (zoom_widget_) { |
+ zoom_widget_->RemoveObserver(this); |
+ aura::RootWindow* root_window = |
+ zoom_widget_->GetNativeView()->GetRootWindow(); |
+ if (root_window) |
+ root_window->RemoveObserver(this); |
+ |
+ zoom_widget_->Close(); |
+ zoom_widget_ = NULL; |
+ } |
+} |
+ |
+void PartialMagnificationController::SwitchTargetRootWindow( |
+ aura::RootWindow* new_root_window) { |
+ if (zoom_widget_ && |
+ new_root_window == zoom_widget_->GetNativeView()->GetRootWindow()) |
+ return; |
+ |
+ float scale = GetScale(); |
+ |
+ CloseMagnifierWindow(); |
+ SetScale(scale); |
sky
2012/10/16 16:29:22
Why the SetScale here? And if you really need it,
Zachary Kuznia
2012/11/12 08:43:46
Added a comment to clarify what's going on.
On 20
|
+} |
+ |
+aura::RootWindow* PartialMagnificationController::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; |
+} |
+ |
+} // namespace internal |
+} // namespace ash |