Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/wm/resize_shadow.h" | 5 #include "ash/wm/resize_shadow.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "ui/aura/window.h" | 9 #include "ui/aura/window.h" |
| 10 #include "ui/base/hit_test.h" | 10 #include "ui/base/hit_test.h" |
| 11 #include "ui/compositor/layer.h" | 11 #include "ui/compositor/layer.h" |
| 12 #include "ui/compositor/scoped_layer_animation_settings.h" | 12 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 13 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/image/canvas_image_source.h" | 14 #include "ui/gfx/image/canvas_image_source.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // The width of the resize shadow that appears on the hovered edge of the | 18 // The width of the resize shadow that appears on the hovered edge of the |
| 19 // window. | 19 // window. |
| 20 constexpr int kVisualThickness = 8; | 20 constexpr int kVisualThickness = 8; |
| 21 | 21 |
| 22 // The corner radius of the resize shadow, which not coincidentally matches | |
| 23 // the corner radius of the actual window. | |
|
varkha
2017/04/18 22:31:58
Should one just be equal to the other, even if you
Evan Stade
2017/04/19 13:23:10
Arguably. I actually think it's easier to read lik
| |
| 24 static constexpr int kCornerRadiusOfResizeShadow = 2; | |
| 25 static constexpr int kCornerRadiusOfWindow = 2; | |
| 26 | |
| 22 // This class simply draws a roundrect. The layout and tiling is handled by | 27 // This class simply draws a roundrect. The layout and tiling is handled by |
| 23 // ResizeShadow and NinePatchLayer. | 28 // ResizeShadow and NinePatchLayer. |
| 24 class ResizeShadowImageSource : public gfx::CanvasImageSource { | 29 class ResizeShadowImageSource : public gfx::CanvasImageSource { |
| 25 public: | 30 public: |
| 26 ResizeShadowImageSource() | 31 ResizeShadowImageSource() |
| 27 : gfx::CanvasImageSource(gfx::Size(kImageSide, kImageSide), | 32 : gfx::CanvasImageSource(gfx::Size(kImageSide, kImageSide), |
| 28 false /* is opaque */) {} | 33 false /* is opaque */) {} |
| 29 | 34 |
| 30 ~ResizeShadowImageSource() override {} | 35 ~ResizeShadowImageSource() override {} |
| 31 | 36 |
| 32 // gfx::CanvasImageSource: | 37 // gfx::CanvasImageSource: |
| 33 void Draw(gfx::Canvas* canvas) override { | 38 void Draw(gfx::Canvas* canvas) override { |
| 34 cc::PaintFlags paint; | 39 cc::PaintFlags paint; |
| 35 paint.setAntiAlias(true); | 40 paint.setAntiAlias(true); |
| 36 paint.setColor(SK_ColorBLACK); | 41 paint.setColor(SK_ColorBLACK); |
| 37 canvas->DrawRoundRect(gfx::RectF(gfx::SizeF(size())), kCornerRadius, paint); | 42 canvas->DrawRoundRect(gfx::RectF(gfx::SizeF(size())), |
| 43 kCornerRadiusOfResizeShadow, paint); | |
| 38 } | 44 } |
| 39 | 45 |
| 40 private: | 46 private: |
| 41 static constexpr int kCornerRadius = 2; | 47 // The image has to have enough space to depict the visual thickness (left and |
| 42 static constexpr int kImageSide = 2 * kVisualThickness + 1; | 48 // right) plus an inset for extending beneath the window's rounded corner plus |
| 49 // one pixel for the center of the nine patch. | |
| 50 static constexpr int kImageSide = | |
| 51 2 * (kVisualThickness + kCornerRadiusOfWindow) + 1; | |
| 43 | 52 |
| 44 DISALLOW_COPY_AND_ASSIGN(ResizeShadowImageSource); | 53 DISALLOW_COPY_AND_ASSIGN(ResizeShadowImageSource); |
| 45 }; | 54 }; |
| 46 | 55 |
| 47 base::LazyInstance<std::unique_ptr<gfx::ImageSkia>>::Leaky g_shadow_image = | 56 base::LazyInstance<std::unique_ptr<gfx::ImageSkia>>::Leaky g_shadow_image = |
| 48 LAZY_INSTANCE_INITIALIZER; | 57 LAZY_INSTANCE_INITIALIZER; |
| 49 | 58 |
| 50 } // namespace | 59 } // namespace |
| 51 | 60 |
| 52 namespace ash { | 61 namespace ash { |
| 53 | 62 |
| 54 ResizeShadow::ResizeShadow(aura::Window* window) | 63 ResizeShadow::ResizeShadow(aura::Window* window) |
| 55 : window_(window), last_hit_test_(HTNOWHERE) { | 64 : window_(window), last_hit_test_(HTNOWHERE) { |
| 56 window_->AddObserver(this); | 65 window_->AddObserver(this); |
| 57 | 66 |
| 58 // Use a NinePatchLayer to tile the shadow image (which is simply a | 67 // Use a NinePatchLayer to tile the shadow image (which is simply a |
| 59 // roundrect). | 68 // roundrect). |
| 60 layer_.reset(new ui::Layer(ui::LAYER_NINE_PATCH)); | 69 layer_.reset(new ui::Layer(ui::LAYER_NINE_PATCH)); |
| 61 layer_->set_name("WindowResizeShadow"); | 70 layer_->set_name("WindowResizeShadow"); |
| 62 layer_->SetFillsBoundsOpaquely(false); | 71 layer_->SetFillsBoundsOpaquely(false); |
| 63 layer_->SetOpacity(0.f); | 72 layer_->SetOpacity(0.f); |
| 64 layer_->SetVisible(false); | 73 layer_->SetVisible(false); |
| 65 | 74 |
| 66 if (!g_shadow_image.Get()) { | 75 if (!g_shadow_image.Get()) { |
| 67 auto* source = new ResizeShadowImageSource(); | 76 auto* source = new ResizeShadowImageSource(); |
| 68 g_shadow_image.Get().reset(new gfx::ImageSkia(source, source->size())); | 77 g_shadow_image.Get().reset(new gfx::ImageSkia(source, source->size())); |
| 69 } | 78 } |
| 70 layer_->UpdateNinePatchLayerImage(*g_shadow_image.Get()); | 79 layer_->UpdateNinePatchLayerImage(*g_shadow_image.Get()); |
| 71 gfx::Rect aperture(g_shadow_image.Get()->size()); | 80 gfx::Rect aperture(g_shadow_image.Get()->size()); |
| 72 constexpr gfx::Insets kApertureInsets(kVisualThickness); | 81 constexpr gfx::Insets kApertureInsets(kVisualThickness + |
| 82 kCornerRadiusOfWindow); | |
| 73 aperture.Inset(kApertureInsets); | 83 aperture.Inset(kApertureInsets); |
| 74 layer_->UpdateNinePatchLayerAperture(aperture); | 84 layer_->UpdateNinePatchLayerAperture(aperture); |
| 75 layer_->UpdateNinePatchLayerBorder( | 85 layer_->UpdateNinePatchLayerBorder( |
| 76 gfx::Rect(kApertureInsets.left(), kApertureInsets.top(), | 86 gfx::Rect(kApertureInsets.left(), kApertureInsets.top(), |
| 77 kApertureInsets.width(), kApertureInsets.height())); | 87 kApertureInsets.width(), kApertureInsets.height())); |
| 78 | 88 |
| 79 ReparentLayer(); | 89 ReparentLayer(); |
| 80 } | 90 } |
| 81 | 91 |
| 82 ResizeShadow::~ResizeShadow() { | 92 ResizeShadow::~ResizeShadow() { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 constexpr int kShadowFadeOutDurationMs = 100; | 154 constexpr int kShadowFadeOutDurationMs = 100; |
| 145 settings.SetTransitionDuration( | 155 settings.SetTransitionDuration( |
| 146 base::TimeDelta::FromMilliseconds(kShadowFadeOutDurationMs)); | 156 base::TimeDelta::FromMilliseconds(kShadowFadeOutDurationMs)); |
| 147 } | 157 } |
| 148 constexpr float kShadowTargetOpacity = 0.5f; | 158 constexpr float kShadowTargetOpacity = 0.5f; |
| 149 layer_->SetOpacity(visible ? kShadowTargetOpacity : 0.f); | 159 layer_->SetOpacity(visible ? kShadowTargetOpacity : 0.f); |
| 150 layer_->SetVisible(visible); | 160 layer_->SetVisible(visible); |
| 151 } | 161 } |
| 152 | 162 |
| 153 } // namespace ash | 163 } // namespace ash |
| OLD | NEW |