projector_button.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2021 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/projector/ui/projector_button.h"
  5. #include "ash/public/cpp/style/color_provider.h"
  6. #include "ash/resources/vector_icons/vector_icons.h"
  7. #include "ash/style/ash_color_provider.h"
  8. #include "ash/style/style_util.h"
  9. #include "ui/gfx/canvas.h"
  10. #include "ui/gfx/paint_vector_icon.h"
  11. #include "ui/views/accessibility/view_accessibility.h"
  12. #include "ui/views/animation/ink_drop.h"
  13. #include "ui/views/background.h"
  14. #include "ui/views/border.h"
  15. #include "ui/views/controls/highlight_path_generator.h"
  16. namespace ash {
  17. namespace {
  18. constexpr gfx::Insets kButtonPadding{0};
  19. } // namespace
  20. ProjectorButton::ProjectorButton(views::Button::PressedCallback callback,
  21. const std::u16string& name)
  22. : ToggleImageButton(callback), name_(name) {
  23. SetPreferredSize(gfx::Size(kProjectorButtonSize, kProjectorButtonSize));
  24. SetBorder(views::CreateEmptyBorder(kButtonPadding));
  25. // Rounded background.
  26. views::InstallRoundRectHighlightPathGenerator(this, gfx::Insets(),
  27. kProjectorButtonSize / 2.f);
  28. views::InkDrop::UseInkDropForFloodFillRipple(views::InkDrop::Get(this),
  29. /*highlight_on_hover=*/true,
  30. /*highlight_on_focus=*/true);
  31. SetTooltipText(name);
  32. }
  33. void ProjectorButton::OnPaintBackground(gfx::Canvas* canvas) {
  34. if (!GetToggled()) {
  35. return;
  36. }
  37. auto* color_provider = AshColorProvider::Get();
  38. // Draw a filled background for the button.
  39. cc::PaintFlags flags;
  40. flags.setAntiAlias(true);
  41. flags.setStyle(cc::PaintFlags::kFill_Style);
  42. flags.setColor(color_provider->GetControlsLayerColor(
  43. AshColorProvider::ControlsLayerType::kControlBackgroundColorInactive));
  44. const gfx::RectF bounds(GetContentsBounds());
  45. canvas->DrawCircle(bounds.CenterPoint(), bounds.width() / 2, flags);
  46. // Draw a border on the background circle.
  47. cc::PaintFlags border_flags;
  48. flags.setAntiAlias(true);
  49. flags.setStyle(cc::PaintFlags::kStroke_Style);
  50. flags.setColor(color_provider->GetControlsLayerColor(
  51. AshColorProvider::ControlsLayerType::kHairlineBorderColor));
  52. flags.setStrokeWidth(kProjectorButtonBorderSize);
  53. canvas->DrawCircle(bounds.CenterPoint(),
  54. (bounds.width() - kProjectorButtonBorderSize * 2) / 2,
  55. flags);
  56. }
  57. void ProjectorButton::OnThemeChanged() {
  58. views::ToggleImageButton::OnThemeChanged();
  59. // Ink Drop.
  60. views::InkDrop::Get(this)->SetMode(views::InkDropHost::InkDropMode::ON);
  61. SetHasInkDropActionOnClick(true);
  62. StyleUtil::ConfigureInkDropAttributes(
  63. this, StyleUtil::kBaseColor | StyleUtil::kHighlightOpacity);
  64. }
  65. void ProjectorButton::GetAccessibleNodeData(ui::AXNodeData* node_data) {
  66. node_data->role = ax::mojom::Role::kButton;
  67. node_data->SetName(name_);
  68. }
  69. } // namespace ash