1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134

ash / system / rotation / rotation_lock_feature_pod_controller.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/rotation/rotation_lock_feature_pod_controller.h"

#include <memory>

#include "ash/constants/quick_settings_catalogs.h"
#include "ash/public/cpp/ash_view_ids.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/unified/feature_tile.h"
#include "ash/system/unified/quick_settings_metrics_util.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "base/functional/bind.h"
#include "ui/base/l10n/l10n_util.h"

namespace ash {

RotationLockFeaturePodController::RotationLockFeaturePodController() {
  DCHECK(Shell::Get());
  Shell::Get()->screen_orientation_controller()->AddObserver(this);
}

RotationLockFeaturePodController::~RotationLockFeaturePodController() {
  if (Shell::Get()->screen_orientation_controller())
    Shell::Get()->screen_orientation_controller()->RemoveObserver(this);
}

// static
bool RotationLockFeaturePodController::CalculateButtonVisibility() {
  // Auto-rotation is supported in both tablet mode and in clamshell mode with
  // `kSupportsClamshellAutoRotation` set, however the button is shown only if
  // the device is in tablet mode.
  return Shell::Get()->tablet_mode_controller()->is_in_tablet_physical_state();
}

std::unique_ptr<FeatureTile> RotationLockFeaturePodController::CreateTile(
    bool compact) {
  DCHECK(!tile_);
  auto tile = std::make_unique<FeatureTile>(
      base::BindRepeating(&RotationLockFeaturePodController::OnIconPressed,
                          weak_factory_.GetWeakPtr()),
      /*is_togglable=*/true,
      compact ? FeatureTile::TileType::kCompact
              : FeatureTile::TileType::kPrimary);
  tile_ = tile.get();

  // The tile label is always "Auto rotate" and there is no sub-label when the
  // tile is `TileType::kPrimary`.
  tile_->SetLabel(
      l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ROTATION_LOCK_AUTO));
  if (!compact) {
    tile_->SetSubLabelVisibility(false);
  }

  // UpdateTile() will update visibility.
  tile_->SetVisible(false);
  UpdateTile();
  return tile;
}

QsFeatureCatalogName RotationLockFeaturePodController::GetCatalogName() {
  return QsFeatureCatalogName::kRotationLock;
}

void RotationLockFeaturePodController::OnIconPressed() {
  TrackToggleUMA(/*target_toggle_state=*/!Shell::Get()
                     ->screen_orientation_controller()
                     ->user_rotation_locked());

  Shell::Get()->screen_orientation_controller()->ToggleUserRotationLock();
}

void RotationLockFeaturePodController::OnUserRotationLockChanged() {
    if (!Shell::Get()
             ->tablet_mode_controller()
             ->is_in_tablet_physical_state()) {
      // Tablet mode transition results in a call to
      // `OnUserRotationLockChanged()`, but the rotation lock feature pod is not
      // visible in clamshell mode. The parent bubble is destroyed during the
      // tablet mode transition. Calling `UpdateTile()` which results in one of
      // two compact `FeatureTile`'s visibilities updating results in
      // undesirable UI. See b/272747756.
      return;
    }
    UpdateTile();
}

void RotationLockFeaturePodController::UpdateTile() {
  bool target_visibility = CalculateButtonVisibility();

  if (!tile_->GetVisible() && target_visibility) {
    TrackVisibilityUMA();
  }
  tile_->SetVisible(target_visibility);

  if (!target_visibility) {
    return;
  }

  auto* screen_orientation_controller =
      Shell::Get()->screen_orientation_controller();
  const bool rotation_locked =
      screen_orientation_controller->user_rotation_locked();
  const bool is_portrait =
      screen_orientation_controller->IsUserLockedOrientationPortrait();

  // The tile is toggled when auto-rotate is on.
  tile_->SetToggled(!rotation_locked);

  std::u16string tooltip_state;
  if (rotation_locked && is_portrait) {
    tile_->SetVectorIcon(kUnifiedMenuRotationLockAutoIcon);
    tooltip_state = l10n_util::GetStringUTF16(
        IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED_VERTICAL_TOOLTIP);
  } else if (rotation_locked && !is_portrait) {
    tile_->SetVectorIcon(kUnifiedMenuRotationLockAutoIcon);
    tooltip_state = l10n_util::GetStringUTF16(
        IDS_ASH_STATUS_TRAY_ROTATION_LOCK_LOCKED_HORIZONTAL_TOOLTIP);
  } else {
    // TODO(b/264428682): Custom icon for auto-rotate (non-locked) state.
    tile_->SetVectorIcon(kUnifiedMenuRotationLockAutoIcon);
    tooltip_state =
        l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ROTATION_LOCK_AUTO_LABEL);
  }

  tile_->SetTooltipText(l10n_util::GetStringFUTF16(
      IDS_ASH_STATUS_TRAY_ROTATION_LOCK_TOOLTIP, tooltip_state));
}

}  // namespace ash