File: checkbox.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 (120 lines) | stat: -rw-r--r-- 3,679 bytes parent folder | download | duplicates (6)
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
// Copyright 2012 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_CONTROLS_BUTTON_CHECKBOX_H_
#define UI_VIEWS_CONTROLS_BUTTON_CHECKBOX_H_

#include <memory>
#include <optional>
#include <string>

#include "cc/paint/paint_flags.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/metadata/view_factory.h"

namespace gfx {
struct VectorIcon;
}

namespace views {

// A native themed class representing a checkbox.  This class does not use
// platform specific objects to replicate the native platforms looks and feel.
class VIEWS_EXPORT Checkbox : public LabelButton {
  METADATA_HEADER(Checkbox, LabelButton)

 public:
  explicit Checkbox(const std::u16string& label = std::u16string(),
                    PressedCallback callback = PressedCallback(),
                    int button_context = style::CONTEXT_BUTTON);

  Checkbox(const Checkbox&) = delete;
  Checkbox& operator=(const Checkbox&) = delete;

  ~Checkbox() override;

  // Sets/Gets whether or not the checkbox is checked.
  virtual void SetChecked(bool checked);
  bool GetChecked() const;

  [[nodiscard]] base::CallbackListSubscription AddCheckedChangedCallback(
      PropertyChangedCallback callback);

  void SetMultiLine(bool multi_line);
  bool GetMultiLine() const;

  void SetCheckedIconImageColor(SkColor color);

  // LabelButton:
  gfx::ImageSkia GetImage(ButtonState for_state) const override;
  std::unique_ptr<LabelButtonBorder> CreateDefaultBorder() const override;
  std::unique_ptr<ActionViewInterface> GetActionViewInterface() override;

 protected:
  // Bitmask constants for GetIconImageColor.
  enum IconState { CHECKED = 0b1, ENABLED = 0b10 };

  // Button:
  void UpdateAccessibleCheckedState() override;

  // LabelButton:
  void OnThemeChanged() override;

  // Returns the path to draw the focus ring around for this Checkbox.
  virtual SkPath GetFocusRingPath() const;

  // |icon_state| is a bitmask using the IconState enum.
  // Returns a color for the container portion of the icon.
  virtual SkColor GetIconImageColor(int icon_state) const;
  // Returns a color for the check portion of the icon.
  virtual SkColor GetIconCheckColor(int icon_state) const;

  // Returns a bitmask using the IconState enum.
  int GetIconState(ButtonState for_state) const;

  // Gets the vector icon to use based on the current state of |checked_|.
  virtual const gfx::VectorIcon& GetVectorIcon() const;

 private:
  class FocusRingHighlightPathGenerator;

  // Button:
  void NotifyClick(const ui::Event& event) override;

  ui::NativeTheme::Part GetThemePart() const override;
  void GetExtraParams(ui::NativeTheme::ExtraParams* params) const override;

  void SetAndUpdateAccessibleDefaultActionVerb();

  // True if the checkbox is checked.
  bool checked_ = false;

  std::optional<SkColor> checked_icon_image_color_;
};

class VIEWS_EXPORT CheckboxActionViewInterface
    : public LabelButtonActionViewInterface {
 public:
  explicit CheckboxActionViewInterface(Checkbox* action_view);
  ~CheckboxActionViewInterface() override = default;

  // LabelButtonActionViewInterface:
  void ActionItemChangedImpl(actions::ActionItem* action_item) override;
  void OnViewChangedImpl(actions::ActionItem* action_item) override;

 private:
  raw_ptr<Checkbox> action_view_;
};

BEGIN_VIEW_BUILDER(VIEWS_EXPORT, Checkbox, LabelButton)
VIEW_BUILDER_PROPERTY(bool, Checked)
VIEW_BUILDER_PROPERTY(bool, MultiLine)
END_VIEW_BUILDER

}  // namespace views

DEFINE_VIEW_BUILDER(VIEWS_EXPORT, Checkbox)

#endif  // UI_VIEWS_CONTROLS_BUTTON_CHECKBOX_H_