Commit 71ff7c1d authored by Kristyn Hamasaki's avatar Kristyn Hamasaki Committed by Commit Bot

Convert more Textfield fields to properties

Change-Id: Ic62f3e022d648bcfb9e23d0928d6efd118d78bb2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1726814Reviewed-by: default avatarAllen Bauer <kylixrd@chromium.org>
Commit-Queue: Kristyn Hamasaki <khamasaki@google.com>
Cr-Commit-Position: refs/heads/master@{#684132}
parent 06caef1b
......@@ -84,6 +84,20 @@ namespace views {
namespace {
// An enum giving different model properties unique keys for the
// OnPropertyChanged call.
enum TextfieldPropertyKey {
kTextfieldText = 1,
kTextfieldTextColor,
kTextfieldSelectionTextColor,
kTextfieldBackgroundColor,
kTextfieldSelectionBackgroundColor,
kTextfieldCursorEnabled,
kTextfieldHorizontalAlignment,
kTextfieldSelectedRange,
};
#if defined(OS_MACOSX)
constexpr gfx::SelectionBehavior kLineSelectionBehavior = gfx::SELECTION_EXTEND;
constexpr gfx::SelectionBehavior kWordSelectionBehavior = gfx::SELECTION_CARET;
......@@ -447,6 +461,9 @@ SkColor Textfield::GetBackgroundColor() const {
}
void Textfield::SetBackgroundColor(SkColor color) {
if (background_color_ == color)
return;
background_color_ = color;
use_default_background_color_ = false;
UpdateBackgroundColor();
......@@ -465,16 +482,20 @@ SkColor Textfield::GetSelectionTextColor() const {
}
void Textfield::SetSelectionTextColor(SkColor color) {
if (selection_text_color_ == color)
return;
selection_text_color_ = color;
use_default_selection_text_color_ = false;
GetRenderText()->set_selection_color(GetSelectionTextColor());
SchedulePaint();
UpdateSelectionTextColor();
}
void Textfield::UseDefaultSelectionTextColor() {
if (use_default_selection_text_color_ == true)
return;
use_default_selection_text_color_ = true;
GetRenderText()->set_selection_color(GetSelectionTextColor());
SchedulePaint();
UpdateSelectionTextColor();
}
SkColor Textfield::GetSelectionBackgroundColor() const {
......@@ -486,18 +507,20 @@ SkColor Textfield::GetSelectionBackgroundColor() const {
}
void Textfield::SetSelectionBackgroundColor(SkColor color) {
if (selection_background_color_ == color)
return;
selection_background_color_ = color;
use_default_selection_background_color_ = false;
GetRenderText()->set_selection_background_focused_color(
GetSelectionBackgroundColor());
SchedulePaint();
UpdateSelectionBackgroundColor();
}
void Textfield::UseDefaultSelectionBackgroundColor() {
if (use_default_selection_background_color_ == true)
return;
use_default_selection_background_color_ = true;
GetRenderText()->set_selection_background_focused_color(
GetSelectionBackgroundColor());
SchedulePaint();
UpdateSelectionBackgroundColor();
}
bool Textfield::GetCursorEnabled() const {
......@@ -511,6 +534,7 @@ void Textfield::SetCursorEnabled(bool enabled) {
GetRenderText()->SetCursorEnabled(enabled);
UpdateCursorViewPosition();
UpdateCursorVisibility();
OnPropertyChanged(&model_ + kTextfieldCursorEnabled, kPropertyEffectsPaint);
}
const gfx::FontList& Textfield::GetFontList() const {
......@@ -542,6 +566,7 @@ void Textfield::SetPlaceholderText(const base::string16& text) {
return;
placeholder_text_ = text;
OnPropertyChanged(&placeholder_text_, kPropertyEffectsPaint);
}
gfx::HorizontalAlignment Textfield::GetHorizontalAlignment() const {
......@@ -550,6 +575,8 @@ gfx::HorizontalAlignment Textfield::GetHorizontalAlignment() const {
void Textfield::SetHorizontalAlignment(gfx::HorizontalAlignment alignment) {
GetRenderText()->SetHorizontalAlignment(alignment);
OnPropertyChanged(&model_ + kTextfieldHorizontalAlignment,
kPropertyEffectsNone);
}
void Textfield::ShowVirtualKeyboardIfEnabled() {
......@@ -569,6 +596,7 @@ const gfx::Range& Textfield::GetSelectedRange() const {
void Textfield::SetSelectedRange(const gfx::Range& range) {
model_->SelectRange(range);
UpdateAfterChange(false, true);
OnPropertyChanged(&model_ + kTextfieldSelectedRange, kPropertyEffectsPaint);
}
const gfx::SelectionModel& Textfield::GetSelectionModel() const {
......@@ -587,7 +615,7 @@ size_t Textfield::GetCursorPosition() const {
void Textfield::SetColor(SkColor value) {
GetRenderText()->SetColor(value);
cursor_view_.layer()->SetColor(value);
SchedulePaint();
OnPropertyChanged(&model_ + kTextfieldTextColor, kPropertyEffectsPaint);
}
void Textfield::ApplyColor(SkColor value, const gfx::Range& range) {
......@@ -618,6 +646,7 @@ void Textfield::SetInvalid(bool invalid) {
UpdateBorder();
if (focus_ring_)
focus_ring_->SetInvalid(invalid);
OnPropertyChanged(&invalid_, kPropertyEffectsNone);
}
void Textfield::ClearEditHistory() {
......@@ -629,7 +658,11 @@ base::string16 Textfield::GetAccessibleName() const {
}
void Textfield::SetAccessibleName(const base::string16& name) {
if (accessible_name_ == name)
return;
accessible_name_ = name;
OnPropertyChanged(&accessible_name_, kPropertyEffectsNone);
}
void Textfield::SetGlyphSpacing(int spacing) {
......@@ -1185,7 +1218,7 @@ gfx::Point Textfield::GetKeyboardContextMenuLocation() {
void Textfield::OnThemeChanged() {
gfx::RenderText* render_text = GetRenderText();
render_text->SetColor(GetTextColor());
SetColor(GetTextColor());
UpdateBackgroundColor();
render_text->set_selection_color(GetSelectionTextColor());
render_text->set_selection_background_focused_color(
......@@ -1202,7 +1235,7 @@ void Textfield::OnCompositionTextConfirmedOrCleared() {
}
void Textfield::OnTextChanged() {
OnPropertyChanged(&model_ + kTextProperty, kPropertyEffectsPaint);
OnPropertyChanged(&model_ + kTextfieldText, kPropertyEffectsPaint);
}
////////////////////////////////////////////////////////////////////////////////
......@@ -2072,7 +2105,7 @@ bool Textfield::ShouldShowPlaceholderText() const {
views::PropertyChangedSubscription Textfield::AddTextChangedCallback(
views::PropertyChangedCallback callback) {
return AddPropertyChangedCallback(&model_ + kTextProperty,
return AddPropertyChangedCallback(&model_ + kTextfieldText,
std::move(callback));
}
......@@ -2166,7 +2199,7 @@ void Textfield::UpdateBackgroundColor() {
// See crbug.com/115198
GetRenderText()->set_subpixel_rendering_suppressed(SkColorGetA(color) !=
SK_AlphaOPAQUE);
SchedulePaint();
OnPropertyChanged(&model_ + kTextfieldBackgroundColor, kPropertyEffectsPaint);
}
void Textfield::UpdateBorder() {
......@@ -2186,6 +2219,19 @@ void Textfield::UpdateBorder() {
View::SetBorder(std::move(border));
}
void Textfield::UpdateSelectionTextColor() {
GetRenderText()->set_selection_color(GetSelectionTextColor());
OnPropertyChanged(&model_ + kTextfieldSelectionTextColor,
kPropertyEffectsPaint);
}
void Textfield::UpdateSelectionBackgroundColor() {
GetRenderText()->set_selection_background_focused_color(
GetSelectionBackgroundColor());
OnPropertyChanged(&model_ + kTextfieldSelectionBackgroundColor,
kPropertyEffectsPaint);
}
void Textfield::UpdateAfterChange(bool text_changed, bool cursor_changed) {
if (text_changed) {
if (controller_)
......@@ -2433,6 +2479,16 @@ ADD_PROPERTY_METADATA(Textfield, bool, ReadOnly)
ADD_PROPERTY_METADATA(Textfield, base::string16, Text)
ADD_PROPERTY_METADATA(Textfield, ui::TextInputType, TextInputType)
ADD_PROPERTY_METADATA(Textfield, int, TextInputFlags)
ADD_PROPERTY_METADATA(Textfield, SkColor, TextColor)
ADD_PROPERTY_METADATA(Textfield, SkColor, SelectionTextColor)
ADD_PROPERTY_METADATA(Textfield, SkColor, BackgroundColor)
ADD_PROPERTY_METADATA(Textfield, SkColor, SelectionBackgroundColor)
ADD_PROPERTY_METADATA(Textfield, bool, CursorEnabled)
ADD_PROPERTY_METADATA(Textfield, base::string16, PlaceholderText)
ADD_PROPERTY_METADATA(Textfield, bool, Invalid)
ADD_PROPERTY_METADATA(Textfield, gfx::HorizontalAlignment, HorizontalAlignment)
ADD_PROPERTY_METADATA(Textfield, gfx::Range, SelectedRange)
ADD_PROPERTY_METADATA(Textfield, base::string16, AccessibleName)
END_METADATA()
} // namespace views
......@@ -73,12 +73,6 @@ class VIEWS_EXPORT Textfield : public View,
public:
METADATA_HEADER(Textfield);
// An enum giving different model properties unique keys for the
// OnPropertyChanged call.
enum ModelPropertyKey {
kTextProperty = 1,
};
// Returns the text cursor blink time, or 0 for no blinking.
static base::TimeDelta GetCaretBlinkInterval();
......@@ -453,6 +447,12 @@ class VIEWS_EXPORT Textfield : public View,
// Updates the border per the state of |invalid_|.
void UpdateBorder();
// Updates the selection text color.
void UpdateSelectionTextColor();
// Updates the selection background color.
void UpdateSelectionBackgroundColor();
// Does necessary updates when the text and/or cursor position changes.
void UpdateAfterChange(bool text_changed, bool cursor_changed);
......
......@@ -72,6 +72,12 @@ base::string16 TypeConverter<gfx::ShadowValues>::ToString(
return ret;
}
base::string16 TypeConverter<gfx::Range>::ToString(
const gfx::Range& source_value) {
return base::ASCIIToUTF16(base::StringPrintf(
"{%i, %i}", source_value.GetMin(), source_value.GetMax()));
}
base::Optional<int8_t> TypeConverter<int8_t>::FromString(
const base::string16& source_value) {
int32_t ret = 0;
......@@ -210,6 +216,19 @@ base::Optional<gfx::ShadowValues> TypeConverter<gfx::ShadowValues>::FromString(
return ret;
}
base::Optional<gfx::Range> TypeConverter<gfx::Range>::FromString(
const base::string16& source_value) {
const auto values =
base::SplitStringPiece(source_value, base::ASCIIToUTF16("{,}"),
base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
int min, max;
if ((values.size() == 2) && base::StringToInt(values[0], &min) &&
base::StringToInt(values[1], &max)) {
return gfx::Range(min, max);
}
return base::nullopt;
}
} // namespace metadata
} // namespace views
......
......@@ -16,6 +16,7 @@
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/range/range.h"
#include "ui/gfx/shadow_value.h"
#include "ui/gfx/text_constants.h"
#include "ui/views/views_export.h"
......@@ -116,6 +117,7 @@ DECLARE_CONVERSIONS(gfx::Size)
DECLARE_CONVERSIONS(base::string16)
DECLARE_CONVERSIONS(const char*)
DECLARE_CONVERSIONS(gfx::ShadowValues)
DECLARE_CONVERSIONS(gfx::Range)
#undef DECLARE_CONVERSIONS
......
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