custom_shape_button.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2019 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/system/unified/custom_shape_button.h"
  5. #include "ash/style/ash_color_provider.h"
  6. #include "ash/style/style_util.h"
  7. #include "ui/base/metadata/metadata_impl_macros.h"
  8. #include "ui/color/color_id.h"
  9. #include "ui/compositor/paint_recorder.h"
  10. #include "ui/gfx/image/image_skia_operations.h"
  11. #include "ui/gfx/skbitmap_operations.h"
  12. #include "ui/views/border.h"
  13. #include "ui/views/controls/focus_ring.h"
  14. #include "ui/views/controls/highlight_path_generator.h"
  15. namespace {
  16. class CustomShapeButtonHighlightPathGenerator
  17. : public views::HighlightPathGenerator {
  18. public:
  19. CustomShapeButtonHighlightPathGenerator() = default;
  20. CustomShapeButtonHighlightPathGenerator(
  21. const CustomShapeButtonHighlightPathGenerator&) = delete;
  22. CustomShapeButtonHighlightPathGenerator& operator=(
  23. const CustomShapeButtonHighlightPathGenerator&) = delete;
  24. SkPath GetHighlightPath(const views::View* view) override {
  25. return static_cast<const ash::CustomShapeButton*>(view)
  26. ->CreateCustomShapePath(view->GetLocalBounds());
  27. }
  28. };
  29. } // namespace
  30. namespace ash {
  31. CustomShapeButton::CustomShapeButton(PressedCallback callback)
  32. : ImageButton(std::move(callback)) {
  33. StyleUtil::SetUpInkDropForButton(this);
  34. views::HighlightPathGenerator::Install(
  35. this, std::make_unique<CustomShapeButtonHighlightPathGenerator>());
  36. views::FocusRing::Get(this)->SetColorId(ui::kColorAshFocusRing);
  37. }
  38. CustomShapeButton::~CustomShapeButton() = default;
  39. void CustomShapeButton::PaintButtonContents(gfx::Canvas* canvas) {
  40. PaintCustomShapePath(canvas);
  41. views::ImageButton::PaintButtonContents(canvas);
  42. }
  43. void CustomShapeButton::PaintCustomShapePath(gfx::Canvas* canvas) {
  44. cc::PaintFlags flags;
  45. flags.setAntiAlias(true);
  46. const SkColor button_color = AshColorProvider::Get()->GetControlsLayerColor(
  47. AshColorProvider::ControlsLayerType::kControlBackgroundColorInactive);
  48. flags.setColor(GetEnabled()
  49. ? button_color
  50. : AshColorProvider::GetDisabledColor(button_color));
  51. flags.setStyle(cc::PaintFlags::kFill_Style);
  52. canvas->DrawPath(CreateCustomShapePath(GetLocalBounds()), flags);
  53. }
  54. BEGIN_METADATA(CustomShapeButton, views::ImageButton)
  55. END_METADATA
  56. } // namespace ash