screen_position_client.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 "ui/aura/client/screen_position_client.h"
  5. #include "ui/base/class_property.h"
  6. #include "ui/gfx/geometry/point_conversions.h"
  7. DEFINE_UI_CLASS_PROPERTY_TYPE(aura::client::ScreenPositionClient*)
  8. namespace aura {
  9. namespace client {
  10. DEFINE_UI_CLASS_PROPERTY_KEY(ScreenPositionClient*,
  11. kScreenPositionClientKey,
  12. nullptr)
  13. void ScreenPositionClient::ConvertPointToScreen(const Window* window,
  14. gfx::Point* point) {
  15. gfx::PointF point_float(*point);
  16. ConvertPointToScreen(window, &point_float);
  17. *point = gfx::ToFlooredPoint(point_float);
  18. }
  19. void ScreenPositionClient::ConvertPointFromScreen(const Window* window,
  20. gfx::Point* point) {
  21. gfx::PointF point_float(*point);
  22. ConvertPointFromScreen(window, &point_float);
  23. *point = gfx::ToFlooredPoint(point_float);
  24. }
  25. void ScreenPositionClient::ConvertPointToRootWindowIgnoringTransforms(
  26. const Window* window,
  27. gfx::Point* point) {
  28. DCHECK(window);
  29. DCHECK(window->GetRootWindow());
  30. Window* ancestor = const_cast<Window*>(window);
  31. while (ancestor && !ancestor->IsRootWindow()) {
  32. const gfx::Point origin = ancestor->bounds().origin();
  33. point->Offset(origin.x(), origin.y());
  34. ancestor = ancestor->parent();
  35. }
  36. }
  37. void ScreenPositionClient::ConvertPointToScreenIgnoringTransforms(
  38. const aura::Window* window,
  39. gfx::Point* point) {
  40. const aura::Window* root_window = window->GetRootWindow();
  41. ConvertPointToRootWindowIgnoringTransforms(window, point);
  42. gfx::Point origin = GetRootWindowOriginInScreen(root_window);
  43. point->Offset(origin.x(), origin.y());
  44. }
  45. void SetScreenPositionClient(Window* root_window,
  46. ScreenPositionClient* client) {
  47. DCHECK_EQ(root_window->GetRootWindow(), root_window);
  48. root_window->SetProperty(kScreenPositionClientKey, client);
  49. }
  50. ScreenPositionClient* GetScreenPositionClient(const Window* root_window) {
  51. if (root_window)
  52. DCHECK_EQ(root_window->GetRootWindow(), root_window);
  53. return root_window ?
  54. root_window->GetProperty(kScreenPositionClientKey) : NULL;
  55. }
  56. } // namespace client
  57. } // namespace aura