#ifndef UI_VIEWS_CONTROLS_BUTTON_BUTTON_H_
#define UI_VIEWS_CONTROLS_BUTTON_BUTTON_H_
#include <memory>
#include <optional>
#include <utility>
#include <variant>
#include "base/callback_list.h"
#include "base/functional/bind.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/safety_checks.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/actions/actions.h"
#include "ui/base/metadata/metadata_types.h"
#include "ui/base/mojom/menu_source_type.mojom-forward.h"
#include "ui/events/event_constants.h"
#include "ui/gfx/animation/throb_animation.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/animation/animation_delegate_views.h"
#include "ui/views/animation/ink_drop_host.h"
#include "ui/views/animation/ink_drop_state.h"
#include "ui/views/controls/button/button_controller_delegate.h"
#include "ui/views/metadata/view_factory.h"
#include "ui/views/painter.h"
#include "ui/views/view.h"
namespace ui {
class Event;
}
namespace views {
namespace test {
class ButtonTestApi;
}
class Button;
class ButtonController;
class VIEWS_EXPORT Button : public View, public AnimationDelegateViews {
METADATA_HEADER(Button, View)
ADVANCED_MEMORY_SAFETY_CHECKS();
public:
enum ButtonState {
STATE_NORMAL = 0,
STATE_HOVERED,
STATE_PRESSED,
STATE_DISABLED,
STATE_COUNT,
};
enum class KeyClickAction {
kOnKeyPress,
kOnKeyRelease,
kNone,
};
class VIEWS_EXPORT DefaultButtonControllerDelegate
: public ButtonControllerDelegate {
public:
explicit DefaultButtonControllerDelegate(Button* button);
DefaultButtonControllerDelegate(const DefaultButtonControllerDelegate&) =
delete;
DefaultButtonControllerDelegate& operator=(
const DefaultButtonControllerDelegate&) = delete;
~DefaultButtonControllerDelegate() override;
void RequestFocusFromEvent() override;
void NotifyClick(const ui::Event& event) override;
void OnClickCanceled(const ui::Event& event) override;
bool IsTriggerableEvent(const ui::Event& event) override;
bool ShouldEnterPushedState(const ui::Event& event) override;
bool ShouldEnterHoveredState() override;
InkDrop* GetInkDrop() override;
int GetDragOperations(const gfx::Point& press_pt) override;
bool InDrag() override;
};
class VIEWS_EXPORT PressedCallback {
public:
using Callback = base::RepeatingCallback<void(const ui::Event& event)>;
PressedCallback(base::OnceClosure closure);
PressedCallback(Callback callback = Callback());
PressedCallback(base::RepeatingClosure closure);
PressedCallback(PressedCallback&&);
PressedCallback& operator=(PressedCallback&&);
~PressedCallback();
explicit operator bool() const;
void Run(const ui::Event& event);
private:
std::variant<base::OnceClosure, base::RepeatingClosure, Callback> callback_;
};
class VIEWS_EXPORT ScopedAnchorHighlight {
public:
explicit ScopedAnchorHighlight(base::WeakPtr<Button> button);
~ScopedAnchorHighlight();
ScopedAnchorHighlight(ScopedAnchorHighlight&&);
ScopedAnchorHighlight& operator=(ScopedAnchorHighlight&&);
private:
base::WeakPtr<Button> button_;
};
static constexpr ButtonState kButtonStates[STATE_COUNT] = {
ButtonState::STATE_NORMAL, ButtonState::STATE_HOVERED,
ButtonState::STATE_PRESSED, ButtonState::STATE_DISABLED};
Button(const Button&) = delete;
Button& operator=(const Button&) = delete;
~Button() override;
static const Button* AsButton(const View* view);
static Button* AsButton(View* view);
static ButtonState GetButtonStateFrom(ui::NativeTheme::State state);
int tag() const { return tag_; }
void set_tag(int tag) { tag_ = tag; }
virtual void SetCallback(PressedCallback callback);
void AdjustAccessibleName(std::u16string& new_name,
ax::mojom::NameFrom& name_from) override;
virtual std::u16string GetAlternativeAccessibleName() const;
ButtonState GetState() const;
void SetState(ButtonState state);
void SetAnimationDuration(base::TimeDelta duration);
void SetTriggerableEventFlags(int triggerable_event_flags);
int GetTriggerableEventFlags() const;
void SetRequestFocusOnPress(bool value);
bool GetRequestFocusOnPress() const;
void SetAnimateOnStateChange(bool value);
bool GetAnimateOnStateChange() const;
void SetHideInkDropWhenShowingContextMenu(bool value);
bool GetHideInkDropWhenShowingContextMenu() const;
void SetShowInkDropWhenHotTracked(bool value);
bool GetShowInkDropWhenHotTracked() const;
void SetHasInkDropActionOnClick(bool value);
bool GetHasInkDropActionOnClick() const;
void SetInstallFocusRingOnFocus(bool install_focus_ring_on_focus);
bool GetInstallFocusRingOnFocus() const;
void SetHotTracked(bool is_hot_tracked);
bool IsHotTracked() const;
void SetTag(int value);
int GetTag() const;
void SetFocusPainter(std::unique_ptr<Painter> focus_painter);
void SetHighlighted(bool highlighted);
ScopedAnchorHighlight AddAnchorHighlight();
base::CallbackListSubscription AddStateChangedCallback(
PropertyChangedCallback callback);
base::CallbackListSubscription AddAnchorCountChangedCallback(
base::RepeatingCallback<void(size_t)> callback);
bool OnMousePressed(const ui::MouseEvent& event) override;
bool OnMouseDragged(const ui::MouseEvent& event) override;
void OnMouseReleased(const ui::MouseEvent& event) override;
void OnMouseCaptureLost() override;
void OnMouseEntered(const ui::MouseEvent& event) override;
void OnMouseExited(const ui::MouseEvent& event) override;
void OnMouseMoved(const ui::MouseEvent& event) override;
bool OnKeyPressed(const ui::KeyEvent& event) override;
bool OnKeyReleased(const ui::KeyEvent& event) override;
void OnGestureEvent(ui::GestureEvent* event) override;
bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) override;
void ShowContextMenu(const gfx::Point& p,
ui::mojom::MenuSourceType source_type) override;
void OnDragDone() override;
void OnPaint(gfx::Canvas* canvas) final;
void VisibilityChanged(View* starting_from, bool is_visible) override;
void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) override;
void OnFocus() override;
void OnBlur() override;
std::unique_ptr<ActionViewInterface> GetActionViewInterface() override;
void AnimationProgressed(const gfx::Animation* animation) override;
virtual KeyClickAction GetKeyClickActionForEvent(const ui::KeyEvent& event);
ButtonController* button_controller() const {
return button_controller_.get();
}
void SetButtonController(std::unique_ptr<ButtonController> button_controller);
gfx::Point GetMenuPosition() const;
View* ink_drop_view() const { return ink_drop_view_; }
void SetInkDropView(View* view);
protected:
explicit Button(PressedCallback callback = PressedCallback());
virtual void RequestFocusFromEvent();
virtual void NotifyClick(const ui::Event& event);
virtual void OnClickCanceled(const ui::Event& event);
virtual void OnSetTooltipText(const std::u16string& tooltip_text);
void OnTooltipTextChanged(const std::u16string& old_tooltip_text) override;
virtual void StateChanged(ButtonState old_state);
virtual bool IsTriggerableEvent(const ui::Event& event);
virtual bool ShouldUpdateInkDropOnClickCanceled() const;
virtual bool ShouldEnterPushedState(const ui::Event& event);
virtual void PaintButtonContents(gfx::Canvas* canvas);
bool ShouldEnterHoveredState();
const gfx::ThrobAnimation& hover_animation() const {
return hover_animation_;
}
const PressedCallback& GetCallback() const { return callback_; }
base::WeakPtr<Button> GetWeakPtr();
virtual void OnEnabledChanged();
virtual void UpdateAccessibleCheckedState();
void SetDefaultActionVerb(ax::mojom::DefaultActionVerb verb);
void UpdateAccessibleDefaultActionVerb();
private:
friend class test::ButtonTestApi;
friend class ScopedAnchorHighlight;
FRIEND_TEST_ALL_PREFIXES(BlueButtonTest, Border);
void ReleaseAnchorHighlight();
PressedCallback callback_;
base::RepeatingCallbackList<void(size_t)> anchor_count_changed_callbacks_;
int tag_ = -1;
ButtonState state_ = STATE_NORMAL;
gfx::ThrobAnimation hover_animation_{this};
bool animate_on_state_change_ = false;
int triggerable_event_flags_ = ui::EF_LEFT_MOUSE_BUTTON;
bool request_focus_on_press_ = false;
bool has_ink_drop_action_on_click_ = false;
bool hide_ink_drop_when_showing_context_menu_ = true;
bool show_ink_drop_when_hot_tracked_ = false;
raw_ptr<View> ink_drop_view_ = this;
std::unique_ptr<Painter> focus_painter_;
std::unique_ptr<ButtonController> button_controller_;
base::CallbackListSubscription enabled_changed_subscription_{
AddEnabledInViewsSubtreeChangedCallback(
base::BindRepeating(&Button::OnEnabledChanged,
base::Unretained(this)))};
size_t anchor_count_ = 0;
ax::mojom::DefaultActionVerb default_action_verb_ =
ax::mojom::DefaultActionVerb::kPress;
base::WeakPtrFactory<Button> weak_ptr_factory_{this};
};
class VIEWS_EXPORT ButtonActionViewInterface : public BaseActionViewInterface {
public:
explicit ButtonActionViewInterface(Button* action_view);
~ButtonActionViewInterface() override = default;
void ActionItemChangedImpl(actions::ActionItem* action_item) override;
void LinkActionInvocationToView(
base::RepeatingClosure invoke_action_callback) override;
private:
raw_ptr<Button> action_view_;
};
BEGIN_VIEW_BUILDER(VIEWS_EXPORT, Button, View)
VIEW_BUILDER_PROPERTY(Button::PressedCallback, Callback)
VIEW_BUILDER_PROPERTY(base::TimeDelta, AnimationDuration)
VIEW_BUILDER_PROPERTY(bool, AnimateOnStateChange)
VIEW_BUILDER_PROPERTY(bool, HasInkDropActionOnClick)
VIEW_BUILDER_PROPERTY(bool, HideInkDropWhenShowingContextMenu)
VIEW_BUILDER_PROPERTY(bool, InstallFocusRingOnFocus)
VIEW_BUILDER_PROPERTY(bool, RequestFocusOnPress)
VIEW_BUILDER_PROPERTY(Button::ButtonState, State)
VIEW_BUILDER_PROPERTY(int, Tag)
VIEW_BUILDER_PROPERTY(std::u16string, TooltipText)
VIEW_BUILDER_PROPERTY(int, TriggerableEventFlags)
END_VIEW_BUILDER
}
DEFINE_VIEW_BUILDER(VIEWS_EXPORT, Button)
#endif