[go: nahoru, domu]

blob: e1c42831ca283caf9d1b528a22c5dcb0bc95d30f [file] [log] [blame]
Avi Drissman3a215d1e2022-09-07 19:43:091// Copyright 2018 The Chromium Authors
Evan Stade1b050fed2018-09-21 21:17:172// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Evan Stadeeb42d772019-05-03 01:51:345#include "ash/frame/snap_controller_impl.h"
Evan Stade1b050fed2018-09-21 21:17:176
7#include "ash/wm/window_positioning_utils.h"
8#include "ash/wm/window_state.h"
9#include "ash/wm/wm_event.h"
Xiaoqian Dai1fbf0102023-04-29 00:39:0410#include "ash/wm/wm_metrics.h"
Evan Stade1b050fed2018-09-21 21:17:1711#include "ash/wm/workspace/phantom_window_controller.h"
Sammie Quon7e9455c2023-10-27 20:06:2712#include "chromeos/utils/haptics_util.h"
Evan Stade1b050fed2018-09-21 21:17:1713#include "ui/aura/window.h"
Yongshun Liu7c7c855b2021-11-16 19:46:0314#include "ui/events/devices/haptic_touchpad_effects.h"
Evan Stade1b050fed2018-09-21 21:17:1715#include "ui/wm/core/coordinate_conversion.h"
16
17namespace ash {
18
Evan Stadeeb42d772019-05-03 01:51:3419SnapControllerImpl::SnapControllerImpl() = default;
20SnapControllerImpl::~SnapControllerImpl() = default;
Evan Stade1b050fed2018-09-21 21:17:1721
Evan Stadeeb42d772019-05-03 01:51:3422bool SnapControllerImpl::CanSnap(aura::Window* window) {
James Cook00e65e92019-07-25 03:19:0823 return WindowState::Get(window)->CanSnap();
Evan Stade1b050fed2018-09-21 21:17:1724}
25
Evan Stadeeb42d772019-05-03 01:51:3426void SnapControllerImpl::ShowSnapPreview(aura::Window* window,
Yongshun Liu7c7c855b2021-11-16 19:46:0327 chromeos::SnapDirection snap,
28 bool allow_haptic_feedback) {
Antonio Gomes5c833252020-10-13 18:42:1929 if (snap == chromeos::SnapDirection::kNone) {
Evan Stade1b050fed2018-09-21 21:17:1730 phantom_window_controller_.reset();
31 return;
32 }
33
34 if (!phantom_window_controller_ ||
35 phantom_window_controller_->window() != window) {
36 phantom_window_controller_ =
37 std::make_unique<PhantomWindowController>(window);
38 }
Cattalyya Nuengsigkapianffc39612021-10-02 00:14:2339 const SnapViewType snap_type = snap == chromeos::SnapDirection::kPrimary
40 ? SnapViewType::kPrimary
41 : SnapViewType::kSecondary;
Evan Stade1b050fed2018-09-21 21:17:1742 gfx::Rect phantom_bounds_in_screen =
Cattalyya Nuengsigkapianffc39612021-10-02 00:14:2343 GetDefaultSnappedWindowBoundsInParent(window, snap_type);
Evan Stade1b050fed2018-09-21 21:17:1744 ::wm::ConvertRectToScreen(window->parent(), &phantom_bounds_in_screen);
Yongshun Liu7c7c855b2021-11-16 19:46:0345
46 const bool need_haptic_feedback =
47 allow_haptic_feedback &&
48 phantom_window_controller_->GetTargetWindowBounds() !=
49 phantom_bounds_in_screen;
50
Evan Stade1b050fed2018-09-21 21:17:1751 phantom_window_controller_->Show(phantom_bounds_in_screen);
Yongshun Liu7c7c855b2021-11-16 19:46:0352
53 // Fire a haptic event if necessary.
54 if (need_haptic_feedback) {
Sammie Quon7e9455c2023-10-27 20:06:2755 chromeos::haptics_util::PlayHapticTouchpadEffect(
Yongshun Liu7c7c855b2021-11-16 19:46:0356 ui::HapticTouchpadEffect::kSnap,
57 ui::HapticTouchpadEffectStrength::kMedium);
58 }
Evan Stade1b050fed2018-09-21 21:17:1759}
60
Antonio Gomes5c833252020-10-13 18:42:1961void SnapControllerImpl::CommitSnap(aura::Window* window,
Sophie Wene41f4512022-07-25 18:38:5862 chromeos::SnapDirection snap,
Xiaoqian Dai1fbf0102023-04-29 00:39:0463 float snap_ratio,
64 SnapRequestSource snap_request_source) {
Evan Stade1b050fed2018-09-21 21:17:1765 phantom_window_controller_.reset();
Xiaoqian Dai1fbf0102023-04-29 00:39:0466 if (snap == chromeos::SnapDirection::kNone) {
Evan Stade1b050fed2018-09-21 21:17:1767 return;
Xiaoqian Dai1fbf0102023-04-29 00:39:0468 }
Evan Stade1b050fed2018-09-21 21:17:1769
Xiaoqian Dai1fbf0102023-04-29 00:39:0470 WindowSnapActionSource snap_action_source =
71 WindowSnapActionSource::kNotSpecified;
72 switch (snap_request_source) {
73 case SnapRequestSource::kSnapButton:
74 snap_action_source =
75 WindowSnapActionSource::kLongPressCaptionButtonToSnap;
76 break;
77 case SnapRequestSource::kWindowLayoutMenu:
78 snap_action_source = WindowSnapActionSource::kSnapByWindowLayoutMenu;
79 break;
80 case SnapRequestSource::kFromLacrosSnapButtonOrWindowLayoutMenu:
Michele Fanfdb375422024-02-07 01:36:2081 snap_action_source =
82 WindowSnapActionSource::kLacrosSnapButtonOrWindowLayoutMenu;
Xiaoqian Dai1fbf0102023-04-29 00:39:0483 break;
84 }
85
Xiaoqian Daibd3d237f2023-04-29 00:18:4686 const WindowSnapWMEvent snap_event(snap == chromeos::SnapDirection::kPrimary
87 ? WM_EVENT_SNAP_PRIMARY
88 : WM_EVENT_SNAP_SECONDARY,
Xiaoqian Dai1fbf0102023-04-29 00:39:0489 snap_ratio, snap_action_source);
90 WindowState::Get(window)->OnWMEvent(&snap_event);
Evan Stade1b050fed2018-09-21 21:17:1791}
92
93} // namespace ash