[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 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" |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 9 | #include "ui/aura/window.h" |
[email protected] | 7a60cd3a | 2014-03-20 20:54:57 | [diff] [blame] | 10 | #include "ui/aura/window_event_dispatcher.h" |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 11 | #include "ui/aura/window_property.h" |
[email protected] | 7a60cd3a | 2014-03-20 20:54:57 | [diff] [blame] | 12 | #include "ui/aura/window_tree_host.h" |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 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" |
[email protected] | ee3ed1077 | 2014-03-11 22:02:01 | [diff] [blame] | 18 | #include "ui/wm/core/compound_event_filter.h" |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | |
| 22 | const float kMinPartialMagnifiedScaleThreshold = 1.1f; |
| 23 | |
| 24 | // Number of pixels to make the border of the magnified area. |
| 25 | const int kZoomInset = 16; |
| 26 | |
| 27 | // Width of the magnified area. |
| 28 | const int kMagnifierWidth = 200; |
| 29 | |
| 30 | // Height of the magnified area. |
| 31 | const int kMagnifierHeight = 200; |
| 32 | |
| 33 | // Name of the magnifier window. |
| 34 | const char kPartialMagniferWindowName[] = "PartialMagnifierWindow"; |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | namespace ash { |
| 39 | |
| 40 | PartialMagnificationController::PartialMagnificationController() |
tfarina | d7f1236f | 2015-01-16 16:21:20 | [diff] [blame] | 41 | : is_enabled_(false), |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 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 | |
[email protected] | d44efe0 | 2012-12-18 06:08:18 | [diff] [blame] | 79 | void PartialMagnificationController::OnMouseEvent(ui::MouseEvent* event) { |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 80 | if (IsPartialMagnified() && event->type() == ui::ET_MOUSE_MOVED) { |
| 81 | aura::Window* target = static_cast<aura::Window*>(event->target()); |
[email protected] | bf9cdb36 | 2013-10-25 19:22:45 | [diff] [blame] | 82 | aura::Window* current_root = target->GetRootWindow(); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 83 | // TODO(zork): Handle the case where the event is captured on a different |
| 84 | // display, such as when a menu is opened. |
| 85 | gfx::Rect root_bounds = current_root->bounds(); |
| 86 | |
| 87 | if (root_bounds.Contains(event->root_location())) { |
| 88 | SwitchTargetRootWindow(current_root); |
| 89 | |
| 90 | OnMouseMove(event->root_location()); |
| 91 | } |
| 92 | } |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 95 | //////////////////////////////////////////////////////////////////////////////// |
| 96 | // PartialMagnificationController: aura::WindowObserver implementation |
| 97 | |
| 98 | void PartialMagnificationController::OnWindowDestroying( |
| 99 | aura::Window* window) { |
| 100 | CloseMagnifierWindow(); |
| 101 | |
[email protected] | c9390bd | 2013-11-08 20:33:13 | [diff] [blame] | 102 | aura::Window* new_root_window = GetCurrentRootWindow(); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 103 | if (new_root_window != window) |
| 104 | SwitchTargetRootWindow(new_root_window); |
| 105 | } |
| 106 | |
[email protected] | 84685520 | 2013-02-05 22:15:35 | [diff] [blame] | 107 | void PartialMagnificationController::OnWidgetDestroying( |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 108 | views::Widget* widget) { |
| 109 | DCHECK_EQ(widget, zoom_widget_); |
| 110 | RemoveZoomWidgetObservers(); |
| 111 | zoom_widget_ = NULL; |
| 112 | } |
| 113 | |
| 114 | void PartialMagnificationController::OnMouseMove( |
| 115 | const gfx::Point& location_in_root) { |
| 116 | gfx::Point origin(location_in_root); |
| 117 | |
| 118 | origin.Offset(-kMagnifierWidth / 2, -kMagnifierHeight / 2); |
| 119 | |
| 120 | if (zoom_widget_) { |
| 121 | zoom_widget_->SetBounds(gfx::Rect(origin.x(), origin.y(), |
| 122 | kMagnifierWidth, kMagnifierHeight)); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | bool PartialMagnificationController::IsPartialMagnified() const { |
| 127 | return scale_ >= kMinPartialMagnifiedScaleThreshold; |
| 128 | } |
| 129 | |
| 130 | void PartialMagnificationController::CreateMagnifierWindow() { |
| 131 | if (zoom_widget_) |
| 132 | return; |
| 133 | |
[email protected] | c9390bd | 2013-11-08 20:33:13 | [diff] [blame] | 134 | aura::Window* root_window = GetCurrentRootWindow(); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 135 | if (!root_window) |
| 136 | return; |
| 137 | |
| 138 | root_window->AddObserver(this); |
| 139 | |
[email protected] | 2374d181 | 2014-03-04 03:42:27 | [diff] [blame] | 140 | gfx::Point mouse( |
| 141 | root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot()); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 142 | |
| 143 | zoom_widget_ = new views::Widget; |
| 144 | views::Widget::InitParams params( |
| 145 | views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
[email protected] | 4f25423 | 2014-05-19 22:59:07 | [diff] [blame] | 146 | params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 147 | params.accept_events = false; |
[email protected] | 28fc473 | 2013-06-27 12:15:09 | [diff] [blame] | 148 | params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 149 | params.parent = root_window; |
| 150 | zoom_widget_->Init(params); |
| 151 | zoom_widget_->SetBounds(gfx::Rect(mouse.x() - kMagnifierWidth / 2, |
| 152 | mouse.y() - kMagnifierHeight / 2, |
| 153 | kMagnifierWidth, kMagnifierHeight)); |
| 154 | zoom_widget_->set_focus_on_creation(false); |
| 155 | zoom_widget_->Show(); |
| 156 | |
| 157 | aura::Window* window = zoom_widget_->GetNativeView(); |
| 158 | window->SetName(kPartialMagniferWindowName); |
| 159 | |
| 160 | zoom_widget_->GetNativeView()->layer()->SetBounds( |
| 161 | gfx::Rect(0, 0, |
| 162 | kMagnifierWidth, |
| 163 | kMagnifierHeight)); |
| 164 | zoom_widget_->GetNativeView()->layer()->SetBackgroundZoom( |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 165 | scale_, |
| 166 | kZoomInset); |
| 167 | |
| 168 | zoom_widget_->AddObserver(this); |
| 169 | } |
| 170 | |
| 171 | void PartialMagnificationController::CloseMagnifierWindow() { |
| 172 | if (zoom_widget_) { |
| 173 | RemoveZoomWidgetObservers(); |
| 174 | zoom_widget_->Close(); |
| 175 | zoom_widget_ = NULL; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void PartialMagnificationController::RemoveZoomWidgetObservers() { |
| 180 | DCHECK(zoom_widget_); |
| 181 | zoom_widget_->RemoveObserver(this); |
[email protected] | bf9cdb36 | 2013-10-25 19:22:45 | [diff] [blame] | 182 | aura::Window* root_window = |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 183 | zoom_widget_->GetNativeView()->GetRootWindow(); |
| 184 | DCHECK(root_window); |
| 185 | root_window->RemoveObserver(this); |
| 186 | } |
| 187 | |
| 188 | void PartialMagnificationController::SwitchTargetRootWindow( |
[email protected] | bf9cdb36 | 2013-10-25 19:22:45 | [diff] [blame] | 189 | aura::Window* new_root_window) { |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 190 | if (zoom_widget_ && |
| 191 | new_root_window == zoom_widget_->GetNativeView()->GetRootWindow()) |
| 192 | return; |
| 193 | |
| 194 | CloseMagnifierWindow(); |
| 195 | |
| 196 | // Recreate the magnifier window by updating the scale factor. |
| 197 | SetScale(GetScale()); |
| 198 | } |
| 199 | |
[email protected] | c9390bd | 2013-11-08 20:33:13 | [diff] [blame] | 200 | aura::Window* PartialMagnificationController::GetCurrentRootWindow() { |
| 201 | aura::Window::Windows root_windows = Shell::GetAllRootWindows(); |
| 202 | for (aura::Window::Windows::const_iterator iter = root_windows.begin(); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 203 | iter != root_windows.end(); ++iter) { |
[email protected] | c9390bd | 2013-11-08 20:33:13 | [diff] [blame] | 204 | aura::Window* root_window = *iter; |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 205 | if (root_window->ContainsPointInRoot( |
[email protected] | 2374d181 | 2014-03-04 03:42:27 | [diff] [blame] | 206 | root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot())) |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 207 | return root_window; |
| 208 | } |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | } // namespace ash |