base_state.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright 2017 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. #include "ash/wm/base_state.h"
  5. #include "ash/public/cpp/window_animation_types.h"
  6. #include "ash/public/cpp/window_properties.h"
  7. #include "ash/screen_util.h"
  8. #include "ash/shell.h"
  9. #include "ash/strings/grit/ash_strings.h"
  10. #include "ash/wm/overview/overview_controller.h"
  11. #include "ash/wm/splitview/split_view_controller.h"
  12. #include "ash/wm/splitview/split_view_utils.h"
  13. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  14. #include "ash/wm/window_positioning_utils.h"
  15. #include "ash/wm/window_state.h"
  16. #include "ash/wm/wm_event.h"
  17. #include "chromeos/ui/base/window_state_type.h"
  18. #include "ui/aura/client/aura_constants.h"
  19. #include "ui/aura/window.h"
  20. #include "ui/compositor/layer.h"
  21. namespace ash {
  22. using ::chromeos::WindowStateType;
  23. BaseState::BaseState(WindowStateType initial_state_type)
  24. : state_type_(initial_state_type) {}
  25. BaseState::~BaseState() = default;
  26. void BaseState::OnWMEvent(WindowState* window_state, const WMEvent* event) {
  27. if (event->IsWorkspaceEvent()) {
  28. HandleWorkspaceEvents(window_state, event);
  29. if (window_state->IsPip())
  30. window_state->UpdatePipBounds();
  31. if (window_state->IsSnapped() && !window_state->CanSnap())
  32. window_state->Restore();
  33. return;
  34. }
  35. if ((window_state->IsTrustedPinned() || window_state->IsPinned()) &&
  36. (event->type() != WM_EVENT_NORMAL && event->type() != WM_EVENT_RESTORE &&
  37. event->IsTransitionEvent())) {
  38. // PIN state can be exited only by normal event or restore event.
  39. return;
  40. }
  41. if (event->IsCompoundEvent()) {
  42. HandleCompoundEvents(window_state, event);
  43. return;
  44. }
  45. if (event->IsBoundsEvent()) {
  46. HandleBoundsEvents(window_state, event);
  47. return;
  48. }
  49. DCHECK(event->IsTransitionEvent());
  50. HandleTransitionEvents(window_state, event);
  51. }
  52. WindowStateType BaseState::GetType() const {
  53. return state_type_;
  54. }
  55. // static
  56. WindowStateType BaseState::GetStateForTransitionEvent(WindowState* window_state,
  57. const WMEvent* event) {
  58. switch (event->type()) {
  59. case WM_EVENT_NORMAL:
  60. if (window_state->window()->GetProperty(aura::client::kIsRestoringKey))
  61. return window_state->GetRestoreWindowState();
  62. return WindowStateType::kNormal;
  63. case WM_EVENT_MAXIMIZE:
  64. return WindowStateType::kMaximized;
  65. case WM_EVENT_MINIMIZE:
  66. return WindowStateType::kMinimized;
  67. case WM_EVENT_FULLSCREEN:
  68. return WindowStateType::kFullscreen;
  69. case WM_EVENT_SNAP_PRIMARY:
  70. return WindowStateType::kPrimarySnapped;
  71. case WM_EVENT_SNAP_SECONDARY:
  72. return WindowStateType::kSecondarySnapped;
  73. case WM_EVENT_RESTORE:
  74. return window_state->GetRestoreWindowState();
  75. case WM_EVENT_SHOW_INACTIVE:
  76. return WindowStateType::kInactive;
  77. case WM_EVENT_PIN:
  78. return WindowStateType::kPinned;
  79. case WM_EVENT_PIP:
  80. return WindowStateType::kPip;
  81. case WM_EVENT_FLOAT:
  82. return WindowStateType::kFloated;
  83. case WM_EVENT_TRUSTED_PIN:
  84. return WindowStateType::kTrustedPinned;
  85. default:
  86. break;
  87. }
  88. #if !defined(NDEBUG)
  89. if (event->IsWorkspaceEvent())
  90. NOTREACHED() << "Can't get the state for Workspace event" << event->type();
  91. if (event->IsCompoundEvent())
  92. NOTREACHED() << "Can't get the state for Compound event:" << event->type();
  93. if (event->IsBoundsEvent())
  94. NOTREACHED() << "Can't get the state for Bounds event:" << event->type();
  95. #endif
  96. return WindowStateType::kNormal;
  97. }
  98. // static
  99. void BaseState::CenterWindow(WindowState* window_state) {
  100. if (!window_state->IsNormalOrSnapped())
  101. return;
  102. aura::Window* window = window_state->window();
  103. if (window_state->IsSnapped()) {
  104. gfx::Rect center_in_screen = display::Screen::GetScreen()
  105. ->GetDisplayNearestWindow(window)
  106. .work_area();
  107. gfx::Size size = window_state->HasRestoreBounds()
  108. ? window_state->GetRestoreBoundsInScreen().size()
  109. : window->bounds().size();
  110. center_in_screen.ClampToCenteredSize(size);
  111. window_state->SetRestoreBoundsInScreen(center_in_screen);
  112. window_state->Restore();
  113. } else {
  114. gfx::Rect center_in_parent =
  115. screen_util::GetDisplayWorkAreaBoundsInParent(window);
  116. center_in_parent.ClampToCenteredSize(window->bounds().size());
  117. const SetBoundsWMEvent event(center_in_parent,
  118. /*animate=*/true);
  119. window_state->OnWMEvent(&event);
  120. }
  121. // Centering window is treated as if a user moved and resized the window.
  122. window_state->set_bounds_changed_by_user(true);
  123. }
  124. // static
  125. void BaseState::CycleSnap(WindowState* window_state, WMEventType event) {
  126. auto* shell = Shell::Get();
  127. // For tablet mode, use |TabletModeWindowState::CycleTabletSnap|.
  128. DCHECK(!shell->tablet_mode_controller()->InTabletMode());
  129. WindowStateType desired_snap_state = event == WM_EVENT_CYCLE_SNAP_PRIMARY
  130. ? WindowStateType::kPrimarySnapped
  131. : WindowStateType::kSecondarySnapped;
  132. aura::Window* window = window_state->window();
  133. // If |window| can be snapped but is not currently in |desired_snap_state|,
  134. // then snap |window| to the side that corresponds to |desired_snap_state|.
  135. if (window_state->CanSnap() &&
  136. window_state->GetStateType() != desired_snap_state) {
  137. const bool is_desired_primary_snapped =
  138. desired_snap_state == WindowStateType::kPrimarySnapped;
  139. if (shell->overview_controller()->InOverviewSession()) {
  140. // |window| must already be in split view, and so we do not need to check
  141. // |SplitViewController::CanSnapWindow|, although in general it is more
  142. // restrictive than |WindowState::CanSnap|.
  143. DCHECK(SplitViewController::Get(window)->IsWindowInSplitView(window));
  144. SplitViewController::Get(window)->SnapWindow(
  145. window, is_desired_primary_snapped ? SplitViewController::LEFT
  146. : SplitViewController::RIGHT);
  147. } else {
  148. const WindowSnapWMEvent event(is_desired_primary_snapped
  149. ? WM_EVENT_SNAP_PRIMARY
  150. : WM_EVENT_SNAP_SECONDARY);
  151. window_state->OnWMEvent(&event);
  152. }
  153. window_state->ReadOutWindowCycleSnapAction(
  154. is_desired_primary_snapped ? IDS_WM_SNAP_WINDOW_TO_LEFT_ON_SHORTCUT
  155. : IDS_WM_SNAP_WINDOW_TO_RIGHT_ON_SHORTCUT);
  156. return;
  157. }
  158. // If |window| is already in |desired_snap_state|, then unsnap |window|.
  159. if (window_state->IsSnapped()) {
  160. window_state->Restore();
  161. window_state->ReadOutWindowCycleSnapAction(
  162. IDS_WM_RESTORE_SNAPPED_WINDOW_ON_SHORTCUT);
  163. return;
  164. }
  165. // If |window| cannot be snapped, then do a window bounce animation.
  166. DCHECK(!window_state->CanSnap());
  167. ::wm::AnimateWindow(window, ::wm::WINDOW_ANIMATION_TYPE_BOUNCE);
  168. }
  169. void BaseState::UpdateMinimizedState(WindowState* window_state,
  170. WindowStateType previous_state_type) {
  171. aura::Window* window = window_state->window();
  172. if (window_state->IsMinimized()) {
  173. // Count minimizing a PIP window as dismissing it. Android apps in PIP mode
  174. // don't exit when they are dismissed, they just go back to being a regular
  175. // app, but minimized.
  176. ::wm::SetWindowVisibilityAnimationType(
  177. window, previous_state_type == WindowStateType::kPip
  178. ? WINDOW_VISIBILITY_ANIMATION_TYPE_FADE_IN_SLIDE_OUT
  179. : WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE);
  180. window->Hide();
  181. if (window_state->IsActive())
  182. window_state->Deactivate();
  183. } else if ((window->layer()->GetTargetVisibility() ||
  184. IsMinimizedWindowStateType(previous_state_type)) &&
  185. !window->layer()->visible()) {
  186. // The layer may be hidden if the window was previously minimized. Make
  187. // sure it's visible.
  188. window->Show();
  189. if (IsMinimizedWindowStateType(previous_state_type) &&
  190. !window_state->IsMaximizedOrFullscreenOrPinned()) {
  191. window_state->set_unminimize_to_restore_bounds(false);
  192. }
  193. }
  194. }
  195. gfx::Rect BaseState::GetSnappedWindowBoundsInParent(
  196. aura::Window* window,
  197. const WindowStateType state_type) {
  198. return BaseState::GetSnappedWindowBoundsInParent(window, state_type,
  199. kDefaultSnapRatio);
  200. }
  201. gfx::Rect BaseState::GetSnappedWindowBoundsInParent(
  202. aura::Window* window,
  203. const WindowStateType state_type,
  204. float snap_ratio) {
  205. DCHECK(state_type == WindowStateType::kPrimarySnapped ||
  206. state_type == WindowStateType::kSecondarySnapped);
  207. gfx::Rect bounds_in_parent;
  208. if (ShouldAllowSplitView()) {
  209. bounds_in_parent =
  210. SplitViewController::Get(window)->GetSnappedWindowBoundsInParent(
  211. (state_type == WindowStateType::kPrimarySnapped)
  212. ? SplitViewController::LEFT
  213. : SplitViewController::RIGHT,
  214. window, snap_ratio);
  215. } else {
  216. // Use `window_positioning_utils` to calculate the snapped window bounds.
  217. bounds_in_parent = ash::GetSnappedWindowBoundsInParent(
  218. window,
  219. state_type == WindowStateType::kPrimarySnapped
  220. ? SnapViewType::kPrimary
  221. : SnapViewType::kSecondary,
  222. snap_ratio);
  223. }
  224. return bounds_in_parent;
  225. }
  226. void BaseState::HandleWindowSnapping(WindowState* window_state,
  227. WMEventType event_type) {
  228. DCHECK(event_type == WM_EVENT_SNAP_PRIMARY ||
  229. event_type == WM_EVENT_SNAP_SECONDARY);
  230. DCHECK(window_state->CanSnap());
  231. window_state->set_bounds_changed_by_user(true);
  232. aura::Window* window = window_state->window();
  233. // SplitViewController will decide if the window needs to be snapped in split
  234. // view.
  235. SplitViewController::Get(window)->OnWindowSnapWMEvent(window, event_type);
  236. }
  237. } // namespace ash