#include "ash/shelf/home_button_controller.h"
#include "ash/app_list/app_list_controller_impl.h"
#include "ash/capture_mode/capture_mode_controller.h"
#include "ash/root_window_controller.h"
#include "ash/scanner/scanner_metrics.h"
#include "ash/shelf/home_button.h"
#include "ash/shelf/home_button_tap_overlay.h"
#include "ash/shelf/shelf_button.h"
#include "ash/shell.h"
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "chromeos/constants/chromeos_features.h"
#include "components/account_id/account_id.h"
#include "ui/display/screen.h"
#include "ui/display/tablet_state.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/animation/ink_drop_state.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace {
constexpr base::TimeDelta kAssistantAnimationDelay = base::Milliseconds(200);
}
HomeButtonController::HomeButtonController(HomeButton* button)
: button_(button) {
DCHECK(button_);
InitializeAssistantOverlay();
DCHECK(tap_overlay_);
Shell* shell = Shell::Get();
sunfish_scanner_feature_observation_.Observe(
shell->sunfish_scanner_feature_watcher());
shell->app_list_controller()->AddObserver(this);
}
HomeButtonController::~HomeButtonController() {
Shell* shell = Shell::Get();
if (shell->app_list_controller())
shell->app_list_controller()->RemoveObserver(this);
}
bool HomeButtonController::MaybeHandleGestureEvent(ui::GestureEvent* event) {
switch (event->type()) {
case ui::EventType::kGestureTap:
case ui::EventType::kGestureTapCancel:
tap_overlay_->EndAnimation();
tap_animation_delay_timer_->Stop();
return false;
case ui::EventType::kGestureTapDown:
if (IsSunfishOrScannerAvailable()) {
tap_animation_delay_timer_->Start(
FROM_HERE, kAssistantAnimationDelay,
base::BindOnce(&HomeButtonController::StartAssistantAnimation,
base::Unretained(this)));
}
return false;
case ui::EventType::kGestureLongPress:
sunfish_scanner_feature_observation_.GetSource()->UpdateFeatureStates();
if (IsSunfishOrScannerAvailable()) {
tap_overlay_->BurstAnimation();
event->SetHandled();
RecordScannerFeatureUserState(
ScannerFeatureUserState::
kSunfishSessionStartedFromHomeButtonLongPress);
CaptureModeController::Get()->StartSunfishSession();
return true;
}
return false;
case ui::EventType::kGestureLongTap:
if (!IsSunfishOrScannerAvailable()) {
return false;
}
event->SetHandled();
return true;
default:
return false;
}
}
bool HomeButtonController::IsLongPressActionAvailable() {
return IsSunfishOrScannerAvailable();
}
bool HomeButtonController::IsSunfishOrScannerAvailable() const {
return sunfish_scanner_feature_observation_.GetSource()
->CanShowSunfishOrScannerUi();
}
void HomeButtonController::OnAppListVisibilityWillChange(bool shown,
int64_t display_id) {
if (button_->GetDisplayId() != display_id)
return;
if (shown)
OnAppListShown();
else
OnAppListDismissed();
}
void HomeButtonController::OnDisplayTabletStateChanged(
display::TabletState state) {
if (state != display::TabletState::kInTabletMode) {
return;
}
}
void HomeButtonController::OnSunfishScannerFeatureStatesChanged(
SunfishScannerFeatureWatcher& source) {
button_->OnIconUpdated();
}
void HomeButtonController::StartAssistantAnimation() {
tap_overlay_->StartAnimation();
}
void HomeButtonController::OnAppListShown() {
if (!display::Screen::Get()->InTabletMode()) {
button_->SetToggled(true);
}
button_->UpdateTooltipText();
}
void HomeButtonController::OnAppListDismissed() {
button_->SetToggled(false);
button_->UpdateTooltipText();
}
void HomeButtonController::InitializeAssistantOverlay() {
DCHECK_EQ(nullptr, tap_overlay_);
tap_overlay_ = new HomeButtonTapOverlay(button_);
button_->AddChildViewRaw(tap_overlay_.get());
tap_overlay_->SetVisible(false);
tap_animation_delay_timer_ = std::make_unique<base::OneShotTimer>();
}
}