shelf_control_button.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2018 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/shelf/shelf_control_button.h"
  5. #include "ash/public/cpp/shelf_config.h"
  6. #include "ash/resources/vector_icons/vector_icons.h"
  7. #include "ash/shelf/shelf_button_delegate.h"
  8. #include "ash/shell.h"
  9. #include "ash/style/ash_color_provider.h"
  10. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  11. #include "ui/accessibility/ax_node_data.h"
  12. #include "ui/color/color_id.h"
  13. #include "ui/gfx/canvas.h"
  14. #include "ui/gfx/paint_vector_icon.h"
  15. #include "ui/views/animation/flood_fill_ink_drop_ripple.h"
  16. #include "ui/views/controls/focus_ring.h"
  17. #include "ui/views/controls/highlight_path_generator.h"
  18. #include "ui/views/widget/widget.h"
  19. namespace ash {
  20. namespace {
  21. class ShelfControlButtonHighlightPathGenerator
  22. : public views::HighlightPathGenerator {
  23. public:
  24. ShelfControlButtonHighlightPathGenerator() = default;
  25. ShelfControlButtonHighlightPathGenerator(
  26. const ShelfControlButtonHighlightPathGenerator&) = delete;
  27. ShelfControlButtonHighlightPathGenerator& operator=(
  28. const ShelfControlButtonHighlightPathGenerator&) = delete;
  29. // views::HighlightPathGenerator:
  30. absl::optional<gfx::RRectF> GetRoundRect(const gfx::RectF& rect) override {
  31. auto* shelf_config = ShelfConfig::Get();
  32. // Some control buttons have a slightly larger size to fill the shelf and
  33. // maximize the click target, but we still want their "visual" size to be
  34. // the same.
  35. gfx::RectF visual_bounds = rect;
  36. visual_bounds.ClampToCenteredSize(
  37. gfx::SizeF(shelf_config->control_size(), shelf_config->control_size()));
  38. if (Shell::Get()->IsInTabletMode() && shelf_config->is_in_app()) {
  39. visual_bounds.Inset(gfx::InsetsF::VH(
  40. shelf_config->in_app_control_button_height_inset(), 0));
  41. }
  42. return gfx::RRectF(visual_bounds, shelf_config->control_border_radius());
  43. }
  44. };
  45. } // namespace
  46. ShelfControlButton::ShelfControlButton(
  47. Shelf* shelf,
  48. ShelfButtonDelegate* shelf_button_delegate)
  49. : ShelfButton(shelf, shelf_button_delegate) {
  50. SetHasInkDropActionOnClick(true);
  51. SetInstallFocusRingOnFocus(true);
  52. views::FocusRing::Get(this)->SetColorId(ui::kColorAshFocusRing);
  53. views::HighlightPathGenerator::Install(
  54. this, std::make_unique<ShelfControlButtonHighlightPathGenerator>());
  55. SetPaintToLayer();
  56. layer()->SetFillsBoundsOpaquely(false);
  57. }
  58. ShelfControlButton::~ShelfControlButton() = default;
  59. gfx::Point ShelfControlButton::GetCenterPoint() const {
  60. return GetLocalBounds().CenterPoint();
  61. }
  62. const char* ShelfControlButton::GetClassName() const {
  63. return "ash/ShelfControlButton";
  64. }
  65. gfx::Size ShelfControlButton::CalculatePreferredSize() const {
  66. return gfx::Size(ShelfConfig::Get()->control_size(),
  67. ShelfConfig::Get()->control_size());
  68. }
  69. void ShelfControlButton::GetAccessibleNodeData(ui::AXNodeData* node_data) {
  70. ShelfButton::GetAccessibleNodeData(node_data);
  71. node_data->SetName(GetAccessibleName());
  72. }
  73. void ShelfControlButton::PaintButtonContents(gfx::Canvas* canvas) {
  74. PaintBackground(canvas, GetContentsBounds());
  75. }
  76. void ShelfControlButton::PaintBackground(gfx::Canvas* canvas,
  77. const gfx::Rect& bounds) {
  78. cc::PaintFlags flags;
  79. flags.setAntiAlias(true);
  80. flags.setColor(ShelfConfig::Get()->GetShelfControlButtonColor());
  81. canvas->DrawRoundRect(bounds, ShelfConfig::Get()->control_border_radius(),
  82. flags);
  83. }
  84. } // namespace ash