screen_position_controller.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/display/screen_position_controller.h"
  5. #include "ash/public/cpp/shell_window_ids.h"
  6. #include "ash/shell.h"
  7. #include "ash/wm/window_positioning_utils.h"
  8. #include "ash/wm/window_properties.h"
  9. #include "ash/wm/window_state.h"
  10. #include "ui/aura/client/screen_position_client.h"
  11. #include "ui/aura/window.h"
  12. #include "ui/aura/window_tree_host.h"
  13. #include "ui/display/display.h"
  14. #include "ui/display/screen.h"
  15. #include "ui/wm/core/window_properties.h"
  16. #include "ui/wm/core/window_util.h"
  17. namespace ash {
  18. // static
  19. void ScreenPositionController::ConvertHostPointToRelativeToRootWindow(
  20. aura::Window* root_window,
  21. const aura::Window::Windows& root_windows,
  22. gfx::Point* point,
  23. aura::Window** target_root) {
  24. DCHECK(!root_window->parent());
  25. gfx::Point point_in_root(*point);
  26. root_window->GetHost()->ConvertPixelsToDIP(&point_in_root);
  27. gfx::Rect host_bounds(root_window->GetHost()->GetBoundsInPixels().size());
  28. if (!host_bounds.Contains(*point)) {
  29. // This conversion is necessary to deal with X's passive input
  30. // grab while dragging window. For example, if we have two
  31. // displays, say 1000x1000 (primary) and 500x500 (extended one
  32. // on the right), and start dragging a window at (999, 123), and
  33. // then move the pointer to the right, the pointer suddenly
  34. // warps to the extended display. The destination is (0, 123) in
  35. // the secondary root window's coordinates, or (1000, 123) in
  36. // the screen coordinates. However, since the mouse is captured
  37. // by X during drag, a weird LocatedEvent, something like (0, 1123)
  38. // in the *primary* root window's coordinates, is sent to Chrome
  39. // (Remember that in the native X11 world, the two root windows
  40. // are always stacked vertically regardless of the display
  41. // layout in Ash). We need to figure out that (0, 1123) in the
  42. // primary root window's coordinates is actually (0, 123) in the
  43. // extended root window's coordinates.
  44. //
  45. // For now Ozone works in a similar manner as X11. Transitioning from one
  46. // display's coordinate system to anothers may cause events in the
  47. // primary's coordinate system which fall in the extended display.
  48. gfx::Point location_in_native(*point);
  49. // |point| is in the native host window coordinate. Convert it to the native
  50. // screen coordinate.
  51. const gfx::Point host_origin =
  52. root_window->GetHost()->GetBoundsInPixels().origin();
  53. location_in_native.Offset(host_origin.x(), host_origin.y());
  54. for (size_t i = 0; i < root_windows.size(); ++i) {
  55. aura::WindowTreeHost* host = root_windows[i]->GetHost();
  56. const gfx::Rect native_bounds = host->GetBoundsInPixels();
  57. if (native_bounds.Contains(location_in_native)) {
  58. *target_root = root_windows[i];
  59. *point = location_in_native;
  60. host->ConvertScreenInPixelsToDIP(point);
  61. return;
  62. }
  63. }
  64. }
  65. *target_root = root_window;
  66. *point = point_in_root;
  67. }
  68. void ScreenPositionController::ConvertPointToScreen(const aura::Window* window,
  69. gfx::PointF* point) {
  70. const aura::Window* root = window->GetRootWindow();
  71. aura::Window::ConvertPointToTarget(window, root, point);
  72. const gfx::Point display_origin =
  73. display::Screen::GetScreen()
  74. ->GetDisplayNearestWindow(const_cast<aura::Window*>(root))
  75. .bounds()
  76. .origin();
  77. point->Offset(display_origin.x(), display_origin.y());
  78. }
  79. void ScreenPositionController::ConvertPointFromScreen(
  80. const aura::Window* window,
  81. gfx::PointF* point) {
  82. const aura::Window* root = window->GetRootWindow();
  83. const gfx::Point display_origin =
  84. display::Screen::GetScreen()
  85. ->GetDisplayNearestWindow(const_cast<aura::Window*>(root))
  86. .bounds()
  87. .origin();
  88. point->Offset(-display_origin.x(), -display_origin.y());
  89. aura::Window::ConvertPointToTarget(root, window, point);
  90. }
  91. void ScreenPositionController::ConvertHostPointToScreen(
  92. aura::Window* root_window,
  93. gfx::Point* point) {
  94. aura::Window* root = root_window->GetRootWindow();
  95. aura::Window* target_root = nullptr;
  96. ConvertHostPointToRelativeToRootWindow(root, Shell::GetAllRootWindows(),
  97. point, &target_root);
  98. aura::client::ScreenPositionClient::ConvertPointToScreen(target_root, point);
  99. }
  100. void ScreenPositionController::SetBounds(aura::Window* window,
  101. const gfx::Rect& bounds,
  102. const display::Display& display) {
  103. if (!window->parent()->GetProperty(::wm::kUsesScreenCoordinatesKey)) {
  104. window->SetBounds(bounds);
  105. return;
  106. }
  107. SetBoundsInScreen(window, bounds, display);
  108. }
  109. gfx::Point ScreenPositionController::GetRootWindowOriginInScreen(
  110. const aura::Window* root_window) {
  111. DCHECK(root_window->IsRootWindow());
  112. const display::Display& display =
  113. display::Screen::GetScreen()->GetDisplayNearestWindow(
  114. const_cast<aura::Window*>(root_window));
  115. return display.bounds().origin();
  116. }
  117. } // namespace ash