#ifndef UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_
#define UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_
#include <array>
#include <memory>
#include <optional>
#include <string_view>
#include "base/functional/bind.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/color/color_id.h"
#include "ui/color/color_variant.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/label_button_image_container.h"
#include "ui/views/controls/button/label_button_label.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/delegating_layout_manager.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/layout/proposed_layout.h"
#include "ui/views/metadata/view_factory.h"
#include "ui/views/native_theme_delegate.h"
#include "ui/views/style/typography.h"
#include "ui/views/widget/widget.h"
namespace actions {
class ActionItem;
}
namespace gfx {
class ImageSkia;
}
namespace views {
class InkDropContainerView;
class LabelButtonBorder;
class VIEWS_EXPORT LabelButton : public Button,
public NativeThemeDelegate,
public LayoutDelegate {
METADATA_HEADER(LabelButton, Button)
public:
explicit LabelButton(
PressedCallback callback = PressedCallback(),
std::u16string_view text = {},
int button_context = style::CONTEXT_BUTTON,
std::unique_ptr<LabelButtonImageContainer> image_container =
std::make_unique<SingleImageContainer>());
LabelButton(const LabelButton&) = delete;
LabelButton& operator=(const LabelButton&) = delete;
~LabelButton() override;
virtual gfx::ImageSkia GetImage(ButtonState state) const;
const std::optional<ui::ImageModel>& GetImageModel(ButtonState state) const;
virtual void SetImageModel(ButtonState state,
const std::optional<ui::ImageModel>& image_model);
bool HasImage(ButtonState state) const;
std::u16string_view GetText() const;
virtual void SetText(std::u16string_view text);
void SetLabelStyle(views::style::TextStyle text_style);
void ShrinkDownThenClearText();
void SetTextColor(ButtonState for_state, ui::ColorVariant color);
virtual void SetEnabledTextColors(std::optional<ui::ColorVariant> color);
SkColor GetCurrentTextColor() const;
void SetTextShadows(const gfx::ShadowValues& shadows);
void SetTextSubpixelRenderingEnabled(bool enabled);
void SetElideBehavior(gfx::ElideBehavior elide_behavior);
void SetHorizontalAlignment(gfx::HorizontalAlignment alignment);
gfx::HorizontalAlignment GetHorizontalAlignment() const;
gfx::Size GetMinSize() const;
void SetMinSize(const gfx::Size& min_size);
gfx::Size GetMaxSize() const;
void SetMaxSize(const gfx::Size& max_size);
bool GetIsDefault() const;
void SetIsDefault(bool is_default);
int GetImageLabelSpacing() const;
void SetImageLabelSpacing(int spacing);
bool GetImageCentered() const;
void SetImageCentered(bool image_centered);
gfx::RoundedCornersF GetFocusRingCornerRadii() const;
void SetFocusRingCornerRadii(const gfx::RoundedCornersF& radii);
void SetFocusRingCornerRadius(float radius);
virtual std::unique_ptr<LabelButtonBorder> CreateDefaultBorder() const;
void SetAppearDisabledInInactiveWidget(bool appear_disabled);
void SetBorder(std::unique_ptr<Border> border) override;
void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
gfx::Size CalculatePreferredSize(
const SizeBounds& available_size) const override;
gfx::Size GetMinimumSize() const override;
void AddLayerToRegion(ui::Layer* new_layer,
views::LayerRegion region) override;
void RemoveLayerFromRegions(ui::Layer* old_layer) override;
std::unique_ptr<ActionViewInterface> GetActionViewInterface() override;
ui::NativeTheme::Part GetThemePart() const override;
gfx::Rect GetThemePaintRect() const override;
ui::NativeTheme::State GetThemeState(
ui::NativeTheme::ExtraParams* params) const override;
const gfx::Animation* GetThemeAnimation() const override;
ui::NativeTheme::State GetBackgroundThemeState(
ui::NativeTheme::ExtraParams* params) const override;
ui::NativeTheme::State GetForegroundThemeState(
ui::NativeTheme::ExtraParams* params) const override;
ProposedLayout CalculateProposedLayout(
const SizeBounds& size_bounds) const override;
ButtonState GetVisualState() const;
bool has_paint_as_active_subscription_for_testing() {
return !!paint_as_active_subscription_;
}
void SetFontList(const gfx::FontList& font_list);
protected:
LabelButtonImageContainer* image_container() {
return image_container_.get();
}
const LabelButtonImageContainer* image_container() const {
return image_container_.get();
}
const View* image_container_view() const {
return image_container_->GetView();
}
View* image_container_view() { return image_container_->GetView(); }
Label* label() const { return label_; }
InkDropContainerView* ink_drop_container() const {
return ink_drop_container_;
}
bool explicitly_set_normal_color() const {
return explicitly_set_colors_[STATE_NORMAL];
}
const std::array<bool, STATE_COUNT>& explicitly_set_colors() const {
return explicitly_set_colors_;
}
void set_explicitly_set_colors(const std::array<bool, STATE_COUNT>& colors) {
explicitly_set_colors_ = colors;
}
void UpdateImage();
virtual void UpdateBackgroundColor() {}
virtual void GetExtraParams(ui::NativeTheme::ExtraParams* params) const;
virtual PropertyEffects UpdateStyleToIndicateDefaultStatus();
void ChildPreferredSizeChanged(View* child) override;
void AddedToWidget() override;
void RemovedFromWidget() override;
void OnFocus() override;
void OnBlur() override;
void OnThemeChanged() override;
void StateChanged(ButtonState old_state) override;
private:
void SetTextInternal(std::u16string_view text);
void ClearTextIfShrunkDown();
gfx::Size GetUnclampedSizeWithoutLabel() const;
void VisualStateChanged();
void ResetColorsFromNativeTheme();
void ResetLabelEnabledColor();
ButtonState ImageStateForState(ButtonState for_state) const;
void FlipCanvasOnPaintForRTLUIChanged();
std::unique_ptr<LabelButtonImageContainer> image_container_;
raw_ptr<internal::LabelButtonLabel> label_;
raw_ptr<InkDropContainerView> ink_drop_container_;
gfx::FontList cached_normal_font_list_;
gfx::FontList cached_default_button_font_list_;
std::array<std::optional<ui::ImageModel>, STATE_COUNT>
button_state_image_models_;
std::array<std::optional<ui::ColorVariant>, STATE_COUNT> button_state_colors_;
std::array<bool, STATE_COUNT> explicitly_set_colors_ = {};
gfx::Size min_size_;
gfx::Size max_size_;
bool shrinking_down_label_ = false;
bool is_default_ = false;
bool explicitly_set_border_ = false;
bool image_centered_ = true;
int image_label_spacing_ = LayoutProvider::Get()->GetDistanceMetric(
DISTANCE_RELATED_LABEL_HORIZONTAL);
gfx::HorizontalAlignment horizontal_alignment_ = gfx::ALIGN_LEFT;
gfx::RoundedCornersF focus_ring_corner_radii_ =
gfx::RoundedCornersF(FocusRing::kDefaultCornerRadiusDp);
base::CallbackListSubscription paint_as_active_subscription_;
bool appear_disabled_in_inactive_widget_ = false;
base::CallbackListSubscription flip_canvas_on_paint_subscription_ =
AddFlipCanvasOnPaintForRTLUIChangedCallback(
base::BindRepeating(&LabelButton::FlipCanvasOnPaintForRTLUIChanged,
base::Unretained(this)));
};
class VIEWS_EXPORT LabelButtonActionViewInterface
: public ButtonActionViewInterface {
public:
explicit LabelButtonActionViewInterface(LabelButton* action_view);
~LabelButtonActionViewInterface() override = default;
void ActionItemChangedImpl(actions::ActionItem* action_item) override;
private:
raw_ptr<LabelButton> action_view_;
};
BEGIN_VIEW_BUILDER(VIEWS_EXPORT, LabelButton, Button)
VIEW_BUILDER_PROPERTY(std::u16string, Text)
VIEW_BUILDER_PROPERTY(gfx::HorizontalAlignment, HorizontalAlignment)
VIEW_BUILDER_PROPERTY(views::style::TextStyle, LabelStyle)
VIEW_BUILDER_PROPERTY(gfx::Size, MinSize)
VIEW_BUILDER_PROPERTY(gfx::Size, MaxSize)
VIEW_BUILDER_PROPERTY(std::optional<ui::ColorVariant>, EnabledTextColors)
VIEW_BUILDER_PROPERTY(bool, IsDefault)
VIEW_BUILDER_PROPERTY(int, ImageLabelSpacing)
VIEW_BUILDER_PROPERTY(bool, ImageCentered)
VIEW_BUILDER_METHOD(SetImageModel, Button::ButtonState, const ui::ImageModel&)
VIEW_BUILDER_METHOD(SetTextColor, Button::ButtonState, ui::ColorVariant)
END_VIEW_BUILDER
}
DEFINE_VIEW_BUILDER(VIEWS_EXPORT, LabelButton)
#endif