File: frame_caption_button.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (163 lines) | stat: -rw-r--r-- 5,586 bytes parent folder | download | duplicates (5)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_VIEWS_WINDOW_FRAME_CAPTION_BUTTON_H_
#define UI_VIEWS_WINDOW_FRAME_CAPTION_BUTTON_H_

#include <memory>
#include <variant>

#include "base/callback_list.h"
#include "base/memory/raw_ptr.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_types.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/views_export.h"
#include "ui/views/window/caption_button_layout_constants.h"
#include "ui/views/window/caption_button_types.h"

namespace cc {
class PaintFlags;
}  // namespace cc

namespace gfx {
class SlideAnimation;
struct VectorIcon;
}  // namespace gfx

namespace views {

// Base class for the window caption buttons (minimize, maximize, restore,
// close).
class VIEWS_EXPORT FrameCaptionButton : public Button {
  METADATA_HEADER(FrameCaptionButton, Button)

 public:
  enum class Animate { kYes, kNo };

  FrameCaptionButton(PressedCallback callback,
                     CaptionButtonIcon icon,
                     int hit_test_type);
  FrameCaptionButton(const FrameCaptionButton&) = delete;
  FrameCaptionButton& operator=(const FrameCaptionButton&) = delete;
  ~FrameCaptionButton() override;

  // Gets the color to use for a frame caption button.
  static SkColor GetButtonColor(SkColor background_color);

  // Gets the alpha ratio for the colors of inactive frame caption buttons.
  static float GetInactiveButtonColorAlphaRatio();

  // Sets the image to use to paint the button. If |animate| is Animate::kYes,
  // the button crossfades to the new visuals. If the image matches the one
  // currently used by the button and |animate| is Animate::kNo, the crossfade
  // animation is progressed to the end.
  void SetImage(CaptionButtonIcon icon,
                Animate animate,
                const gfx::VectorIcon& icon_image);

  // Returns true if the button is crossfading to new visuals set in
  // SetImage().
  bool IsAnimatingImageSwap() const;

  // Sets the alpha to use for painting. Used to animate visibility changes.
  void SetAlpha(SkAlpha alpha);

  // views::Button:
  void OnGestureEvent(ui::GestureEvent* event) override;
  views::PaintInfo::ScaleType GetPaintScaleType() const override;

  // TODO(b/292154873): Replace them to set and get the foreground color.
  void SetBackgroundColor(SkColor background_color);
  SkColor GetBackgroundColor() const;

  void SetIconColorId(ui::ColorId icon_color_id);

  void SetPaintAsActive(bool paint_as_active);
  bool GetPaintAsActive() const;

  void SetInkDropCornerRadius(int ink_drop_corner_radius);
  int GetInkDropCornerRadius() const;

  base::CallbackListSubscription AddBackgroundColorChangedCallback(
      PropertyChangedCallback callback);

  CaptionButtonIcon GetIcon() const { return icon_; }

  const gfx::ImageSkia& icon_image() const { return icon_image_; }

  const gfx::VectorIcon* icon_definition_for_test() const {
    return icon_definition_;
  }

 protected:
  // views::Button override:
  void PaintButtonContents(gfx::Canvas* canvas) override;
  void OnThemeChanged() override;

  virtual void DrawHighlight(gfx::Canvas* canvas, cc::PaintFlags flags);
  virtual void DrawIconContents(gfx::Canvas* canvas,
                                gfx::ImageSkia image,
                                int x,
                                int y,
                                cc::PaintFlags flags);
  // Returns the size of the inkdrop ripple.
  virtual gfx::Size GetInkDropSize() const;

  // Returns the amount by which the inkdrop ripple and mask should be insetted
  // from the button size in order to draw the inkdrop with a size returned by
  // GetInkDropSize().
  gfx::Insets GetInkdropInsets(const gfx::Size& button_size) const;

  // Called when the `background_color_` or `icon_color_id_` is updated to
  // reflect the color change on icon and inkdrop.
  void MaybeRefreshIconAndInkdropBaseColor();

 private:
  class HighlightPathGenerator;

  // Determines what alpha to use for the icon based on animation and
  // active state.
  SkAlpha GetAlphaForIcon(SkAlpha base_alpha) const;

  void UpdateInkDropBaseColor();

  // The button's current icon.
  CaptionButtonIcon icon_;

  // The color used to compute the icon's color. If it's SkColor type, it's the
  // background color of the container view, call `GetButtonColor` to get
  // contrast color. If it's ColorId type, directly resolve the color from color
  // id.
  // TODO(b/292154873): Store the foreground color instead of the background
  // color for the SkColor type.
  std::variant<ui::ColorId, SkColor> color_ = gfx::kPlaceholderColor;

  // Whether the button should be painted as active.
  bool paint_as_active_ = false;

  // Current alpha to use for painting.
  SkAlpha alpha_ = SK_AlphaOPAQUE;

  // Radius of the ink drop highlight and mask.
  int ink_drop_corner_radius_ = kCaptionButtonInkDropDefaultCornerRadius;

  // The image id (kept for the purposes of testing) and image used to paint the
  // button's icon.
  raw_ptr<const gfx::VectorIcon> icon_definition_ = nullptr;
  gfx::ImageSkia icon_image_;

  // The icon image to crossfade from.
  gfx::ImageSkia crossfade_icon_image_;

  // Crossfade animation started when the button's images are changed by
  // SetImage().
  std::unique_ptr<gfx::SlideAnimation> swap_images_animation_;
};

}  // namespace views

#endif  // UI_VIEWS_WINDOW_FRAME_CAPTION_BUTTON_H_