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

ash / system / dark_mode / dark_mode_feature_pod_controller.cc [blame]

// Copyright 2020 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/dark_mode/dark_mode_feature_pod_controller.h"

#include "ash/constants/quick_settings_catalogs.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/dark_light_mode_controller_impl.h"
#include "ash/system/unified/feature_tile.h"
#include "ash/system/unified/quick_settings_metrics_util.h"
#include "base/metrics/histogram_functions.h"
#include "ui/base/l10n/l10n_util.h"

namespace ash {

namespace {

bool IsVisible() {
  // TODO(minch): Add the logic for login screen.
  // Disable dark mode feature pod in OOBE since only light mode should be
  // allowed there.
  return Shell::Get()->session_controller()->IsActiveUserSessionStarted() &&
         Shell::Get()->session_controller()->GetSessionState() !=
             session_manager::SessionState::OOBE;
}

}  // namespace

DarkModeFeaturePodController::DarkModeFeaturePodController(
    UnifiedSystemTrayController* tray_controller) {
  DarkLightModeControllerImpl::Get()->AddObserver(this);
}

DarkModeFeaturePodController::~DarkModeFeaturePodController() {
  DarkLightModeControllerImpl::Get()->RemoveObserver(this);
}

std::unique_ptr<FeatureTile> DarkModeFeaturePodController::CreateTile(
    bool compact) {
  DCHECK(!tile_);
  auto tile = std::make_unique<FeatureTile>(
      base::BindRepeating(&DarkModeFeaturePodController::OnIconPressed,
                          weak_ptr_factory_.GetWeakPtr()));
  tile_ = tile.get();
  tile_->SetVectorIcon(kUnifiedMenuDarkModeIcon);
  tile_->SetLabel(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_DARK_THEME));
  const bool visible = IsVisible();
  if (visible) {
    TrackVisibilityUMA();
  }
  tile_->SetVisible(visible);

  UpdateTile(DarkLightModeControllerImpl::Get()->IsDarkModeEnabled());
  return tile;
}

QsFeatureCatalogName DarkModeFeaturePodController::GetCatalogName() {
  return QsFeatureCatalogName::kDarkMode;
}

void DarkModeFeaturePodController::OnIconPressed() {
  // Toggling Dark theme feature tile inside quick settings should cancel auto
  // scheduling. This ensures that on and off states of the tile match the
  // non-scheduled states of Dark and Light buttons in personalization hub
  // respectively.
  auto* dark_light_mode_controller = DarkLightModeControllerImpl::Get();
  TrackToggleUMA(
      /*target_toggle_state=*/!dark_light_mode_controller->IsDarkModeEnabled());
  dark_light_mode_controller->SetAutoScheduleEnabled(
      /*enabled=*/false);
  dark_light_mode_controller->ToggleColorMode();
  base::UmaHistogramBoolean("Ash.DarkTheme.SystemTray.IsDarkModeEnabled",
                            dark_light_mode_controller->IsDarkModeEnabled());
}

void DarkModeFeaturePodController::OnLabelPressed() {
  return;
}

void DarkModeFeaturePodController::OnColorModeChanged(bool dark_mode_enabled) {
  UpdateTile(dark_mode_enabled);
}

void DarkModeFeaturePodController::UpdateTile(bool dark_mode_enabled) {
  tile_->SetToggled(dark_mode_enabled);
  if (Shell::Get()->dark_light_mode_controller()->GetAutoScheduleEnabled()) {
    tile_->SetSubLabel(l10n_util::GetStringUTF16(
        dark_mode_enabled
            ? IDS_ASH_STATUS_TRAY_DARK_THEME_ON_STATE_AUTO_SCHEDULED
            : IDS_ASH_STATUS_TRAY_DARK_THEME_OFF_STATE_AUTO_SCHEDULED));
  } else {
    tile_->SetSubLabel(l10n_util::GetStringUTF16(
        dark_mode_enabled ? IDS_ASH_STATUS_TRAY_DARK_THEME_ON_STATE
                          : IDS_ASH_STATUS_TRAY_DARK_THEME_OFF_STATE));
  }

  std::u16string tooltip_state = l10n_util::GetStringUTF16(
      dark_mode_enabled
          ? IDS_ASH_STATUS_TRAY_DARK_THEME_ENABLED_STATE_TOOLTIP
          : IDS_ASH_STATUS_TRAY_DARK_THEME_DISABLED_STATE_TOOLTIP);
  tile_->SetTooltipText(l10n_util::GetStringFUTF16(
      IDS_ASH_STATUS_TRAY_DARK_THEME_TOGGLE_TOOLTIP, tooltip_state));
  tile_->SetVectorIcon(dark_mode_enabled ? kUnifiedMenuDarkModeIcon
                                         : kUnifiedMenuDarkModeOffIcon);
}

}  // namespace ash