#include "ash/capture_mode/capture_mode_session.h"
#include <tuple>
#include <utility>
#include "ash/accessibility/accessibility_controller_impl.h"
#include "ash/accessibility/magnifier/magnifier_glass.h"
#include "ash/capture_mode/capture_label_view.h"
#include "ash/capture_mode/capture_mode_bar_view.h"
#include "ash/capture_mode/capture_mode_behavior.h"
#include "ash/capture_mode/capture_mode_camera_controller.h"
#include "ash/capture_mode/capture_mode_camera_preview_view.h"
#include "ash/capture_mode/capture_mode_constants.h"
#include "ash/capture_mode/capture_mode_controller.h"
#include "ash/capture_mode/capture_mode_session_focus_cycler.h"
#include "ash/capture_mode/capture_mode_settings_view.h"
#include "ash/capture_mode/capture_mode_type_view.h"
#include "ash/capture_mode/capture_mode_types.h"
#include "ash/capture_mode/capture_mode_util.h"
#include "ash/capture_mode/capture_window_observer.h"
#include "ash/capture_mode/folder_selection_dialog_controller.h"
#include "ash/capture_mode/recording_type_menu_view.h"
#include "ash/capture_mode/user_nudge_controller.h"
#include "ash/display/mouse_cursor_event_filter.h"
#include "ash/display/screen_orientation_controller.h"
#include "ash/display/window_tree_host_manager.h"
#include "ash/keyboard/ui/keyboard_ui_controller.h"
#include "ash/projector/projector_controller_impl.h"
#include "ash/public/cpp/resources/grit/ash_public_unscaled_resources.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/wm/mru_window_tracker.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "ash/wm/window_dimmer.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "cc/paint/paint_flags.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/aura/window_observer.h"
#include "ui/aura/window_tracker.h"
#include "ui/base/cursor/cursor_factory.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/color/color_id.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animation_element.h"
#include "ui/compositor/layer_type.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/display/screen.h"
#include "ui/display/types/display_constants.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/transform_util.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/gfx/shadow_value.h"
#include "ui/gfx/skia_paint_util.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/background.h"
#include "ui/views/controls/label.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/coordinate_conversion.h"
#include "ui/wm/core/cursor_util.h"
namespace ash {
namespace {
constexpr int kAffordanceCircleRadiusDp = 4;
constexpr int kAffordanceCircleTouchHitRadiusDp = 16;
constexpr MagnifierGlass::Params kMagnifierParams{
2.f,
60,
2,
0,
SK_ColorWHITE,
SK_ColorTRANSPARENT,
gfx::ShadowValue(gfx::Vector2d(0, 1),
2,
SkColorSetARGB(0x4C, 0x00, 0x00, 0x00)),
gfx::ShadowValue(gfx::Vector2d(0, 1),
3,
SkColorSetARGB(0x26, 0x00, 0x00, 0x00))};
constexpr int kSizeLabelBorderRadius = 4;
constexpr int kSizeLabelHorizontalPadding = 8;
constexpr int kRegionAffordanceCircleShadow2Blur = 6;
constexpr gfx::ShadowValue kRegionOutlineShadow(gfx::Vector2d(0, 0),
2,
SkColorSetARGB(41, 0, 0, 0));
constexpr gfx::ShadowValue kRegionAffordanceCircleShadow1(
gfx::Vector2d(0, 1),
2,
SkColorSetARGB(76, 0, 0, 0));
constexpr gfx::ShadowValue kRegionAffordanceCircleShadow2(
gfx::Vector2d(0, 2),
kRegionAffordanceCircleShadow2Blur,
SkColorSetARGB(38, 0, 0, 0));
constexpr int kFocusRingStrokeWidthDp = 2;
constexpr int kFocusRingSpacingDp = 2;
constexpr int kDamageInsetDp = capture_mode::kCaptureRegionBorderStrokePx +
kAffordanceCircleRadiusDp +
kRegionAffordanceCircleShadow2Blur;
constexpr int kCaptureRegionMinimumPaddingDp = 16;
constexpr base::TimeDelta kCaptureLabelCountdownStartDuration =
base::Milliseconds(267);
constexpr base::TimeDelta kCaptureWidgetFadeOutDuration =
base::Milliseconds(167);
constexpr base::TimeDelta kCaptureShieldFadeOutDuration =
base::Milliseconds(333);
constexpr float kLabelScaleUpOnCountdown = 1.2;
constexpr base::TimeDelta kCaptureLabelRegionPhaseChangeDuration =
base::Milliseconds(167);
constexpr base::TimeDelta kCaptureLabelRegionPhaseChangeDelay =
base::Milliseconds(67);
constexpr float kLabelScaleDownOnPhaseChange = 0.8;
constexpr base::TimeDelta kCaptureUIOpacityChangeDuration =
base::Milliseconds(100);
constexpr float kRegionDefaultRatio = 0.24f;
bool SetMouseWarpEnabled(bool enable) {
auto* mouse_cursor_filter = Shell::Get()->mouse_cursor_filter();
const bool old_value = mouse_cursor_filter->mouse_warp_enabled();
mouse_cursor_filter->set_mouse_warp_enabled(enable);
return old_value;
}
aura::Window* GetParentContainer(aura::Window* root) {
DCHECK(root);
DCHECK(root->IsRootWindow());
return root->GetChildById(kShellWindowId_MenuContainer);
}
gfx::Rect GetRectEnclosingPoints(const std::vector<gfx::Point>& points) {
DCHECK_GE(points.size(), 2u);
int x = INT_MAX;
int y = INT_MAX;
int right = INT_MIN;
int bottom = INT_MIN;
for (const gfx::Point& point : points) {
x = std::min(point.x(), x);
y = std::min(point.y(), y);
right = std::max(point.x(), right);
bottom = std::max(point.y(), bottom);
}
return gfx::Rect(x, y, right - x, bottom - y);
}
views::Widget::InitParams CreateWidgetParams(aura::Window* parent,
const gfx::Rect& bounds,
const std::string& name) {
views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
params.parent = parent;
params.bounds = bounds;
params.name = name;
return params;
}
ui::Cursor GetCursorForFullscreenOrWindowCapture(bool capture_image) {
const display::Display display =
display::Screen::GetScreen()->GetDisplayNearestWindow(
capture_mode_util::GetPreferredRootWindow());
const float device_scale_factor = display.device_scale_factor();
const gfx::ImageSkia* icon =
ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
capture_image ? IDR_CAPTURE_IMAGE_CURSOR : IDR_CAPTURE_VIDEO_CURSOR);
SkBitmap bitmap = *icon->bitmap();
gfx::Point hotspot(bitmap.width() / 2, bitmap.height() / 2);
wm::ScaleAndRotateCursorBitmapAndHotpoint(
device_scale_factor, display.panel_rotation(), &bitmap, &hotspot);
ui::Cursor cursor = ui::Cursor::NewCustom(
std::move(bitmap), std::move(hotspot), device_scale_factor);
cursor.SetPlatformCursor(ui::CursorFactory::GetInstance()->CreateImageCursor(
cursor.type(), cursor.custom_bitmap(), cursor.custom_hotspot()));
return cursor;
}
ui::mojom::CursorType GetCursorTypeForFineTunePosition(
FineTunePosition position) {
switch (position) {
case FineTunePosition::kTopLeft:
return ui::mojom::CursorType::kNorthWestResize;
case FineTunePosition::kBottomRight:
return ui::mojom::CursorType::kSouthEastResize;
case FineTunePosition::kTopCenter:
case FineTunePosition::kBottomCenter:
return ui::mojom::CursorType::kNorthSouthResize;
case FineTunePosition::kTopRight:
return ui::mojom::CursorType::kNorthEastResize;
case FineTunePosition::kBottomLeft:
return ui::mojom::CursorType::kSouthWestResize;
case FineTunePosition::kLeftCenter:
case FineTunePosition::kRightCenter:
return ui::mojom::CursorType::kEastWestResize;
case FineTunePosition::kCenter:
return ui::mojom::CursorType::kMove;
default:
return ui::mojom::CursorType::kCell;
}
}
int GetArrowKeyPressChange(int event_flags) {
if ((event_flags & ui::EF_SHIFT_DOWN) != 0)
return capture_mode::kShiftArrowKeyboardRegionChangeDp;
if ((event_flags & ui::EF_CONTROL_DOWN) != 0)
return capture_mode::kCtrlArrowKeyboardRegionChangeDp;
return capture_mode::kArrowKeyboardRegionChangeDp;
}
void ClipRectToFit(gfx::Rect* out_bounds, const gfx::Rect& rect) {
out_bounds->SetByBounds(std::max(rect.x(), out_bounds->x()),
std::max(rect.y(), out_bounds->y()),
std::min(rect.right(), out_bounds->right()),
std::min(rect.bottom(), out_bounds->bottom()));
}
int GetMessageIdForInitialCaptureSource(CaptureModeSource source) {
switch (source) {
case CaptureModeSource::kFullscreen:
return IDS_ASH_SCREEN_CAPTURE_SOURCE_FULLSCREEN;
case CaptureModeSource::kRegion:
return IDS_ASH_SCREEN_CAPTURE_SOURCE_PARTIAL;
default:
return IDS_ASH_SCREEN_CAPTURE_SOURCE_WINDOW;
}
}
void UpdateFloatingPanelBoundsIfNeeded() {
Shell::Get()->accessibility_controller()->UpdateFloatingPanelBoundsIfNeeded();
}
views::Widget* GetCameraPreviewWidget() {
return CaptureModeController::Get()
->camera_controller()
->camera_preview_widget();
}
bool ShouldPassEventToCameraPreview(ui::LocatedEvent* event) {
auto* controller = CaptureModeController::Get();
if (controller->is_recording_in_progress())
return false;
auto* camera_preview_widget = GetCameraPreviewWidget();
if (!camera_preview_widget || !camera_preview_widget->IsVisible())
return false;
if (controller->camera_controller()->is_drag_in_progress())
return true;
auto* target = static_cast<aura::Window*>(event->target());
if (camera_preview_widget->GetNativeWindow()->Contains(target))
return true;
return false;
}
bool IsWidgetOverlappedWithCameraPreview(views::Widget* widget) {
if (CaptureModeController::Get()->is_recording_in_progress())
return false;
auto* camera_preview_widget = GetCameraPreviewWidget();
if (!camera_preview_widget)
return false;
return camera_preview_widget->IsVisible() &&
camera_preview_widget->GetLayer()->GetTargetOpacity() > 0.f &&
camera_preview_widget->GetWindowBoundsInScreen().Intersects(
widget->GetWindowBoundsInScreen());
}
}
class CaptureModeSession::CursorSetter {
public:
CursorSetter()
: cursor_manager_(Shell::Get()->cursor_manager()),
original_cursor_(cursor_manager_->GetCursor()),
original_cursor_visible_(cursor_manager_->IsCursorVisible()),
original_cursor_locked_(cursor_manager_->IsCursorLocked()),
current_orientation_(GetCurrentScreenOrientation()) {
if (!cursor_manager_->IsMouseEventsEnabled())
cursor_manager_->EnableMouseEvents();
}
CursorSetter(const CursorSetter&) = delete;
CursorSetter& operator=(const CursorSetter&) = delete;
~CursorSetter() { ResetCursor(); }
void UpdateCursor(const ui::Cursor& cursor) {
if (original_cursor_locked_)
return;
if (in_cursor_update_)
return;
base::AutoReset<bool> auto_reset_in_cursor_update(&in_cursor_update_, true);
const ui::mojom::CursorType current_cursor_type =
cursor_manager_->GetCursor().type();
const ui::mojom::CursorType new_cursor_type = cursor.type();
const CaptureModeType capture_type = CaptureModeController::Get()->type();
const float device_scale_factor =
display::Screen::GetScreen()
->GetDisplayNearestWindow(
capture_mode_util::GetPreferredRootWindow())
.device_scale_factor();
const chromeos::OrientationType orientation = GetCurrentScreenOrientation();
const bool is_cursor_changed =
current_cursor_type != new_cursor_type ||
(current_cursor_type == ui::mojom::CursorType::kCustom &&
(custom_cursor_capture_type_ != capture_type ||
custom_cursor_device_scale_factor_ != device_scale_factor ||
current_orientation_ != orientation));
const bool is_cursor_visibility_changed =
cursor_manager_->IsCursorVisible() !=
(new_cursor_type != ui::mojom::CursorType::kNone);
if (new_cursor_type == ui::mojom::CursorType::kCustom) {
custom_cursor_capture_type_ = capture_type;
custom_cursor_device_scale_factor_ = device_scale_factor;
}
current_orientation_ = orientation;
if (!is_cursor_changed && !is_cursor_visibility_changed)
return;
if (cursor_manager_->IsCursorLocked())
cursor_manager_->UnlockCursor();
if (new_cursor_type == ui::mojom::CursorType::kNone) {
cursor_manager_->HideCursor();
} else {
cursor_manager_->SetCursor(cursor);
cursor_manager_->ShowCursor();
}
cursor_manager_->LockCursor();
was_cursor_reset_to_original_ = false;
}
void ResetCursor() {
if (original_cursor_locked_)
return;
if (was_cursor_reset_to_original_)
return;
if (cursor_manager_->IsCursorLocked())
cursor_manager_->UnlockCursor();
cursor_manager_->SetCursor(original_cursor_);
if (original_cursor_visible_)
cursor_manager_->ShowCursor();
else
cursor_manager_->HideCursor();
was_cursor_reset_to_original_ = true;
}
bool IsCursorVisible() const { return cursor_manager_->IsCursorVisible(); }
void HideCursor() {
if (original_cursor_locked_ || !IsCursorVisible())
return;
if (cursor_manager_->IsCursorLocked())
cursor_manager_->UnlockCursor();
cursor_manager_->HideCursor();
cursor_manager_->LockCursor();
was_cursor_reset_to_original_ = false;
}
bool IsUsingCustomCursor(CaptureModeType type) const {
return cursor_manager_->GetCursor().type() ==
ui::mojom::CursorType::kCustom &&
custom_cursor_capture_type_ == type;
}
private:
const raw_ptr<wm::CursorManager, ExperimentalAsh> cursor_manager_;
const gfx::NativeCursor original_cursor_;
const bool original_cursor_visible_;
const bool original_cursor_locked_;
CaptureModeType custom_cursor_capture_type_ = CaptureModeType::kImage;
float custom_cursor_device_scale_factor_ = 1.f;
chromeos::OrientationType current_orientation_;
bool was_cursor_reset_to_original_ = true;
bool in_cursor_update_ = false;
};
class CaptureModeSession::ParentContainerObserver
: public aura::WindowObserver {
public:
ParentContainerObserver(aura::Window* parent_container,
CaptureModeSession* capture_mode_session)
: parent_container_(parent_container),
capture_mode_session_(capture_mode_session) {
parent_container_->AddObserver(this);
}
ParentContainerObserver(const ParentContainerObserver&) = delete;
ParentContainerObserver& operator=(const ParentContainerObserver&) = delete;
~ParentContainerObserver() override {
if (parent_container_)
parent_container_->RemoveObserver(this);
}
void OnWindowAdded(aura::Window* window) override {
DeferredRefreshStackingOrder();
}
void OnWindowRemoved(aura::Window* window) override {
DeferredRefreshStackingOrder();
}
void OnWindowDestroying(aura::Window* window) override {
parent_container_->RemoveObserver(this);
parent_container_ = nullptr;
}
private:
void DeferredRefreshStackingOrder() {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(
[](base::WeakPtr<CaptureModeSession> capture_mode_session) {
if (capture_mode_session) {
capture_mode_session->RefreshStackingOrder();
capture_mode_session->MaybeUpdateCaptureUisOpacity();
}
},
capture_mode_session_->weak_ptr_factory_.GetWeakPtr()));
}
raw_ptr<aura::Window, ExperimentalAsh> parent_container_;
const raw_ptr<CaptureModeSession, ExperimentalAsh> capture_mode_session_;
};
CaptureModeSession::CaptureModeSession(CaptureModeController* controller,
CaptureModeBehavior* active_behavior,
bool projector_mode)
: controller_(controller),
active_behavior_(active_behavior),
current_root_(capture_mode_util::GetPreferredRootWindow()),
magnifier_glass_(kMagnifierParams),
is_in_projector_mode_(projector_mode),
cursor_setter_(std::make_unique<CursorSetter>()),
focus_cycler_(std::make_unique<CaptureModeSessionFocusCycler>(this)),
capture_toast_controller_(this) {
CHECK(current_root_);
}
CaptureModeSession::~CaptureModeSession() = default;
void CaptureModeSession::Initialize() {
capture_mode_util::TriggerAccessibilityAlert(l10n_util::GetStringFUTF8(
IDS_ASH_SCREEN_CAPTURE_ALERT_OPEN,
l10n_util::GetStringUTF16(
GetMessageIdForInitialCaptureSource(controller_->source())),
l10n_util::GetStringUTF16(
controller_->type() == CaptureModeType::kImage
? IDS_ASH_SCREEN_CAPTURE_TYPE_SCREENSHOT
: IDS_ASH_SCREEN_CAPTURE_TYPE_SCREEN_RECORDING)));
if (auto* capture_client = aura::client::GetCaptureClient(current_root_)) {
input_capture_window_ = capture_client->GetCaptureWindow();
if (input_capture_window_) {
aura::WindowTracker tracker({input_capture_window_});
capture_client->ReleaseCapture(input_capture_window_);
if (tracker.windows().empty()) {
input_capture_window_ = nullptr;
} else {
input_capture_window_->AddObserver(this);
}
}
}
SetLayer(std::make_unique<ui::Layer>(ui::LAYER_TEXTURED));
layer()->SetFillsBoundsOpaquely(false);
layer()->set_delegate(this);
auto* parent = GetParentContainer(current_root_);
parent_container_observer_ =
std::make_unique<ParentContainerObserver>(parent, this);
parent->layer()->Add(layer());
layer()->SetBounds(parent->bounds());
layer()->SetName("CaptureModeSession");
ClampCaptureRegionToRootWindowSize();
capture_mode_bar_widget_->Init(CreateWidgetParams(
parent, CaptureModeBarView::GetBounds(current_root_, active_behavior_),
"CaptureModeBarWidget"));
capture_mode_bar_view_ = capture_mode_bar_widget_->SetContentsView(
std::make_unique<CaptureModeBarView>(active_behavior_));
capture_mode_bar_widget_->GetNativeWindow()->SetTitle(
l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_A11Y_TITLE));
capture_mode_bar_widget_->Show();
if (Shell::Get()->accessibility_controller()->spoken_feedback().enabled())
focus_cycler_->AdvanceFocus(false);
UpdateCaptureLabelWidget(CaptureLabelAnimation::kNone);
UpdateCursor(display::Screen::GetScreen()->GetCursorScreenPoint(),
false);
if (controller_->source() == CaptureModeSource::kWindow)
capture_window_observer_ = std::make_unique<CaptureWindowObserver>(this);
UpdateRootWindowDimmers();
TabletModeController::Get()->AddObserver(this);
display_observer_.emplace(this);
aura::Env::GetInstance()->AddPreTargetHandler(
this, ui::EventTarget::Priority::kSystem);
UpdateFloatingPanelBoundsIfNeeded();
capture_mode_bar_view_->capture_type_view()->OnCaptureTypeChanged(
controller_->type());
MaybeCreateUserNudge();
if (active_behavior_->ShouldAutoSelectFirstCamera()) {
controller_->camera_controller()->MaybeSelectFirstCamera();
}
Shell::Get()->AddShellObserver(this);
}
void CaptureModeSession::Shutdown() {
is_shutting_down_ = true;
aura::Env::GetInstance()->RemovePreTargetHandler(this);
display_observer_.reset();
user_nudge_controller_.reset();
capture_window_observer_.reset();
TabletModeController::Get()->RemoveObserver(this);
if (input_capture_window_) {
input_capture_window_->RemoveObserver(this);
if (auto* client = aura::client::GetCaptureClient(
input_capture_window_->GetRootWindow())) {
client->SetCapture(input_capture_window_);
}
}
if (old_mouse_warp_status_)
SetMouseWarpEnabled(*old_mouse_warp_status_);
for (auto* widget : GetAvailableWidgets())
widget->CloseNow();
if (a11y_alert_on_session_exit_) {
capture_mode_util::TriggerAccessibilityAlert(
IDS_ASH_SCREEN_CAPTURE_ALERT_CLOSE);
}
UpdateFloatingPanelBoundsIfNeeded();
if (!is_stopping_to_start_video_recording_) {
if (!controller_->is_recording_in_progress())
controller_->camera_controller()->SetShouldShowPreview(false);
if (is_in_projector_mode_) {
ProjectorControllerImpl::Get()->OnRecordingStartAborted();
controller_->camera_controller()->MaybeRevertAutoCameraSelection();
controller_->MaybeRestoreCachedCaptureConfigurations();
}
}
Shell::Get()->RemoveShellObserver(this);
}
aura::Window* CaptureModeSession::GetSelectedWindow() const {
return capture_window_observer_ ? capture_window_observer_->window()
: nullptr;
}
void CaptureModeSession::A11yAlertCaptureSource(bool trigger_now) {
auto* controller = CaptureModeController::Get();
const bool is_capturing_image = controller->type() == CaptureModeType::kImage;
std::string message;
switch (controller->source()) {
case CaptureModeSource::kFullscreen:
message = l10n_util::GetStringUTF8(
is_capturing_image
? IDS_ASH_SCREEN_CAPTURE_ALERT_FULLSCREEN_SCREENSHOT
: IDS_ASH_SCREEN_CAPTURE_ALERT_FULLSCREEN_RECORD);
break;
case CaptureModeSource::kRegion:
if (!controller->user_capture_region().IsEmpty()) {
message = l10n_util::GetStringUTF8(
is_capturing_image ? IDS_ASH_SCREEN_CAPTURE_ALERT_REGION_SCREENSHOT
: IDS_ASH_SCREEN_CAPTURE_ALERT_REGION_RECORD);
}
break;
case CaptureModeSource::kWindow:
if (GetSelectedWindow()) {
message = l10n_util::GetStringUTF8(
is_capturing_image ? IDS_ASH_SCREEN_CAPTURE_ALERT_WINDOW_SCREENSHOT
: IDS_ASH_SCREEN_CAPTURE_ALERT_WINDOW_RECORD);
}
break;
}
if (!message.empty()) {
if (trigger_now)
capture_mode_util::TriggerAccessibilityAlert(message);
else
capture_mode_util::TriggerAccessibilityAlertSoon(message);
}
}
void CaptureModeSession::A11yAlertCaptureType() {
capture_mode_util::TriggerAccessibilityAlert(
CaptureModeController::Get()->type() == CaptureModeType::kImage
? IDS_ASH_SCREEN_CAPTURE_ALERT_SELECT_TYPE_IMAGE
: IDS_ASH_SCREEN_CAPTURE_ALERT_SELECT_TYPE_VIDEO);
A11yAlertCaptureSource(false);
}
void CaptureModeSession::OnCaptureSourceChanged(CaptureModeSource new_source) {
capture_source_changed_ = true;
if (new_source == CaptureModeSource::kWindow)
capture_window_observer_ = std::make_unique<CaptureWindowObserver>(this);
else
capture_window_observer_.reset();
if (new_source == CaptureModeSource::kRegion)
num_capture_region_adjusted_ = 0;
capture_mode_bar_view_->OnCaptureSourceChanged(new_source);
UpdateDimensionsLabelWidget(false);
layer()->SchedulePaint(layer()->bounds());
UpdateCaptureLabelWidget(CaptureLabelAnimation::kNone);
UpdateCursor(display::Screen::GetScreen()->GetCursorScreenPoint(),
false);
if (focus_cycler_->RegionGroupFocused())
focus_cycler_->ClearFocus();
A11yAlertCaptureSource(true);
MaybeReparentCameraPreviewWidget();
}
void CaptureModeSession::OnCaptureTypeChanged(CaptureModeType new_type) {
capture_mode_bar_view_->OnCaptureTypeChanged(new_type);
UpdateCaptureLabelWidget(CaptureLabelAnimation::kNone);
UpdateCursor(display::Screen::GetScreen()->GetCursorScreenPoint(),
false);
A11yAlertCaptureType();
}
void CaptureModeSession::OnRecordingTypeChanged() {
if (capture_label_view_) {
capture_label_view_->UpdateIconAndText();
UpdateCaptureLabelWidgetBounds(CaptureLabelAnimation::kNone);
}
}
void CaptureModeSession::OnWaitingForDlpConfirmationStarted() {
is_waiting_for_dlp_confirmation_ = true;
HideAllUis();
}
void CaptureModeSession::OnWaitingForDlpConfirmationEnded(bool reshow_uis) {
is_waiting_for_dlp_confirmation_ = false;
if (reshow_uis)
ShowAllUis();
}
void CaptureModeSession::SetSettingsMenuShown(bool shown, bool by_key_event) {
capture_mode_bar_view_->SetSettingsMenuShown(shown);
if (!shown) {
capture_mode_settings_widget_.reset();
capture_mode_settings_view_ = nullptr;
if (capture_label_widget_ && !capture_label_widget_->IsVisible())
capture_label_widget_->Show();
return;
}
if (!capture_mode_settings_widget_) {
SetRecordingTypeMenuShown(false);
auto* parent = GetParentContainer(current_root_);
capture_mode_settings_widget_ = std::make_unique<views::Widget>();
MaybeDismissUserNudgeForever();
capture_toast_controller_.DismissCurrentToastIfAny();
capture_mode_settings_widget_->Init(CreateWidgetParams(
parent, CaptureModeSettingsView::GetBounds(capture_mode_bar_view_),
"CaptureModeSettingsWidget"));
capture_mode_settings_view_ =
capture_mode_settings_widget_->SetContentsView(
std::make_unique<CaptureModeSettingsView>(this,
is_in_projector_mode_));
OnCaptureFolderMayHaveChanged();
parent->layer()->StackAtTop(capture_mode_settings_widget_->GetLayer());
focus_cycler_->OnMenuOpened(
capture_mode_settings_widget_.get(),
CaptureModeSessionFocusCycler::FocusGroup::kPendingSettings,
by_key_event);
if (capture_label_widget_ && capture_label_widget_->IsVisible()) {
if (capture_mode_settings_widget_->GetWindowBoundsInScreen().Intersects(
capture_label_widget_->GetWindowBoundsInScreen())) {
capture_label_widget_->Hide();
}
}
capture_mode_settings_widget_->GetNativeWindow()->SetTitle(
l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_SETTINGS_A11Y_TITLE));
capture_mode_settings_widget_->Show();
}
}
void CaptureModeSession::ReportSessionHistograms() {
const CaptureModeSource source = controller_->source();
const RecordingType recording_type = controller_->recording_type();
if (source == CaptureModeSource::kRegion) {
RecordNumberOfCaptureRegionAdjustments(num_capture_region_adjusted_,
is_in_projector_mode_);
const auto region_in_root = controller_->user_capture_region();
if (is_stopping_to_start_video_recording_ &&
recording_type == RecordingType::kGif && !region_in_root.IsEmpty()) {
RecordGifRegionToScreenRatio(100.0f * region_in_root.size().GetArea() /
current_root_->bounds().size().GetArea());
}
}
num_capture_region_adjusted_ = 0;
RecordCaptureModeSwitchesFromInitialMode(capture_source_changed_);
RecordCaptureModeConfiguration(controller_->type(), source, recording_type,
controller_->GetAudioRecordingEnabled(),
is_in_projector_mode_);
}
void CaptureModeSession::StartCountDown(
base::OnceClosure countdown_finished_callback) {
DCHECK(capture_label_widget_);
if (!capture_label_widget_->IsVisible())
capture_label_widget_->Show();
DCHECK(capture_label_view_);
capture_label_view_->StartCountDown(std::move(countdown_finished_callback));
UpdateCaptureLabelWidgetBounds(CaptureLabelAnimation::kCountdownStart);
UpdateCursor(display::Screen::GetScreen()->GetCursorScreenPoint(),
false);
std::vector<ui::Layer*> layers_to_fade_out{
capture_mode_bar_widget_->GetLayer()};
if (capture_mode_settings_widget_)
layers_to_fade_out.push_back(capture_mode_settings_widget_->GetLayer());
if (recording_type_menu_widget_)
layers_to_fade_out.push_back(recording_type_menu_widget_->GetLayer());
if (auto* toast_layer = capture_toast_controller_.MaybeGetToastLayer())
layers_to_fade_out.push_back(toast_layer);
for (auto* layer : layers_to_fade_out) {
ui::ScopedLayerAnimationSettings layer_settings(layer->GetAnimator());
layer_settings.SetTransitionDuration(kCaptureWidgetFadeOutDuration);
layer_settings.SetTweenType(gfx::Tween::FAST_OUT_SLOW_IN);
layer_settings.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
layer->SetOpacity(0.f);
}
RepaintRegion();
if (controller_->source() == CaptureModeSource::kFullscreen) {
ui::ScopedLayerAnimationSettings shield_settings(layer()->GetAnimator());
shield_settings.SetTransitionDuration(kCaptureShieldFadeOutDuration);
shield_settings.SetTweenType(gfx::Tween::FAST_OUT_SLOW_IN);
shield_settings.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
layer()->SetOpacity(0.f);
}
}
void CaptureModeSession::OpenFolderSelectionDialog() {
DCHECK(!folder_selection_dialog_controller_);
folder_selection_dialog_controller_ =
std::make_unique<FolderSelectionDialogController>(this,
current_root_);
MaybeUpdateCameraPreviewBounds();
}
bool CaptureModeSession::IsInCountDownAnimation() const {
if (is_shutting_down_)
return false;
DCHECK(capture_label_view_);
return capture_label_view_->IsInCountDownAnimation();
}
void CaptureModeSession::OnCaptureFolderMayHaveChanged() {
if (!capture_mode_settings_widget_)
return;
DCHECK(capture_mode_settings_view_);
capture_mode_settings_view_->OnCaptureFolderMayHaveChanged();
MaybeUpdateSettingsBounds();
}
void CaptureModeSession::OnDefaultCaptureFolderSelectionChanged() {
if (!capture_mode_settings_widget_)
return;
DCHECK(capture_mode_settings_view_);
capture_mode_settings_view_->OnDefaultCaptureFolderSelectionChanged();
}
aura::Window* CaptureModeSession::GetOnCaptureSurfaceWidgetParentWindow()
const {
auto* controller = CaptureModeController::Get();
DCHECK(!controller->is_recording_in_progress());
auto* menu_container =
current_root_->GetChildById(kShellWindowId_MenuContainer);
auto* unparented_container =
current_root_->GetChildById(kShellWindowId_UnparentedContainer);
switch (controller->source()) {
case CaptureModeSource::kFullscreen:
return menu_container;
case CaptureModeSource::kRegion:
return controller_->user_capture_region().IsEmpty() ? unparented_container
: menu_container;
case CaptureModeSource::kWindow:
aura::Window* selected_window = GetSelectedWindow();
return selected_window ? selected_window : unparented_container;
}
}
gfx::Rect CaptureModeSession::GetCaptureSurfaceConfineBounds() const {
auto* controller = CaptureModeController::Get();
DCHECK(!controller->is_recording_in_progress());
switch (controller->source()) {
case CaptureModeSource::kFullscreen: {
auto* parent = GetOnCaptureSurfaceWidgetParentWindow();
DCHECK(parent);
return display::Screen::GetScreen()
->GetDisplayNearestWindow(parent)
.work_area();
}
case CaptureModeSource::kWindow: {
aura::Window* selected_window = GetSelectedWindow();
return selected_window ? gfx::Rect(selected_window->bounds().size())
: gfx::Rect();
}
case CaptureModeSource::kRegion: {
gfx::Rect capture_region = controller->user_capture_region();
wm::ConvertRectToScreen(current_root_, &capture_region);
return capture_region;
}
}
}
bool CaptureModeSession::CalculateCameraPreviewTargetVisibility() const {
if (controller_->is_recording_in_progress())
return true;
if (folder_selection_dialog_controller_)
return false;
if (controller_->source() != CaptureModeSource::kRegion)
return true;
if (controller_->user_capture_region().IsEmpty())
return false;
return !is_drag_in_progress_ ||
fine_tune_position_ == FineTunePosition::kCenter;
}
void CaptureModeSession::OnPaintLayer(const ui::PaintContext& context) {
if (!is_all_uis_visible_ &&
!controller_->camera_controller()->is_drag_in_progress()) {
return;
}
ui::PaintRecorder recorder(context, layer()->size());
recorder.canvas()->DrawColor(capture_mode::kDimmingShieldColor);
PaintCaptureRegion(recorder.canvas());
}
void CaptureModeSession::OnKeyEvent(ui::KeyEvent* event) {
if (is_waiting_for_dlp_confirmation_)
return;
if (folder_selection_dialog_controller_) {
if (folder_selection_dialog_controller_->ShouldConsumeEvent(event))
event->StopPropagation();
return;
}
if (event->type() != ui::ET_KEY_PRESSED)
return;
auto* camera_preview_view =
controller_->camera_controller()->camera_preview_view();
if (camera_preview_view && camera_preview_view->MaybeHandleKeyEvent(event)) {
event->StopPropagation();
return;
}
auto should_update_opacity = std::make_unique<bool>(false);
auto* should_update_opacity_ptr = should_update_opacity.get();
base::ScopedClosureRunner deferred_runner(base::BindOnce(
[](base::WeakPtr<CaptureModeSession> session,
std::unique_ptr<bool> should_update_opacity) {
if (*should_update_opacity && session)
session->MaybeUpdateCaptureUisOpacity();
},
weak_ptr_factory_.GetWeakPtr(), std::move(should_update_opacity)));
ui::KeyboardCode key_code = event->key_code();
switch (key_code) {
case ui::VKEY_ESCAPE: {
event->StopPropagation();
*should_update_opacity_ptr = true;
const bool is_in_count_down = IsInCountDownAnimation();
if (recording_type_menu_widget_ && !is_in_count_down)
SetRecordingTypeMenuShown(false);
else if (capture_mode_settings_widget_ && !is_in_count_down)
SetSettingsMenuShown(false);
else if (focus_cycler_->HasFocus() && !is_in_count_down)
focus_cycler_->ClearFocus();
else if (can_exit_on_escape_)
controller_->Stop();
return;
}
case ui::VKEY_RETURN: {
event->StopPropagation();
if (!IsInCountDownAnimation())
DoPerformCapture();
return;
}
case ui::VKEY_SPACE: {
event->StopPropagation();
event->SetHandled();
if (focus_cycler_->OnSpacePressed()) {
*should_update_opacity_ptr = true;
return;
}
if (controller_->source() == CaptureModeSource::kRegion &&
controller_->user_capture_region().IsEmpty()) {
SelectDefaultRegion();
}
return;
}
case ui::VKEY_TAB: {
event->StopPropagation();
event->SetHandled();
focus_cycler_->AdvanceFocus(event->IsShiftDown());
*should_update_opacity_ptr = true;
return;
}
case ui::VKEY_UP:
case ui::VKEY_DOWN: {
event->StopPropagation();
event->SetHandled();
UpdateRegionVertically(key_code == ui::VKEY_UP, event->flags());
return;
}
case ui::VKEY_LEFT:
case ui::VKEY_RIGHT: {
event->StopPropagation();
event->SetHandled();
UpdateRegionHorizontally(key_code == ui::VKEY_LEFT,
event->flags());
return;
}
default:
return;
}
}
void CaptureModeSession::OnMouseEvent(ui::MouseEvent* event) {
if (is_waiting_for_dlp_confirmation_)
return;
OnLocatedEvent(event, false);
}
void CaptureModeSession::OnTouchEvent(ui::TouchEvent* event) {
if (is_waiting_for_dlp_confirmation_)
return;
OnLocatedEvent(event, true);
}
void CaptureModeSession::OnTabletModeStarted() {
UpdateCaptureLabelWidget(CaptureLabelAnimation::kNone);
UpdateCursor(display::Screen::GetScreen()->GetCursorScreenPoint(),
false);
}
void CaptureModeSession::OnTabletModeEnded() {
UpdateCaptureLabelWidget(CaptureLabelAnimation::kNone);
UpdateCursor(display::Screen::GetScreen()->GetCursorScreenPoint(),
false);
}
void CaptureModeSession::OnWindowDestroying(aura::Window* window) {
DCHECK_EQ(window, input_capture_window_);
input_capture_window_->RemoveObserver(this);
input_capture_window_ = nullptr;
}
void CaptureModeSession::OnRootWindowWillShutdown(aura::Window* root_window) {
if (root_window == current_root_) {
DCHECK_NE(Shell::GetPrimaryRootWindow(), current_root_);
MaybeChangeRoot(Shell::GetPrimaryRootWindow());
}
}
void CaptureModeSession::OnDisplayMetricsChanged(
const display::Display& display,
uint32_t metrics) {
if (!(metrics &
(DISPLAY_METRIC_BOUNDS | DISPLAY_METRIC_ROTATION |
DISPLAY_METRIC_DEVICE_SCALE_FACTOR | DISPLAY_METRIC_WORK_AREA))) {
return;
}
EndSelection();
UpdateCursor(display::Screen::GetScreen()->GetCursorScreenPoint(),
false);
ClampCaptureRegionToRootWindowSize();
auto* parent = GetParentContainer(current_root_);
DCHECK_EQ(parent->layer(), layer()->parent());
layer()->SetBounds(parent->bounds());
RefreshBarWidgetBounds();
if (controller_->source() == CaptureModeSource::kFullscreen &&
!controller_->is_recording_in_progress()) {
controller_->camera_controller()->MaybeUpdatePreviewWidget();
}
if (capture_label_widget_)
UpdateCaptureLabelWidget(CaptureLabelAnimation::kNone);
layer()->SchedulePaint(layer()->bounds());
MaybeUpdateCaptureUisOpacity();
}
void CaptureModeSession::OnFolderSelected(const base::FilePath& path) {
CaptureModeController::Get()->SetCustomCaptureFolder(path);
if (controller_->GetCurrentCaptureFolder().is_default_downloads_folder) {
RecordSwitchToDefaultFolderReason(
CaptureModeSwitchToDefaultReason::
kUserSelectedFromFolderSelectionDialog);
}
}
void CaptureModeSession::OnSelectionWindowAdded() {
HideAllUis();
}
void CaptureModeSession::OnSelectionWindowClosed() {
DCHECK(folder_selection_dialog_controller_);
ShowAllUis();
const bool did_user_select_a_folder =
folder_selection_dialog_controller_->did_user_select_a_folder();
folder_selection_dialog_controller_.reset();
MaybeUpdateCameraPreviewBounds();
if (!did_user_select_a_folder)
OnCaptureFolderMayHaveChanged();
keyboard::KeyboardUIController::Get()->HideKeyboardExplicitlyBySystem();
}
void CaptureModeSession::UpdateCursor(const gfx::Point& location_in_screen,
bool is_touch) {
if (is_shutting_down_ || !cursor_setter_)
return;
auto* tablet_mode_controller = Shell::Get()->tablet_mode_controller();
if (tablet_mode_controller->InTabletMode() &&
!tablet_mode_controller->IsInDevTabletMode()) {
cursor_setter_->HideCursor();
return;
}
if (IsInCountDownAnimation()) {
cursor_setter_->UpdateCursor(ui::mojom::CursorType::kPointer);
return;
}
if (should_pass_located_event_to_camera_preview_) {
cursor_setter_->UpdateCursor(ui::mojom::CursorType::kPointer);
return;
}
DCHECK(capture_label_view_);
const bool is_event_on_capture_button =
capture_label_widget_->GetWindowBoundsInScreen().Contains(
location_in_screen) &&
capture_label_view_->ShouldHandleEvent();
if (is_event_on_capture_button) {
cursor_setter_->UpdateCursor(ui::mojom::CursorType::kHand);
return;
}
const bool is_event_on_capture_bar =
capture_mode_bar_widget_->GetLayer()->GetTargetOpacity() &&
capture_mode_bar_widget_->GetWindowBoundsInScreen().Contains(
location_in_screen);
if (capture_mode_settings_widget_ || is_event_on_capture_bar ||
recording_type_menu_widget_) {
cursor_setter_->UpdateCursor(ui::mojom::CursorType::kPointer);
return;
}
const CaptureModeSource source = controller_->source();
if (source == CaptureModeSource::kWindow && !GetSelectedWindow()) {
cursor_setter_->UpdateCursor(ui::mojom::CursorType::kPointer);
return;
}
if (source == CaptureModeSource::kFullscreen ||
source == CaptureModeSource::kWindow) {
cursor_setter_->UpdateCursor(GetCursorForFullscreenOrWindowCapture(
controller_->type() == CaptureModeType::kImage));
return;
}
DCHECK_EQ(source, CaptureModeSource::kRegion);
if (fine_tune_position_ != FineTunePosition::kNone) {
if (capture_mode_util::IsCornerFineTunePosition(fine_tune_position_)) {
cursor_setter_->HideCursor();
} else {
cursor_setter_->UpdateCursor(
GetCursorTypeForFineTunePosition(fine_tune_position_));
}
} else {
cursor_setter_->UpdateCursor(GetCursorTypeForFineTunePosition(
GetFineTunePosition(location_in_screen, is_touch)));
}
}
void CaptureModeSession::HighlightWindowForTab(aura::Window* window) {
DCHECK(window);
DCHECK_EQ(CaptureModeSource::kWindow, controller_->source());
MaybeChangeRoot(window->GetRootWindow());
capture_window_observer_->SetSelectedWindow(window);
}
void CaptureModeSession::MaybeUpdateSettingsBounds() {
if (!capture_mode_settings_widget_)
return;
capture_mode_settings_widget_->SetBounds(CaptureModeSettingsView::GetBounds(
capture_mode_bar_view_, capture_mode_settings_view_));
}
void CaptureModeSession::MaybeUpdateCaptureUisOpacity(
absl::optional<gfx::Point> cursor_screen_location) {
if (is_shutting_down_)
return;
if (!cursor_screen_location) {
cursor_screen_location =
display::Screen::GetScreen()->GetCursorScreenPoint();
}
base::flat_map<views::Widget*, /*opacity=*/float> widget_opacity_map;
if (capture_mode_bar_widget_)
widget_opacity_map[capture_mode_bar_widget_.get()] = 1.f;
if (capture_label_widget_)
widget_opacity_map[capture_label_widget_.get()] = 1.f;
const bool is_settings_visible = capture_mode_settings_widget_ &&
capture_mode_settings_widget_->IsVisible();
const bool is_recording_type_menu_visible =
recording_type_menu_widget_ && recording_type_menu_widget_->IsVisible();
gfx::Rect capture_region = controller_->user_capture_region();
wm::ConvertRectToScreen(current_root_, &capture_region);
for (auto& pair : widget_opacity_map) {
views::Widget* widget = pair.first;
float& opacity = pair.second;
DCHECK(widget->GetLayer());
const gfx::Rect window_bounds_in_screen = widget->GetWindowBoundsInScreen();
const bool is_cursor_on_top_of_widget =
window_bounds_in_screen.Contains(*cursor_screen_location);
if (widget == capture_mode_bar_widget_.get()) {
if (is_settings_visible)
continue;
if (is_drag_in_progress_) {
opacity = 0.f;
continue;
}
if (is_cursor_on_top_of_widget)
continue;
if (capture_label_widget_ &&
capture_label_widget_->GetWindowBoundsInScreen().Contains(
*cursor_screen_location)) {
continue;
}
const bool capture_bar_intersects_region =
controller_->source() == CaptureModeSource::kRegion &&
window_bounds_in_screen.Intersects(capture_region);
if (capture_bar_intersects_region) {
opacity = capture_mode::kCaptureUiOverlapOpacity;
continue;
}
if (focus_cycler_->CaptureBarFocused())
continue;
}
if (widget == capture_label_widget_.get() &&
(is_cursor_on_top_of_widget || focus_cycler_->CaptureLabelFocused() ||
is_recording_type_menu_visible)) {
continue;
}
if (IsWidgetOverlappedWithCameraPreview(widget))
opacity = capture_mode::kCaptureUiOverlapOpacity;
}
for (const auto& pair : widget_opacity_map) {
ui::Layer* layer = pair.first->GetLayer();
const float& opacity = pair.second;
if (layer->GetTargetOpacity() == opacity)
continue;
views::AnimationBuilder()
.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
.Once()
.SetDuration(kCaptureUIOpacityChangeDuration)
.SetOpacity(layer, opacity, gfx::Tween::FAST_OUT_SLOW_IN);
}
}
void CaptureModeSession::OnCameraPreviewDragStarted() {
DCHECK(!controller_->is_recording_in_progress());
SetSettingsMenuShown(false);
SetRecordingTypeMenuShown(false);
HideAllUis();
}
void CaptureModeSession::OnCameraPreviewDragEnded(
const gfx::Point& screen_location,
bool is_touch) {
auto* camera_preview_widget = GetCameraPreviewWidget();
DCHECK(camera_preview_widget);
if (!camera_preview_widget->GetWindowBoundsInScreen().Contains(
screen_location)) {
should_pass_located_event_to_camera_preview_ = false;
}
MaybeUpdateCaptureUisOpacity(screen_location);
ShowAllUis();
UpdateCursor(screen_location, is_touch);
}
void CaptureModeSession::OnCameraPreviewBoundsOrVisibilityChanged(
bool capture_surface_became_too_small,
bool did_bounds_or_visibility_change) {
auto* camera_preview_widget = GetCameraPreviewWidget();
DCHECK(camera_preview_widget);
const bool is_parented_to_unparented_container =
camera_preview_widget->GetNativeWindow()->parent()->GetId() ==
kShellWindowId_UnparentedContainer;
if (capture_surface_became_too_small && !is_drag_in_progress_ &&
!is_parented_to_unparented_container) {
MaybeDismissUserNudgeForever();
capture_toast_controller_.ShowCaptureToast(
CaptureToastType::kCameraPreview);
} else {
capture_toast_controller_.MaybeDismissCaptureToast(
CaptureToastType::kCameraPreview);
}
if (did_bounds_or_visibility_change)
MaybeUpdateCaptureUisOpacity();
}
void CaptureModeSession::OnCameraPreviewDestroyed() {
capture_toast_controller_.MaybeDismissCaptureToast(
CaptureToastType::kCameraPreview);
}
std::vector<views::Widget*> CaptureModeSession::GetAvailableWidgets() {
std::vector<views::Widget*> result;
DCHECK(capture_mode_bar_widget_);
result.push_back(capture_mode_bar_widget_.get());
if (capture_label_widget_)
result.push_back(capture_label_widget_.get());
if (recording_type_menu_widget_)
result.push_back(recording_type_menu_widget_.get());
if (capture_mode_settings_widget_)
result.push_back(capture_mode_settings_widget_.get());
if (dimensions_label_widget_)
result.push_back(dimensions_label_widget_.get());
if (auto* toast = capture_toast_controller_.capture_toast_widget())
result.push_back(toast);
return result;
}
void CaptureModeSession::HideAllUis() {
is_all_uis_visible_ = false;
cursor_setter_.reset();
for (auto* widget : GetAvailableWidgets()) {
widget->GetNativeWindow()->SetProperty(aura::client::kAnimationsDisabledKey,
true);
widget->GetLayer()->SetOpacity(1.f);
widget->Hide();
}
layer()->SchedulePaint(layer()->bounds());
}
void CaptureModeSession::ShowAllUis() {
is_all_uis_visible_ = true;
cursor_setter_ = std::make_unique<CursorSetter>();
for (auto* widget : GetAvailableWidgets()) {
if (CanShowWidget(widget))
widget->Show();
widget->GetNativeWindow()->SetProperty(aura::client::kAnimationsDisabledKey,
false);
}
layer()->SchedulePaint(layer()->bounds());
}
bool CaptureModeSession::CanShowWidget(views::Widget* widget) const {
if (widget == capture_toast_controller_.capture_toast_widget())
return !!capture_toast_controller_.current_toast_type();
return !(capture_label_widget_ && capture_mode_settings_widget_ &&
capture_label_widget_.get() == widget &&
capture_mode_settings_widget_->GetWindowBoundsInScreen().Intersects(
capture_label_widget_->GetWindowBoundsInScreen()));
}
void CaptureModeSession::RefreshBarWidgetBounds() {
DCHECK(capture_mode_bar_widget_);
capture_mode_bar_widget_->SetBounds(
CaptureModeBarView::GetBounds(current_root_, active_behavior_));
MaybeUpdateSettingsBounds();
if (user_nudge_controller_)
user_nudge_controller_->Reposition();
capture_toast_controller_.MaybeRepositionCaptureToast();
}
void CaptureModeSession::MaybeCreateUserNudge() {
user_nudge_controller_.reset();
if (!active_behavior_->ShouldShowUserNudge()) {
return;
}
if (!controller_->CanShowUserNudge()) {
return;
}
user_nudge_controller_ = std::make_unique<UserNudgeController>(
this, capture_mode_bar_view_->settings_button());
user_nudge_controller_->SetVisible(true);
}
void CaptureModeSession::MaybeDismissUserNudgeForever() {
if (user_nudge_controller_)
user_nudge_controller_->set_should_dismiss_nudge_forever(true);
user_nudge_controller_.reset();
}
void CaptureModeSession::DoPerformCapture() {
MaybeDismissUserNudgeForever();
controller_->PerformCapture();
}
void CaptureModeSession::OnRecordingTypeDropDownButtonPressed(
const ui::Event& event) {
SetRecordingTypeMenuShown(
!recording_type_menu_widget_ || !recording_type_menu_widget_->IsVisible(),
event.IsKeyEvent());
}
gfx::Rect CaptureModeSession::GetSelectedWindowBounds() const {
auto* window = GetSelectedWindow();
return window ? window->bounds() : gfx::Rect();
}
void CaptureModeSession::RefreshStackingOrder() {
if (is_shutting_down_)
return;
auto* parent_container = GetParentContainer(current_root_);
DCHECK(parent_container);
auto* session_layer = layer();
auto* parent_container_layer = parent_container->layer();
parent_container_layer->StackAtTop(session_layer);
std::vector<views::Widget*> widget_in_order;
auto* camera_preview_widget = GetCameraPreviewWidget();
if (camera_preview_widget && !controller_->is_recording_in_progress())
widget_in_order.emplace_back(camera_preview_widget);
if (auto* toast = capture_toast_controller_.capture_toast_widget())
widget_in_order.emplace_back(toast);
if (capture_label_widget_)
widget_in_order.emplace_back(capture_label_widget_.get());
if (capture_mode_bar_widget_)
widget_in_order.emplace_back(capture_mode_bar_widget_.get());
if (recording_type_menu_widget_)
widget_in_order.emplace_back(recording_type_menu_widget_.get());
if (capture_mode_settings_widget_)
widget_in_order.emplace_back(capture_mode_settings_widget_.get());
for (auto* widget : widget_in_order) {
auto* widget_window = widget->GetNativeWindow();
if (widget_window->parent() == parent_container) {
parent_container->StackChildAtTop(widget_window);
parent_container_layer->StackAtTop(widget_window->layer());
}
}
}
void CaptureModeSession::PaintCaptureRegion(gfx::Canvas* canvas) {
gfx::Rect region;
bool adjustable_region = false;
switch (controller_->source()) {
case CaptureModeSource::kFullscreen:
region = current_root_->bounds();
break;
case CaptureModeSource::kWindow:
region = GetSelectedWindowBounds();
break;
case CaptureModeSource::kRegion:
region = controller_->user_capture_region();
adjustable_region = true;
break;
}
if (region.IsEmpty())
return;
gfx::ScopedCanvas scoped_canvas(canvas);
const float dsf = canvas->UndoDeviceScaleFactor();
region = gfx::ScaleToEnclosingRect(region, dsf);
const auto* color_provider =
capture_mode_util::GetColorProviderForNativeTheme();
if (!adjustable_region) {
canvas->FillRect(region, SK_ColorTRANSPARENT, SkBlendMode::kClear);
canvas->FillRect(region,
color_provider->GetColor(kColorAshCaptureRegionColor));
return;
}
region.Inset(-capture_mode::kCaptureRegionBorderStrokePx);
canvas->FillRect(region, SK_ColorTRANSPARENT, SkBlendMode::kClear);
cc::PaintFlags border_flags;
border_flags.setColor(capture_mode::kRegionBorderColor);
border_flags.setStyle(cc::PaintFlags::kStroke_Style);
border_flags.setStrokeWidth(capture_mode::kCaptureRegionBorderStrokePx);
border_flags.setLooper(gfx::CreateShadowDrawLooper({kRegionOutlineShadow}));
canvas->DrawRect(gfx::RectF(region), border_flags);
auto maybe_draw_focus_ring = [&canvas, ®ion, dsf,
color_provider](FineTunePosition position) {
if (position == FineTunePosition::kNone)
return;
cc::PaintFlags focus_ring_flags;
focus_ring_flags.setAntiAlias(true);
focus_ring_flags.setColor(color_provider->GetColor(ui::kColorAshFocusRing));
focus_ring_flags.setStyle(cc::PaintFlags::kStroke_Style);
focus_ring_flags.setStrokeWidth(kFocusRingStrokeWidthDp);
if (position == FineTunePosition::kCenter) {
gfx::RectF focus_rect(region);
focus_rect.Inset(
gfx::InsetsF(-kFocusRingSpacingDp - kFocusRingStrokeWidthDp / 2));
canvas->DrawRect(focus_rect, focus_ring_flags);
return;
}
const float radius =
dsf * (kAffordanceCircleRadiusDp + kFocusRingSpacingDp +
kFocusRingStrokeWidthDp / 2);
canvas->DrawCircle(
capture_mode_util::GetLocationForFineTunePosition(region, position),
radius, focus_ring_flags);
};
const FineTunePosition focused_fine_tune_position =
focus_cycler_->GetFocusedFineTunePosition();
if (is_selecting_region_ || fine_tune_position_ != FineTunePosition::kNone) {
maybe_draw_focus_ring(focused_fine_tune_position);
return;
}
if (IsInCountDownAnimation())
return;
cc::PaintFlags circle_flags;
circle_flags.setColor(capture_mode::kRegionBorderColor);
circle_flags.setStyle(cc::PaintFlags::kFill_Style);
circle_flags.setAntiAlias(true);
circle_flags.setLooper(gfx::CreateShadowDrawLooper(
{kRegionAffordanceCircleShadow1, kRegionAffordanceCircleShadow2}));
auto draw_circle = [&canvas, &circle_flags,
&dsf](const gfx::Point& location) {
canvas->DrawCircle(location, dsf * kAffordanceCircleRadiusDp, circle_flags);
};
draw_circle(region.origin());
draw_circle(region.top_center());
draw_circle(region.top_right());
draw_circle(region.right_center());
draw_circle(region.bottom_right());
draw_circle(region.bottom_center());
draw_circle(region.bottom_left());
draw_circle(region.left_center());
maybe_draw_focus_ring(focused_fine_tune_position);
}
void CaptureModeSession::OnLocatedEvent(ui::LocatedEvent* event,
bool is_touch) {
if (folder_selection_dialog_controller_) {
if (folder_selection_dialog_controller_->ShouldConsumeEvent(event))
event->StopPropagation();
return;
}
if (IsInCountDownAnimation()) {
event->StopPropagation();
return;
}
if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED ||
event->type() == ui::ET_MOUSE_EXITED ||
event->type() == ui::ET_MOUSE_ENTERED) {
return;
}
if (event->flags() & ui::EF_IS_SYNTHESIZED)
return;
gfx::Point screen_location = event->location();
aura::Window* event_target = static_cast<aura::Window*>(event->target());
wm::ConvertPointToScreen(event_target, &screen_location);
const CaptureModeSource capture_source = controller_->source();
const bool is_capture_region = capture_source == CaptureModeSource::kRegion;
const bool is_press_event = event->type() == ui::ET_MOUSE_PRESSED ||
event->type() == ui::ET_TOUCH_PRESSED;
if (is_press_event && focus_cycler_->HasFocus())
focus_cycler_->ClearFocus();
const bool can_change_root = !is_capture_region || is_press_event;
if (can_change_root)
MaybeChangeRoot(capture_mode_util::GetPreferredRootWindow(screen_location));
const bool is_release_event = event->type() == ui::ET_MOUSE_RELEASED ||
event->type() == ui::ET_TOUCH_RELEASED;
if (is_release_event && is_capture_region &&
current_root_ !=
capture_mode_bar_widget_->GetNativeWindow()->GetRootWindow()) {
RefreshBarWidgetBounds();
}
MaybeUpdateCaptureUisOpacity(screen_location);
should_pass_located_event_to_camera_preview_ =
ShouldPassEventToCameraPreview(event);
base::ScopedClosureRunner deferred_cursor_updater(base::BindOnce(
&CaptureModeSession::UpdateCursor, weak_ptr_factory_.GetWeakPtr(),
screen_location, is_touch));
if (should_pass_located_event_to_camera_preview_) {
DCHECK(!controller_->is_recording_in_progress());
return;
}
if (ShouldCaptureLabelHandleEvent(event_target))
return;
if (capture_mode_util::IsEventTargetedOnWidget(
*event, recording_type_menu_widget_.get())) {
return;
}
if (capture_mode_util::IsEventTargetedOnWidget(
*event, capture_mode_settings_widget_.get())) {
return;
}
const bool is_event_on_settings_button =
capture_mode_bar_view_->settings_button()->GetBoundsInScreen().Contains(
screen_location);
const bool should_close_settings = is_press_event &&
!is_event_on_settings_button &&
capture_mode_settings_widget_;
if (should_close_settings) {
ignore_located_events_ = true;
SetSettingsMenuShown(false);
}
const bool should_close_recording_type_menu =
is_press_event &&
!IsPointOnRecordingTypeDropDownButton(screen_location) &&
recording_type_menu_widget_;
if (should_close_recording_type_menu) {
ignore_located_events_ = true;
SetRecordingTypeMenuShown(false);
}
const bool old_ignore_located_events = ignore_located_events_;
if (ignore_located_events_) {
if (is_release_event)
ignore_located_events_ = false;
}
if (capture_mode_util::IsEventTargetedOnWidget(
*event, capture_mode_bar_widget_.get())) {
return;
}
event->SetHandled();
event->StopPropagation();
if (should_close_settings || old_ignore_located_events ||
should_close_recording_type_menu) {
return;
}
const bool is_capture_fullscreen =
capture_source == CaptureModeSource::kFullscreen;
const bool is_capture_window = capture_source == CaptureModeSource::kWindow;
if (is_capture_fullscreen || is_capture_window) {
switch (event->type()) {
case ui::ET_MOUSE_MOVED:
case ui::ET_TOUCH_PRESSED:
case ui::ET_TOUCH_MOVED: {
if (is_capture_window) {
capture_window_observer_->UpdateSelectedWindowAtPosition(
screen_location, {});
}
break;
}
case ui::ET_MOUSE_RELEASED:
case ui::ET_TOUCH_RELEASED:
if (is_capture_fullscreen ||
(is_capture_window && GetSelectedWindow())) {
DoPerformCapture();
}
break;
default:
break;
}
return;
}
DCHECK(is_capture_region);
const gfx::Point& location_in_root = event->root_location();
switch (event->type()) {
case ui::ET_MOUSE_PRESSED:
case ui::ET_TOUCH_PRESSED:
old_mouse_warp_status_ = SetMouseWarpEnabled(false);
OnLocatedEventPressed(location_in_root, is_touch);
break;
case ui::ET_MOUSE_DRAGGED:
case ui::ET_TOUCH_MOVED:
OnLocatedEventDragged(location_in_root);
break;
case ui::ET_MOUSE_RELEASED:
case ui::ET_TOUCH_RELEASED:
if (old_mouse_warp_status_)
SetMouseWarpEnabled(*old_mouse_warp_status_);
old_mouse_warp_status_.reset();
OnLocatedEventReleased(location_in_root);
break;
default:
break;
}
}
FineTunePosition CaptureModeSession::GetFineTunePosition(
const gfx::Point& location_in_screen,
bool is_touch) const {
if (controller_->user_capture_region().IsEmpty())
return FineTunePosition::kNone;
gfx::Rect capture_region_in_screen = controller_->user_capture_region();
wm::ConvertRectToScreen(current_root_, &capture_region_in_screen);
static const std::vector<FineTunePosition> drag_positions = {
FineTunePosition::kBottomRight, FineTunePosition::kBottomLeft,
FineTunePosition::kTopLeft, FineTunePosition::kTopRight,
FineTunePosition::kBottomCenter, FineTunePosition::kLeftCenter,
FineTunePosition::kTopCenter, FineTunePosition::kRightCenter};
const int hit_radius =
is_touch ? kAffordanceCircleTouchHitRadiusDp : kAffordanceCircleRadiusDp;
const int hit_radius_squared = hit_radius * hit_radius;
for (FineTunePosition position : drag_positions) {
const gfx::Point position_location =
capture_mode_util::GetLocationForFineTunePosition(
capture_region_in_screen, position);
if ((position_location - location_in_screen).LengthSquared() <=
hit_radius_squared) {
return position;
}
}
if (capture_region_in_screen.Contains(location_in_screen))
return FineTunePosition::kCenter;
return FineTunePosition::kNone;
}
void CaptureModeSession::OnLocatedEventPressed(
const gfx::Point& location_in_root,
bool is_touch) {
initial_location_in_root_ = location_in_root;
previous_location_in_root_ = location_in_root;
is_drag_in_progress_ = true;
Shell::Get()->UpdateCursorCompositingEnabled();
if (user_nudge_controller_)
user_nudge_controller_->SetVisible(false);
gfx::Point screen_location = location_in_root;
wm::ConvertPointToScreen(current_root_, &screen_location);
MaybeUpdateCaptureUisOpacity(screen_location);
base::ScopedClosureRunner deferred_runner(
base::BindOnce(&CaptureModeSession::MaybeUpdateCameraPreviewBounds,
weak_ptr_factory_.GetWeakPtr()));
if (is_selecting_region_)
return;
fine_tune_position_ = GetFineTunePosition(screen_location, is_touch);
if (fine_tune_position_ == FineTunePosition::kNone) {
is_selecting_region_ = true;
UpdateCaptureRegion(gfx::Rect(), true, true);
num_capture_region_adjusted_ = 0;
return;
}
if (fine_tune_position_ != FineTunePosition::kNone) {
++num_capture_region_adjusted_;
RepaintRegion();
}
if (fine_tune_position_ != FineTunePosition::kCenter &&
fine_tune_position_ != FineTunePosition::kNone) {
anchor_points_ = GetAnchorPointsForPosition(fine_tune_position_);
const gfx::Point position_location =
capture_mode_util::GetLocationForFineTunePosition(
controller_->user_capture_region(), fine_tune_position_);
MaybeShowMagnifierGlassAtPoint(position_location);
}
}
void CaptureModeSession::OnLocatedEventDragged(
const gfx::Point& location_in_root) {
const gfx::Point previous_location_in_root = previous_location_in_root_;
previous_location_in_root_ = location_in_root;
if (is_selecting_region_) {
UpdateCaptureRegion(
GetRectEnclosingPoints({initial_location_in_root_, location_in_root}),
true, true);
return;
}
if (fine_tune_position_ == FineTunePosition::kNone)
return;
if (fine_tune_position_ == FineTunePosition::kCenter) {
gfx::Rect new_capture_region = controller_->user_capture_region();
new_capture_region.Offset(location_in_root - previous_location_in_root);
new_capture_region.AdjustToFit(current_root_->bounds());
UpdateCaptureRegion(new_capture_region, false,
true);
return;
}
std::vector<gfx::Point> points = anchor_points_;
DCHECK(!points.empty());
gfx::Point resizing_point = location_in_root;
if (fine_tune_position_ == FineTunePosition::kLeftCenter ||
fine_tune_position_ == FineTunePosition::kRightCenter) {
resizing_point.set_y(points.front().y());
} else if (fine_tune_position_ == FineTunePosition::kTopCenter ||
fine_tune_position_ == FineTunePosition::kBottomCenter) {
resizing_point.set_x(points.front().x());
}
points.push_back(resizing_point);
UpdateCaptureRegion(GetRectEnclosingPoints(points), true,
true);
MaybeShowMagnifierGlassAtPoint(location_in_root);
}
void CaptureModeSession::OnLocatedEventReleased(
const gfx::Point& location_in_root) {
if (user_nudge_controller_)
user_nudge_controller_->SetVisible(true);
gfx::Point screen_location = location_in_root;
wm::ConvertPointToScreen(current_root_, &screen_location);
EndSelection(screen_location);
RepaintRegion();
base::ScopedClosureRunner deferred_runner(
base::BindOnce(&CaptureModeSession::MaybeUpdateCameraPreviewBounds,
weak_ptr_factory_.GetWeakPtr()));
if (!is_selecting_region_)
return;
is_selecting_region_ = false;
UpdateCaptureLabelWidget(CaptureLabelAnimation::kRegionPhaseChange);
A11yAlertCaptureSource(true);
}
void CaptureModeSession::UpdateCaptureRegion(
const gfx::Rect& new_capture_region,
bool is_resizing,
bool by_user) {
const gfx::Rect old_capture_region = controller_->user_capture_region();
if (old_capture_region == new_capture_region)
return;
gfx::Rect damage_region = old_capture_region;
damage_region.Union(new_capture_region);
damage_region.Inset(gfx::Insets(-kDamageInsetDp));
layer()->SchedulePaint(damage_region);
controller_->SetUserCaptureRegion(new_capture_region, by_user);
UpdateDimensionsLabelWidget(is_resizing);
UpdateCaptureLabelWidget(CaptureLabelAnimation::kNone);
}
void CaptureModeSession::UpdateDimensionsLabelWidget(bool is_resizing) {
const bool should_not_show =
!is_resizing || controller_->source() != CaptureModeSource::kRegion ||
controller_->user_capture_region().IsEmpty();
if (should_not_show) {
dimensions_label_widget_.reset();
return;
}
if (!dimensions_label_widget_) {
auto* parent = GetParentContainer(current_root_);
dimensions_label_widget_ = std::make_unique<views::Widget>();
dimensions_label_widget_->Init(
CreateWidgetParams(parent, gfx::Rect(), "CaptureModeDimensionsLabel"));
auto size_label = std::make_unique<views::Label>();
size_label->SetEnabledColorId(kColorAshTextColorPrimary);
size_label->SetBackground(views::CreateThemedRoundedRectBackground(
kColorAshShieldAndBase80, kSizeLabelBorderRadius));
size_label->SetAutoColorReadabilityEnabled(false);
dimensions_label_widget_->SetContentsView(std::move(size_label));
dimensions_label_widget_->Show();
if (parent == capture_mode_bar_widget_->GetNativeWindow()->parent()) {
parent->StackChildBelow(dimensions_label_widget_->GetNativeWindow(),
capture_mode_bar_widget_->GetNativeWindow());
}
}
views::Label* size_label =
static_cast<views::Label*>(dimensions_label_widget_->GetContentsView());
const gfx::Rect capture_region = controller_->user_capture_region();
size_label->SetText(base::UTF8ToUTF16(base::StringPrintf(
"%d x %d", capture_region.width(), capture_region.height())));
UpdateDimensionsLabelBounds();
}
void CaptureModeSession::UpdateDimensionsLabelBounds() {
DCHECK(dimensions_label_widget_ &&
dimensions_label_widget_->GetContentsView());
gfx::Rect bounds(
dimensions_label_widget_->GetContentsView()->GetPreferredSize());
const gfx::Rect capture_region = controller_->user_capture_region();
gfx::Rect screen_region = current_root_->bounds();
bounds.set_width(bounds.width() + 2 * kSizeLabelHorizontalPadding);
bounds.set_x(capture_region.CenterPoint().x() - bounds.width() / 2);
bounds.set_y(capture_region.bottom() + kSizeLabelYDistanceFromRegionDp);
screen_region.Inset(
gfx::Insets::TLBR(0, 0, kSizeLabelYDistanceFromRegionDp, 0));
bounds.AdjustToFit(screen_region);
wm::ConvertRectToScreen(current_root_, &bounds);
dimensions_label_widget_->SetBounds(bounds);
}
void CaptureModeSession::MaybeShowMagnifierGlassAtPoint(
const gfx::Point& location_in_root) {
if (!capture_mode_util::IsCornerFineTunePosition(fine_tune_position_))
return;
magnifier_glass_.ShowFor(current_root_, location_in_root);
}
void CaptureModeSession::CloseMagnifierGlass() {
magnifier_glass_.Close();
}
std::vector<gfx::Point> CaptureModeSession::GetAnchorPointsForPosition(
FineTunePosition position) {
std::vector<gfx::Point> anchor_points;
const gfx::Rect rect = controller_->user_capture_region();
switch (position) {
case FineTunePosition::kNone:
case FineTunePosition::kCenter:
break;
case FineTunePosition::kTopLeft:
anchor_points.push_back(rect.bottom_right());
break;
case FineTunePosition::kTopCenter:
anchor_points.push_back(rect.bottom_left());
anchor_points.push_back(rect.bottom_right());
break;
case FineTunePosition::kTopRight:
anchor_points.push_back(rect.bottom_left());
break;
case FineTunePosition::kLeftCenter:
anchor_points.push_back(rect.top_right());
anchor_points.push_back(rect.bottom_right());
break;
case FineTunePosition::kRightCenter:
anchor_points.push_back(rect.origin());
anchor_points.push_back(rect.bottom_left());
break;
case FineTunePosition::kBottomLeft:
anchor_points.push_back(rect.top_right());
break;
case FineTunePosition::kBottomCenter:
anchor_points.push_back(rect.origin());
anchor_points.push_back(rect.top_right());
break;
case FineTunePosition::kBottomRight:
anchor_points.push_back(rect.origin());
break;
}
DCHECK(!anchor_points.empty());
DCHECK_LE(anchor_points.size(), 2u);
return anchor_points;
}
void CaptureModeSession::UpdateCaptureLabelWidget(
CaptureLabelAnimation animation_type) {
if (!capture_label_widget_) {
capture_label_widget_ = std::make_unique<views::Widget>();
auto* parent = GetParentContainer(current_root_);
capture_label_widget_->Init(
CreateWidgetParams(parent, gfx::Rect(), "CaptureLabel"));
capture_label_view_ = capture_label_widget_->SetContentsView(
std::make_unique<CaptureLabelView>(
this,
base::BindRepeating(&CaptureModeSession::DoPerformCapture,
base::Unretained(this)),
base::BindRepeating(
&CaptureModeSession::OnRecordingTypeDropDownButtonPressed,
base::Unretained(this))));
capture_label_widget_->GetNativeWindow()->SetTitle(
l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_A11Y_TITLE));
capture_label_widget_->Show();
}
capture_label_view_->UpdateIconAndText();
UpdateCaptureLabelWidgetBounds(animation_type);
MaybeUpdateRecordingTypeMenu();
focus_cycler_->OnCaptureLabelWidgetUpdated();
}
void CaptureModeSession::UpdateCaptureLabelWidgetBounds(
CaptureLabelAnimation animation_type) {
DCHECK(capture_label_widget_);
const gfx::Rect bounds = CalculateCaptureLabelWidgetBounds();
const gfx::Rect old_bounds =
capture_label_widget_->GetNativeWindow()->GetBoundsInScreen();
if (old_bounds == bounds)
return;
if (animation_type == CaptureLabelAnimation::kNone) {
capture_label_widget_->SetBounds(bounds);
return;
}
ui::Layer* layer = capture_label_widget_->GetLayer();
ui::LayerAnimator* animator = layer->GetAnimator();
if (animation_type == CaptureLabelAnimation::kRegionPhaseChange) {
capture_label_widget_->SetBounds(bounds);
const gfx::Point center_point = bounds.CenterPoint();
layer->SetTransform(
gfx::GetScaleTransform(gfx::Point(center_point.x() - bounds.x(),
center_point.y() - bounds.y()),
kLabelScaleDownOnPhaseChange));
layer->SetOpacity(0.f);
ui::ScopedLayerAnimationSettings settings(animator);
settings.SetTransitionDuration(kCaptureLabelRegionPhaseChangeDuration);
settings.SetTweenType(gfx::Tween::ACCEL_LIN_DECEL_100);
settings.SetPreemptionStrategy(ui::LayerAnimator::ENQUEUE_NEW_ANIMATION);
animator->SchedulePauseForProperties(
kCaptureLabelRegionPhaseChangeDelay,
ui::LayerAnimationElement::TRANSFORM |
ui::LayerAnimationElement::OPACITY);
layer->SetTransform(gfx::Transform());
layer->SetOpacity(1.f);
return;
}
DCHECK_EQ(CaptureLabelAnimation::kCountdownStart, animation_type);
if (!old_bounds.IsEmpty()) {
ui::ScopedLayerAnimationSettings settings(animator);
settings.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN);
settings.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
settings.SetTransitionDuration(kCaptureLabelCountdownStartDuration);
capture_label_widget_->SetBounds(bounds);
} else {
capture_label_widget_->SetBounds(bounds);
const gfx::Point center_point = bounds.CenterPoint();
layer->SetTransform(
gfx::GetScaleTransform(gfx::Point(center_point.x() - bounds.x(),
center_point.y() - bounds.y()),
kLabelScaleUpOnCountdown));
layer->SetOpacity(0.f);
ui::ScopedLayerAnimationSettings settings(animator);
settings.SetTransitionDuration(kCaptureLabelCountdownStartDuration);
settings.SetTweenType(gfx::Tween::LINEAR);
settings.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
layer->SetOpacity(1.f);
settings.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN);
layer->SetTransform(gfx::Transform());
}
}
gfx::Rect CaptureModeSession::CalculateCaptureLabelWidgetBounds() {
DCHECK(capture_label_widget_);
DCHECK(capture_label_view_);
const gfx::Size preferred_size = capture_label_view_->GetPreferredSize();
const gfx::Rect capture_bar_bounds =
capture_mode_bar_widget_->GetNativeWindow()->bounds();
auto calculate_bounds = [&preferred_size, &capture_bar_bounds](
const gfx::Rect& capture_bounds,
aura::Window* root) {
gfx::Rect label_bounds(capture_bounds);
gfx::Size capture_bounds_min_size = preferred_size;
capture_bounds_min_size.Enlarge(kCaptureRegionMinimumPaddingDp,
kCaptureRegionMinimumPaddingDp);
if (label_bounds.width() > capture_bounds_min_size.width() &&
label_bounds.height() > capture_bounds_min_size.height()) {
label_bounds.ClampToCenteredSize(preferred_size);
if (!label_bounds.Intersects(capture_bar_bounds))
return label_bounds;
}
enum class Direction { kBottom, kTop, kLeft, kRight };
const std::vector<Direction> directions = {
Direction::kBottom, Direction::kTop, Direction::kLeft,
Direction::kRight};
gfx::Rect centered_label_bounds(preferred_size);
centered_label_bounds.set_x(capture_bounds.CenterPoint().x() -
preferred_size.width() / 2);
centered_label_bounds.set_y(capture_bounds.CenterPoint().y() -
preferred_size.height() / 2);
const int spacing = kCaptureButtonDistanceFromRegionDp;
for (Direction direction : directions) {
label_bounds = centered_label_bounds;
switch (direction) {
case Direction::kBottom:
label_bounds.set_y(capture_bounds.bottom() + spacing);
break;
case Direction::kTop:
label_bounds.set_y(capture_bounds.y() - spacing -
preferred_size.height());
break;
case Direction::kLeft:
label_bounds.set_x(capture_bounds.x() - spacing -
preferred_size.width());
break;
case Direction::kRight:
label_bounds.set_x(capture_bounds.right() + spacing);
break;
}
if (!label_bounds.Intersects(capture_bar_bounds) &&
root->bounds().Contains(label_bounds)) {
return label_bounds;
}
}
label_bounds.set_size(preferred_size);
label_bounds.set_x(capture_bar_bounds.CenterPoint().x() -
preferred_size.width() / 2);
label_bounds.set_y(capture_bar_bounds.y() -
kCaptureButtonDistanceFromRegionDp -
preferred_size.height());
return label_bounds;
};
gfx::Rect bounds(current_root_->bounds());
const gfx::Rect capture_region = controller_->user_capture_region();
const gfx::Rect window_bounds = GetSelectedWindowBounds();
const CaptureModeSource source = controller_->source();
if (source == CaptureModeSource::kRegion && !is_selecting_region_ &&
!capture_region.IsEmpty()) {
if (capture_label_view_->IsInCountDownAnimation()) {
bounds = capture_label_widget_->GetNativeWindow()->bounds();
bounds.ClampToCenteredSize(preferred_size);
} else {
bounds = calculate_bounds(capture_region, current_root_);
}
} else if (source == CaptureModeSource::kWindow && !window_bounds.IsEmpty()) {
bounds = calculate_bounds(window_bounds, current_root_);
} else {
bounds.ClampToCenteredSize(preferred_size);
}
wm::ConvertRectToScreen(current_root_, &bounds);
return bounds;
}
bool CaptureModeSession::ShouldCaptureLabelHandleEvent(
aura::Window* event_target) {
if (!capture_label_widget_ ||
capture_label_widget_->GetNativeWindow() != event_target) {
return false;
}
DCHECK(capture_label_view_);
return capture_label_view_->ShouldHandleEvent();
}
void CaptureModeSession::MaybeChangeRoot(aura::Window* new_root) {
DCHECK(new_root->IsRootWindow());
if (new_root == current_root_)
return;
auto* new_parent = GetParentContainer(new_root);
parent_container_observer_ =
std::make_unique<ParentContainerObserver>(new_parent, this);
new_parent->layer()->Add(layer());
layer()->SetBounds(new_parent->bounds());
current_root_ = new_root;
if (controller_->source() != CaptureModeSource::kRegion)
RefreshBarWidgetBounds();
UpdateCursor(display::Screen::GetScreen()->GetCursorScreenPoint(),
false);
if (controller_->user_capture_region().IsEmpty())
UpdateCaptureLabelWidgetBounds(CaptureLabelAnimation::kNone);
is_selecting_region_ = true;
UpdateCaptureRegion(gfx::Rect(), false, false);
UpdateRootWindowDimmers();
MaybeReparentCameraPreviewWidget();
RefreshStackingOrder();
}
void CaptureModeSession::UpdateRootWindowDimmers() {
root_window_dimmers_.clear();
for (aura::Window* root_window : Shell::GetAllRootWindows()) {
if (root_window == current_root_)
continue;
auto dimmer = std::make_unique<WindowDimmer>(root_window);
dimmer->window()->Show();
root_window_dimmers_.emplace(std::move(dimmer));
}
}
bool CaptureModeSession::IsUsingCustomCursor(CaptureModeType type) const {
return cursor_setter_->IsUsingCustomCursor(type);
}
void CaptureModeSession::ClampCaptureRegionToRootWindowSize() {
gfx::Rect new_capture_region = controller_->user_capture_region();
new_capture_region.AdjustToFit(current_root_->bounds());
controller_->SetUserCaptureRegion(new_capture_region, false);
}
void CaptureModeSession::EndSelection(
absl::optional<gfx::Point> cursor_screen_location) {
fine_tune_position_ = FineTunePosition::kNone;
anchor_points_.clear();
is_drag_in_progress_ = false;
Shell::Get()->UpdateCursorCompositingEnabled();
MaybeUpdateCaptureUisOpacity(cursor_screen_location);
UpdateDimensionsLabelWidget(false);
CloseMagnifierGlass();
}
void CaptureModeSession::RepaintRegion() {
gfx::Rect damage_region = controller_->user_capture_region();
damage_region.Inset(gfx::Insets(-kDamageInsetDp));
layer()->SchedulePaint(damage_region);
}
void CaptureModeSession::SelectDefaultRegion() {
is_selecting_region_ = false;
gfx::Rect default_capture_region = current_root_->bounds();
default_capture_region.ClampToCenteredSize(gfx::ScaleToCeiledSize(
default_capture_region.size(), kRegionDefaultRatio));
UpdateCaptureRegion(default_capture_region, false,
true);
}
void CaptureModeSession::UpdateRegionHorizontally(bool left, int event_flags) {
const FineTunePosition focused_fine_tune_position =
focus_cycler_->GetFocusedFineTunePosition();
if (focused_fine_tune_position == FineTunePosition::kNone ||
focused_fine_tune_position == FineTunePosition::kTopCenter ||
focused_fine_tune_position == FineTunePosition::kBottomCenter) {
return;
}
const int change = GetArrowKeyPressChange(event_flags);
gfx::Rect new_capture_region = controller_->user_capture_region();
if (focused_fine_tune_position == FineTunePosition::kCenter) {
new_capture_region.Offset(left ? -change : change, 0);
new_capture_region.AdjustToFit(current_root_->bounds());
} else {
const gfx::Point location =
capture_mode_util::GetLocationForFineTunePosition(
new_capture_region, focused_fine_tune_position);
const bool affordance_on_left = location.x() == new_capture_region.x();
const bool shrink = affordance_on_left ^ left;
if (shrink && new_capture_region.width() < change)
return;
const int inset = shrink ? change : -change;
auto insets = gfx::Insets::TLBR(0, affordance_on_left ? inset : 0, 0,
affordance_on_left ? 0 : inset);
new_capture_region.Inset(insets);
ClipRectToFit(&new_capture_region, current_root_->bounds());
}
UpdateCaptureRegion(new_capture_region, false,
true);
}
void CaptureModeSession::UpdateRegionVertically(bool up, int event_flags) {
const FineTunePosition focused_fine_tune_position =
focus_cycler_->GetFocusedFineTunePosition();
if (focused_fine_tune_position == FineTunePosition::kNone ||
focused_fine_tune_position == FineTunePosition::kLeftCenter ||
focused_fine_tune_position == FineTunePosition::kRightCenter) {
return;
}
const int change = GetArrowKeyPressChange(event_flags);
gfx::Rect new_capture_region = controller_->user_capture_region();
if (focused_fine_tune_position == FineTunePosition::kCenter) {
new_capture_region.Offset(0, up ? -change : change);
new_capture_region.AdjustToFit(current_root_->bounds());
} else {
const gfx::Point location =
capture_mode_util::GetLocationForFineTunePosition(
new_capture_region, focused_fine_tune_position);
const bool affordance_on_top = location.y() == new_capture_region.y();
const bool shrink = affordance_on_top ^ up;
if (shrink && new_capture_region.height() < change)
return;
const int inset = shrink ? change : -change;
auto insets = gfx::Insets::TLBR(affordance_on_top ? inset : 0, 0,
affordance_on_top ? 0 : inset, 0);
new_capture_region.Inset(insets);
ClipRectToFit(&new_capture_region, current_root_->bounds());
}
UpdateCaptureRegion(new_capture_region, false,
true);
}
void CaptureModeSession::MaybeReparentCameraPreviewWidget() {
if (!controller_->is_recording_in_progress())
controller_->camera_controller()->MaybeReparentPreviewWidget();
}
void CaptureModeSession::MaybeUpdateCameraPreviewBounds() {
if (!controller_->is_recording_in_progress()) {
controller_->camera_controller()->MaybeUpdatePreviewWidget(
false);
}
}
void CaptureModeSession::SetRecordingTypeMenuShown(bool shown,
bool by_key_event) {
if (!shown) {
recording_type_menu_widget_.reset();
recording_type_menu_view_ = nullptr;
return;
}
if (!recording_type_menu_widget_) {
DCHECK(features::IsGifRecordingEnabled());
DCHECK(capture_label_widget_);
DCHECK(capture_label_widget_->IsVisible());
SetSettingsMenuShown(false);
auto* parent = GetParentContainer(current_root_);
recording_type_menu_widget_ = std::make_unique<views::Widget>();
MaybeDismissUserNudgeForever();
capture_toast_controller_.DismissCurrentToastIfAny();
recording_type_menu_widget_->Init(CreateWidgetParams(
parent,
RecordingTypeMenuView::GetIdealScreenBounds(
capture_label_widget_->GetWindowBoundsInScreen()),
"RecordingTypeMenuWidget"));
recording_type_menu_view_ = recording_type_menu_widget_->SetContentsView(
std::make_unique<RecordingTypeMenuView>(
base::BindRepeating(&CaptureModeSession::SetRecordingTypeMenuShown,
weak_ptr_factory_.GetWeakPtr(), false,
false)));
auto* menu_window = recording_type_menu_widget_->GetNativeWindow();
parent->StackChildAtTop(menu_window);
menu_window->SetTitle(l10n_util::GetStringUTF16(
IDS_ASH_SCREEN_CAPTURE_RECORDING_TYPE_MENU_A11Y_TITLE));
focus_cycler_->OnMenuOpened(
recording_type_menu_widget_.get(),
CaptureModeSessionFocusCycler::FocusGroup::kPendingRecordingType,
by_key_event);
}
recording_type_menu_widget_->Show();
}
bool CaptureModeSession::IsPointOnRecordingTypeDropDownButton(
const gfx::Point& screen_location) const {
if (!capture_label_widget_ || !capture_label_widget_->IsVisible())
return false;
DCHECK(capture_label_view_);
return capture_label_view_->IsPointOnRecordingTypeDropDownButton(
screen_location);
}
void CaptureModeSession::MaybeUpdateRecordingTypeMenu() {
if (!recording_type_menu_widget_)
return;
if (!capture_label_widget_ ||
!capture_label_view_->IsRecordingTypeDropDownButtonVisible()) {
SetRecordingTypeMenuShown(false);
return;
}
recording_type_menu_widget_->SetBounds(
RecordingTypeMenuView::GetIdealScreenBounds(
capture_label_widget_->GetWindowBoundsInScreen(),
recording_type_menu_widget_->GetContentsView()));
}
}