rotation_lock_feature_pod_controller.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/system/rotation/rotation_lock_feature_pod_controller.h"
  5. #include "ash/resources/vector_icons/vector_icons.h"
  6. #include "ash/shell.h"
  7. #include "ash/strings/grit/ash_strings.h"
  8. #include "ash/system/unified/feature_pod_button.h"
  9. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  10. #include "ui/base/l10n/l10n_util.h"
  11. namespace ash {
  12. RotationLockFeaturePodController::RotationLockFeaturePodController() {
  13. DCHECK(Shell::Get());
  14. Shell::Get()->tablet_mode_controller()->AddObserver(this);
  15. Shell::Get()->screen_orientation_controller()->AddObserver(this);
  16. }
  17. RotationLockFeaturePodController::~RotationLockFeaturePodController() {
  18. if (Shell::Get()->screen_orientation_controller())
  19. Shell::Get()->screen_orientation_controller()->RemoveObserver(this);
  20. if (Shell::Get()->tablet_mode_controller())
  21. Shell::Get()->tablet_mode_controller()->RemoveObserver(this);
  22. }
  23. FeaturePodButton* RotationLockFeaturePodController::CreateButton() {
  24. DCHECK(!button_);
  25. button_ = new FeaturePodButton(this);
  26. button_->DisableLabelButtonFocus();
  27. UpdateButton();
  28. return button_;
  29. }
  30. void RotationLockFeaturePodController::OnIconPressed() {
  31. Shell::Get()->screen_orientation_controller()->ToggleUserRotationLock();
  32. }
  33. SystemTrayItemUmaType RotationLockFeaturePodController::GetUmaType() const {
  34. return SystemTrayItemUmaType::UMA_ROTATION_LOCK;
  35. }
  36. void RotationLockFeaturePodController::OnTabletPhysicalStateChanged() {
  37. UpdateButton();
  38. }
  39. void RotationLockFeaturePodController::OnUserRotationLockChanged() {
  40. UpdateButton();
  41. }
  42. void RotationLockFeaturePodController::UpdateButton() {
  43. // Even though auto-rotation is also supported when the device is not in a
  44. // tablet physical state but kSupportsClamshellAutoRotation is set. The "Auto
  45. // rotate" feature pod button in the system tray menu is not expected to be
  46. // shown in the case.
  47. const bool is_auto_rotation_allowed =
  48. Shell::Get()->tablet_mode_controller()->is_in_tablet_physical_state();
  49. button_->SetVisible(is_auto_rotation_allowed);
  50. if (!is_auto_rotation_allowed)
  51. return;
  52. auto* screen_orientation_controller =
  53. Shell::Get()->screen_orientation_controller();
  54. const bool rotation_locked =
  55. screen_orientation_controller->user_rotation_locked();
  56. const bool is_portrait =
  57. screen_orientation_controller->IsUserLockedOrientationPortrait();
  58. button_->SetToggled(rotation_locked);
  59. std::u16string tooltip_state;
  60. if (rotation_locked && is_portrait) {
  61. button_->SetVectorIcon(kUnifiedMenuRotationLockPortraitIcon);
  62. button_->SetLabel(l10n_util::GetStringUTF16(
  63. IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED_LABEL));
  64. button_->SetSubLabel(l10n_util::GetStringUTF16(
  65. IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED_VERTICAL_SUBLABEL));
  66. tooltip_state = l10n_util::GetStringUTF16(
  67. IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED_VERTICAL_TOOLTIP);
  68. } else if (rotation_locked && !is_portrait) {
  69. button_->SetVectorIcon(kUnifiedMenuRotationLockLandscapeIcon);
  70. button_->SetLabel(l10n_util::GetStringUTF16(
  71. IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED_LABEL));
  72. button_->SetSubLabel(l10n_util::GetStringUTF16(
  73. IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED_HORIZONTAL_SUBLABEL));
  74. tooltip_state = l10n_util::GetStringUTF16(
  75. IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED_HORIZONTAL_TOOLTIP);
  76. } else {
  77. button_->SetVectorIcon(kUnifiedMenuRotationLockAutoIcon);
  78. button_->SetLabel(l10n_util::GetStringUTF16(
  79. IDS_ASH_STATUS_TRAY_ROTATION_LOCK_AUTO_LABEL));
  80. button_->SetSubLabel(l10n_util::GetStringUTF16(
  81. IDS_ASH_STATUS_TRAY_ROTATION_LOCK_AUTO_SUBLABEL));
  82. tooltip_state =
  83. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ROTATION_LOCK_AUTO_LABEL);
  84. }
  85. button_->SetIconAndLabelTooltips(l10n_util::GetStringFUTF16(
  86. IDS_ASH_STATUS_TRAY_ROTATION_LOCK_TOOLTIP, tooltip_state));
  87. }
  88. } // namespace ash