summaryrefslogtreecommitdiff
path: root/chromium/ui/views/animation/installable_ink_drop.h
blob: 1b91ea19e0fbee5e510b7a7de58280a55e2fe96a (plain)
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
145
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_VIEWS_ANIMATION_INSTALLABLE_INK_DROP_H_
#define UI_VIEWS_ANIMATION_INSTALLABLE_INK_DROP_H_

#include <memory>

#include "base/feature_list.h"
#include "base/memory/scoped_refptr.h"
#include "ui/compositor/layer_delegate.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/animation/ink_drop_event_handler.h"
#include "ui/views/animation/ink_drop_state.h"
#include "ui/views/animation/installable_ink_drop_animator.h"
#include "ui/views/animation/installable_ink_drop_config.h"
#include "ui/views/animation/installable_ink_drop_painter.h"
#include "ui/views/view_observer.h"
#include "ui/views/views_export.h"

namespace gfx {
class AnimationContainer;
class Size;
}  // namespace gfx

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

namespace views {

class InkDropHostView;
class View;

extern const VIEWS_EXPORT base::Feature kInstallableInkDropFeature;

// Stub for future InkDrop implementation that will be installable on any View
// without needing InkDropHostView. This is currently non-functional and fails
// on some method calls. TODO(crbug.com/931964): implement the necessary parts
// of the API and remove the rest from the InkDrop interface.
class VIEWS_EXPORT InstallableInkDrop : public InkDrop,
                                        public InkDropEventHandler::Delegate,
                                        public ui::LayerDelegate,
                                        public ViewObserver {
 public:
  // Create ink drop for |view|. Note that |view| must live longer than us.
  explicit InstallableInkDrop(View* view);

  // Overload for working within the InkDropHostView hierarchy. Similar to
  // above, |ink_drop_host_view| must outlive us.
  //
  // TODO(crbug.com/931964): Remove this.
  explicit InstallableInkDrop(InkDropHostView* ink_drop_host_view);

  InstallableInkDrop(const InstallableInkDrop&) = delete;
  InstallableInkDrop(InstallableInkDrop&&) = delete;

  ~InstallableInkDrop() override;

  void SetConfig(InstallableInkDropConfig config);
  InstallableInkDropConfig config() const { return config_; }

  // Registers |callback| to be called whenever the highlighted state changes.
  base::CallbackListSubscription RegisterHighlightedChangedCallback(
      base::RepeatingClosure callback);

  // Should only be used for inspecting properties of the layer in tests.
  const ui::Layer* layer_for_testing() const { return layer_.get(); }

  // InkDrop:
  void HostSizeChanged(const gfx::Size& new_size) override;
  void HostTransformChanged(const gfx::Transform& new_transform) override;
  InkDropState GetTargetInkDropState() const override;
  void AnimateToState(InkDropState ink_drop_state) override;
  void SetHoverHighlightFadeDuration(base::TimeDelta duration) override;
  void UseDefaultHoverHighlightFadeDuration() override;
  void SnapToActivated() override;
  void SnapToHidden() override;
  void SetHovered(bool is_hovered) override;
  void SetFocused(bool is_focused) override;
  bool IsHighlightFadingInOrVisible() const override;
  void SetShowHighlightOnHover(bool show_highlight_on_hover) override;
  void SetShowHighlightOnFocus(bool show_highlight_on_focus) override;

  // InkDropEventHandler::Delegate:
  InkDrop* GetInkDrop() override;
  bool HasInkDrop() const override;
  bool SupportsGestureEvents() const override;

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

  // ui::LayerDelegate:
  void OnPaintLayer(const ui::PaintContext& context) override;
  void OnDeviceScaleFactorChanged(float old_device_scale_factor,
                                  float new_device_scale_factor) override;

 private:
  void SchedulePaint();
  void UpdateAnimatorHighlight();

  // The view this ink drop is showing for. |layer_| is added to the layer
  // hierarchy that |view_| belongs to. We track events on |view_| to update our
  // visual state.
  View* const view_;

  // If we were installed on an InkDropHostView, this will be non-null. We store
  // this to to remove our InkDropEventHandler override.
  InkDropHostView* ink_drop_host_view_ = nullptr;

  // Contains the colors and opacities used to paint.
  InstallableInkDropConfig config_;

  // The layer we paint to.
  std::unique_ptr<ui::Layer> layer_;

  // Observes |view_| and updates our visual state accordingly.
  InkDropEventHandler event_handler_;

  // Completely describes the current visual state of the ink drop, including
  // progress of animations.
  InstallableInkDropPainter::State visual_state_;

  // Handles painting |visual_state_| on request.
  InstallableInkDropPainter painter_;

  // Used to synchronize the hover and activation animations within this ink
  // drop. Since we use |views::CompositorAnimationRunner|, this also
  // synchronizes them with compositor frames.
  scoped_refptr<gfx::AnimationContainer> animation_container_;

  // Manages our animations and maniuplates |visual_state_| for us.
  InstallableInkDropAnimator animator_;

  base::RepeatingClosureList highlighted_changed_list_;

  bool is_hovered_ = false;
  bool is_focused_ = false;
};

}  // namespace views

#endif  // UI_VIEWS_ANIMATION_INSTALLABLE_INK_DROP_H_