solid_source_background.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2020 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. #include "ash/hud_display/solid_source_background.h"
  5. #include "cc/paint/paint_flags.h"
  6. #include "third_party/skia/include/core/SkBlendMode.h"
  7. #include "third_party/skia/include/core/SkPath.h"
  8. #include "ui/gfx/canvas.h"
  9. #include "ui/views/view.h"
  10. namespace ash {
  11. namespace hud_display {
  12. SolidSourceBackground::SolidSourceBackground(SkColor color,
  13. SkScalar top_rounding_radius)
  14. : top_rounding_radius_(top_rounding_radius) {
  15. SetNativeControlColor(color);
  16. }
  17. void SolidSourceBackground::Paint(gfx::Canvas* canvas,
  18. views::View* view) const {
  19. if (top_rounding_radius_ == 0) {
  20. // Fill the background. Note that we don't constrain to the bounds as
  21. // canvas is already clipped for us.
  22. canvas->DrawColor(get_color(), SkBlendMode::kSrc);
  23. } else {
  24. const SkScalar circle_size = top_rounding_radius_ * 2;
  25. const SkScalar right_edge = view->width();
  26. const SkScalar bottom_edge = view->height();
  27. SkPath path;
  28. path.moveTo(0, bottom_edge);
  29. // |false| will draw straight line to the start of the arc.
  30. path.arcTo({0, 0, circle_size, circle_size}, -180, 90, false);
  31. path.arcTo({right_edge - circle_size, 0, right_edge, circle_size}, -90, 90,
  32. false);
  33. path.lineTo(right_edge, bottom_edge);
  34. cc::PaintFlags flags;
  35. flags.setAntiAlias(true);
  36. flags.setBlendMode(SkBlendMode::kSrc);
  37. flags.setStyle(cc::PaintFlags::kFill_Style);
  38. flags.setColor(get_color());
  39. canvas->DrawPath(path, flags);
  40. }
  41. }
  42. } // namespace hud_display
  43. } // namespace ash