Commit 8b2d8d39 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Misc. cleanup for LabelButton:

* Use in-declaration initializers where possible
* Avoid bare new
* Reset the cached preferred size on PreferredSizeChanged()

Bug: none
Change-Id: I68a8a9a3b868d2065d767e5f30b86ae70842378a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1601577
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarMichael Wasserman <msw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#658858}
parent d27792da
...@@ -21,17 +21,12 @@ ...@@ -21,17 +21,12 @@
#include "ui/views/animation/ink_drop.h" #include "ui/views/animation/ink_drop.h"
#include "ui/views/background.h" #include "ui/views/background.h"
#include "ui/views/controls/button/label_button_border.h" #include "ui/views/controls/button/label_button_border.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/painter.h" #include "ui/views/painter.h"
#include "ui/views/style/platform_style.h" #include "ui/views/style/platform_style.h"
#include "ui/views/view_class_properties.h" #include "ui/views/view_class_properties.h"
#include "ui/views/window/dialog_delegate.h" #include "ui/views/window/dialog_delegate.h"
namespace views { namespace views {
namespace {
// The length of the hover fade animation.
constexpr int kHoverAnimationDurationMs = 170;
} // namespace
// static // static
const char LabelButton::kViewClassName[] = "LabelButton"; const char LabelButton::kViewClassName[] = "LabelButton";
...@@ -40,33 +35,24 @@ LabelButton::LabelButton(ButtonListener* listener, ...@@ -40,33 +35,24 @@ LabelButton::LabelButton(ButtonListener* listener,
const base::string16& text, const base::string16& text,
int button_context) int button_context)
: Button(listener), : Button(listener),
image_(new ImageView()),
label_(new LabelButtonLabel(text, button_context)),
ink_drop_container_(new InkDropContainerView()),
cached_normal_font_list_( cached_normal_font_list_(
style::GetFont(button_context, style::STYLE_PRIMARY)), style::GetFont(button_context, style::STYLE_PRIMARY)),
cached_default_button_font_list_( cached_default_button_font_list_(
style::GetFont(button_context, style::STYLE_DIALOG_BUTTON_DEFAULT)), style::GetFont(button_context, style::STYLE_DIALOG_BUTTON_DEFAULT)) {
button_state_colors_(), ink_drop_container_ = AddChildView(std::make_unique<InkDropContainerView>());
explicitly_set_colors_(),
is_default_(false),
style_(STYLE_TEXTBUTTON),
border_is_themed_border_(true),
image_label_spacing_(LayoutProvider::Get()->GetDistanceMetric(
DISTANCE_RELATED_LABEL_HORIZONTAL)),
horizontal_alignment_(gfx::ALIGN_LEFT) {
SetAnimationDuration(kHoverAnimationDurationMs);
SetTextInternal(text);
AddChildView(ink_drop_container_);
ink_drop_container_->SetVisible(false); ink_drop_container_->SetVisible(false);
AddChildView(image_); image_ = AddChildView(std::make_unique<ImageView>());
image_->set_can_process_events_within_subtree(false); image_->set_can_process_events_within_subtree(false);
AddChildView(label_); label_ =
AddChildView(std::make_unique<LabelButtonLabel>(text, button_context));
label_->SetAutoColorReadabilityEnabled(false); label_->SetAutoColorReadabilityEnabled(false);
label_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD); label_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
constexpr int kHoverAnimationDurationMs = 170;
SetAnimationDuration(kHoverAnimationDurationMs);
SetTextInternal(text);
} }
LabelButton::~LabelButton() = default; LabelButton::~LabelButton() = default;
...@@ -137,11 +123,12 @@ void LabelButton::SetIsDefault(bool is_default) { ...@@ -137,11 +123,12 @@ void LabelButton::SetIsDefault(bool is_default) {
// TODO(estade): move this to MdTextButton once |style_| is removed. // TODO(estade): move this to MdTextButton once |style_| is removed.
if (is_default == is_default_) if (is_default == is_default_)
return; return;
is_default_ = is_default; is_default_ = is_default;
ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE); ui::Accelerator accel(ui::VKEY_RETURN, ui::EF_NONE);
is_default_ ? AddAccelerator(accel) : RemoveAccelerator(accel); if (is_default_)
AddAccelerator(accel);
else
RemoveAccelerator(accel);
UpdateStyleToIndicateDefaultStatus(); UpdateStyleToIndicateDefaultStatus();
} }
...@@ -176,8 +163,7 @@ void LabelButton::SetImageLabelSpacing(int spacing) { ...@@ -176,8 +163,7 @@ void LabelButton::SetImageLabelSpacing(int spacing) {
std::unique_ptr<LabelButtonBorder> LabelButton::CreateDefaultBorder() const { std::unique_ptr<LabelButtonBorder> LabelButton::CreateDefaultBorder() const {
if (style_ != Button::STYLE_TEXTBUTTON) if (style_ != Button::STYLE_TEXTBUTTON)
return std::make_unique<LabelButtonAssetBorder>(style_); return std::make_unique<LabelButtonAssetBorder>(style_);
std::unique_ptr<LabelButtonBorder> border = auto border = std::make_unique<LabelButtonBorder>();
std::make_unique<LabelButtonBorder>();
border->set_insets( border->set_insets(
views::LabelButtonAssetBorder::GetDefaultInsetsForStyle(style_)); views::LabelButtonAssetBorder::GetDefaultInsetsForStyle(style_));
return border; return border;
...@@ -191,7 +177,7 @@ void LabelButton::SetBorder(std::unique_ptr<Border> border) { ...@@ -191,7 +177,7 @@ void LabelButton::SetBorder(std::unique_ptr<Border> border) {
gfx::Size LabelButton::CalculatePreferredSize() const { gfx::Size LabelButton::CalculatePreferredSize() const {
// Cache the computed size, as recomputing it is an expensive operation. // Cache the computed size, as recomputing it is an expensive operation.
if (!cached_preferred_size_valid_) { if (!cached_preferred_size_) {
// Use a temporary label copy for sizing to avoid calculation side-effects. // Use a temporary label copy for sizing to avoid calculation side-effects.
Label label(GetText(), {label_->font_list()}); Label label(GetText(), {label_->font_list()});
label.SetLineHeight(label_->line_height()); label.SetLineHeight(label_->line_height());
...@@ -223,11 +209,10 @@ gfx::Size LabelButton::CalculatePreferredSize() const { ...@@ -223,11 +209,10 @@ gfx::Size LabelButton::CalculatePreferredSize() const {
if (max_size_.height() > 0) if (max_size_.height() > 0)
size.set_height(std::min(max_size_.height(), size.height())); size.set_height(std::min(max_size_.height(), size.height()));
cached_preferred_size_valid_ = true;
cached_preferred_size_ = size; cached_preferred_size_ = size;
} }
return cached_preferred_size_; return cached_preferred_size_.value();
} }
int LabelButton::GetHeightForWidth(int width) const { int LabelButton::GetHeightForWidth(int width) const {
...@@ -240,9 +225,7 @@ int LabelButton::GetHeightForWidth(int width) const { ...@@ -240,9 +225,7 @@ int LabelButton::GetHeightForWidth(int width) const {
// Height is the larger of size without label and label height with insets. // Height is the larger of size without label and label height with insets.
int height = std::max(size_without_label.height(), label_height_with_insets); int height = std::max(size_without_label.height(), label_height_with_insets);
// Make sure height respects min_size_. height = std::max(height, min_size_.height());
if (height < min_size_.height())
height = min_size_.height();
// Clamp height to the maximum height (if valid). // Clamp height to the maximum height (if valid).
if (max_size_.height() > 0) if (max_size_.height() > 0)
...@@ -261,7 +244,7 @@ void LabelButton::Layout() { ...@@ -261,7 +244,7 @@ void LabelButton::Layout() {
// label is short. // label is short.
gfx::Rect label_area(child_area); gfx::Rect label_area(child_area);
gfx::Insets insets(GetInsets()); gfx::Insets insets = GetInsets();
child_area.Inset(insets); child_area.Inset(insets);
// Labels can paint over the vertical component of the border insets. // Labels can paint over the vertical component of the border insets.
label_area.Inset(insets.left(), 0, insets.right(), 0); label_area.Inset(insets.left(), 0, insets.right(), 0);
...@@ -292,8 +275,9 @@ void LabelButton::Layout() { ...@@ -292,8 +275,9 @@ void LabelButton::Layout() {
image_origin.Offset(0, (child_area.height() - image_size.height()) / 2); image_origin.Offset(0, (child_area.height() - image_size.height()) / 2);
} }
if (horizontal_alignment_ == gfx::ALIGN_CENTER) { if (horizontal_alignment_ == gfx::ALIGN_CENTER) {
const int spacing = (image_size.width() > 0 && label_size.width() > 0) ? const int spacing = (image_size.width() > 0 && label_size.width() > 0)
image_label_spacing_ : 0; ? image_label_spacing_
: 0;
const int total_width = image_size.width() + label_size.width() + const int total_width = image_size.width() + label_size.width() +
spacing; spacing;
image_origin.Offset((child_area.width() - total_width) / 2, 0); image_origin.Offset((child_area.width() - total_width) / 2, 0);
...@@ -376,7 +360,6 @@ ui::NativeTheme::State LabelButton::GetForegroundThemeState( ...@@ -376,7 +360,6 @@ ui::NativeTheme::State LabelButton::GetForegroundThemeState(
void LabelButton::UpdateImage() { void LabelButton::UpdateImage() {
image_->SetImage(GetImage(GetVisualState())); image_->SetImage(GetImage(GetVisualState()));
ResetCachedPreferredSize();
} }
void LabelButton::UpdateThemedBorder() { void LabelButton::UpdateThemedBorder() {
...@@ -475,10 +458,14 @@ void LabelButton::UpdateStyleToIndicateDefaultStatus() { ...@@ -475,10 +458,14 @@ void LabelButton::UpdateStyleToIndicateDefaultStatus() {
} }
void LabelButton::ChildPreferredSizeChanged(View* child) { void LabelButton::ChildPreferredSizeChanged(View* child) {
ResetCachedPreferredSize();
PreferredSizeChanged(); PreferredSizeChanged();
} }
void LabelButton::PreferredSizeChanged() {
ResetCachedPreferredSize();
Button::PreferredSizeChanged();
}
void LabelButton::OnFocus() { void LabelButton::OnFocus() {
Button::OnFocus(); Button::OnFocus();
// Typically the border renders differently when focused. // Typically the border renders differently when focused.
...@@ -518,8 +505,7 @@ void LabelButton::SetTextInternal(const base::string16& text) { ...@@ -518,8 +505,7 @@ void LabelButton::SetTextInternal(const base::string16& text) {
} }
void LabelButton::ResetCachedPreferredSize() { void LabelButton::ResetCachedPreferredSize() {
cached_preferred_size_valid_ = false; cached_preferred_size_ = base::nullopt;
cached_preferred_size_ = gfx::Size();
} }
gfx::Size LabelButton::GetUnclampedSizeWithoutLabel() const { gfx::Size LabelButton::GetUnclampedSizeWithoutLabel() const {
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "ui/views/controls/button/label_button_label.h" #include "ui/views/controls/button/label_button_label.h"
#include "ui/views/controls/image_view.h" #include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h" #include "ui/views/controls/label.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/native_theme_delegate.h" #include "ui/views/native_theme_delegate.h"
#include "ui/views/style/typography.h" #include "ui/views/style/typography.h"
...@@ -146,12 +147,12 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate { ...@@ -146,12 +147,12 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate {
// Resets colors from the NativeTheme, explicitly set colors are unchanged. // Resets colors from the NativeTheme, explicitly set colors are unchanged.
virtual void ResetColorsFromNativeTheme(); virtual void ResetColorsFromNativeTheme();
// Changes the visual styling of this button to reflect the state of // Changes the visual styling to match changes in the default state.
// |is_default()|.
virtual void UpdateStyleToIndicateDefaultStatus(); virtual void UpdateStyleToIndicateDefaultStatus();
// Button: // Button:
void ChildPreferredSizeChanged(View* child) override; void ChildPreferredSizeChanged(View* child) override;
void PreferredSizeChanged() override;
void OnFocus() override; void OnFocus() override;
void OnBlur() override; void OnBlur() override;
void OnThemeChanged() override; void OnThemeChanged() override;
...@@ -160,8 +161,7 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate { ...@@ -160,8 +161,7 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate {
private: private:
void SetTextInternal(const base::string16& text); void SetTextInternal(const base::string16& text);
// Resets |cached_preferred_size_| and marks |cached_preferred_size_valid_| // Resets |cached_preferred_size_|.
// as false.
void ResetCachedPreferredSize(); void ResetCachedPreferredSize();
// Gets the preferred size (without respecting min_size_ or max_size_), but // Gets the preferred size (without respecting min_size_ or max_size_), but
...@@ -185,7 +185,7 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate { ...@@ -185,7 +185,7 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate {
// A separate view is necessary to hold the ink drop layer so that it can // A separate view is necessary to hold the ink drop layer so that it can
// be stacked below |image_| and on top of |label_|, without resorting to // be stacked below |image_| and on top of |label_|, without resorting to
// drawing |label_| on a layer (which can mess with subpixel anti-aliasing). // drawing |label_| on a layer (which can mess with subpixel anti-aliasing).
InkDropContainerView* const ink_drop_container_; InkDropContainerView* ink_drop_container_;
// The cached font lists in the normal and default button style. The latter // The cached font lists in the normal and default button style. The latter
// may be bold. // may be bold.
...@@ -193,39 +193,39 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate { ...@@ -193,39 +193,39 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate {
gfx::FontList cached_default_button_font_list_; gfx::FontList cached_default_button_font_list_;
// The images and colors for each button state. // The images and colors for each button state.
gfx::ImageSkia button_state_images_[STATE_COUNT]; gfx::ImageSkia button_state_images_[STATE_COUNT] = {};
SkColor button_state_colors_[STATE_COUNT]; SkColor button_state_colors_[STATE_COUNT] = {};
// Used to track whether SetTextColor() has been invoked. // Used to track whether SetTextColor() has been invoked.
std::array<bool, STATE_COUNT> explicitly_set_colors_; std::array<bool, STATE_COUNT> explicitly_set_colors_ = {};
// |min_size_| and |max_size_| may be set to clamp the preferred size. // |min_size_| and |max_size_| may be set to clamp the preferred size.
gfx::Size min_size_; gfx::Size min_size_;
gfx::Size max_size_; gfx::Size max_size_;
// Cache the last computed preferred size. // Cache the last computed preferred size.
mutable gfx::Size cached_preferred_size_; mutable base::Optional<gfx::Size> cached_preferred_size_;
mutable bool cached_preferred_size_valid_;
// Flag indicating default handling of the return key via an accelerator. // Flag indicating default handling of the return key via an accelerator.
// Whether or not the button appears or behaves as the default button in its // Whether or not the button appears or behaves as the default button in its
// current context; // current context;
bool is_default_; bool is_default_ = false;
// The button's overall style. // The button's overall style.
ButtonStyle style_; ButtonStyle style_ = STYLE_TEXTBUTTON;
// True if current border was set by UpdateThemedBorder. Defaults to true. // True if current border was set by UpdateThemedBorder.
bool border_is_themed_border_; bool border_is_themed_border_ = true;
// Spacing between the image and the text. // Spacing between the image and the text.
int image_label_spacing_; int image_label_spacing_ = LayoutProvider::Get()->GetDistanceMetric(
DISTANCE_RELATED_LABEL_HORIZONTAL);
// Alignment of the button. This can be different from the alignment of the // Alignment of the button. This can be different from the alignment of the
// text; for example, the label may be set to ALIGN_TO_HEAD (alignment matches // text; for example, the label may be set to ALIGN_TO_HEAD (alignment matches
// text direction) while |this| is laid out as ALIGN_LEFT (alignment matches // text direction) while |this| is laid out as ALIGN_LEFT (alignment matches
// UI direction). // UI direction).
gfx::HorizontalAlignment horizontal_alignment_; gfx::HorizontalAlignment horizontal_alignment_ = gfx::ALIGN_LEFT;
DISALLOW_COPY_AND_ASSIGN(LabelButton); DISALLOW_COPY_AND_ASSIGN(LabelButton);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment