coordinate_conversion.mm 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2014 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. #import "ui/gfx/mac/coordinate_conversion.h"
  5. #import <Cocoa/Cocoa.h>
  6. #include "ui/gfx/geometry/point.h"
  7. #include "ui/gfx/geometry/rect.h"
  8. namespace gfx {
  9. namespace {
  10. // The height of the primary display, which OSX defines as the monitor with the
  11. // menubar. This is always at index 0.
  12. CGFloat PrimaryDisplayHeight() {
  13. return NSMaxY([[[NSScreen screens] firstObject] frame]);
  14. }
  15. } // namespace
  16. NSRect ScreenRectToNSRect(const Rect& rect) {
  17. return NSMakeRect(rect.x(),
  18. PrimaryDisplayHeight() - rect.y() - rect.height(),
  19. rect.width(),
  20. rect.height());
  21. }
  22. Rect ScreenRectFromNSRect(const NSRect& rect) {
  23. return Rect(rect.origin.x,
  24. PrimaryDisplayHeight() - rect.origin.y - rect.size.height,
  25. rect.size.width, rect.size.height);
  26. }
  27. NSPoint ScreenPointToNSPoint(const Point& point) {
  28. return NSMakePoint(point.x(), PrimaryDisplayHeight() - point.y());
  29. }
  30. Point ScreenPointFromNSPoint(const NSPoint& point) {
  31. return Point(point.x, PrimaryDisplayHeight() - point.y);
  32. }
  33. } // namespace gfx