def set_location(self, x, y): window_frame = self._nswindow.frame() rect = self._nswindow.contentRectForFrameRect_(window_frame) screen_frame = self._nswindow.screen().frame() screen_width = int(screen_frame.size.width) screen_height = int(screen_frame.size.height) origin = cocoapy.NSPoint(x, screen_height - y - rect.size.height) self._nswindow.setFrameOrigin_(origin)
def notify(self): with AutoReleasePool(): notifyEvent = NSEvent.otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_( cocoapy.NSApplicationDefined, # type cocoapy.NSPoint(0.0, 0.0), # location 0, # modifierFlags 0, # timestamp 0, # windowNumber None, # graphicsContext 0, # subtype 0, # data1 0, # data2 ) self.NSApp.postEvent_atStart_(notifyEvent, False)
def set_mouse_position(self, x, y, absolute=False): if absolute: # If absolute, then x, y is given in global display coordinates # which sets (0,0) at top left corner of main display. It is possible # to warp the mouse position to a point inside of another display. quartz.CGWarpMouseCursorPosition(CGPoint(x,y)) else: # Window-relative coordinates: (x, y) are given in window coords # with (0,0) at bottom-left corner of window and y up. We find # which display the window is in and then convert x, y into local # display coords where (0,0) is now top-left of display and y down. screenInfo = self._nswindow.screen().deviceDescription() displayID = screenInfo.objectForKey_(cocoapy.get_NSString('NSScreenNumber')) displayID = displayID.intValue() displayBounds = quartz.CGDisplayBounds(displayID) frame = self._nswindow.frame() windowOrigin = frame.origin x += windowOrigin.x y = displayBounds.size.height - windowOrigin.y - y quartz.CGDisplayMoveCursorToPoint(displayID, cocoapy.NSPoint(x, y))
def _center_window(self): # [NSWindow center] does not move the window to a true center position # and also always moves the window to the main display. x = self.screen.x + int((self.screen.width - self._width) // 2) y = self.screen.y + int((self.screen.height - self._height) // 2) self._nswindow.setFrameOrigin_(cocoapy.NSPoint(x, y))