Skip to content

Commit

Permalink
Add ColorId support to LabelButton SetTextColor.
Browse files Browse the repository at this point in the history
Also add SetEnabledTextColorIds function to LabelButtons.
Add ColorId support to the LabelButtonLabel class.
Get rid of OnThemeChanged in LockContentsView and LoginShelfView by
using the introduced LabelButton functions.

Bug: b:262260963
Change-Id: I4eb4f33937e2719b6f4257689a096827466f1cca
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4306536
Reviewed-by: Xiaodan Zhu <zxdan@chromium.org>
Reviewed-by: Allen Bauer <kylixrd@chromium.org>
Commit-Queue: Istvan Nagy <iscsi@google.com>
Reviewed-by: Denis Kuznetsov <antrim@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1116273}
  • Loading branch information
Istvan Nagy authored and Chromium LUCI CQ committed Mar 13, 2023
1 parent dc7b688 commit 69ee26f
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 71 deletions.
8 changes: 3 additions & 5 deletions ash/login/ui/bottom_status_indicator.cc
Expand Up @@ -26,11 +26,9 @@ BottomStatusIndicator::BottomStatusIndicator(TappedCallback on_tapped_callback)
BottomStatusIndicator::~BottomStatusIndicator() = default;

void BottomStatusIndicator::SetIcon(const gfx::VectorIcon& vector_icon,
AshColorProvider::ContentLayerType type) {
SetImage(
views::Button::STATE_NORMAL,
gfx::CreateVectorIcon(
vector_icon, AshColorProvider::Get()->GetContentLayerColor(type)));
ui::ColorId color_id) {
SetImageModel(views::Button::STATE_NORMAL,
ui::ImageModel::FromVectorIcon(vector_icon, color_id));
}

void BottomStatusIndicator::GetAccessibleNodeData(ui::AXNodeData* node_data) {
Expand Down
5 changes: 2 additions & 3 deletions ash/login/ui/bottom_status_indicator.h
Expand Up @@ -5,7 +5,7 @@
#ifndef ASH_LOGIN_UI_BOTTOM_STATUS_INDICATOR_H_
#define ASH_LOGIN_UI_BOTTOM_STATUS_INDICATOR_H_

#include "ash/style/ash_color_provider.h"
#include "ash/style/ash_color_id.h"
#include "base/memory/weak_ptr.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/views/controls/button/label_button.h"
Expand All @@ -28,8 +28,7 @@ class BottomStatusIndicator
BottomStatusIndicator& operator=(const BottomStatusIndicator&) = delete;
~BottomStatusIndicator() override;

void SetIcon(const gfx::VectorIcon& vector_icon,
AshColorProvider::ContentLayerType type);
void SetIcon(const gfx::VectorIcon& vector_icon, ui::ColorId color_id);

void set_role_for_accessibility(ax::mojom::Role role) { role_ = role; }

Expand Down
28 changes: 8 additions & 20 deletions ash/login/ui/lock_contents_view.cc
Expand Up @@ -674,12 +674,6 @@ bool LockContentsView::AcceleratorPressed(const ui::Accelerator& accelerator) {
return true;
}

void LockContentsView::OnThemeChanged() {
NonAccessibleView::OnThemeChanged();
UpdateBottomStatusIndicatorColors();
UpdateSystemInfoColors();
}

void LockContentsView::OnUsersChanged(const std::vector<LoginUserInfo>& users) {
// The debug view will potentially call this method many times. Make sure to
// invalidate any child references.
Expand Down Expand Up @@ -2493,8 +2487,7 @@ bool LockContentsView::GetSystemInfoVisibility() const {
void LockContentsView::UpdateSystemInfoColors() {
for (auto* child : system_info_->children()) {
views::Label* label = static_cast<views::Label*>(child);
label->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
AshColorProvider::ContentLayerType::kTextColorPrimary));
label->SetEnabledColorId(kColorAshTextColorPrimary);
}
}

Expand All @@ -2503,21 +2496,16 @@ void LockContentsView::UpdateBottomStatusIndicatorColors() {
case BottomIndicatorState::kNone:
return;
case BottomIndicatorState::kManagedDevice: {
bottom_status_indicator_->SetIcon(
chromeos::kEnterpriseIcon,
AshColorProvider::ContentLayerType::kIconColorPrimary);
bottom_status_indicator_->SetEnabledTextColors(
AshColorProvider::Get()->GetContentLayerColor(
AshColorProvider::ContentLayerType::kTextColorPrimary));
bottom_status_indicator_->SetIcon(chromeos::kEnterpriseIcon,
kColorAshIconColorPrimary);
bottom_status_indicator_->SetEnabledTextColorIds(
kColorAshTextColorPrimary);
break;
}
case BottomIndicatorState::kAdbSideLoadingEnabled: {
bottom_status_indicator_->SetIcon(
kLockScreenAlertIcon,
AshColorProvider::ContentLayerType::kIconColorAlert);
bottom_status_indicator_->SetEnabledTextColors(
AshColorProvider::Get()->GetContentLayerColor(
AshColorProvider::ContentLayerType::kTextColorAlert));
bottom_status_indicator_->SetIcon(kLockScreenAlertIcon,
kColorAshIconColorAlert);
bottom_status_indicator_->SetEnabledTextColorIds(kColorAshTextColorAlert);
break;
}
}
Expand Down
3 changes: 0 additions & 3 deletions ash/login/ui/lock_contents_view.h
Expand Up @@ -134,9 +134,6 @@ class ASH_EXPORT LockContentsView
void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
bool AcceleratorPressed(const ui::Accelerator& accelerator) override;

// NonAccessibleView:
void OnThemeChanged() override;

// LoginDataDispatcher::Observer:
void OnUsersChanged(const std::vector<LoginUserInfo>& users) override;
void OnUserAvatarChanged(const AccountId& account_id,
Expand Down
9 changes: 2 additions & 7 deletions ash/shelf/login_shelf_view.cc
Expand Up @@ -205,7 +205,7 @@ class LoginShelfButton : public PillButton {
}

void UpdateButtonColors() {
SetEnabledTextColors(GetColorProvider()->GetColor(GetButtonTextColorId()));
SetEnabledTextColorIds(GetButtonTextColorId());
SetImageModel(
views::Button::STATE_NORMAL,
ui::ImageModel::FromVectorIcon(icon_, GetButtonIconColorId()));
Expand Down Expand Up @@ -337,7 +337,7 @@ class KioskAppsButton : public views::MenuButton,
}

void UpdateButtonColors() {
SetEnabledTextColors(GetColorProvider()->GetColor(GetButtonTextColorId()));
SetEnabledTextColorIds(GetButtonTextColorId());
SetImageModel(views::Button::STATE_NORMAL,
ui::ImageModel::FromVectorIcon(kShelfAppsButtonIcon,
GetButtonIconColorId()));
Expand Down Expand Up @@ -649,11 +649,6 @@ void LoginShelfView::Layout() {
UpdateButtonUnionBounds();
}

void LoginShelfView::OnThemeChanged() {
views::View::OnThemeChanged();
UpdateButtonsColors();
}

void LoginShelfView::OnShelfConfigUpdated() {
views::InstallRoundRectHighlightPathGenerator(
kiosk_apps_button_, gfx::Insets(),
Expand Down
1 change: 0 additions & 1 deletion ash/shelf/login_shelf_view.h
Expand Up @@ -135,7 +135,6 @@ class ASH_EXPORT LoginShelfView : public views::View,
void AboutToRequestFocusFromTabTraversal(bool reverse) override;
void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
void Layout() override;
void OnThemeChanged() override;

// ShelfConfig::Observer:
void OnShelfConfigUpdated() override;
Expand Down
56 changes: 39 additions & 17 deletions ui/views/controls/button/label_button.cc
Expand Up @@ -120,6 +120,16 @@ void LabelButton::SetTextColor(ButtonState for_state, SkColor color) {
explicitly_set_colors_[for_state] = true;
}

void LabelButton::SetTextColorId(ButtonState for_state, ui::ColorId color_id) {
button_state_colors_[for_state] = color_id;
if (for_state == STATE_DISABLED) {
label_->SetDisabledColorId(color_id);
} else if (for_state == GetState()) {
label_->SetEnabledColorId(color_id);
}
explicitly_set_colors_[for_state] = true;
}

float LabelButton::GetFocusRingCornerRadius() const {
return focus_ring_corner_radius_;
}
Expand Down Expand Up @@ -147,6 +157,20 @@ void LabelButton::SetEnabledTextColors(absl::optional<SkColor> color) {
ResetColorsFromNativeTheme();
}

void LabelButton::SetEnabledTextColorIds(absl::optional<ui::ColorId> color_id) {
ButtonState states[] = {STATE_NORMAL, STATE_HOVERED, STATE_PRESSED};
if (color_id.has_value()) {
for (auto state : states) {
SetTextColorId(state, color_id.value());
}
return;
}
for (auto state : states) {
explicitly_set_colors_[state] = false;
}
ResetColorsFromNativeTheme();
}

SkColor LabelButton::GetCurrentTextColor() const {
return label_->GetEnabledColor();
}
Expand Down Expand Up @@ -602,36 +626,34 @@ void LabelButton::VisualStateChanged() {
}

void LabelButton::ResetColorsFromNativeTheme() {
if (!GetWidget()) {
// If there is no widget, we can't actually get the real colors here.
// An OnThemeChanged() will fire once a widget is available.
return;
}

const ui::ColorProvider* color_provider = GetColorProvider();
// Since this is a LabelButton, use the label colors.
SkColor colors[STATE_COUNT] = {
color_provider->GetColor(ui::kColorLabelForeground),
color_provider->GetColor(ui::kColorLabelForeground),
color_provider->GetColor(ui::kColorLabelForeground),
color_provider->GetColor(ui::kColorLabelForegroundDisabled),
};
ui::ColorId color_ids[STATE_COUNT] = {
ui::kColorLabelForeground, ui::kColorLabelForeground,
ui::kColorLabelForeground, ui::kColorLabelForegroundDisabled};

label_->SetBackground(nullptr);
label_->SetAutoColorReadabilityEnabled(false);

for (size_t state = STATE_NORMAL; state < STATE_COUNT; ++state) {
if (!explicitly_set_colors_[state]) {
SetTextColor(static_cast<ButtonState>(state), colors[state]);
SetTextColorId(static_cast<ButtonState>(state), color_ids[state]);
explicitly_set_colors_[state] = false;
}
}
}

void LabelButton::ResetLabelEnabledColor() {
const SkColor color = button_state_colors_[GetState()];
if (GetState() != STATE_DISABLED && label_->GetEnabledColor() != color)
label_->SetEnabledColor(color);
if (GetState() != STATE_DISABLED) {
const absl::variant<SkColor, ui::ColorId>& color =
button_state_colors_[GetState()];
if (absl::holds_alternative<SkColor>(color) &&
label_->GetEnabledColor() != absl::get<SkColor>(color)) {
label_->SetEnabledColor(absl::get<SkColor>(color));
} else if (absl::holds_alternative<ui::ColorId>(color) &&
label_->GetEnabledColorId() != absl::get<ui::ColorId>(color)) {
label_->SetEnabledColorId(absl::get<ui::ColorId>(color));
}
}
}

Button::ButtonState LabelButton::ImageStateForState(
Expand Down
8 changes: 7 additions & 1 deletion ui/views/controls/button/label_button.h
Expand Up @@ -70,9 +70,15 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate {
// Sets the text color shown for the specified button |for_state| to |color|.
void SetTextColor(ButtonState for_state, SkColor color);

// Sets the text color as above but using ColorId.
void SetTextColorId(ButtonState for_state, ui::ColorId color_id);

// Sets the text colors shown for the non-disabled states to |color|.
virtual void SetEnabledTextColors(absl::optional<SkColor> color);

// Sets the text colors shown for the non-disabled states to |color_id|.
void SetEnabledTextColorIds(absl::optional<ui::ColorId> color_id);

// Gets the current state text color.
SkColor GetCurrentTextColor() const;

Expand Down Expand Up @@ -233,7 +239,7 @@ class VIEWS_EXPORT LabelButton : public Button, public NativeThemeDelegate {

// The image models and colors for each button state.
ui::ImageModel button_state_image_models_[STATE_COUNT] = {};
SkColor button_state_colors_[STATE_COUNT] = {};
absl::variant<SkColor, ui::ColorId> button_state_colors_[STATE_COUNT] = {};

// Used to track whether SetTextColor() has been invoked.
std::array<bool, STATE_COUNT> explicitly_set_colors_ = {};
Expand Down
61 changes: 51 additions & 10 deletions ui/views/controls/button/label_button_label.cc
Expand Up @@ -8,7 +8,7 @@

#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_provider.h"
#include "ui/views/layout/layout_provider.h"

namespace views::internal {

Expand All @@ -23,12 +23,47 @@ void LabelButtonLabel::SetDisabledColor(SkColor color) {
Label::SetEnabledColor(color);
}

void LabelButtonLabel::SetDisabledColorId(
absl::optional<ui::ColorId> color_id) {
if (!color_id.has_value()) {
return;
}
requested_disabled_color_ = color_id.value();
if (!GetEnabled()) {
Label::SetEnabledColorId(color_id.value());
}
}

absl::optional<ui::ColorId> LabelButtonLabel::GetDisabledColorId() const {
if (absl::holds_alternative<ui::ColorId>(requested_disabled_color_)) {
return absl::get<ui::ColorId>(requested_disabled_color_);
}
return absl::nullopt;
}

void LabelButtonLabel::SetEnabledColor(SkColor color) {
requested_enabled_color_ = color;
if (GetEnabled())
Label::SetEnabledColor(color);
}

void LabelButtonLabel::SetEnabledColorId(absl::optional<ui::ColorId> color_id) {
if (!color_id.has_value()) {
return;
}
requested_enabled_color_ = color_id.value();
if (GetEnabled()) {
Label::SetEnabledColorId(color_id.value());
}
}

absl::optional<ui::ColorId> LabelButtonLabel::GetEnabledColorId() const {
if (absl::holds_alternative<ui::ColorId>(requested_enabled_color_)) {
return absl::get<ui::ColorId>(requested_enabled_color_);
}
return absl::nullopt;
}

void LabelButtonLabel::OnThemeChanged() {
SetColorForEnableState();
Label::OnThemeChanged();
Expand All @@ -39,20 +74,26 @@ void LabelButtonLabel::OnEnabledChanged() {
}

void LabelButtonLabel::SetColorForEnableState() {
const absl::optional<SkColor>& color =
const absl::variant<absl::monostate, SkColor, ui::ColorId>& color =
GetEnabled() ? requested_enabled_color_ : requested_disabled_color_;
if (color) {
Label::SetEnabledColor(*color);
} else if (GetWidget()) {
// If there is no widget, we can't actually get the colors here.
// An OnThemeChanged() will fire once a widget is available.
Label::SetEnabledColor(GetColorProvider()->GetColor(style::GetColorId(
GetTextContext(),
GetEnabled() ? style::STYLE_PRIMARY : style::STYLE_DISABLED)));
if (absl::holds_alternative<SkColor>(color)) {
Label::SetEnabledColor(absl::get<SkColor>(color));
} else if (absl::holds_alternative<ui::ColorId>(color)) {
Label::SetEnabledColorId(absl::get<ui::ColorId>(color));
} else {
// Get default color Id.
const ui::ColorId default_color_id =
LayoutProvider::Get()->GetTypographyProvider().GetColorId(
GetTextContext(),
GetEnabled() ? style::STYLE_PRIMARY : style::STYLE_DISABLED);
// Set default color Id.
Label::SetEnabledColorId(default_color_id);
}
}

BEGIN_METADATA(LabelButtonLabel, Label)
ADD_PROPERTY_METADATA(absl::optional<ui::ColorId>, EnabledColorId)
ADD_PROPERTY_METADATA(absl::optional<ui::ColorId>, DisabledColorId)
END_METADATA

} // namespace views::internal
21 changes: 19 additions & 2 deletions ui/views/controls/button/label_button_label.h
Expand Up @@ -11,6 +11,7 @@
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/color/color_id.h"
#include "ui/views/controls/label.h"
#include "ui/views/views_export.h"

Expand All @@ -30,9 +31,17 @@ class VIEWS_EXPORT LabelButtonLabel : public Label {
// changes in the native theme for disabled colors.
void SetDisabledColor(SkColor color);

// Sets/Gets the explicit disable color as above, but using color_id.
void SetDisabledColorId(absl::optional<ui::ColorId> color_id);
absl::optional<ui::ColorId> GetDisabledColorId() const;

// Label:
void SetEnabledColor(SkColor color) override;

// Sets/Gets the explicit enabled color with color_id.
void SetEnabledColorId(absl::optional<ui::ColorId> color_id);
absl::optional<ui::ColorId> GetEnabledColorId() const;

protected:
// Label:
void OnThemeChanged() override;
Expand All @@ -41,14 +50,22 @@ class VIEWS_EXPORT LabelButtonLabel : public Label {
void OnEnabledChanged();
void SetColorForEnableState();

absl::optional<SkColor> requested_disabled_color_;
absl::optional<SkColor> requested_enabled_color_;
absl::variant<absl::monostate, SkColor, ui::ColorId> requested_enabled_color_;
absl::variant<absl::monostate, SkColor, ui::ColorId>
requested_disabled_color_;
base::CallbackListSubscription enabled_changed_subscription_ =
AddEnabledChangedCallback(
base::BindRepeating(&LabelButtonLabel::OnEnabledChanged,
base::Unretained(this)));
};

BEGIN_VIEW_BUILDER(VIEWS_EXPORT, LabelButtonLabel, Label)
VIEW_BUILDER_PROPERTY(absl::optional<ui::ColorId>, EnabledColorId)
VIEW_BUILDER_PROPERTY(absl::optional<ui::ColorId>, DisabledColorId)
END_VIEW_BUILDER

} // namespace views::internal

DEFINE_VIEW_BUILDER(VIEWS_EXPORT, internal::LabelButtonLabel)

#endif // UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_LABEL_H_

0 comments on commit 69ee26f

Please sign in to comment.