diff options
Diffstat (limited to 'ui')
-rw-r--r-- | ui/views/controls/button/label_button.cc | 38 | ||||
-rw-r--r-- | ui/views/controls/button/label_button.h | 8 | ||||
-rw-r--r-- | ui/views/controls/button/text_button.cc | 115 | ||||
-rw-r--r-- | ui/views/controls/button/text_button.h | 46 | ||||
-rw-r--r-- | ui/views/native_theme_delegate.h | 2 | ||||
-rw-r--r-- | ui/views/view.cc | 17 | ||||
-rw-r--r-- | ui/views/view.h | 17 |
7 files changed, 183 insertions, 60 deletions
diff --git a/ui/views/controls/button/label_button.cc b/ui/views/controls/button/label_button.cc index a2c77fe0..1de7867 100644 --- a/ui/views/controls/button/label_button.cc +++ b/ui/views/controls/button/label_button.cc @@ -7,6 +7,7 @@ #include "base/logging.h" #include "grit/ui_resources.h" #include "ui/base/animation/throb_animation.h" +#include "ui/base/native_theme/native_theme.h" #include "ui/base/resource/resource_bundle.h" #include "ui/views/controls/button/label_button_border.h" #include "ui/views/focus_border.h" @@ -77,6 +78,7 @@ void LabelButton::SetTextColor(ButtonState for_state, SkColor color) { label_->SetDisabledColor(color); else if (for_state == state()) label_->SetEnabledColor(color); + explicitly_set_colors_[for_state] = true; } bool LabelButton::GetTextMultiLine() const { @@ -115,8 +117,14 @@ void LabelButton::SetDefaultButton(bool default_button) { void LabelButton::SetNativeTheme(bool native_theme) { native_theme_ = native_theme; static_cast<LabelButtonBorder*>(border())->set_native_theme(native_theme); + // Invalidate the layout to pickup the new insets from the border. + InvalidateLayout(); + + ResetColorsFromNativeTheme(true); +} - const ui::NativeTheme* theme = ui::NativeTheme::instance(); +void LabelButton::ResetColorsFromNativeTheme(bool reset_all) { + const ui::NativeTheme* theme = GetNativeTheme(); SkColor colors[BS_COUNT] = { theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonEnabledColor), theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonHoverColor), @@ -124,18 +132,19 @@ void LabelButton::SetNativeTheme(bool native_theme) { theme->GetSystemColor(ui::NativeTheme::kColorId_TextButtonDisabledColor), }; #if defined(OS_WIN) - if (native_theme) { + if (native_theme_ && theme == ui::NativeThemeWin::instance()) { colors[BS_NORMAL] = color_utils::GetSysSkColor(COLOR_BTNTEXT); colors[BS_HOT] = color_utils::GetSysSkColor(COLOR_BTNTEXT); colors[BS_PUSHED] = color_utils::GetSysSkColor(COLOR_BTNTEXT); colors[BS_DISABLED] = color_utils::GetSysSkColor(COLOR_GRAYTEXT); } #endif - for (size_t state = BS_NORMAL; state < BS_COUNT; ++state) - SetTextColor(static_cast<ButtonState>(state), colors[state]); - - // Invalidate the layout to pickup the new insets from the border. - InvalidateLayout(); + for (size_t state = BS_NORMAL; state < BS_COUNT; ++state) { + if (reset_all || !explicitly_set_colors_[state]) { + SetTextColor(static_cast<ButtonState>(state), colors[state]); + explicitly_set_colors_[state] = false; + } + } } void LabelButton::StateChanged() { @@ -215,6 +224,11 @@ void LabelButton::ChildPreferredSizeChanged(View* child) { PreferredSizeChanged(); } +void LabelButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { + if (native_theme_) + ResetColorsFromNativeTheme(false); +} + ui::NativeTheme::Part LabelButton::GetThemePart() const { return ui::NativeTheme::kPushButton; } @@ -237,9 +251,11 @@ ui::NativeTheme::State LabelButton::GetThemeState( } const ui::Animation* LabelButton::GetThemeAnimation() const { -#if defined(OS_WIN) && !defined(USE_AURA) - if (native_theme() && !ui::NativeThemeWin::instance()->IsThemingActive()) - return NULL; +#if defined(OS_WIN) + if (native_theme_ && GetNativeTheme() == ui::NativeThemeWin::instance()) { + return ui::NativeThemeWin::instance()->IsThemingActive() ? + hover_animation_.get() : NULL; + } #endif return hover_animation_.get(); } @@ -263,7 +279,7 @@ void LabelButton::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { params->button.is_focused = HasFocus() && IsAccessibilityFocusable(); params->button.has_border = native_theme(); params->button.classic_state = 0; - params->button.background_color = ui::NativeTheme::instance()->GetSystemColor( + params->button.background_color = GetNativeTheme()->GetSystemColor( ui::NativeTheme::kColorId_TextButtonBackgroundColor); } diff --git a/ui/views/controls/button/label_button.h b/ui/views/controls/button/label_button.h index e328dc5..09a89c9 100644 --- a/ui/views/controls/button/label_button.h +++ b/ui/views/controls/button/label_button.h @@ -67,6 +67,10 @@ class VIEWS_EXPORT LabelButton : public CustomButton, FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, LabelAndImage); FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Font); + // Resets the colors from the NativeTheme. If |reset_all| is true all colors + // are reset, otherwise only those not explicitly set are changed. + void ResetColorsFromNativeTheme(bool reset_all); + // Overridden from CustomButton: virtual void StateChanged() OVERRIDE; @@ -75,6 +79,7 @@ class VIEWS_EXPORT LabelButton : public CustomButton, virtual void Layout() OVERRIDE; virtual std::string GetClassName() const OVERRIDE; virtual void ChildPreferredSizeChanged(View* child) OVERRIDE; + virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE; // Overridden from NativeThemeDelegate: virtual ui::NativeTheme::Part GetThemePart() const OVERRIDE; @@ -98,6 +103,9 @@ class VIEWS_EXPORT LabelButton : public CustomButton, gfx::ImageSkia button_state_images_[BS_COUNT]; SkColor button_state_colors_[BS_COUNT]; + // Used to track whether SetTextColor() has been invoked. + bool explicitly_set_colors_[BS_COUNT]; + // |min_size_| increases monotonically with the preferred size. gfx::Size min_size_; // |max_size_| may be set to clamp the preferred size. diff --git a/ui/views/controls/button/text_button.cc b/ui/views/controls/button/text_button.cc index 5c18f808..23f92c0 100644 --- a/ui/views/controls/button/text_button.cc +++ b/ui/views/controls/button/text_button.cc @@ -143,8 +143,8 @@ TextButtonNativeThemeBorder::~TextButtonNativeThemeBorder() { void TextButtonNativeThemeBorder::Paint(const View& view, gfx::Canvas* canvas) const { + const ui::NativeTheme* theme = view.GetNativeTheme(); const TextButtonBase* tb = static_cast<const TextButton*>(&view); - const ui::NativeTheme* native_theme = ui::NativeTheme::instance(); ui::NativeTheme::Part part = delegate_->GetThemePart(); gfx::Rect rect(delegate_->GetThemePaintRect()); @@ -155,20 +155,19 @@ void TextButtonNativeThemeBorder::Paint(const View& view, ui::NativeTheme::ExtraParams prev_extra; ui::NativeTheme::State prev_state = delegate_->GetBackgroundThemeState(&prev_extra); - native_theme->Paint(canvas->sk_canvas(), part, prev_state, rect, - prev_extra); + theme->Paint(canvas->sk_canvas(), part, prev_state, rect, prev_extra); // Composite foreground state above it. ui::NativeTheme::ExtraParams extra; ui::NativeTheme::State state = delegate_->GetForegroundThemeState(&extra); int alpha = delegate_->GetThemeAnimation()->CurrentValueBetween(0, 255); canvas->SaveLayerAlpha(static_cast<uint8>(alpha)); - native_theme->Paint(canvas->sk_canvas(), part, state, rect, extra); + theme->Paint(canvas->sk_canvas(), part, state, rect, extra); canvas->Restore(); } else { ui::NativeTheme::ExtraParams extra; ui::NativeTheme::State state = delegate_->GetThemeState(&extra); - native_theme->Paint(canvas->sk_canvas(), part, state, rect, extra); + theme->Paint(canvas->sk_canvas(), part, state, rect, extra); } } @@ -188,16 +187,6 @@ TextButtonBase::TextButtonBase(ButtonListener* listener, const string16& text) alignment_(ALIGN_LEFT), font_(ResourceBundle::GetSharedInstance().GetFont( ResourceBundle::BaseFont)), - color_(ui::NativeTheme::instance()->GetSystemColor( - ui::NativeTheme::kColorId_TextButtonEnabledColor)), - color_enabled_(ui::NativeTheme::instance()->GetSystemColor( - ui::NativeTheme::kColorId_TextButtonEnabledColor)), - color_disabled_(ui::NativeTheme::instance()->GetSystemColor( - ui::NativeTheme::kColorId_TextButtonDisabledColor)), - color_highlight_(ui::NativeTheme::instance()->GetSystemColor( - ui::NativeTheme::kColorId_TextButtonHighlightColor)), - color_hover_(ui::NativeTheme::instance()->GetSystemColor( - ui::NativeTheme::kColorId_TextButtonHoverColor)), has_text_shadow_(false), active_text_shadow_color_(0), inactive_text_shadow_color_(0), @@ -208,7 +197,21 @@ TextButtonBase::TextButtonBase(ButtonListener* listener, const string16& text) show_multiple_icon_states_(true), is_default_(false), multi_line_(false), - prefix_type_(PREFIX_NONE) { + prefix_type_(PREFIX_NONE), + color_(ui::NativeTheme::instance()->GetSystemColor( + ui::NativeTheme::kColorId_TextButtonEnabledColor)), + color_enabled_(ui::NativeTheme::instance()->GetSystemColor( + ui::NativeTheme::kColorId_TextButtonEnabledColor)), + color_disabled_(ui::NativeTheme::instance()->GetSystemColor( + ui::NativeTheme::kColorId_TextButtonDisabledColor)), + color_highlight_(ui::NativeTheme::instance()->GetSystemColor( + ui::NativeTheme::kColorId_TextButtonHighlightColor)), + color_hover_(ui::NativeTheme::instance()->GetSystemColor( + ui::NativeTheme::kColorId_TextButtonHoverColor)), + use_enabled_color_from_theme_(true), + use_disabled_color_from_theme_(true), + use_highlight_color_from_theme_(true), + use_hover_color_from_theme_(true) { SetText(text); SetAnimationDuration(kHoverAnimationDurationMs); } @@ -242,20 +245,24 @@ void TextButtonBase::SetFont(const gfx::Font& font) { void TextButtonBase::SetEnabledColor(SkColor color) { color_enabled_ = color; + use_enabled_color_from_theme_ = false; UpdateColor(); } void TextButtonBase::SetDisabledColor(SkColor color) { color_disabled_ = color; + use_disabled_color_from_theme_ = false; UpdateColor(); } void TextButtonBase::SetHighlightColor(SkColor color) { color_highlight_ = color; + use_highlight_color_from_theme_ = false; } void TextButtonBase::SetHoverColor(SkColor color) { color_hover_ = color; + use_hover_color_from_theme_ = false; } void TextButtonBase::SetTextShadowColors(SkColor active_color, @@ -517,6 +524,26 @@ std::string TextButtonBase::GetClassName() const { return kViewClassName; } +void TextButtonBase::OnNativeThemeChanged(const ui::NativeTheme* theme) { + if (use_enabled_color_from_theme_) { + color_enabled_ = theme->GetSystemColor( + ui::NativeTheme::kColorId_TextButtonEnabledColor); + } + if (use_disabled_color_from_theme_) { + color_disabled_ = theme->GetSystemColor( + ui::NativeTheme::kColorId_TextButtonDisabledColor); + } + if (use_highlight_color_from_theme_) { + color_highlight_ = theme->GetSystemColor( + ui::NativeTheme::kColorId_TextButtonHighlightColor); + } + if (use_hover_color_from_theme_) { + color_hover_ = theme->GetSystemColor( + ui::NativeTheme::kColorId_TextButtonHoverColor); + } + UpdateColor(); +} + //////////////////////////////////////////////////////////////////////////////// // TextButtonBase, NativeThemeDelegate overrides: @@ -543,14 +570,13 @@ ui::NativeTheme::State TextButtonBase::GetThemeState( } const ui::Animation* TextButtonBase::GetThemeAnimation() const { -#if defined(USE_AURA) - return hover_animation_.get(); -#elif defined(OS_WIN) - return ui::NativeThemeWin::instance()->IsThemingActive() - ? hover_animation_.get() : NULL; -#else - return hover_animation_.get(); +#if defined(OS_WIN) + if (GetNativeTheme() == ui::NativeThemeWin::instance()) { + return ui::NativeThemeWin::instance()->IsThemingActive() ? + hover_animation_.get() : NULL; + } #endif + return hover_animation_.get(); } ui::NativeTheme::State TextButtonBase::GetBackgroundThemeState( @@ -732,12 +758,7 @@ NativeTextButton::NativeTextButton(ButtonListener* listener, } void NativeTextButton::Init() { -#if defined(OS_WIN) - // Use applicable Windows system colors. - color_enabled_ = skia::COLORREFToSkColor(GetSysColor(COLOR_BTNTEXT)); - color_disabled_ = skia::COLORREFToSkColor(GetSysColor(COLOR_GRAYTEXT)); - color_hover_ = color_ = color_enabled_; -#endif + SetThemeSpecificState(GetNativeTheme()); set_border(new TextButtonNativeThemeBorder(this)); #if !defined(OS_WIN) // Paint nothing, focus will be indicated with a border highlight drawn by @@ -757,18 +778,46 @@ std::string NativeTextButton::GetClassName() const { return kViewClassName; } +void NativeTextButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { + SetThemeSpecificState(theme); +} + +void NativeTextButton::SetThemeSpecificState(const ui::NativeTheme* theme) { +#if defined(OS_WIN) + if (theme == ui::NativeThemeWin::instance()) { + if (use_enabled_color_from_theme()) + set_color_enabled(skia::COLORREFToSkColor(GetSysColor(COLOR_BTNTEXT))); + if (use_disabled_color_from_theme()) + set_color_disabled(skia::COLORREFToSkColor(GetSysColor(COLOR_GRAYTEXT))); + if (use_hover_color_from_theme()) + set_color_hover(skia::COLORREFToSkColor(GetSysColor(COLOR_BTNTEXT))); + UpdateColor(); + set_focus_border(FocusBorder::CreateDashedFocusBorder(kFocusRectInset, + kFocusRectInset, + kFocusRectInset, + kFocusRectInset)); + } else { + // Paint nothing, focus will be indicated with a border highlight drawn by + // NativeThemeBase::PaintButton. + set_focus_border(NULL); + } +#endif +} + void NativeTextButton::GetExtraParams( ui::NativeTheme::ExtraParams* params) const { TextButton::GetExtraParams(params); params->button.has_border = true; -#if !defined(OS_WIN) // Windows may paint a dotted focus rect in // NativeTextButton::OnPaintFocusBorder. To avoid getting two focus - // indications (A dotted rect and a highlighted border) only set is_focused on - // non windows platforms. + // indications (A dotted rect and a highlighted border) only set |is_focused| + // when not using NativeThemeWin. +#if defined(OS_WIN) + if (GetNativeTheme() == ui::NativeThemeWin::instance()) + return; +#endif params->button.is_focused = HasFocus() && (focusable() || IsAccessibilityFocusable()); -#endif } } // namespace views diff --git a/ui/views/controls/button/text_button.h b/ui/views/controls/button/text_button.h index 1a0ffa6..4146be2 100644 --- a/ui/views/controls/button/text_button.h +++ b/ui/views/controls/button/text_button.h @@ -195,6 +195,7 @@ class VIEWS_EXPORT TextButtonBase : public CustomButton, virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; virtual std::string GetClassName() const OVERRIDE; + virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE; protected: TextButtonBase(ButtonListener* listener, const string16& text); @@ -210,6 +211,22 @@ class VIEWS_EXPORT TextButtonBase : public CustomButton, // Calculate the size of the text size without setting any of the members. void CalculateTextSize(gfx::Size* text_size, int max_width); + void set_color_enabled(SkColor color) { color_enabled_ = color; } + void set_color_disabled(SkColor color) { color_disabled_ = color; } + void set_color_hover(SkColor color) { color_hover_ = color; } + + bool use_enabled_color_from_theme() const { + return use_enabled_color_from_theme_; + } + + bool use_disabled_color_from_theme() const { + return use_disabled_color_from_theme_; + } + + bool use_hover_color_from_theme() const { + return use_hover_color_from_theme_; + } + // Overridden from NativeThemeDelegate: virtual gfx::Rect GetThemePaintRect() const OVERRIDE; virtual ui::NativeTheme::State GetThemeState( @@ -247,15 +264,6 @@ class VIEWS_EXPORT TextButtonBase : public CustomButton, // The font used to paint the text. gfx::Font font_; - // Text color. - SkColor color_; - - // State colors. - SkColor color_enabled_; - SkColor color_disabled_; - SkColor color_highlight_; - SkColor color_hover_; - // Flag indicating if a shadow should be drawn behind the text. bool has_text_shadow_; // Optional shadow text colors for active and inactive widget states. @@ -284,6 +292,22 @@ class VIEWS_EXPORT TextButtonBase : public CustomButton, PrefixType prefix_type_; + private: + // Text color. + SkColor color_; + + // State colors. + SkColor color_enabled_; + SkColor color_disabled_; + SkColor color_highlight_; + SkColor color_hover_; + + // True if the specified color should be used from the theme. + bool use_enabled_color_from_theme_; + bool use_disabled_color_from_theme_; + bool use_highlight_color_from_theme_; + bool use_hover_color_from_theme_; + DISALLOW_COPY_AND_ASSIGN(TextButtonBase); }; @@ -395,10 +419,14 @@ class VIEWS_EXPORT NativeTextButton : public TextButton { // Overridden from TextButton: virtual gfx::Size GetMinimumSize() OVERRIDE; virtual std::string GetClassName() const OVERRIDE; + virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE; private: void Init(); + // Sets the necessary theme specific state from |theme|. + void SetThemeSpecificState(const ui::NativeTheme* theme); + // Overridden from TextButton: virtual void GetExtraParams( ui::NativeTheme::ExtraParams* params) const OVERRIDE; diff --git a/ui/views/native_theme_delegate.h b/ui/views/native_theme_delegate.h index b14a62a..1cdd7ce 100644 --- a/ui/views/native_theme_delegate.h +++ b/ui/views/native_theme_delegate.h @@ -15,7 +15,7 @@ namespace views { // theme states. This delegate can be used to control a native theme Border // or Painter object. // -// If animation is onging, the native theme border or painter will +// If animation is ongoing, the native theme border or painter will // composite the foreground state over the backgroud state using an alpha // between 0 and 255 based on the current value of the animation. class VIEWS_EXPORT NativeThemeDelegate { diff --git a/ui/views/view.cc b/ui/views/view.cc index 46993d1..71b8acb 100644 --- a/ui/views/view.cc +++ b/ui/views/view.cc @@ -15,6 +15,7 @@ #include "third_party/skia/include/core/SkRect.h" #include "ui/base/accessibility/accessibility_types.h" #include "ui/base/dragdrop/drag_drop_types.h" +#include "ui/base/native_theme/native_theme.h" #include "ui/compositor/compositor.h" #include "ui/compositor/layer.h" #include "ui/compositor/layer_animator.h" @@ -162,6 +163,7 @@ void View::AddChildViewAt(View* view, int index) { // If |view| has a parent, remove it from its parent. View* parent = view->parent_; + const ui::NativeTheme* old_theme = view->GetNativeTheme(); if (parent) { if (parent == this) { ReorderChildView(view, index); @@ -182,8 +184,13 @@ void View::AddChildViewAt(View* view, int index) { view->PropagateAddNotifications(this, view); UpdateTooltip(); - if (GetWidget()) + views::Widget* widget = GetWidget(); + if (widget) { RegisterChildrenForVisibleBoundsNotification(view); + const ui::NativeTheme* new_theme = widget->GetNativeTheme(); + if (new_theme != old_theme) + PropagateNativeThemeChanged(new_theme); + } if (layout_manager_.get()) layout_manager_->ViewAdded(this, view); @@ -736,7 +743,7 @@ ui::ThemeProvider* View::GetThemeProvider() const { const ui::NativeTheme* View::GetNativeTheme() const { const Widget* widget = GetWidget(); - return widget ? widget->GetNativeTheme() : NULL; + return widget ? widget->GetNativeTheme() : ui::NativeTheme::instance(); } // Accelerated Painting -------------------------------------------------------- @@ -1660,6 +1667,12 @@ void View::ViewHierarchyChangedImpl(bool register_accelerators, parent->needs_layout_ = true; } +void View::PropagateNativeThemeChanged(const ui::NativeTheme* theme) { + for (int i = 0, count = child_count(); i < count; ++i) + child_at(i)->PropagateNativeThemeChanged(theme); + OnNativeThemeChanged(theme); +} + // Size and disposition -------------------------------------------------------- void View::PropagateVisibilityNotifications(View* start, bool is_visible) { diff --git a/ui/views/view.h b/ui/views/view.h index ca7658c..63992ae 100644 --- a/ui/views/view.h +++ b/ui/views/view.h @@ -475,7 +475,7 @@ class VIEWS_EXPORT View : public ui::LayerDelegate, // Returns the NativeTheme to use for this View. This calls through to // GetNativeTheme() on the Widget this View is in. If this View is not in a - // Widget this returns NULL. + // Widget this returns ui::NativeTheme::instance(). ui::NativeTheme* GetNativeTheme() { return const_cast<ui::NativeTheme*>( const_cast<const View*>(this)->GetNativeTheme()); @@ -1062,9 +1062,10 @@ class VIEWS_EXPORT View : public ui::LayerDelegate, // System events ------------------------------------------------------------- - // Called when the UI theme has changed, overriding allows individual Views to - // do special cleanup and processing (such as dropping resource caches). - // To dispatch a theme changed notification, call Widget::ThemeChanged(). + // Called when the UI theme (not the NativeTheme) has changed, overriding + // allows individual Views to do special cleanup and processing (such as + // dropping resource caches). To dispatch a theme changed notification, call + // Widget::ThemeChanged(). virtual void OnThemeChanged() {} // Called when the locale has changed, overriding allows individual Views to @@ -1106,6 +1107,11 @@ class VIEWS_EXPORT View : public ui::LayerDelegate, static int GetHorizontalDragThreshold(); static int GetVerticalDragThreshold(); + // NativeTheme --------------------------------------------------------------- + + // Invoked when the NativeTheme associated with this View changes. + virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) {} + // Debugging ----------------------------------------------------------------- #if !defined(NDEBUG) @@ -1195,6 +1201,9 @@ class VIEWS_EXPORT View : public ui::LayerDelegate, View* parent, View* child); + // Invokes OnNativeThemeChanged() on this and all descendants. + void PropagateNativeThemeChanged(const ui::NativeTheme* theme); + // Size and disposition ------------------------------------------------------ // Call VisibilityChanged() recursively for all children. |