1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144

ash / style / system_shadow_on_nine_patch_layer.h [blame]

// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_STYLE_SYSTEM_SHADOW_ON_NINE_PATCH_LAYER_H_
#define ASH_STYLE_SYSTEM_SHADOW_ON_NINE_PATCH_LAYER_H_

#include "ash/style/system_shadow.h"
#include "base/scoped_observation.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#include "ui/compositor_extra/shadow.h"
#include "ui/views/view_observer.h"
#include "ui/views/view_shadow.h"

namespace ui {
class ColorProvider;
class Layer;
}  // namespace ui

namespace ash {

// SystemShadowOnNinePatchLayer is an interface for the shadows based on
// ui::Shadow which paints shadow on a nine patch layer. The shadow attributes
// are set and get from ui::Shadow's functions. The child classes need to expose
// their ui::Shadow pointer in `shadow()`.
class SystemShadowOnNinePatchLayer : public SystemShadow {
 public:
  ~SystemShadowOnNinePatchLayer() override;

  // SystemShadow:
  void SetType(SystemShadow::Type type) override;
  void SetContentBounds(const gfx::Rect& bounds) override;
  void SetRoundedCornerRadius(int corner_radius) override;
  void SetRoundedCorners(const gfx::RoundedCornersF& rounded_corners) override;
  const gfx::Rect& GetContentBounds() override;
  ui::Layer* GetLayer() override;
  ui::Layer* GetNinePatchLayer() override;
  const gfx::ShadowValues GetShadowValuesForTesting() const override;

 protected:
  virtual ui::Shadow* shadow() = 0;
  virtual const ui::Shadow* shadow() const = 0;

 private:
  // SystemShadow:
  void UpdateShadowColors(const ui::ColorProvider* color_provider) override;
};

// An implementation of `SystemShadowOnNinePatchLayer`. It is directly based on
// the ui::Shadow.
class SystemShadowOnNinePatchLayerImpl : public SystemShadowOnNinePatchLayer,
                                         public ui::LayerOwner::Observer {
 public:
  SystemShadowOnNinePatchLayerImpl(
      SystemShadow::Type type,
      const LayerRecreatedCallback& layer_recreated_callback);
  SystemShadowOnNinePatchLayerImpl(const SystemShadowOnNinePatchLayerImpl&) =
      delete;
  SystemShadowOnNinePatchLayerImpl& operator=(
      const SystemShadowOnNinePatchLayerImpl&) = delete;
  ~SystemShadowOnNinePatchLayerImpl() override;

  // ui::LayerOwner::Observer:
  void OnLayerRecreated(ui::Layer* old_layer) override;

 private:
  // SystemShadowOnNinePatchLayer:
  ui::Shadow* shadow() override;
  const ui::Shadow* shadow() const override;

  LayerRecreatedCallback layer_recreated_callback_;
  ui::Shadow shadow_;

  base::ScopedObservation<ui::LayerOwner, SystemShadowOnNinePatchLayerImpl>
      shadow_observation_{this};
};

// An implementation of `SystemShadowOnNinePatchLayer`. It is based on
// ViewShadow. The ViewShadow is added in the layers beneath the view and
// adjusts its content bounds with the view's bounds. Do not manually set the
// content bounds.
class SystemViewShadowOnNinePatchLayer : public SystemShadowOnNinePatchLayer,
                                         public views::ViewObserver {
 public:
  SystemViewShadowOnNinePatchLayer(views::View* view, SystemShadow::Type type);
  SystemViewShadowOnNinePatchLayer(const SystemViewShadowOnNinePatchLayer&) =
      delete;
  SystemViewShadowOnNinePatchLayer& operator=(
      const SystemViewShadowOnNinePatchLayer&) = delete;
  ~SystemViewShadowOnNinePatchLayer() override;

  // SystemShadow:
  void SetRoundedCornerRadius(int corner_radius) override;

  // views::ViewObserver:
  void OnViewAddedToWidget(views::View* observed_view) override;
  void OnViewIsDeleting(views::View* observed_view) override;

 private:
  // SystemShadowOnNinePatchLayer:
  void SetContentBounds(const gfx::Rect& content_bounds) override;
  ui::Shadow* shadow() override;
  const ui::Shadow* shadow() const override;

  views::ViewShadow view_shadow_;
  base::ScopedObservation<views::View, views::ViewObserver> view_observation_{
      this};
};

// An extension of SystemShadowOnNinePatchLayerImpl. The shadow is added at the
// bottom of a window's layer and adjusts its content bounds with the window's
// bounds. Do not manually set the content bounds.
class SystemWindowShadowOnNinePatchLayer
    : public SystemShadowOnNinePatchLayerImpl,
      public aura::WindowObserver {
 public:
  SystemWindowShadowOnNinePatchLayer(aura::Window* window,
                                     SystemShadow::Type type);
  SystemWindowShadowOnNinePatchLayer(
      const SystemWindowShadowOnNinePatchLayer&) = delete;
  SystemWindowShadowOnNinePatchLayer& operator=(
      const SystemWindowShadowOnNinePatchLayer&) = delete;
  ~SystemWindowShadowOnNinePatchLayer() override;

  // aura::WindowObserver:
  void OnWindowBoundsChanged(aura::Window* window,
                             const gfx::Rect& old_bounds,
                             const gfx::Rect& new_bounds,
                             ui::PropertyChangeReason reason) override;
  void OnWindowDestroyed(aura::Window* window) override;
  void OnWindowAddedToRootWindow(aura::Window* window) override;

 private:
  // SystemShadowOnNinePatchLayerImpl:
  void SetContentBounds(const gfx::Rect& content_bounds) override;

  base::ScopedObservation<aura::Window, aura::WindowObserver>
      window_observation_{this};
};

}  // namespace ash

#endif  // ASH_STYLE_SYSTEM_SHADOW_ON_NINE_PATCH_LAYER_H_