Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/libgtk2ui/gtk2_border.h" | 5 #include "chrome/browser/ui/libgtk2ui/gtk2_border.h" |
| 6 | 6 |
| 7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
| 8 | 8 |
| 9 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.h" | 9 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.h" |
| 10 #include "third_party/skia/include/effects/SkLerpXfermode.h" | |
| 11 #include "ui/gfx/animation/animation.h" | |
| 10 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
| 11 #include "ui/gfx/image/image_skia_source.h" | 13 #include "ui/gfx/image/image_skia_source.h" |
| 12 #include "ui/views/controls/button/custom_button.h" | 14 #include "ui/gfx/rect.h" |
| 15 #include "ui/gfx/skia_util.h" | |
| 16 #include "ui/views/controls/button/label_button.h" | |
| 17 #include "ui/views/controls/button/label_button_border.h" | |
| 18 #include "ui/views/native_theme_delegate.h" | |
| 19 | |
| 20 using views::Button; | |
| 21 using views::NativeThemeDelegate; | |
| 13 | 22 |
| 14 namespace libgtk2ui { | 23 namespace libgtk2ui { |
| 15 | 24 |
| 16 namespace { | 25 namespace { |
| 17 | 26 |
| 18 GtkStateType ViewsToGtkState(views::Button::ButtonState state) { | 27 Button::ButtonState GetViewsButtonState(ui::NativeTheme::State state) { |
|
msw
2014/01/24 00:22:37
nit: Rather than duplicate this label_button_borde
Elliot Glaysher
2014/01/24 21:56:00
Done.
| |
| 19 switch (state) { | 28 switch (state) { |
| 20 case views::Button::STATE_HOVERED: | 29 case ui::NativeTheme::kDisabled: return Button::STATE_DISABLED; |
| 21 return GTK_STATE_PRELIGHT; | 30 case ui::NativeTheme::kHovered: return Button::STATE_HOVERED; |
| 22 case views::Button::STATE_PRESSED: | 31 case ui::NativeTheme::kNormal: return Button::STATE_NORMAL; |
| 23 return GTK_STATE_ACTIVE; | 32 case ui::NativeTheme::kPressed: return Button::STATE_PRESSED; |
| 24 case views::Button::STATE_DISABLED: | 33 case ui::NativeTheme::kMaxState: NOTREACHED() << "Unknown state: " << state; |
| 25 return GTK_STATE_INSENSITIVE; | |
| 26 default: | |
| 27 return GTK_STATE_NORMAL; | |
| 28 } | 34 } |
| 35 return Button::STATE_NORMAL; | |
| 36 } | |
| 37 | |
| 38 GtkStateType GetGtkState(ui::NativeTheme::State state) { | |
| 39 switch (state) { | |
| 40 case ui::NativeTheme::kDisabled: return GTK_STATE_INSENSITIVE; | |
| 41 case ui::NativeTheme::kHovered: return GTK_STATE_PRELIGHT; | |
| 42 case ui::NativeTheme::kNormal: return GTK_STATE_NORMAL; | |
| 43 case ui::NativeTheme::kPressed: return GTK_STATE_ACTIVE; | |
| 44 case ui::NativeTheme::kMaxState: NOTREACHED() << "Unknown state: " << state; | |
| 45 } | |
| 46 return GTK_STATE_NORMAL; | |
| 29 } | 47 } |
| 30 | 48 |
| 31 class ButtonImageSkiaSource : public gfx::ImageSkiaSource { | 49 class ButtonImageSkiaSource : public gfx::ImageSkiaSource { |
| 32 public: | 50 public: |
| 33 ButtonImageSkiaSource(const Gtk2UI* gtk2_ui, | 51 ButtonImageSkiaSource(const Gtk2UI* gtk2_ui, |
| 34 GtkStateType state, | 52 const GtkStateType state, |
| 53 const bool focused, | |
| 35 const gfx::Size& size) | 54 const gfx::Size& size) |
| 36 : gtk2_ui_(gtk2_ui), | 55 : gtk2_ui_(gtk2_ui), |
| 37 state_(state), | 56 state_(state), |
| 57 focused_(focused), | |
| 38 size_(size) { | 58 size_(size) { |
| 39 } | 59 } |
| 40 | 60 |
| 41 virtual ~ButtonImageSkiaSource() { | 61 virtual ~ButtonImageSkiaSource() { |
| 42 } | 62 } |
| 43 | 63 |
| 44 virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE { | 64 virtual gfx::ImageSkiaRep GetImageForScale(float scale) OVERRIDE { |
| 45 int w = size_.width() * scale; | 65 int w = size_.width() * scale; |
| 46 int h = size_.height() * scale; | 66 int h = size_.height() * scale; |
| 47 return gfx::ImageSkiaRep( | 67 return gfx::ImageSkiaRep( |
| 48 gtk2_ui_->DrawGtkButtonBorder(state_, w, h), scale); | 68 gtk2_ui_->DrawGtkButtonBorder(state_, focused_, w, h), scale); |
| 49 } | 69 } |
| 50 | 70 |
| 51 private: | 71 private: |
| 52 const Gtk2UI* gtk2_ui_; | 72 const Gtk2UI* gtk2_ui_; |
| 53 GtkStateType state_; | 73 const GtkStateType state_; |
| 54 gfx::Size size_; | 74 const bool focused_; |
| 75 const gfx::Size size_; | |
| 55 | 76 |
| 56 DISALLOW_COPY_AND_ASSIGN(ButtonImageSkiaSource); | 77 DISALLOW_COPY_AND_ASSIGN(ButtonImageSkiaSource); |
| 57 }; | 78 }; |
| 58 | 79 |
| 59 } // namespace | 80 } // namespace |
| 60 | 81 |
| 61 Gtk2Border::Gtk2Border(Gtk2UI* gtk2_ui, | 82 Gtk2Border::Gtk2Border(Gtk2UI* gtk2_ui, |
| 62 views::CustomButton* owning_button, | 83 views::LabelButton* owning_button, |
| 63 views::Border* border) | 84 views::LabelButtonBorder* border) |
| 64 : gtk2_ui_(gtk2_ui), | 85 : gtk2_ui_(gtk2_ui), |
| 65 use_gtk_(gtk2_ui_->GetUseSystemTheme()), | 86 use_gtk_(gtk2_ui_->GetUseSystemTheme()), |
| 66 owning_button_(owning_button), | 87 owning_button_(owning_button), |
| 67 border_(border) { | 88 border_(border) { |
| 68 gtk2_ui_->AddGtkBorder(this); | 89 gtk2_ui_->AddGtkBorder(this); |
| 69 } | 90 } |
| 70 | 91 |
| 71 Gtk2Border::~Gtk2Border() { | 92 Gtk2Border::~Gtk2Border() { |
| 72 gtk2_ui_->RemoveGtkBorder(this); | 93 gtk2_ui_->RemoveGtkBorder(this); |
| 73 } | 94 } |
| 74 | 95 |
| 75 void Gtk2Border::InvalidateAndSetUsesGtk(bool use_gtk) { | 96 void Gtk2Border::InvalidateAndSetUsesGtk(bool use_gtk) { |
| 76 hovered_ = gfx::ImageSkia(); | 97 for (int i = 0; i < 2; ++i) |
| 77 pressed_ = gfx::ImageSkia(); | 98 for (int j = 0; j < views::Button::STATE_COUNT; ++j) |
| 99 button_images_[i][j] = gfx::ImageSkia(); | |
| 78 | 100 |
| 79 // Our owning view must have its layout invalidated because the insets could | 101 // Our owning view must have its layout invalidated because the insets could |
| 80 // have changed. | 102 // have changed. |
| 81 owning_button_->InvalidateLayout(); | 103 owning_button_->InvalidateLayout(); |
| 82 | 104 |
| 83 use_gtk_ = use_gtk; | 105 use_gtk_ = use_gtk; |
| 84 } | 106 } |
| 85 | 107 |
| 86 void Gtk2Border::Paint(const views::View& view, gfx::Canvas* canvas) { | 108 void Gtk2Border::Paint(const views::View& view, gfx::Canvas* canvas) { |
| 87 if (!use_gtk_) { | 109 if (!use_gtk_) { |
| 88 border_->Paint(view, canvas); | 110 border_->Paint(view, canvas); |
| 89 return; | 111 return; |
| 90 } | 112 } |
| 91 | 113 |
| 92 DCHECK_EQ(&view, owning_button_); | 114 DCHECK_EQ(&view, owning_button_); |
| 93 GtkStateType state = ViewsToGtkState(owning_button_->state()); | 115 const NativeThemeDelegate* native_theme_delegate = owning_button_; |
| 116 gfx::Rect rect(native_theme_delegate->GetThemePaintRect()); | |
|
msw
2014/01/24 00:22:37
Rather than copy this from LabelButtonBorder, does
Elliot Glaysher
2014/01/24 21:56:00
I don't think so. Several people working on views
| |
| 117 ui::NativeTheme::ExtraParams extra; | |
| 118 ui::NativeTheme::State state = native_theme_delegate->GetThemeState(&extra); | |
| 94 | 119 |
| 95 if (state == GTK_STATE_PRELIGHT) { | 120 const gfx::Animation* animation = native_theme_delegate->GetThemeAnimation(); |
| 96 if (hovered_.isNull() || hovered_.size() != view.size()) { | 121 if (animation && animation->is_animating()) { |
| 97 hovered_ = gfx::ImageSkia( | 122 // Linearly interpolate background and foreground painters during |
| 98 new ButtonImageSkiaSource(gtk2_ui_, state, view.size()), view.size()); | 123 // animation. |
|
msw
2014/01/24 00:22:37
nit: fits on line above, just like where this is c
| |
| 99 } | 124 const SkRect sk_rect = gfx::RectToSkRect(rect); |
| 125 canvas->sk_canvas()->saveLayer(&sk_rect, NULL); | |
| 126 state = native_theme_delegate->GetBackgroundThemeState(&extra); | |
| 127 PaintState(state, extra, rect, canvas); | |
| 100 | 128 |
| 101 canvas->DrawImageInt(hovered_, 0, 0); | 129 SkPaint paint; |
| 102 } else if (state == GTK_STATE_ACTIVE) { | 130 skia::RefPtr<SkXfermode> sk_lerp_xfer = |
| 103 if (pressed_.isNull() || pressed_.size() != view.size()) { | 131 skia::AdoptRef(SkLerpXfermode::Create(animation->GetCurrentValue())); |
| 104 pressed_ = gfx::ImageSkia( | 132 paint.setXfermode(sk_lerp_xfer.get()); |
| 105 new ButtonImageSkiaSource(gtk2_ui_, state, view.size()), view.size()); | 133 canvas->sk_canvas()->saveLayer(&sk_rect, &paint); |
| 106 } | 134 state = native_theme_delegate->GetForegroundThemeState(&extra); |
| 135 PaintState(state, extra, rect, canvas); | |
| 136 canvas->sk_canvas()->restore(); | |
| 107 | 137 |
| 108 canvas->DrawImageInt(pressed_, 0, 0); | 138 canvas->sk_canvas()->restore(); |
| 139 } else { | |
| 140 PaintState(state, extra, rect, canvas); | |
| 109 } | 141 } |
| 110 } | 142 } |
| 111 | 143 |
| 112 gfx::Insets Gtk2Border::GetInsets() const { | 144 gfx::Insets Gtk2Border::GetInsets() const { |
| 113 if (!use_gtk_) | 145 if (!use_gtk_) |
| 114 return border_->GetInsets(); | 146 return border_->GetInsets(); |
| 115 | 147 |
| 116 return gtk2_ui_->GetButtonInsets(); | 148 return gtk2_ui_->GetButtonInsets(); |
| 117 } | 149 } |
| 118 | 150 |
| 119 gfx::Size Gtk2Border::GetMinimumSize() const { | 151 gfx::Size Gtk2Border::GetMinimumSize() const { |
| 120 if (!use_gtk_) | 152 if (!use_gtk_) |
| 121 return border_->GetMinimumSize(); | 153 return border_->GetMinimumSize(); |
| 122 | 154 |
| 123 gfx::Insets insets = GetInsets(); | 155 gfx::Insets insets = GetInsets(); |
| 124 return gfx::Size(insets.width(), insets.height()); | 156 return gfx::Size(insets.width(), insets.height()); |
| 125 } | 157 } |
| 126 | 158 |
| 159 void Gtk2Border::PaintState(const ui::NativeTheme::State state, | |
| 160 const ui::NativeTheme::ExtraParams& extra, | |
| 161 const gfx::Rect& rect, | |
| 162 gfx::Canvas* canvas) { | |
| 163 bool focused = extra.button.is_focused; | |
| 164 Button::ButtonState views_state = GetViewsButtonState(state); | |
| 165 | |
| 166 // If the LabelButtonBorder has a painter for our focused/state combination, | |
|
msw
2014/01/24 00:22:37
Why do you check this? Perhaps just check the butt
Elliot Glaysher
2014/01/24 21:56:00
Taking these two questions out of order...
On 201
msw
2014/01/24 23:57:16
To clarify, will back/forward/reload behave differ
Elliot Glaysher
2014/01/25 00:38:19
In the current GTK port, there is no visual differ
| |
| 167 // that means it was configured to draw a border then, which means that we | |
| 168 // should draw a GTKish border. | |
| 169 if (border_->GetPainter(focused, views_state)) { | |
| 170 gfx::ImageSkia* image = &button_images_[focused][views_state]; | |
| 171 | |
| 172 if (image->isNull() || image->size() != rect.size()) { | |
| 173 GtkStateType gtk_state = GetGtkState(state); | |
| 174 *image = gfx::ImageSkia( | |
| 175 new ButtonImageSkiaSource(gtk2_ui_, gtk_state, focused, rect.size()), | |
| 176 rect.size()); | |
| 177 } | |
| 178 canvas->DrawImageInt(*image, rect.x(), rect.y()); | |
| 179 } | |
| 180 } | |
| 181 | |
| 127 } // namespace libgtk2ui | 182 } // namespace libgtk2ui |
| OLD | NEW |