Evan Stade | 1b050fed | 2018-09-21 21:17:17 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Evan Stade | eb42d77 | 2019-05-03 01:51:34 | [diff] [blame^] | 5 | #include "ash/frame/snap_controller_impl.h" |
Evan Stade | 1b050fed | 2018-09-21 21:17:17 | [diff] [blame] | 6 | |
| 7 | #include "ash/wm/window_positioning_utils.h" |
| 8 | #include "ash/wm/window_state.h" |
| 9 | #include "ash/wm/wm_event.h" |
| 10 | #include "ash/wm/workspace/phantom_window_controller.h" |
| 11 | #include "ui/aura/window.h" |
| 12 | #include "ui/wm/core/coordinate_conversion.h" |
| 13 | |
| 14 | namespace ash { |
| 15 | |
Evan Stade | eb42d77 | 2019-05-03 01:51:34 | [diff] [blame^] | 16 | SnapControllerImpl::SnapControllerImpl() = default; |
| 17 | SnapControllerImpl::~SnapControllerImpl() = default; |
Evan Stade | 1b050fed | 2018-09-21 21:17:17 | [diff] [blame] | 18 | |
Evan Stade | eb42d77 | 2019-05-03 01:51:34 | [diff] [blame^] | 19 | bool SnapControllerImpl::CanSnap(aura::Window* window) { |
Evan Stade | 1b050fed | 2018-09-21 21:17:17 | [diff] [blame] | 20 | return wm::GetWindowState(window)->CanSnap(); |
| 21 | } |
| 22 | |
Evan Stade | eb42d77 | 2019-05-03 01:51:34 | [diff] [blame^] | 23 | void SnapControllerImpl::ShowSnapPreview(aura::Window* window, |
| 24 | SnapDirection snap) { |
| 25 | if (snap == SnapDirection::kNone) { |
Evan Stade | 1b050fed | 2018-09-21 21:17:17 | [diff] [blame] | 26 | phantom_window_controller_.reset(); |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | if (!phantom_window_controller_ || |
| 31 | phantom_window_controller_->window() != window) { |
| 32 | phantom_window_controller_ = |
| 33 | std::make_unique<PhantomWindowController>(window); |
| 34 | } |
| 35 | gfx::Rect phantom_bounds_in_screen = |
Evan Stade | eb42d77 | 2019-05-03 01:51:34 | [diff] [blame^] | 36 | (snap == SnapDirection::kLeft) |
Evan Stade | 1b050fed | 2018-09-21 21:17:17 | [diff] [blame] | 37 | ? wm::GetDefaultLeftSnappedWindowBoundsInParent(window) |
| 38 | : wm::GetDefaultRightSnappedWindowBoundsInParent(window); |
| 39 | ::wm::ConvertRectToScreen(window->parent(), &phantom_bounds_in_screen); |
| 40 | phantom_window_controller_->Show(phantom_bounds_in_screen); |
| 41 | } |
| 42 | |
Evan Stade | eb42d77 | 2019-05-03 01:51:34 | [diff] [blame^] | 43 | void SnapControllerImpl::CommitSnap(aura::Window* window, SnapDirection snap) { |
Evan Stade | 1b050fed | 2018-09-21 21:17:17 | [diff] [blame] | 44 | phantom_window_controller_.reset(); |
Evan Stade | eb42d77 | 2019-05-03 01:51:34 | [diff] [blame^] | 45 | if (snap == SnapDirection::kNone) |
Evan Stade | 1b050fed | 2018-09-21 21:17:17 | [diff] [blame] | 46 | return; |
| 47 | |
| 48 | wm::WindowState* window_state = wm::GetWindowState(window); |
Evan Stade | eb42d77 | 2019-05-03 01:51:34 | [diff] [blame^] | 49 | const wm::WMEvent snap_event(snap == SnapDirection::kLeft |
Evan Stade | 4b099f9 | 2018-09-27 16:30:19 | [diff] [blame] | 50 | ? wm::WM_EVENT_SNAP_LEFT |
| 51 | : wm::WM_EVENT_SNAP_RIGHT); |
Evan Stade | 1b050fed | 2018-09-21 21:17:17 | [diff] [blame] | 52 | window_state->OnWMEvent(&snap_event); |
| 53 | } |
| 54 | |
| 55 | } // namespace ash |