def initWithWindow_(self, window): self = ObjCInstance(send_super(self, 'init')) if not self: return None # CocoaWindow object. self._window = window window._nswindow.setDelegate_(self) # Register delegate for hide and unhide notifications so that we # can dispatch the corresponding pyglet events. notificationCenter = NSNotificationCenter.defaultCenter() notificationCenter.addObserver_selector_name_object_( self, get_selector('applicationDidHide:'), NSApplicationDidHideNotification, None) notificationCenter.addObserver_selector_name_object_( self, get_selector('applicationDidUnhide:'), NSApplicationDidUnhideNotification, None) # Flag set when we pause exclusive mouse mode if window loses key status. self.did_pause_exclusive_mouse = False return self
def dispatch_events(self): self._allow_dispatch_event = True # Process all pyglet events. self.dispatch_pending_events() event = True # Dequeue and process all of the pending Cocoa events. pool = NSAutoreleasePool.new() NSApp = NSApplication.sharedApplication() while event and self._nswindow and self._context: event = NSApp.nextEventMatchingMask_untilDate_inMode_dequeue_( cocoapy.NSAnyEventMask, None, cocoapy.NSEventTrackingRunLoopMode, True) if event: event_type = event.type() # Pass on all events. NSApp.sendEvent_(event) # And resend key events to special handlers. if event_type == cocoapy.NSKeyDown and not event.isARepeat(): NSApp.sendAction_to_from_( cocoapy.get_selector('pygletKeyDown:'), None, event) elif event_type == cocoapy.NSKeyUp: NSApp.sendAction_to_from_( cocoapy.get_selector('pygletKeyUp:'), None, event) elif event_type == cocoapy.NSFlagsChanged: NSApp.sendAction_to_from_( cocoapy.get_selector('pygletFlagsChanged:'), None, event) NSApp.updateWindows() pool.drain() self._allow_dispatch_event = False
def step(self, timeout=None): with AutoReleasePool(): self.dispatch_posted_events() # Determine the timeout date. if timeout is None: # Using distantFuture as untilDate means that nextEventMatchingMask # will wait until the next event comes along. timeout_date = NSDate.distantFuture() else: timeout_date = NSDate.dateWithTimeIntervalSinceNow_(timeout) # Retrieve the next event (if any). We wait for an event to show up # and then process it, or if timeout_date expires we simply return. # We only process one event per call of step(). self._is_running.set() event = self.NSApp.nextEventMatchingMask_untilDate_inMode_dequeue_( cocoapy.NSAnyEventMask, timeout_date, cocoapy.NSDefaultRunLoopMode, True) # Dispatch the event (if any). if event is not None: event_type = event.type() if event_type != cocoapy.NSApplicationDefined: # Send out event as normal. Responders will still receive # keyUp:, keyDown:, and flagsChanged: events. self.NSApp.sendEvent_(event) # Resend key events as special pyglet-specific messages # which supplant the keyDown:, keyUp:, and flagsChanged: messages # because NSApplication translates multiple key presses into key # equivalents before sending them on, which means that some keyUp: # messages are never sent for individual keys. Our pyglet-specific # replacements ensure that we see all the raw key presses & releases. # We also filter out key-down repeats since pyglet only sends one # on_key_press event per key press. if event_type == cocoapy.NSKeyDown and not event.isARepeat( ): self.NSApp.sendAction_to_from_( cocoapy.get_selector("pygletKeyDown:"), None, event) elif event_type == cocoapy.NSKeyUp: self.NSApp.sendAction_to_from_( cocoapy.get_selector("pygletKeyUp:"), None, event) elif event_type == cocoapy.NSFlagsChanged: self.NSApp.sendAction_to_from_( cocoapy.get_selector("pygletFlagsChanged:"), None, event) self.NSApp.updateWindows() did_time_out = False else: did_time_out = True self._is_running.clear() return did_time_out
def add_menu_item(menu, title, action, key): with AutoReleasePool(): title = cocoapy.CFSTR(title) action = cocoapy.get_selector(action) key = cocoapy.CFSTR(key) menuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_( title, action, key) menu.addItem_(menuItem) # cleanup title.release() key.release() menuItem.release()
def validateMenuItem_(self, menuitem): # Disable quitting with command-q when in keyboard exclusive mode. if menuitem.action() == get_selector('terminate:'): return not self._window._is_keyboard_exclusive return True