[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" |
James Cook | a35a1e2 | 2017-04-08 02:33:08 | [diff] [blame^] | 8 | #include "ash/system/palette/palette_utils.h" |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 9 | #include "third_party/skia/include/core/SkDrawLooper.h" |
[email protected] | 7a60cd3a | 2014-03-20 20:54:57 | [diff] [blame] | 10 | #include "ui/aura/window_event_dispatcher.h" |
[email protected] | 7a60cd3a | 2014-03-20 20:54:57 | [diff] [blame] | 11 | #include "ui/aura/window_tree_host.h" |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 12 | #include "ui/compositor/layer.h" |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 13 | #include "ui/compositor/paint_recorder.h" |
| 14 | #include "ui/events/event.h" |
| 15 | #include "ui/events/event_constants.h" |
estade | c6686c5 | 2017-01-05 21:31:19 | [diff] [blame] | 16 | #include "ui/gfx/shadow_value.h" |
enne | eb8c638a | 2017-01-31 23:29:56 | [diff] [blame] | 17 | #include "ui/gfx/skia_paint_util.h" |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 18 | #include "ui/views/widget/widget.h" |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 19 | #include "ui/wm/core/coordinate_conversion.h" |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 20 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 21 | namespace ash { |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 22 | namespace { |
| 23 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 24 | // Ratio of magnifier scale. |
| 25 | const float kMagnificationScale = 2.f; |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 26 | // Radius of the magnifying glass in DIP. This does not include the thickness |
| 27 | // of the magnifying glass shadow and border. |
| 28 | const int kMagnifierRadius = 188; |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 29 | // Size of the border around the magnifying glass in DIP. |
| 30 | const int kBorderSize = 10; |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 31 | // Thickness of the outline around magnifying glass border in DIP. |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 32 | const int kBorderOutlineThickness = 1; |
| 33 | // Thickness of the shadow around the magnifying glass in DIP. |
| 34 | const int kShadowThickness = 24; |
| 35 | // Offset of the shadow around the magnifying glass in DIP. One of the shadows |
| 36 | // is lowered a bit, so we have to include |kShadowOffset| in our calculations |
| 37 | // to compensate. |
| 38 | const int kShadowOffset = 24; |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 39 | // The color of the border and its outlines. The border has an outline on both |
| 40 | // sides, producing a black/white/black ring. |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 41 | const SkColor kBorderColor = SkColorSetARGB(204, 255, 255, 255); |
| 42 | const SkColor kBorderOutlineColor = SkColorSetARGB(51, 0, 0, 0); |
| 43 | // The colors of the two shadow around the magnifiying glass. |
| 44 | const SkColor kTopShadowColor = SkColorSetARGB(26, 0, 0, 0); |
| 45 | const SkColor kBottomShadowColor = SkColorSetARGB(61, 0, 0, 0); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 46 | // Inset on the zoom filter. |
| 47 | const int kZoomInset = 0; |
| 48 | // Vertical offset between the center of the magnifier and the tip of the |
| 49 | // pointer. TODO(jdufault): The vertical offset should only apply to the window |
| 50 | // location, not the magnified contents. See crbug.com/637617. |
| 51 | const int kVerticalOffset = 0; |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 52 | |
| 53 | // Name of the magnifier window. |
| 54 | const char kPartialMagniferWindowName[] = "PartialMagnifierWindow"; |
| 55 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 56 | gfx::Size GetWindowSize() { |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 57 | // The diameter of the window is the diameter of the magnifier, border and |
| 58 | // shadow combined. We apply |kShadowOffset| on all sides even though the |
| 59 | // shadow is only thicker on the bottom so as to keep the circle centered in |
| 60 | // the view and keep calculations (border rendering and content masking) |
| 61 | // simpler. |
| 62 | int window_diameter = |
| 63 | (kMagnifierRadius + kBorderSize + kShadowThickness + kShadowOffset) * 2; |
| 64 | return gfx::Size(window_diameter, window_diameter); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | gfx::Rect GetBounds(gfx::Point mouse) { |
| 68 | gfx::Size size = GetWindowSize(); |
| 69 | gfx::Point origin(mouse.x() - (size.width() / 2), |
| 70 | mouse.y() - (size.height() / 2) - kVerticalOffset); |
| 71 | return gfx::Rect(origin, size); |
| 72 | } |
| 73 | |
| 74 | aura::Window* GetCurrentRootWindow() { |
| 75 | aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows(); |
| 76 | for (aura::Window* root_window : root_windows) { |
| 77 | if (root_window->ContainsPointInRoot( |
| 78 | root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot())) |
| 79 | return root_window; |
| 80 | } |
| 81 | return nullptr; |
| 82 | } |
| 83 | |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 84 | } // namespace |
| 85 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 86 | // The content mask provides a clipping layer for the magnification window so we |
| 87 | // can show a circular magnifier. |
| 88 | class PartialMagnificationController::ContentMask : public ui::LayerDelegate { |
| 89 | public: |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 90 | // If |is_border| is true, the circle will be a stroke. This is useful if we |
| 91 | // wish to clip a border. |
| 92 | ContentMask(bool is_border, gfx::Size mask_bounds) |
| 93 | : layer_(ui::LAYER_TEXTURED), is_border_(is_border) { |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 94 | layer_.set_delegate(this); |
| 95 | layer_.SetFillsBoundsOpaquely(false); |
| 96 | layer_.SetBounds(gfx::Rect(mask_bounds)); |
| 97 | } |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 98 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 99 | ~ContentMask() override { layer_.set_delegate(nullptr); } |
| 100 | |
| 101 | ui::Layer* layer() { return &layer_; } |
| 102 | |
| 103 | private: |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 104 | // ui::LayerDelegate: |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 105 | void OnPaintLayer(const ui::PaintContext& context) override { |
| 106 | ui::PaintRecorder recorder(context, layer()->size()); |
| 107 | |
enne | 00bd4b7 | 2017-02-08 01:38:14 | [diff] [blame] | 108 | cc::PaintFlags flags; |
| 109 | flags.setAlpha(255); |
| 110 | flags.setAntiAlias(true); |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 111 | // Stroke is used for clipping the border which consists of the rendered |
| 112 | // border |kBorderSize| and the magnifier shadow |kShadowThickness| and |
| 113 | // |kShadowOffset|. |
enne | 00bd4b7 | 2017-02-08 01:38:14 | [diff] [blame] | 114 | flags.setStrokeWidth(kBorderSize + kShadowThickness + kShadowOffset); |
| 115 | flags.setStyle(is_border_ ? cc::PaintFlags::kStroke_Style |
enne | 34f6084c | 2017-02-02 22:39:08 | [diff] [blame] | 116 | : cc::PaintFlags::kFill_Style); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 117 | |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 118 | // If we want to clip the magnifier zone use the magnifiers radius. |
| 119 | // Otherwise we want to clip the border, shadow and shadow offset so we |
| 120 | // start |
| 121 | // at the halfway point of the stroke width. |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 122 | gfx::Rect rect(layer()->bounds().size()); |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 123 | int clipping_radius = kMagnifierRadius; |
| 124 | if (is_border_) |
| 125 | clipping_radius += (kShadowThickness + kShadowOffset + kBorderSize) / 2; |
enne | 00bd4b7 | 2017-02-08 01:38:14 | [diff] [blame] | 126 | recorder.canvas()->DrawCircle(rect.CenterPoint(), clipping_radius, flags); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {} |
| 130 | |
| 131 | void OnDeviceScaleFactorChanged(float device_scale_factor) override { |
| 132 | // Redrawing will take care of scale factor change. |
| 133 | } |
| 134 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 135 | ui::Layer layer_; |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 136 | bool is_border_; |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 137 | DISALLOW_COPY_AND_ASSIGN(ContentMask); |
| 138 | }; |
| 139 | |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 140 | // The border renderer draws the border as well as outline on both the outer and |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 141 | // inner radius to increase visibility. The border renderer also handles drawing |
| 142 | // the shadow. |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 143 | class PartialMagnificationController::BorderRenderer |
| 144 | : public ui::LayerDelegate { |
| 145 | public: |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 146 | explicit BorderRenderer(const gfx::Rect& window_bounds) |
| 147 | : magnifier_window_bounds_(window_bounds) { |
| 148 | magnifier_shadows_.push_back(gfx::ShadowValue( |
| 149 | gfx::Vector2d(0, kShadowOffset), kShadowThickness, kBottomShadowColor)); |
| 150 | magnifier_shadows_.push_back(gfx::ShadowValue( |
| 151 | gfx::Vector2d(0, 0), kShadowThickness, kTopShadowColor)); |
| 152 | } |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 153 | |
| 154 | ~BorderRenderer() override {} |
| 155 | |
| 156 | private: |
| 157 | // ui::LayerDelegate: |
| 158 | void OnPaintLayer(const ui::PaintContext& context) override { |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 159 | ui::PaintRecorder recorder(context, magnifier_window_bounds_.size()); |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 160 | |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 161 | // Draw the shadow. |
enne | 00bd4b7 | 2017-02-08 01:38:14 | [diff] [blame] | 162 | cc::PaintFlags shadow_flags; |
| 163 | shadow_flags.setAntiAlias(true); |
| 164 | shadow_flags.setColor(SK_ColorTRANSPARENT); |
estade | 9ef3c96 | 2017-03-09 05:16:59 | [diff] [blame] | 165 | shadow_flags.setLooper(gfx::CreateShadowDrawLooper(magnifier_shadows_)); |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 166 | gfx::Rect shadow_bounds(magnifier_window_bounds_.size()); |
| 167 | recorder.canvas()->DrawCircle( |
| 168 | shadow_bounds.CenterPoint(), |
| 169 | shadow_bounds.width() / 2 - kShadowThickness - kShadowOffset, |
enne | 00bd4b7 | 2017-02-08 01:38:14 | [diff] [blame] | 170 | shadow_flags); |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 171 | |
enne | 00bd4b7 | 2017-02-08 01:38:14 | [diff] [blame] | 172 | cc::PaintFlags border_flags; |
| 173 | border_flags.setAntiAlias(true); |
| 174 | border_flags.setStyle(cc::PaintFlags::kStroke_Style); |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 175 | |
| 176 | // The radius of the magnifier and its border. |
| 177 | const int magnifier_radius = kMagnifierRadius + kBorderSize; |
| 178 | |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 179 | // Draw the inner border. |
enne | 00bd4b7 | 2017-02-08 01:38:14 | [diff] [blame] | 180 | border_flags.setStrokeWidth(kBorderSize); |
| 181 | border_flags.setColor(kBorderColor); |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 182 | recorder.canvas()->DrawCircle(magnifier_window_bounds_.CenterPoint(), |
| 183 | magnifier_radius - kBorderSize / 2, |
enne | 00bd4b7 | 2017-02-08 01:38:14 | [diff] [blame] | 184 | border_flags); |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 185 | |
| 186 | // Draw border outer outline and then draw the border inner outline. |
enne | 00bd4b7 | 2017-02-08 01:38:14 | [diff] [blame] | 187 | border_flags.setStrokeWidth(kBorderOutlineThickness); |
| 188 | border_flags.setColor(kBorderOutlineColor); |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 189 | recorder.canvas()->DrawCircle( |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 190 | magnifier_window_bounds_.CenterPoint(), |
enne | 00bd4b7 | 2017-02-08 01:38:14 | [diff] [blame] | 191 | magnifier_radius - kBorderOutlineThickness / 2, border_flags); |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 192 | recorder.canvas()->DrawCircle( |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 193 | magnifier_window_bounds_.CenterPoint(), |
| 194 | magnifier_radius - kBorderSize + kBorderOutlineThickness / 2, |
enne | 00bd4b7 | 2017-02-08 01:38:14 | [diff] [blame] | 195 | border_flags); |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void OnDelegatedFrameDamage(const gfx::Rect& damage_rect_in_dip) override {} |
| 199 | |
| 200 | void OnDeviceScaleFactorChanged(float device_scale_factor) override {} |
| 201 | |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 202 | gfx::Rect magnifier_window_bounds_; |
| 203 | std::vector<gfx::ShadowValue> magnifier_shadows_; |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 204 | |
| 205 | DISALLOW_COPY_AND_ASSIGN(BorderRenderer); |
| 206 | }; |
| 207 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 208 | PartialMagnificationController::PartialMagnificationController() { |
sky | cb4be5b | 2017-04-06 17:52:45 | [diff] [blame] | 209 | Shell::Get()->AddPreTargetHandler(this); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | PartialMagnificationController::~PartialMagnificationController() { |
| 213 | CloseMagnifierWindow(); |
| 214 | |
sky | cb4be5b | 2017-04-06 17:52:45 | [diff] [blame] | 215 | Shell::Get()->RemovePreTargetHandler(this); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 216 | } |
| 217 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 218 | void PartialMagnificationController::SetEnabled(bool enabled) { |
| 219 | is_enabled_ = enabled; |
| 220 | SetActive(false); |
| 221 | } |
| 222 | |
| 223 | void PartialMagnificationController::SwitchTargetRootWindowIfNeeded( |
| 224 | aura::Window* new_root_window) { |
| 225 | if (host_widget_ && |
| 226 | new_root_window == host_widget_->GetNativeView()->GetRootWindow()) |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 227 | return; |
| 228 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 229 | if (!new_root_window) |
| 230 | new_root_window = GetCurrentRootWindow(); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 231 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 232 | if (is_enabled_ && is_active_) { |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 233 | CloseMagnifierWindow(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 234 | CreateMagnifierWindow(new_root_window); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 238 | void PartialMagnificationController::OnTouchEvent(ui::TouchEvent* event) { |
| 239 | OnLocatedEvent(event, event->pointer_details()); |
| 240 | } |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 241 | |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 242 | void PartialMagnificationController::OnWindowDestroying(aura::Window* window) { |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 243 | CloseMagnifierWindow(); |
| 244 | |
[email protected] | c9390bd | 2013-11-08 20:33:13 | [diff] [blame] | 245 | aura::Window* new_root_window = GetCurrentRootWindow(); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 246 | if (new_root_window != window) |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 247 | SwitchTargetRootWindowIfNeeded(new_root_window); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 248 | } |
| 249 | |
jamescook | b8dcef52 | 2016-06-25 14:42:55 | [diff] [blame] | 250 | void PartialMagnificationController::OnWidgetDestroying(views::Widget* widget) { |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 251 | DCHECK_EQ(widget, host_widget_); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 252 | RemoveZoomWidgetObservers(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 253 | host_widget_ = nullptr; |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 254 | } |
| 255 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 256 | void PartialMagnificationController::SetActive(bool active) { |
| 257 | // Fail if we're trying to activate while disabled. |
| 258 | DCHECK(is_enabled_ || !active); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 259 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 260 | is_active_ = active; |
| 261 | if (is_active_) { |
| 262 | CreateMagnifierWindow(GetCurrentRootWindow()); |
| 263 | } else { |
| 264 | CloseMagnifierWindow(); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 268 | void PartialMagnificationController::OnLocatedEvent( |
| 269 | ui::LocatedEvent* event, |
| 270 | const ui::PointerDetails& pointer_details) { |
| 271 | if (!is_enabled_) |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 272 | return; |
| 273 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 274 | if (pointer_details.pointer_type != ui::EventPointerType::POINTER_TYPE_PEN) |
| 275 | return; |
| 276 | |
jdufault | d8d4991 | 2016-09-08 00:03:03 | [diff] [blame] | 277 | // Compute the event location in screen space. |
| 278 | aura::Window* target = static_cast<aura::Window*>(event->target()); |
| 279 | aura::Window* event_root = target->GetRootWindow(); |
| 280 | gfx::Point screen_point = event->root_location(); |
| 281 | wm::ConvertPointToScreen(event_root, &screen_point); |
| 282 | |
sammiequon | 203ae02 | 2016-09-18 17:23:42 | [diff] [blame] | 283 | // If the stylus is pressed on the palette icon or widget, do not activate. |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 284 | if (event->type() == ui::ET_TOUCH_PRESSED && |
jdufault | d08c50b | 2017-02-08 02:14:22 | [diff] [blame] | 285 | !palette_utils::PaletteContainsPointInScreen(screen_point)) { |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 286 | SetActive(true); |
jdufault | d8d4991 | 2016-09-08 00:03:03 | [diff] [blame] | 287 | } |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 288 | |
jdufault | 1df36f3 | 2016-12-05 20:47:02 | [diff] [blame] | 289 | if (event->type() == ui::ET_TOUCH_RELEASED) |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 290 | SetActive(false); |
| 291 | |
| 292 | if (!is_active_) |
| 293 | return; |
| 294 | |
| 295 | // If the previous root window was detached host_widget_ will be null; |
| 296 | // reconstruct it. We also need to change the root window if the cursor has |
| 297 | // crossed display boundries. |
| 298 | SwitchTargetRootWindowIfNeeded(GetCurrentRootWindow()); |
| 299 | |
| 300 | // If that failed for any reason return. |
| 301 | if (!host_widget_) { |
| 302 | SetActive(false); |
| 303 | return; |
| 304 | } |
| 305 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 306 | // Remap point from where it was captured to the display it is actually on. |
jdufault | d8d4991 | 2016-09-08 00:03:03 | [diff] [blame] | 307 | gfx::Point point = event->root_location(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 308 | aura::Window::ConvertPointToTarget( |
| 309 | event_root, host_widget_->GetNativeView()->GetRootWindow(), &point); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 310 | host_widget_->SetBounds(GetBounds(point)); |
| 311 | |
sammiequon | 203ae02 | 2016-09-18 17:23:42 | [diff] [blame] | 312 | // If the stylus is over the palette icon or widget, do not consume the event. |
jdufault | d08c50b | 2017-02-08 02:14:22 | [diff] [blame] | 313 | if (!palette_utils::PaletteContainsPointInScreen(screen_point)) |
jdufault | d8d4991 | 2016-09-08 00:03:03 | [diff] [blame] | 314 | event->StopPropagation(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void PartialMagnificationController::CreateMagnifierWindow( |
| 318 | aura::Window* root_window) { |
| 319 | if (host_widget_ || !root_window) |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 320 | return; |
| 321 | |
| 322 | root_window->AddObserver(this); |
| 323 | |
[email protected] | 2374d181 | 2014-03-04 03:42:27 | [diff] [blame] | 324 | gfx::Point mouse( |
| 325 | root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot()); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 326 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 327 | host_widget_ = new views::Widget; |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 328 | views::Widget::InitParams params( |
| 329 | views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
[email protected] | 4f25423 | 2014-05-19 22:59:07 | [diff] [blame] | 330 | params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 331 | params.accept_events = false; |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 332 | params.bounds = GetBounds(mouse); |
[email protected] | 28fc473 | 2013-06-27 12:15:09 | [diff] [blame] | 333 | params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 334 | params.parent = root_window; |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 335 | host_widget_->Init(params); |
| 336 | host_widget_->set_focus_on_creation(false); |
| 337 | host_widget_->Show(); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 338 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 339 | aura::Window* window = host_widget_->GetNativeView(); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 340 | window->SetName(kPartialMagniferWindowName); |
| 341 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 342 | ui::Layer* root_layer = host_widget_->GetNativeView()->layer(); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 343 | |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 344 | zoom_layer_.reset(new ui::Layer(ui::LayerType::LAYER_SOLID_COLOR)); |
| 345 | zoom_layer_->SetBounds(gfx::Rect(GetWindowSize())); |
| 346 | zoom_layer_->SetBackgroundZoom(kMagnificationScale, kZoomInset); |
| 347 | root_layer->Add(zoom_layer_.get()); |
| 348 | |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 349 | border_layer_.reset(new ui::Layer(ui::LayerType::LAYER_TEXTURED)); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 350 | border_layer_->SetBounds(gfx::Rect(GetWindowSize())); |
sammiequon | 9041921 | 2016-09-16 17:25:16 | [diff] [blame] | 351 | border_renderer_.reset(new BorderRenderer(gfx::Rect(GetWindowSize()))); |
| 352 | border_layer_->set_delegate(border_renderer_.get()); |
sammiequon | 8de2aed7 | 2016-10-05 21:57:07 | [diff] [blame] | 353 | border_layer_->SetFillsBoundsOpaquely(false); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 354 | root_layer->Add(border_layer_.get()); |
| 355 | |
| 356 | border_mask_.reset(new ContentMask(true, GetWindowSize())); |
| 357 | border_layer_->SetMaskLayer(border_mask_->layer()); |
| 358 | |
| 359 | zoom_mask_.reset(new ContentMask(false, GetWindowSize())); |
| 360 | zoom_layer_->SetMaskLayer(zoom_mask_->layer()); |
| 361 | |
| 362 | host_widget_->AddObserver(this); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void PartialMagnificationController::CloseMagnifierWindow() { |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 366 | if (host_widget_) { |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 367 | RemoveZoomWidgetObservers(); |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 368 | host_widget_->Close(); |
| 369 | host_widget_ = nullptr; |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | void PartialMagnificationController::RemoveZoomWidgetObservers() { |
jdufault | 62b8b6e | 2016-08-24 20:34:53 | [diff] [blame] | 374 | DCHECK(host_widget_); |
| 375 | host_widget_->RemoveObserver(this); |
| 376 | aura::Window* root_window = host_widget_->GetNativeView()->GetRootWindow(); |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 377 | DCHECK(root_window); |
| 378 | root_window->RemoveObserver(this); |
| 379 | } |
| 380 | |
[email protected] | 77f7c13 | 2012-11-15 06:52:54 | [diff] [blame] | 381 | } // namespace ash |