OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/magnifier/partial_magnification_controller.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "ash/shell_window_ids.h" |
| 9 #include "ui/aura/root_window.h" |
| 10 #include "ui/views/corewm/compound_event_filter.h" |
| 11 #include "ui/aura/window.h" |
| 12 #include "ui/aura/window_property.h" |
| 13 #include "ui/gfx/screen.h" |
| 14 #include "ui/compositor/layer.h" |
| 15 #include "ui/views/layout/fill_layout.h" |
| 16 #include "ui/views/widget/widget.h" |
| 17 #include "ui/views/widget/widget_delegate.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 const float kMinPartialMagnifiedScaleThreshold = 1.1f; |
| 22 |
| 23 // Number of pixels to make the border of the magnified area. |
| 24 const int kZoomInset = 16; |
| 25 |
| 26 // Width of the magnified area. |
| 27 const int kMagnifierWidth = 200; |
| 28 |
| 29 // Height of the magnified area. |
| 30 const int kMagnifierHeight = 200; |
| 31 |
| 32 // Name of the magnifier window. |
| 33 const char kPartialMagniferWindowName[] = "PartialMagnifierWindow"; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 namespace ash { |
| 38 |
| 39 PartialMagnificationController::PartialMagnificationController() |
| 40 : is_on_zooming_(false), |
| 41 is_enabled_(false), |
| 42 scale_(kNonPartialMagnifiedScale), |
| 43 zoom_widget_(NULL) { |
| 44 Shell::GetInstance()->AddPreTargetHandler(this); |
| 45 } |
| 46 |
| 47 PartialMagnificationController::~PartialMagnificationController() { |
| 48 CloseMagnifierWindow(); |
| 49 |
| 50 Shell::GetInstance()->RemovePreTargetHandler(this); |
| 51 } |
| 52 |
| 53 void PartialMagnificationController::SetScale(float scale) { |
| 54 if (!is_enabled_) |
| 55 return; |
| 56 |
| 57 scale_ = scale; |
| 58 |
| 59 if (IsPartialMagnified()) { |
| 60 CreateMagnifierWindow(); |
| 61 } else { |
| 62 CloseMagnifierWindow(); |
| 63 } |
| 64 } |
| 65 |
| 66 void PartialMagnificationController::SetEnabled(bool enabled) { |
| 67 if (enabled) { |
| 68 is_enabled_ = enabled; |
| 69 SetScale(kDefaultPartialMagnifiedScale); |
| 70 } else { |
| 71 SetScale(kNonPartialMagnifiedScale); |
| 72 is_enabled_ = enabled; |
| 73 } |
| 74 } |
| 75 |
| 76 //////////////////////////////////////////////////////////////////////////////// |
| 77 // PartialMagnificationController: ui::EventHandler implementation |
| 78 |
| 79 ui::EventResult PartialMagnificationController::OnKeyEvent( |
| 80 ui::KeyEvent* event) { |
| 81 return ui::ER_UNHANDLED; |
| 82 } |
| 83 |
| 84 ui::EventResult PartialMagnificationController::OnMouseEvent( |
| 85 ui::MouseEvent* event) { |
| 86 if (IsPartialMagnified() && event->type() == ui::ET_MOUSE_MOVED) { |
| 87 aura::Window* target = static_cast<aura::Window*>(event->target()); |
| 88 aura::RootWindow* current_root = target->GetRootWindow(); |
| 89 // TODO(zork): Handle the case where the event is captured on a different |
| 90 // display, such as when a menu is opened. |
| 91 gfx::Rect root_bounds = current_root->bounds(); |
| 92 |
| 93 if (root_bounds.Contains(event->root_location())) { |
| 94 SwitchTargetRootWindow(current_root); |
| 95 |
| 96 OnMouseMove(event->root_location()); |
| 97 } |
| 98 } |
| 99 |
| 100 return ui::ER_UNHANDLED; |
| 101 } |
| 102 |
| 103 ui::EventResult PartialMagnificationController::OnScrollEvent( |
| 104 ui::ScrollEvent* event) { |
| 105 return ui::ER_UNHANDLED; |
| 106 } |
| 107 |
| 108 ui::EventResult PartialMagnificationController::OnTouchEvent( |
| 109 ui::TouchEvent* event) { |
| 110 return ui::ER_UNHANDLED; |
| 111 } |
| 112 |
| 113 ui::EventResult PartialMagnificationController::OnGestureEvent( |
| 114 ui::GestureEvent* event) { |
| 115 return ui::ER_UNHANDLED; |
| 116 } |
| 117 |
| 118 //////////////////////////////////////////////////////////////////////////////// |
| 119 // PartialMagnificationController: aura::WindowObserver implementation |
| 120 |
| 121 void PartialMagnificationController::OnWindowDestroying( |
| 122 aura::Window* window) { |
| 123 CloseMagnifierWindow(); |
| 124 |
| 125 aura::RootWindow* new_root_window = GetCurrentRootWindow(); |
| 126 if (new_root_window != window) |
| 127 SwitchTargetRootWindow(new_root_window); |
| 128 } |
| 129 |
| 130 void PartialMagnificationController::OnWidgetClosing( |
| 131 views::Widget* widget) { |
| 132 DCHECK_EQ(widget, zoom_widget_); |
| 133 RemoveZoomWidgetObservers(); |
| 134 zoom_widget_ = NULL; |
| 135 } |
| 136 |
| 137 void PartialMagnificationController::OnMouseMove( |
| 138 const gfx::Point& location_in_root) { |
| 139 gfx::Point origin(location_in_root); |
| 140 |
| 141 origin.Offset(-kMagnifierWidth / 2, -kMagnifierHeight / 2); |
| 142 |
| 143 if (zoom_widget_) { |
| 144 zoom_widget_->SetBounds(gfx::Rect(origin.x(), origin.y(), |
| 145 kMagnifierWidth, kMagnifierHeight)); |
| 146 } |
| 147 } |
| 148 |
| 149 bool PartialMagnificationController::IsPartialMagnified() const { |
| 150 return scale_ >= kMinPartialMagnifiedScaleThreshold; |
| 151 } |
| 152 |
| 153 void PartialMagnificationController::CreateMagnifierWindow() { |
| 154 if (zoom_widget_) |
| 155 return; |
| 156 |
| 157 aura::RootWindow* root_window = GetCurrentRootWindow(); |
| 158 if (!root_window) |
| 159 return; |
| 160 |
| 161 root_window->AddObserver(this); |
| 162 |
| 163 gfx::Point mouse(root_window->GetLastMouseLocationInRoot()); |
| 164 |
| 165 zoom_widget_ = new views::Widget; |
| 166 views::Widget::InitParams params( |
| 167 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 168 params.can_activate = false; |
| 169 params.accept_events = false; |
| 170 params.transparent = true; |
| 171 params.parent = root_window; |
| 172 zoom_widget_->Init(params); |
| 173 zoom_widget_->SetBounds(gfx::Rect(mouse.x() - kMagnifierWidth / 2, |
| 174 mouse.y() - kMagnifierHeight / 2, |
| 175 kMagnifierWidth, kMagnifierHeight)); |
| 176 zoom_widget_->set_focus_on_creation(false); |
| 177 zoom_widget_->Show(); |
| 178 |
| 179 aura::Window* window = zoom_widget_->GetNativeView(); |
| 180 window->SetName(kPartialMagniferWindowName); |
| 181 |
| 182 zoom_widget_->GetNativeView()->layer()->SetBounds( |
| 183 gfx::Rect(0, 0, |
| 184 kMagnifierWidth, |
| 185 kMagnifierHeight)); |
| 186 zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom( |
| 187 (kMagnifierWidth - (kMagnifierWidth / scale_)) / 2, |
| 188 (kMagnifierHeight - (kMagnifierHeight / scale_)) / 2, |
| 189 scale_, |
| 190 kZoomInset); |
| 191 |
| 192 zoom_widget_->AddObserver(this); |
| 193 } |
| 194 |
| 195 void PartialMagnificationController::CloseMagnifierWindow() { |
| 196 if (zoom_widget_) { |
| 197 RemoveZoomWidgetObservers(); |
| 198 zoom_widget_->Close(); |
| 199 zoom_widget_ = NULL; |
| 200 } |
| 201 } |
| 202 |
| 203 void PartialMagnificationController::RemoveZoomWidgetObservers() { |
| 204 DCHECK(zoom_widget_); |
| 205 zoom_widget_->RemoveObserver(this); |
| 206 aura::RootWindow* root_window = |
| 207 zoom_widget_->GetNativeView()->GetRootWindow(); |
| 208 DCHECK(root_window); |
| 209 root_window->RemoveObserver(this); |
| 210 } |
| 211 |
| 212 void PartialMagnificationController::SwitchTargetRootWindow( |
| 213 aura::RootWindow* new_root_window) { |
| 214 if (zoom_widget_ && |
| 215 new_root_window == zoom_widget_->GetNativeView()->GetRootWindow()) |
| 216 return; |
| 217 |
| 218 CloseMagnifierWindow(); |
| 219 |
| 220 // Recreate the magnifier window by updating the scale factor. |
| 221 SetScale(GetScale()); |
| 222 } |
| 223 |
| 224 aura::RootWindow* PartialMagnificationController::GetCurrentRootWindow() { |
| 225 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); |
| 226 for (Shell::RootWindowList::const_iterator iter = root_windows.begin(); |
| 227 iter != root_windows.end(); ++iter) { |
| 228 aura::RootWindow* root_window = *iter; |
| 229 if (root_window->ContainsPointInRoot( |
| 230 root_window->GetLastMouseLocationInRoot())) |
| 231 return root_window; |
| 232 } |
| 233 return NULL; |
| 234 } |
| 235 |
| 236 } // namespace ash |
OLD | NEW |