style_util.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/style/style_util.h"
  5. #include "ash/root_window_controller.h"
  6. #include "ash/style/ash_color_provider.h"
  7. #include "ash/style/dark_light_mode_controller_impl.h"
  8. #include "ui/color/color_id.h"
  9. #include "ui/views/animation/flood_fill_ink_drop_ripple.h"
  10. #include "ui/views/animation/ink_drop.h"
  11. #include "ui/views/animation/ink_drop_highlight.h"
  12. #include "ui/views/animation/ink_drop_host_view.h"
  13. #include "ui/views/controls/button/button.h"
  14. #include "ui/views/controls/focus_ring.h"
  15. namespace ash {
  16. // static
  17. float StyleUtil::GetInkDropOpacity() {
  18. return DarkLightModeControllerImpl::Get()->IsDarkModeEnabled()
  19. ? kDarkInkDropOpacity
  20. : kLightInkDropOpacity;
  21. }
  22. // static
  23. std::unique_ptr<views::InkDrop> StyleUtil::CreateInkDrop(
  24. views::Button* host,
  25. bool highlight_on_hover,
  26. bool highlight_on_focus) {
  27. return views::InkDrop::CreateInkDropForFloodFillRipple(
  28. views::InkDrop::Get(host), highlight_on_hover, highlight_on_focus);
  29. }
  30. // static
  31. std::unique_ptr<views::InkDropRipple> StyleUtil::CreateInkDropRipple(
  32. const gfx::Insets& insets,
  33. const views::View* host,
  34. SkColor background_color) {
  35. const std::pair<SkColor, float> base_color_and_opacity =
  36. AshColorProvider::Get()->GetInkDropBaseColorAndOpacity(background_color);
  37. return std::make_unique<views::FloodFillInkDropRipple>(
  38. host->size(), insets,
  39. views::InkDrop::Get(host)->GetInkDropCenterBasedOnLastEvent(),
  40. base_color_and_opacity.first, base_color_and_opacity.second);
  41. }
  42. // static
  43. std::unique_ptr<views::InkDropHighlight> StyleUtil::CreateInkDropHighlight(
  44. const views::View* host,
  45. SkColor background_color) {
  46. const std::pair<SkColor, float> base_color_and_opacity =
  47. AshColorProvider::Get()->GetInkDropBaseColorAndOpacity(background_color);
  48. auto highlight = std::make_unique<views::InkDropHighlight>(
  49. gfx::SizeF(host->size()), base_color_and_opacity.first);
  50. highlight->set_visible_opacity(base_color_and_opacity.second);
  51. return highlight;
  52. }
  53. // static
  54. void StyleUtil::SetRippleParams(views::View* host,
  55. const gfx::Insets& insets,
  56. SkColor background_color) {
  57. views::InkDrop::Get(host)->SetCreateRippleCallback(base::BindRepeating(
  58. &CreateInkDropRipple, insets, host, background_color));
  59. }
  60. // static
  61. void StyleUtil::SetUpInkDropForButton(views::Button* button,
  62. const gfx::Insets& ripple_insets,
  63. bool highlight_on_hover,
  64. bool highlight_on_focus,
  65. SkColor background_color) {
  66. button->SetInstallFocusRingOnFocus(true);
  67. views::InkDropHost* const ink_drop = views::InkDrop::Get(button);
  68. ink_drop->SetMode(views::InkDropHost::InkDropMode::ON);
  69. button->SetHasInkDropActionOnClick(true);
  70. ink_drop->SetCreateInkDropCallback(base::BindRepeating(
  71. &CreateInkDrop, button, highlight_on_hover, highlight_on_focus));
  72. ink_drop->SetCreateRippleCallback(base::BindRepeating(
  73. &CreateInkDropRipple, ripple_insets, button, background_color));
  74. ink_drop->SetCreateHighlightCallback(
  75. base::BindRepeating(&CreateInkDropHighlight, button, background_color));
  76. }
  77. // static
  78. void StyleUtil::ConfigureInkDropAttributes(views::View* view,
  79. uint32_t ripple_config_attributes,
  80. SkColor background_color) {
  81. const std::pair<SkColor, float> base_color_and_opacity =
  82. AshColorProvider::Get()->GetInkDropBaseColorAndOpacity(background_color);
  83. auto* host = views::InkDrop::Get(view);
  84. if (ripple_config_attributes & kBaseColor)
  85. host->SetBaseColor(base_color_and_opacity.first);
  86. if (ripple_config_attributes & kInkDropOpacity)
  87. host->SetVisibleOpacity(base_color_and_opacity.second);
  88. if (ripple_config_attributes & kHighlightOpacity)
  89. host->SetHighlightOpacity(base_color_and_opacity.second);
  90. }
  91. // static
  92. views::FocusRing* StyleUtil::SetUpFocusRingForView(
  93. views::View* view,
  94. absl::optional<int> halo_inset) {
  95. DCHECK(view);
  96. views::FocusRing::Install(view);
  97. views::FocusRing* focus_ring = views::FocusRing::Get(view);
  98. focus_ring->SetColorId(ui::kColorAshFocusRing);
  99. if (halo_inset)
  100. focus_ring->SetHaloInset(*halo_inset);
  101. return focus_ring;
  102. }
  103. // static
  104. AshColorProviderSource* StyleUtil::GetColorProviderSourceForWindow(
  105. const aura::Window* window) {
  106. DCHECK(window);
  107. auto* root_window = window->GetRootWindow();
  108. if (!root_window)
  109. return nullptr;
  110. return RootWindowController::ForWindow(root_window)->color_provider_source();
  111. }
  112. } // namespace ash