wm_highlight_item_border.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/wm/wm_highlight_item_border.h"
  5. #include "ash/style/ash_color_provider.h"
  6. #include "cc/paint/paint_flags.h"
  7. #include "ui/gfx/canvas.h"
  8. #include "ui/gfx/geometry/rect_f.h"
  9. #include "ui/views/view.h"
  10. namespace ash {
  11. namespace {
  12. // The desk preview border size and padding in dips.
  13. constexpr int kBorderSize = 2;
  14. constexpr int kBorderPadding = 2;
  15. } // namespace
  16. WmHighlightItemBorder::WmHighlightItemBorder(int corner_radius,
  17. gfx::Insets padding)
  18. : views::Border(SK_ColorTRANSPARENT),
  19. corner_radius_(corner_radius),
  20. border_insets_(gfx::Insets(kBorderSize + kBorderPadding) + padding) {}
  21. bool WmHighlightItemBorder::SetFocused(bool focused) {
  22. // Note that all WM features that use this custom border currently have dark
  23. // mode as the default color mode.
  24. const SkColor new_color =
  25. focused ? AshColorProvider::Get()->GetControlsLayerColor(
  26. AshColorProvider::ControlsLayerType::kFocusRingColor)
  27. : SK_ColorTRANSPARENT;
  28. if (new_color == color())
  29. return false;
  30. set_color(new_color);
  31. return true;
  32. }
  33. void WmHighlightItemBorder::Paint(const views::View& view,
  34. gfx::Canvas* canvas) {
  35. if (color() == SK_ColorTRANSPARENT)
  36. return;
  37. cc::PaintFlags flags;
  38. flags.setStrokeWidth(kBorderSize);
  39. flags.setColor(color());
  40. flags.setStyle(cc::PaintFlags::kStroke_Style);
  41. flags.setAntiAlias(true);
  42. gfx::RectF bounds(view.GetLocalBounds());
  43. // The following inset is needed for the rounded corners of the border to
  44. // look correct. Otherwise, the borders will be painted at the edge of the
  45. // view, resulting in this border looking chopped.
  46. bounds.Inset(kBorderSize / 2);
  47. canvas->DrawRoundRect(bounds, corner_radius_, flags);
  48. }
  49. gfx::Insets WmHighlightItemBorder::GetInsets() const {
  50. return border_insets_;
  51. }
  52. gfx::Size WmHighlightItemBorder::GetMinimumSize() const {
  53. const int minmum_length = 2 * (kBorderSize + kBorderPadding);
  54. return gfx::Size(minmum_length, minmum_length);
  55. }
  56. } // namespace ash