PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/external/chromium_org/ash/wm/base_layout_manager.cc

https://gitlab.com/brian0218/rk3288_r-box_android4.4.2_sdk
C++ | 274 lines | 198 code | 35 blank | 41 comment | 45 complexity | 377b694a4ce013ec5da9ebcdfa1142e1 MD5 | raw file
  1. // Copyright (c) 2012 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_layout_manager.h"
  5. #include "ash/screen_ash.h"
  6. #include "ash/session_state_delegate.h"
  7. #include "ash/shelf/shelf_layout_manager.h"
  8. #include "ash/shell.h"
  9. #include "ash/wm/window_animations.h"
  10. #include "ash/wm/window_properties.h"
  11. #include "ash/wm/window_util.h"
  12. #include "ash/wm/workspace/workspace_window_resizer.h"
  13. #include "ui/aura/client/activation_client.h"
  14. #include "ui/aura/client/aura_constants.h"
  15. #include "ui/aura/root_window.h"
  16. #include "ui/aura/window.h"
  17. #include "ui/base/ui_base_types.h"
  18. #include "ui/compositor/layer.h"
  19. #include "ui/gfx/screen.h"
  20. #include "ui/views/corewm/corewm_switches.h"
  21. #include "ui/views/corewm/window_util.h"
  22. namespace ash {
  23. namespace internal {
  24. /////////////////////////////////////////////////////////////////////////////
  25. // BaseLayoutManager, public:
  26. BaseLayoutManager::BaseLayoutManager(aura::RootWindow* root_window)
  27. : root_window_(root_window) {
  28. Shell::GetInstance()->activation_client()->AddObserver(this);
  29. Shell::GetInstance()->AddShellObserver(this);
  30. root_window_->AddObserver(this);
  31. }
  32. BaseLayoutManager::~BaseLayoutManager() {
  33. if (root_window_)
  34. root_window_->RemoveObserver(this);
  35. for (WindowSet::const_iterator i = windows_.begin(); i != windows_.end(); ++i)
  36. (*i)->RemoveObserver(this);
  37. Shell::GetInstance()->RemoveShellObserver(this);
  38. Shell::GetInstance()->activation_client()->RemoveObserver(this);
  39. }
  40. // static
  41. gfx::Rect BaseLayoutManager::BoundsWithScreenEdgeVisible(
  42. aura::Window* window,
  43. const gfx::Rect& restore_bounds) {
  44. gfx::Rect max_bounds =
  45. ash::ScreenAsh::GetMaximizedWindowBoundsInParent(window);
  46. // If the restore_bounds are more than 1 grid step away from the size the
  47. // window would be when maximized, inset it.
  48. max_bounds.Inset(ash::internal::WorkspaceWindowResizer::kScreenEdgeInset,
  49. ash::internal::WorkspaceWindowResizer::kScreenEdgeInset);
  50. if (restore_bounds.Contains(max_bounds))
  51. return max_bounds;
  52. return restore_bounds;
  53. }
  54. /////////////////////////////////////////////////////////////////////////////
  55. // BaseLayoutManager, LayoutManager overrides:
  56. void BaseLayoutManager::OnWindowResized() {
  57. }
  58. void BaseLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
  59. windows_.insert(child);
  60. child->AddObserver(this);
  61. // Only update the bounds if the window has a show state that depends on the
  62. // workspace area.
  63. if (wm::IsWindowMaximized(child) || wm::IsWindowFullscreen(child))
  64. UpdateBoundsFromShowState(child);
  65. }
  66. void BaseLayoutManager::OnWillRemoveWindowFromLayout(aura::Window* child) {
  67. windows_.erase(child);
  68. child->RemoveObserver(this);
  69. }
  70. void BaseLayoutManager::OnWindowRemovedFromLayout(aura::Window* child) {
  71. }
  72. void BaseLayoutManager::OnChildWindowVisibilityChanged(aura::Window* child,
  73. bool visible) {
  74. if (visible && wm::IsWindowMinimized(child)) {
  75. // Attempting to show a minimized window. Unminimize it.
  76. child->SetProperty(aura::client::kShowStateKey,
  77. child->GetProperty(aura::client::kRestoreShowStateKey));
  78. child->ClearProperty(aura::client::kRestoreShowStateKey);
  79. }
  80. }
  81. void BaseLayoutManager::SetChildBounds(aura::Window* child,
  82. const gfx::Rect& requested_bounds) {
  83. gfx::Rect child_bounds(requested_bounds);
  84. // Some windows rely on this to set their initial bounds.
  85. if (wm::IsWindowMaximized(child))
  86. child_bounds = ScreenAsh::GetMaximizedWindowBoundsInParent(child);
  87. else if (wm::IsWindowFullscreen(child))
  88. child_bounds = ScreenAsh::GetDisplayBoundsInParent(child);
  89. SetChildBoundsDirect(child, child_bounds);
  90. }
  91. /////////////////////////////////////////////////////////////////////////////
  92. // BaseLayoutManager, ash::ShellObserver overrides:
  93. void BaseLayoutManager::OnDisplayWorkAreaInsetsChanged() {
  94. AdjustAllWindowsBoundsForWorkAreaChange(
  95. ADJUST_WINDOW_WORK_AREA_INSETS_CHANGED);
  96. }
  97. /////////////////////////////////////////////////////////////////////////////
  98. // BaseLayoutManager, WindowObserver overrides:
  99. void BaseLayoutManager::OnWindowPropertyChanged(aura::Window* window,
  100. const void* key,
  101. intptr_t old) {
  102. if (key == aura::client::kShowStateKey) {
  103. ui::WindowShowState old_state = static_cast<ui::WindowShowState>(old);
  104. ui::WindowShowState new_state =
  105. window->GetProperty(aura::client::kShowStateKey);
  106. if (old_state != new_state && old_state != ui::SHOW_STATE_MINIMIZED &&
  107. !GetRestoreBoundsInScreen(window) &&
  108. ((new_state == ui::SHOW_STATE_MAXIMIZED &&
  109. old_state != ui::SHOW_STATE_FULLSCREEN) ||
  110. (new_state == ui::SHOW_STATE_FULLSCREEN &&
  111. old_state != ui::SHOW_STATE_MAXIMIZED))) {
  112. SetRestoreBoundsInParent(window, window->bounds());
  113. }
  114. UpdateBoundsFromShowState(window);
  115. ShowStateChanged(window, old_state);
  116. }
  117. }
  118. void BaseLayoutManager::OnWindowDestroying(aura::Window* window) {
  119. if (root_window_ == window) {
  120. root_window_->RemoveObserver(this);
  121. root_window_ = NULL;
  122. }
  123. }
  124. void BaseLayoutManager::OnWindowBoundsChanged(aura::Window* window,
  125. const gfx::Rect& old_bounds,
  126. const gfx::Rect& new_bounds) {
  127. if (root_window_ == window)
  128. AdjustAllWindowsBoundsForWorkAreaChange(ADJUST_WINDOW_DISPLAY_SIZE_CHANGED);
  129. }
  130. //////////////////////////////////////////////////////////////////////////////
  131. // BaseLayoutManager, aura::client::ActivationChangeObserver implementation:
  132. void BaseLayoutManager::OnWindowActivated(aura::Window* gained_active,
  133. aura::Window* lost_active) {
  134. if (views::corewm::UseFocusController()) {
  135. if (gained_active && wm::IsWindowMinimized(gained_active) &&
  136. !gained_active->IsVisible()) {
  137. gained_active->Show();
  138. DCHECK(!wm::IsWindowMinimized(gained_active));
  139. }
  140. }
  141. }
  142. //////////////////////////////////////////////////////////////////////////////
  143. // BaseLayoutManager, protected:
  144. void BaseLayoutManager::ShowStateChanged(aura::Window* window,
  145. ui::WindowShowState last_show_state) {
  146. if (wm::IsWindowMinimized(window)) {
  147. // Save the previous show state so that we can correctly restore it.
  148. window->SetProperty(aura::client::kRestoreShowStateKey, last_show_state);
  149. views::corewm::SetWindowVisibilityAnimationType(
  150. window, WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE);
  151. // Hide the window.
  152. window->Hide();
  153. // Activate another window.
  154. if (wm::IsActiveWindow(window))
  155. wm::DeactivateWindow(window);
  156. } else if ((window->TargetVisibility() ||
  157. last_show_state == ui::SHOW_STATE_MINIMIZED) &&
  158. !window->layer()->visible()) {
  159. // The layer may be hidden if the window was previously minimized. Make
  160. // sure it's visible.
  161. window->Show();
  162. if (last_show_state == ui::SHOW_STATE_MINIMIZED &&
  163. !wm::IsWindowMaximized(window) &&
  164. !wm::IsWindowFullscreen(window)) {
  165. window->ClearProperty(internal::kWindowRestoresToRestoreBounds);
  166. }
  167. }
  168. }
  169. void BaseLayoutManager::AdjustAllWindowsBoundsForWorkAreaChange(
  170. AdjustWindowReason reason) {
  171. // Don't do any adjustments of the insets while we are in screen locked mode.
  172. // This would happen if the launcher was auto hidden before the login screen
  173. // was shown and then gets shown when the login screen gets presented.
  174. if (reason == ADJUST_WINDOW_WORK_AREA_INSETS_CHANGED &&
  175. Shell::GetInstance()->session_state_delegate()->IsScreenLocked())
  176. return;
  177. // If a user plugs an external display into a laptop running Aura the
  178. // display size will change. Maximized windows need to resize to match.
  179. // We also do this when developers running Aura on a desktop manually resize
  180. // the host window.
  181. // We also need to do this when the work area insets changes.
  182. for (WindowSet::const_iterator it = windows_.begin();
  183. it != windows_.end();
  184. ++it) {
  185. AdjustWindowBoundsForWorkAreaChange(*it, reason);
  186. }
  187. }
  188. void BaseLayoutManager::AdjustWindowBoundsForWorkAreaChange(
  189. aura::Window* window,
  190. AdjustWindowReason reason) {
  191. if (wm::IsWindowMaximized(window)) {
  192. SetChildBoundsDirect(
  193. window, ScreenAsh::GetMaximizedWindowBoundsInParent(window));
  194. } else if (wm::IsWindowFullscreen(window)) {
  195. SetChildBoundsDirect(
  196. window, ScreenAsh::GetDisplayBoundsInParent(window));
  197. } else {
  198. // The work area may be smaller than the full screen.
  199. gfx::Rect display_rect =
  200. ScreenAsh::GetDisplayWorkAreaBoundsInParent(window);
  201. // Put as much of the window as possible within the display area.
  202. gfx::Rect bounds = window->bounds();
  203. bounds.AdjustToFit(display_rect);
  204. window->SetBounds(bounds);
  205. }
  206. }
  207. //////////////////////////////////////////////////////////////////////////////
  208. // BaseLayoutManager, private:
  209. void BaseLayoutManager::UpdateBoundsFromShowState(aura::Window* window) {
  210. switch (window->GetProperty(aura::client::kShowStateKey)) {
  211. case ui::SHOW_STATE_DEFAULT:
  212. case ui::SHOW_STATE_NORMAL: {
  213. const gfx::Rect* restore = GetRestoreBoundsInScreen(window);
  214. if (restore) {
  215. gfx::Rect bounds_in_parent =
  216. ScreenAsh::ConvertRectFromScreen(window->parent(), *restore);
  217. SetChildBoundsDirect(window,
  218. BoundsWithScreenEdgeVisible(window,
  219. bounds_in_parent));
  220. }
  221. ClearRestoreBounds(window);
  222. break;
  223. }
  224. case ui::SHOW_STATE_MAXIMIZED:
  225. SetChildBoundsDirect(window,
  226. ScreenAsh::GetMaximizedWindowBoundsInParent(window));
  227. break;
  228. case ui::SHOW_STATE_FULLSCREEN:
  229. // Don't animate the full-screen window transition.
  230. // TODO(jamescook): Use animation here. Be sure the lock screen works.
  231. SetChildBoundsDirect(
  232. window, ScreenAsh::GetDisplayBoundsInParent(window));
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. } // namespace internal
  239. } // namespace ash