| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/gtk/infobar_arrow_model.h" | |
| 6 | |
| 7 #include "chrome/browser/gtk/infobar_gtk.h" | |
| 8 #include "gfx/canvas_skia_paint.h" | |
| 9 #include "gfx/color_utils.h" | |
| 10 #include "gfx/point.h" | |
| 11 #include "gfx/skia_utils_gtk.h" | |
| 12 #include "third_party/skia/include/effects/SkGradientShader.h" | |
| 13 | |
| 14 InfoBarArrowModel::InfoBarArrowModel(Observer* observer) | |
| 15 : observer_(observer), | |
| 16 animation_(this) { | |
| 17 animation_.SetTweenType(ui::Tween::LINEAR); | |
| 18 animation_.Reset(1.0); | |
| 19 target_colors_.top = target_colors_.bottom = SkColorSetARGB(0, 0, 0, 0); | |
| 20 previous_colors_ = target_colors_; | |
| 21 } | |
| 22 | |
| 23 InfoBarArrowModel::~InfoBarArrowModel() { | |
| 24 } | |
| 25 | |
| 26 InfoBarArrowModel::InfoBarColors InfoBarArrowModel::CurrentInfoBarColors() { | |
| 27 double alpha = animation_.GetCurrentValue(); | |
| 28 InfoBarColors colors = { | |
| 29 color_utils::AlphaBlend(target_colors_.top, | |
| 30 previous_colors_.top, | |
| 31 alpha * 0xff), | |
| 32 color_utils::AlphaBlend(target_colors_.bottom, | |
| 33 previous_colors_.bottom, | |
| 34 alpha * 0xff)}; | |
| 35 return colors; | |
| 36 } | |
| 37 | |
| 38 bool InfoBarArrowModel::NeedToDrawInfoBarArrow() { | |
| 39 return SkColorGetA(CurrentInfoBarColors().top) != 0; | |
| 40 } | |
| 41 | |
| 42 void InfoBarArrowModel::ShowArrowFor(InfoBar* bar, bool animate) { | |
| 43 scoped_ptr<std::pair<SkColor, SkColor> > colors; | |
| 44 | |
| 45 previous_colors_ = CurrentInfoBarColors(); | |
| 46 | |
| 47 if (bar) { | |
| 48 double r, g, b; | |
| 49 bar->GetTopColor(bar->delegate()->GetInfoBarType(), &r, &g, &b); | |
| 50 target_colors_.top = SkColorSetRGB(r * 0xff, g * 0xff, b * 0xff); | |
| 51 bar->GetBottomColor(bar->delegate()->GetInfoBarType(), &r, &g, &b); | |
| 52 target_colors_.bottom = SkColorSetRGB(r * 0xff, g * 0xff, b * 0xff); | |
| 53 } else { | |
| 54 target_colors_.bottom = target_colors_.top = SkColorSetARGB(0, 0, 0, 0); | |
| 55 } | |
| 56 | |
| 57 if (animate) { | |
| 58 // Fade from the current color to the target color. | |
| 59 animation_.Reset(); | |
| 60 animation_.Show(); | |
| 61 } else { | |
| 62 // Skip straight to showing the target color. | |
| 63 animation_.Reset(1.0); | |
| 64 } | |
| 65 | |
| 66 observer_->PaintStateChanged(); | |
| 67 } | |
| 68 | |
| 69 void InfoBarArrowModel::Paint(GtkWidget* widget, | |
| 70 GdkEventExpose* expose, | |
| 71 const gfx::Point& origin, | |
| 72 const GdkColor& border_color) { | |
| 73 if (!NeedToDrawInfoBarArrow()) | |
| 74 return; | |
| 75 | |
| 76 // The size of the arrow (its height; also half its width). | |
| 77 const int kArrowSize = 10; | |
| 78 | |
| 79 SkPath path; | |
| 80 path.moveTo(SkPoint::Make(origin.x() - kArrowSize, origin.y())); | |
| 81 path.rLineTo(kArrowSize, -kArrowSize); | |
| 82 path.rLineTo(kArrowSize, kArrowSize); | |
| 83 path.close(); | |
| 84 | |
| 85 SkPaint paint; | |
| 86 paint.setStrokeWidth(1); | |
| 87 paint.setStyle(SkPaint::kFill_Style); | |
| 88 | |
| 89 SkPoint grad_points[2]; | |
| 90 grad_points[0].set(SkIntToScalar(0), SkIntToScalar(origin.y())); | |
| 91 grad_points[1].set(SkIntToScalar(0), | |
| 92 SkIntToScalar(origin.y() + InfoBar::kInfoBarHeight)); | |
| 93 | |
| 94 InfoBarColors colors = CurrentInfoBarColors(); | |
| 95 SkColor grad_colors[2]; | |
| 96 grad_colors[0] = colors.top; | |
| 97 grad_colors[1] = colors.bottom; | |
| 98 | |
| 99 SkShader* gradient_shader = SkGradientShader::CreateLinear( | |
| 100 grad_points, grad_colors, NULL, 2, SkShader::kMirror_TileMode); | |
| 101 paint.setShader(gradient_shader); | |
| 102 gradient_shader->unref(); | |
| 103 | |
| 104 gfx::CanvasSkiaPaint canvas(expose, false); | |
| 105 canvas.drawPath(path, paint); | |
| 106 | |
| 107 paint.setShader(NULL); | |
| 108 paint.setColor(SkColorSetA(gfx::GdkColorToSkColor(border_color), | |
| 109 SkColorGetA(colors.top))); | |
| 110 paint.setStyle(SkPaint::kStroke_Style); | |
| 111 canvas.drawPath(path, paint); | |
| 112 } | |
| 113 | |
| 114 void InfoBarArrowModel::AnimationEnded(const ui::Animation* animation) { | |
| 115 observer_->PaintStateChanged(); | |
| 116 } | |
| 117 | |
| 118 void InfoBarArrowModel::AnimationProgressed(const ui::Animation* animation) { | |
| 119 observer_->PaintStateChanged(); | |
| 120 } | |
| 121 | |
| 122 void InfoBarArrowModel::AnimationCanceled(const ui::Animation* animation) { | |
| 123 observer_->PaintStateChanged(); | |
| 124 } | |
| OLD | NEW |