[go: nahoru, domu]

blob: aac2a4bf5b4c70c7a86e9cbaa60c6582905bd255 [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
Sophie Wen68b8f2f2024-06-20 18:27:367#include "ash/wm/splitview/split_view_utils.h"
Evan Stade1b050fed2018-09-21 21:17:178#include "ash/wm/window_positioning_utils.h"
9#include "ash/wm/window_state.h"
10#include "ash/wm/wm_event.h"
Xiaoqian Dai1fbf0102023-04-29 00:39:0411#include "ash/wm/wm_metrics.h"
Evan Stade1b050fed2018-09-21 21:17:1712#include "ash/wm/workspace/phantom_window_controller.h"
Sammie Quon7e9455c2023-10-27 20:06:2713#include "chromeos/utils/haptics_util.h"
Evan Stade1b050fed2018-09-21 21:17:1714#include "ui/aura/window.h"
Yongshun Liu7c7c855b2021-11-16 19:46:0315#include "ui/events/devices/haptic_touchpad_effects.h"
Evan Stade1b050fed2018-09-21 21:17:1716#include "ui/wm/core/coordinate_conversion.h"
17
18namespace ash {
19
Evan Stadeeb42d772019-05-03 01:51:3420SnapControllerImpl::SnapControllerImpl() = default;
21SnapControllerImpl::~SnapControllerImpl() = default;
Evan Stade1b050fed2018-09-21 21:17:1722
Evan Stadeeb42d772019-05-03 01:51:3423bool SnapControllerImpl::CanSnap(aura::Window* window) {
James Cook00e65e92019-07-25 03:19:0824 return WindowState::Get(window)->CanSnap();
Evan Stade1b050fed2018-09-21 21:17:1725}
26
Evan Stadeeb42d772019-05-03 01:51:3427void SnapControllerImpl::ShowSnapPreview(aura::Window* window,
Yongshun Liu7c7c855b2021-11-16 19:46:0328 chromeos::SnapDirection snap,
29 bool allow_haptic_feedback) {
Antonio Gomes5c833252020-10-13 18:42:1930 if (snap == chromeos::SnapDirection::kNone) {
Evan Stade1b050fed2018-09-21 21:17:1731 phantom_window_controller_.reset();
32 return;
33 }
34
35 if (!phantom_window_controller_ ||
36 phantom_window_controller_->window() != window) {
37 phantom_window_controller_ =
38 std::make_unique<PhantomWindowController>(window);
39 }
Cattalyya Nuengsigkapianffc39612021-10-02 00:14:2340 const SnapViewType snap_type = snap == chromeos::SnapDirection::kPrimary
41 ? SnapViewType::kPrimary
42 : SnapViewType::kSecondary;
Sophie Wen68b8f2f2024-06-20 18:27:3643 gfx::Rect phantom_bounds_in_screen = GetSnappedWindowBoundsInParent(
44 window, snap_type,
45 GetPhantomSnapRatio(window, window->GetRootWindow(), snap_type));
Evan Stade1b050fed2018-09-21 21:17:1746 ::wm::ConvertRectToScreen(window->parent(), &phantom_bounds_in_screen);
Yongshun Liu7c7c855b2021-11-16 19:46:0347
48 const bool need_haptic_feedback =
49 allow_haptic_feedback &&
50 phantom_window_controller_->GetTargetWindowBounds() !=
51 phantom_bounds_in_screen;
52
Evan Stade1b050fed2018-09-21 21:17:1753 phantom_window_controller_->Show(phantom_bounds_in_screen);
Yongshun Liu7c7c855b2021-11-16 19:46:0354
55 // Fire a haptic event if necessary.
56 if (need_haptic_feedback) {
Sammie Quon7e9455c2023-10-27 20:06:2757 chromeos::haptics_util::PlayHapticTouchpadEffect(
Yongshun Liu7c7c855b2021-11-16 19:46:0358 ui::HapticTouchpadEffect::kSnap,
59 ui::HapticTouchpadEffectStrength::kMedium);
60 }
Evan Stade1b050fed2018-09-21 21:17:1761}
62
Antonio Gomes5c833252020-10-13 18:42:1963void SnapControllerImpl::CommitSnap(aura::Window* window,
Sophie Wene41f4512022-07-25 18:38:5864 chromeos::SnapDirection snap,
Xiaoqian Dai1fbf0102023-04-29 00:39:0465 float snap_ratio,
66 SnapRequestSource snap_request_source) {
Evan Stade1b050fed2018-09-21 21:17:1767 phantom_window_controller_.reset();
Xiaoqian Dai1fbf0102023-04-29 00:39:0468 if (snap == chromeos::SnapDirection::kNone) {
Evan Stade1b050fed2018-09-21 21:17:1769 return;
Xiaoqian Dai1fbf0102023-04-29 00:39:0470 }
Evan Stade1b050fed2018-09-21 21:17:1771
Xiaoqian Dai1fbf0102023-04-29 00:39:0472 WindowSnapActionSource snap_action_source =
73 WindowSnapActionSource::kNotSpecified;
74 switch (snap_request_source) {
75 case SnapRequestSource::kSnapButton:
76 snap_action_source =
77 WindowSnapActionSource::kLongPressCaptionButtonToSnap;
78 break;
79 case SnapRequestSource::kWindowLayoutMenu:
80 snap_action_source = WindowSnapActionSource::kSnapByWindowLayoutMenu;
81 break;
82 case SnapRequestSource::kFromLacrosSnapButtonOrWindowLayoutMenu:
Michele Fanfdb375422024-02-07 01:36:2083 snap_action_source =
84 WindowSnapActionSource::kLacrosSnapButtonOrWindowLayoutMenu;
Xiaoqian Dai1fbf0102023-04-29 00:39:0485 break;
86 }
87
Xiaoqian Daibd3d237f2023-04-29 00:18:4688 const WindowSnapWMEvent snap_event(snap == chromeos::SnapDirection::kPrimary
89 ? WM_EVENT_SNAP_PRIMARY
90 : WM_EVENT_SNAP_SECONDARY,
Xiaoqian Dai1fbf0102023-04-29 00:39:0491 snap_ratio, snap_action_source);
92 WindowState::Get(window)->OnWMEvent(&snap_event);
Evan Stade1b050fed2018-09-21 21:17:1793}
94
95} // namespace ash