tray_toggle_button.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/system/tray/tray_toggle_button.h"
  5. #include "ash/style/ash_color_provider.h"
  6. #include "ash/system/tray/tray_constants.h"
  7. #include "ash/utility/haptics_util.h"
  8. #include "ui/base/l10n/l10n_util.h"
  9. #include "ui/base/metadata/metadata_impl_macros.h"
  10. #include "ui/color/color_id.h"
  11. #include "ui/events/devices/haptic_touchpad_effects.h"
  12. #include "ui/events/event.h"
  13. #include "ui/views/border.h"
  14. #include "ui/views/controls/focus_ring.h"
  15. namespace ash {
  16. TrayToggleButton::TrayToggleButton(PressedCallback callback,
  17. int accessible_name_id)
  18. : ToggleButton(std::move(callback)) {
  19. const gfx::Size toggle_size(GetPreferredSize());
  20. const int vertical_padding = (kMenuButtonSize - toggle_size.height()) / 2;
  21. const int horizontal_padding =
  22. (kTrayToggleButtonWidth - toggle_size.width()) / 2;
  23. SetBorder(views::CreateEmptyBorder(
  24. gfx::Insets::VH(vertical_padding, horizontal_padding)));
  25. SetAccessibleName(l10n_util::GetStringUTF16(accessible_name_id));
  26. views::FocusRing::Get(this)->SetColorId(ui::kColorAshFocusRing);
  27. }
  28. void TrayToggleButton::OnThemeChanged() {
  29. views::ToggleButton::OnThemeChanged();
  30. auto* color_provider = AshColorProvider::Get();
  31. SetThumbOnColor(color_provider->GetContentLayerColor(
  32. AshColorProvider::ContentLayerType::kSwitchKnobColorActive));
  33. SetThumbOffColor(color_provider->GetContentLayerColor(
  34. AshColorProvider::ContentLayerType::kSwitchKnobColorInactive));
  35. SetTrackOnColor(color_provider->GetContentLayerColor(
  36. AshColorProvider::ContentLayerType::kSwitchTrackColorActive));
  37. SetTrackOffColor(color_provider->GetContentLayerColor(
  38. AshColorProvider::ContentLayerType::kSwitchTrackColorInactive));
  39. }
  40. void TrayToggleButton::NotifyClick(const ui::Event& event) {
  41. haptics_util::PlayHapticToggleEffect(
  42. !GetIsOn(), ui::HapticTouchpadEffectStrength::kMedium);
  43. views::ToggleButton::NotifyClick(event);
  44. }
  45. BEGIN_METADATA(TrayToggleButton, views::ToggleButton)
  46. END_METADATA
  47. } // namespace ash