diff options
author | msw@chromium.org <msw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-10 22:04:17 +0000 |
---|---|---|
committer | msw@chromium.org <msw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-10 22:04:17 +0000 |
commit | 7f98defa88b14435de270c5e0cc4419c4dba68b9 (patch) | |
tree | 1a22ba756263b83ebb3ea1470cde0514bd48e445 /ui/views/controls | |
parent | 56e691305912ef4fb469c9d0004fc697c3b9a88c (diff) | |
download | chromium_src-7f98defa88b14435de270c5e0cc4419c4dba68b9.zip chromium_src-7f98defa88b14435de270c5e0cc4419c4dba68b9.tar.gz chromium_src-7f98defa88b14435de270c5e0cc4419c4dba68b9.tar.bz2 |
Use native theme colors for textfields; etc.
Make LocationBarView::GetColor use NativeTheme colors.
(except the CrOS transparent Omnibox background color)
Add NativeTheme read-only textfield color/background IDs.
Cache a set of Windows system colors in NativeThemeWin.
Update cached colors via gfx::SysColorChangeListener impl.
Init colors in NativeTextfieldViews, not TextfieldViewsModel.
Refactor Textfield and NativeTextfieldViews color code.
Nix frivolous AutocompleteTextfield::PaintChildren OVERRIDE.
Nix Textfield cursor color settings and visibility kludge.
(replace with proper cursor enabled accessors for views)
TODO(followup): Fix related existing issues (not affected here):
(existing textfields don't update with system color changes)
(bubbles seem to cache the system colors at startup...)
BUG=134766
TEST=Views textfields are created with the current system colors.
R=sky@chromium.org,varunjain@chromium.org,samarth@chromium.org
TBR=marja@chromium.org
Review URL: https://chromiumcodereview.appspot.com/11421204
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@172142 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/views/controls')
-rw-r--r-- | ui/views/controls/textfield/native_textfield_views.cc | 69 | ||||
-rw-r--r-- | ui/views/controls/textfield/native_textfield_views.h | 6 | ||||
-rw-r--r-- | ui/views/controls/textfield/native_textfield_win.cc | 29 | ||||
-rw-r--r-- | ui/views/controls/textfield/native_textfield_win.h | 3 | ||||
-rw-r--r-- | ui/views/controls/textfield/native_textfield_wrapper.h | 7 | ||||
-rw-r--r-- | ui/views/controls/textfield/textfield.cc | 37 | ||||
-rw-r--r-- | ui/views/controls/textfield/textfield.h | 36 | ||||
-rw-r--r-- | ui/views/controls/textfield/textfield_views_model.cc | 11 |
8 files changed, 85 insertions, 113 deletions
diff --git a/ui/views/controls/textfield/native_textfield_views.cc b/ui/views/controls/textfield/native_textfield_views.cc index 8fc0fc5..da216a9 100644 --- a/ui/views/controls/textfield/native_textfield_views.cc +++ b/ui/views/controls/textfield/native_textfield_views.cc @@ -47,9 +47,6 @@ namespace { -// Text color for read only. -const SkColor kReadonlyTextColor = SK_ColorDKGRAY; - // Default "system" color for text cursor. const SkColor kDefaultCursorColor = SK_ColorBLACK; @@ -82,12 +79,8 @@ NativeTextfieldViews::NativeTextfieldViews(Textfield* parent) #else GetRenderText()->SetFont(textfield_->font()); #endif - // Set the default text style. - gfx::StyleRange default_style; - default_style.foreground = textfield_->text_color(); - GetRenderText()->set_default_style(default_style); - GetRenderText()->ApplyDefaultStyle(); + UpdateColorsFromTheme(GetNativeTheme()); set_context_menu_controller(this); set_drag_controller(this); } @@ -292,15 +285,7 @@ void NativeTextfieldViews::OnBlur() { } void NativeTextfieldViews::OnNativeThemeChanged(const ui::NativeTheme* theme) { - gfx::RenderText* render_text = GetRenderText(); - render_text->set_selection_color( - theme->GetSystemColor(ui::NativeTheme::kColorId_TextfieldSelectionColor)); - render_text->set_selection_background_focused_color( - theme->GetSystemColor( - ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)); - render_text->set_selection_background_unfocused_color( - theme->GetSystemColor( - ui::NativeTheme::kColorId_TextfieldSelectionBackgroundUnfocused)); + UpdateColorsFromTheme(theme); } void NativeTextfieldViews::SelectRect(const gfx::Point& start, @@ -442,29 +427,21 @@ void NativeTextfieldViews::UpdateBorder() { } void NativeTextfieldViews::UpdateTextColor() { + gfx::StyleRange default_style(GetRenderText()->default_style()); + default_style.foreground = textfield_->GetTextColor(); + GetRenderText()->set_default_style(default_style); + GetRenderText()->ApplyDefaultStyle(); SchedulePaint(); } void NativeTextfieldViews::UpdateBackgroundColor() { - // TODO(oshima): Background has to match the border's shape. - set_background( - Background::CreateSolidBackground(textfield_->background_color())); - SchedulePaint(); -} - -void NativeTextfieldViews::UpdateCursorColor() { + const SkColor color = textfield_->GetBackgroundColor(); + set_background(Background::CreateSolidBackground(color)); + GetRenderText()->set_background_is_transparent(SkColorGetA(color) != 0xFF); SchedulePaint(); } void NativeTextfieldViews::UpdateReadOnly() { - // Update the default text style. - gfx::StyleRange default_style(GetRenderText()->default_style()); - default_style.foreground = textfield_->read_only() ? kReadonlyTextColor : - textfield_->text_color(); - GetRenderText()->set_default_style(default_style); - GetRenderText()->ApplyDefaultStyle(); - - SchedulePaint(); OnTextInputTypeChanged(); } @@ -559,6 +536,14 @@ size_t NativeTextfieldViews::GetCursorPosition() const { return model_->GetCursorPosition(); } +bool NativeTextfieldViews::GetCursorEnabled() const { + return GetRenderText()->cursor_enabled(); +} + +void NativeTextfieldViews::SetCursorEnabled(bool enabled) { + GetRenderText()->SetCursorEnabled(enabled); +} + bool NativeTextfieldViews::HandleKeyPressed(const ui::KeyEvent& e) { TextfieldController* controller = textfield_->GetController(); bool handled = false; @@ -932,6 +917,19 @@ string16 NativeTextfieldViews::GetTextForDisplay(const string16& text) { base::i18n::ToLower(text) : text; } +void NativeTextfieldViews::UpdateColorsFromTheme(const ui::NativeTheme* theme) { + UpdateTextColor(); + UpdateBackgroundColor(); + gfx::RenderText* render_text = GetRenderText(); + render_text->set_cursor_color(kDefaultCursorColor); + render_text->set_selection_color(theme->GetSystemColor( + ui::NativeTheme::kColorId_TextfieldSelectionColor)); + render_text->set_selection_background_focused_color(theme->GetSystemColor( + ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)); + render_text->set_selection_background_unfocused_color(theme->GetSystemColor( + ui::NativeTheme::kColorId_TextfieldSelectionBackgroundUnfocused)); +} + void NativeTextfieldViews::UpdateCursor() { is_cursor_visible_ = !is_cursor_visible_; RepaintCursor(); @@ -951,15 +949,8 @@ void NativeTextfieldViews::RepaintCursor() { void NativeTextfieldViews::PaintTextAndCursor(gfx::Canvas* canvas) { TRACE_EVENT0("views", "NativeTextfieldViews::PaintTextAndCursor"); canvas->Save(); - GetRenderText()->set_background_is_transparent( - !textfield_->use_default_background_color() && - SkColorGetA(textfield_->background_color()) != 0xFF); GetRenderText()->set_cursor_visible(is_drop_cursor_visible_ || (is_cursor_visible_ && !model_->HasSelection())); - GetRenderText()->set_cursor_color( - textfield_->use_default_cursor_color() ? - kDefaultCursorColor : - textfield_->cursor_color()); // Draw the text, cursor, and selection. GetRenderText()->Draw(canvas); diff --git a/ui/views/controls/textfield/native_textfield_views.h b/ui/views/controls/textfield/native_textfield_views.h index 822bab8..104f881 100644 --- a/ui/views/controls/textfield/native_textfield_views.h +++ b/ui/views/controls/textfield/native_textfield_views.h @@ -106,7 +106,6 @@ class VIEWS_EXPORT NativeTextfieldViews : public TouchSelectionClientView, virtual void UpdateBorder() OVERRIDE; virtual void UpdateTextColor() OVERRIDE; virtual void UpdateBackgroundColor() OVERRIDE; - virtual void UpdateCursorColor() OVERRIDE; virtual void UpdateReadOnly() OVERRIDE; virtual void UpdateFont() OVERRIDE; virtual void UpdateIsObscured() OVERRIDE; @@ -123,6 +122,8 @@ class VIEWS_EXPORT NativeTextfieldViews : public TouchSelectionClientView, virtual void GetSelectionModel(gfx::SelectionModel* sel) const OVERRIDE; virtual void SelectSelectionModel(const gfx::SelectionModel& sel) OVERRIDE; virtual size_t GetCursorPosition() const OVERRIDE; + virtual bool GetCursorEnabled() const OVERRIDE; + virtual void SetCursorEnabled(bool enabled) OVERRIDE; virtual bool HandleKeyPressed(const ui::KeyEvent& e) OVERRIDE; virtual bool HandleKeyReleased(const ui::KeyEvent& e) OVERRIDE; virtual void HandleFocus() OVERRIDE; @@ -191,6 +192,9 @@ class VIEWS_EXPORT NativeTextfieldViews : public TouchSelectionClientView, // |textfield_| has STYLE_LOWERCASE style. string16 GetTextForDisplay(const string16& text); + // Updates any colors that have not been explicitly set from the theme. + void UpdateColorsFromTheme(const ui::NativeTheme* theme); + // A callback function to periodically update the cursor state. void UpdateCursor(); diff --git a/ui/views/controls/textfield/native_textfield_win.cc b/ui/views/controls/textfield/native_textfield_win.cc index 916b19d..7566baf 100644 --- a/ui/views/controls/textfield/native_textfield_win.cc +++ b/ui/views/controls/textfield/native_textfield_win.cc @@ -249,25 +249,13 @@ void NativeTextfieldWin::UpdateBorder() { void NativeTextfieldWin::UpdateTextColor() { CHARFORMAT cf = {0}; cf.dwMask = CFM_COLOR; - cf.crTextColor = textfield_->use_default_text_color() ? - GetSysColor(textfield_->read_only() ? COLOR_GRAYTEXT : COLOR_WINDOWTEXT) : - skia::SkColorToCOLORREF(textfield_->text_color()); + cf.crTextColor = skia::SkColorToCOLORREF(textfield_->GetTextColor()); CRichEditCtrl::SetDefaultCharFormat(cf); } void NativeTextfieldWin::UpdateBackgroundColor() { - if (!textfield_->use_default_background_color()) { - bg_color_ = skia::SkColorToCOLORREF(textfield_->background_color()); - } else { - bg_color_ = GetSysColor(textfield_->read_only() ? COLOR_3DFACE - : COLOR_WINDOW); - } - CRichEditCtrl::SetBackgroundColor(bg_color_); -} - -void NativeTextfieldWin::UpdateCursorColor() { - if (!textfield_->use_default_cursor_color()) - NOTIMPLEMENTED(); + CRichEditCtrl::SetBackgroundColor( + skia::SkColorToCOLORREF(textfield_->GetBackgroundColor())); } void NativeTextfieldWin::UpdateReadOnly() { @@ -387,6 +375,17 @@ size_t NativeTextfieldWin::GetCursorPosition() const { return 0U; } +bool NativeTextfieldWin::GetCursorEnabled() const { + // TODO(msw): Implement. + NOTIMPLEMENTED(); + return true; +} + +void NativeTextfieldWin::SetCursorEnabled(bool enabled) { + // TODO(msw): Implement. + NOTIMPLEMENTED(); +} + bool NativeTextfieldWin::HandleKeyPressed(const ui::KeyEvent& event) { return false; } diff --git a/ui/views/controls/textfield/native_textfield_win.h b/ui/views/controls/textfield/native_textfield_win.h index fda34dd..d9b205f 100644 --- a/ui/views/controls/textfield/native_textfield_win.h +++ b/ui/views/controls/textfield/native_textfield_win.h @@ -77,7 +77,6 @@ class NativeTextfieldWin virtual void UpdateBorder() OVERRIDE; virtual void UpdateTextColor() OVERRIDE; virtual void UpdateBackgroundColor() OVERRIDE; - virtual void UpdateCursorColor() OVERRIDE; virtual void UpdateReadOnly() OVERRIDE; virtual void UpdateFont() OVERRIDE; virtual void UpdateIsObscured() OVERRIDE; @@ -94,6 +93,8 @@ class NativeTextfieldWin virtual void GetSelectionModel(gfx::SelectionModel* sel) const OVERRIDE; virtual void SelectSelectionModel(const gfx::SelectionModel& sel) OVERRIDE; virtual size_t GetCursorPosition() const OVERRIDE; + virtual bool GetCursorEnabled() const OVERRIDE; + virtual void SetCursorEnabled(bool enabled) OVERRIDE; virtual bool HandleKeyPressed(const ui::KeyEvent& event) OVERRIDE; virtual bool HandleKeyReleased(const ui::KeyEvent& event) OVERRIDE; virtual void HandleFocus() OVERRIDE; diff --git a/ui/views/controls/textfield/native_textfield_wrapper.h b/ui/views/controls/textfield/native_textfield_wrapper.h index f5a6c5d..aaaaa15 100644 --- a/ui/views/controls/textfield/native_textfield_wrapper.h +++ b/ui/views/controls/textfield/native_textfield_wrapper.h @@ -72,9 +72,6 @@ class VIEWS_EXPORT NativeTextfieldWrapper { // Updates the background color used when painting the native text field. virtual void UpdateBackgroundColor() = 0; - // Updates the cursor color used when painting the native text field. - virtual void UpdateCursorColor() = 0; - // Updates the read-only state of the native text field. virtual void UpdateReadOnly() = 0; @@ -124,6 +121,10 @@ class VIEWS_EXPORT NativeTextfieldWrapper { // Returns the currnet cursor position. virtual size_t GetCursorPosition() const = 0; + // Get or set whether or not the cursor is enabled. + virtual bool GetCursorEnabled() const = 0; + virtual void SetCursorEnabled(bool enabled) = 0; + // Following methods are to forward key/focus related events to the // views wrapper so that TextfieldViews can handle key inputs without // having focus. diff --git a/ui/views/controls/textfield/textfield.cc b/ui/views/controls/textfield/textfield.cc index 88f9766..d353101 100644 --- a/ui/views/controls/textfield/textfield.cc +++ b/ui/views/controls/textfield/textfield.cc @@ -17,6 +17,7 @@ #include "ui/base/ui_base_switches.h" #include "ui/gfx/insets.h" #include "ui/gfx/selection_model.h" +#include "ui/native_theme/native_theme.h" #include "ui/views/controls/native/native_view_host.h" #include "ui/views/controls/textfield/native_textfield_views.h" #include "ui/views/controls/textfield/native_textfield_wrapper.h" @@ -65,8 +66,6 @@ Textfield::Textfield() use_default_text_color_(true), background_color_(SK_ColorWHITE), use_default_background_color_(true), - cursor_color_(SK_ColorBLACK), - use_default_cursor_color_(true), initialized_(false), horizontal_margins_were_set_(false), vertical_margins_were_set_(false), @@ -86,8 +85,6 @@ Textfield::Textfield(StyleFlags style) use_default_text_color_(true), background_color_(SK_ColorWHITE), use_default_background_color_(true), - cursor_color_(SK_ColorBLACK), - use_default_cursor_color_(true), initialized_(false), horizontal_margins_were_set_(false), vertical_margins_were_set_(false), @@ -116,7 +113,6 @@ void Textfield::SetReadOnly(bool read_only) { native_wrapper_->UpdateReadOnly(); native_wrapper_->UpdateTextColor(); native_wrapper_->UpdateBackgroundColor(); - native_wrapper_->UpdateCursorColor(); } } @@ -197,6 +193,15 @@ bool Textfield::HasSelection() const { return !range.is_empty(); } +SkColor Textfield::GetTextColor() const { + if (!use_default_text_color_) + return text_color_; + + return GetNativeTheme()->GetSystemColor(read_only() ? + ui::NativeTheme::kColorId_TextfieldReadOnlyColor : + ui::NativeTheme::kColorId_TextfieldDefaultColor); +} + void Textfield::SetTextColor(SkColor color) { text_color_ = color; use_default_text_color_ = false; @@ -210,6 +215,15 @@ void Textfield::UseDefaultTextColor() { native_wrapper_->UpdateTextColor(); } +SkColor Textfield::GetBackgroundColor() const { + if (!use_default_background_color_) + return background_color_; + + return GetNativeTheme()->GetSystemColor(read_only() ? + ui::NativeTheme::kColorId_TextfieldReadOnlyBackground : + ui::NativeTheme::kColorId_TextfieldDefaultBackground); +} + void Textfield::SetBackgroundColor(SkColor color) { background_color_ = color; use_default_background_color_ = false; @@ -223,17 +237,13 @@ void Textfield::UseDefaultBackgroundColor() { native_wrapper_->UpdateBackgroundColor(); } -void Textfield::SetCursorColor(SkColor color) { - cursor_color_ = color; - use_default_cursor_color_ = false; - if (native_wrapper_) - native_wrapper_->UpdateCursorColor(); +bool Textfield::GetCursorEnabled() const { + return native_wrapper_ && native_wrapper_->GetCursorEnabled(); } -void Textfield::UseDefaultCursorColor() { - use_default_cursor_color_ = true; +void Textfield::SetCursorEnabled(bool enabled) { if (native_wrapper_) - native_wrapper_->UpdateCursorColor(); + native_wrapper_->SetCursorEnabled(enabled); } void Textfield::SetFont(const gfx::Font& font) { @@ -289,7 +299,6 @@ void Textfield::UpdateAllProperties() { native_wrapper_->UpdateText(); native_wrapper_->UpdateTextColor(); native_wrapper_->UpdateBackgroundColor(); - native_wrapper_->UpdateCursorColor(); native_wrapper_->UpdateReadOnly(); native_wrapper_->UpdateFont(); native_wrapper_->UpdateEnabled(); diff --git a/ui/views/controls/textfield/textfield.h b/ui/views/controls/textfield/textfield.h index 880c13d..1831073 100644 --- a/ui/views/controls/textfield/textfield.h +++ b/ui/views/controls/textfield/textfield.h @@ -108,36 +108,20 @@ class VIEWS_EXPORT Textfield : public View { StyleFlags style() const { return style_; } // Gets/Sets the text color to be used when painting the Textfield. - // Call |UseDefaultTextColor| to return to the system default colors. - SkColor text_color() const { return text_color_; } + // Call |UseDefaultTextColor| to restore the default system color. + SkColor GetTextColor() const; void SetTextColor(SkColor color); - - // Gets/Sets whether the default text color should be used when painting the - // Textfield. - bool use_default_text_color() const { - return use_default_text_color_; - } void UseDefaultTextColor(); // Gets/Sets the background color to be used when painting the Textfield. - // Call |UseDefaultBackgroundColor| to return to the system default colors. - SkColor background_color() const { return background_color_; } + // Call |UseDefaultBackgroundColor| to restore the default system color. + SkColor GetBackgroundColor() const; void SetBackgroundColor(SkColor color); - - // Gets/Sets whether the default background color should be used when painting - // the Textfield. - bool use_default_background_color() const { - return use_default_background_color_; - } void UseDefaultBackgroundColor(); - // Gets/Sets the color to be used for the cursor. - SkColor cursor_color() const { return cursor_color_; } - void SetCursorColor(SkColor color); - - // Gets/Sets whether we use the system's default color for the cursor. - bool use_default_cursor_color() const { return use_default_cursor_color_; } - void UseDefaultCursorColor(); + // Gets/Sets whether or not the cursor is enabled. + bool GetCursorEnabled() const; + void SetCursorEnabled(bool enabled); // Gets/Sets the font used when rendering the text within the Textfield. const gfx::Font& font() const { return font_; } @@ -303,12 +287,6 @@ class VIEWS_EXPORT Textfield : public View { // Should we use the system background color instead of |background_color_|? bool use_default_background_color_; - // Cursor color. Only used if |use_default_cursor_color_| is false. - SkColor cursor_color_; - - // Should we use the system cursor color instead of |cursor_color_|? - bool use_default_cursor_color_; - // TODO(beng): remove this once NativeTextfieldWin subclasses // NativeControlWin. bool initialized_; diff --git a/ui/views/controls/textfield/textfield_views_model.cc b/ui/views/controls/textfield/textfield_views_model.cc index c779927..8ed8ba0 100644 --- a/ui/views/controls/textfield/textfield_views_model.cc +++ b/ui/views/controls/textfield/textfield_views_model.cc @@ -18,7 +18,6 @@ #include "ui/gfx/font.h" #include "ui/gfx/render_text.h" #include "ui/gfx/text_constants.h" -#include "ui/native_theme/native_theme.h" #include "ui/views/controls/textfield/textfield.h" namespace views { @@ -284,16 +283,6 @@ TextfieldViewsModel::TextfieldViewsModel(Delegate* delegate) : delegate_(delegate), render_text_(gfx::RenderText::CreateInstance()), current_edit_(edit_history_.end()) { - const ui::NativeTheme* theme = ui::NativeTheme::instance(); - render_text_->set_selection_color( - theme->GetSystemColor( - ui::NativeTheme::kColorId_TextfieldSelectionColor)); - render_text_->set_selection_background_focused_color( - theme->GetSystemColor( - ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)); - render_text_->set_selection_background_unfocused_color( - theme->GetSystemColor( - ui::NativeTheme::kColorId_TextfieldSelectionBackgroundUnfocused)); } TextfieldViewsModel::~TextfieldViewsModel() { |