var
window:NSWindow;
screenwidth,screenheight:dword;
type
TCocoaAppDelegate = objcclass (NSObject, NSApplicationDelegateProtocol)
end;
TCocoaApp = objcclass (NSApplication)
procedure poll; message 'poll';
end;
TCocoaWindow = objcclass (NSWindow)
function initWithContentRect_styleMask_backing_defer (contentRect: NSRect; aStyle: NSUInteger; bufferingType: NSBackingStoreType; flag: boolean): id; message 'initWithContentRect:styleMask:backing:defer:';
procedure dealloc; override;
function windowShouldClose (notification: NSNotification):boolean; message 'windowShouldClose:';
procedure screenParametersChanged (notification: NSNotification); message 'screenParametersChanged:';
end;
procedure TCocoaWindow.screenParametersChanged (notification: NSNotification);
var theframe:NSRect;
begin
theframe := NSMakeRect(0,0,screenwidth,screenheight); // NSScreen.mainScreen.frame;
setFrame_display(theframe, true);
end;
function TCocoaWindow.windowShouldClose (notification: NSNotification):boolean;
begin
gfxDone:=true;
result:=true;
end;
procedure TCocoaWindow.dealloc;
begin
NSNotificationCenter.defaultCenter.removeObserver(self);
inherited dealloc;
end;
function TCocoaWindow.initWithContentRect_styleMask_backing_defer (contentRect: NSRect; aStyle: NSUInteger; bufferingType: NSBackingStoreType; flag: boolean): id;
begin
result := inherited initWithContentRect_styleMask_backing_defer(contentRect, aStyle, bufferingType, flag);
if result <> nil then
NSNotificationCenter(NSNotificationCenter.defaultCenter).addObserver_selector_name_object(result, objcselector('screenParametersChanged:'), NSApplicationDidChangeScreenParametersNotification, nil);
end;
procedure TCocoaApp.poll;
var
event: NSEvent;
pool: NSAutoreleasePool;
myType:longint;
mySendEvent : boolean;
myKeyCode:longint;
myMouseX,myMouseY:longint;
begin
pool := NSAutoreleasePool.alloc.init;
event := nextEventMatchingMask_untilDate_inMode_dequeue(NSAnyEventMask, {NSDate.distantPast}nil, NSDefaultRunLoopMode, true);
if event<>nil then begin
fillchar(gfxMessanger^,sizeof(longint)*4,0);
myType:=event.type_;
mySendEvent:=true;
if (myType = NSKeyDown) or (myType = NSKeyUp) then begin myKeyCode:=event.keycode; mySendEvent:=false; end;
if myType = NSMouseMoved then myMouseX:=round(event.locationInWindow.x);
if myType = NSMouseMoved then myMouseY:=round(vscreen.height-event.locationInWindow.y);
system.move(myType,gfxMessanger^,sizeof(longint));
system.move(myKeyCode,(gfxMessanger+sizeof(longint))^,sizeof(longint));
system.move(myMouseX,(gfxMessanger+sizeof(longint)*2)^,sizeof(longint));
system.move(myMouseY,(gfxMessanger+sizeof(longint)*3)^,sizeof(longint));
if mySendEvent then begin
sendEvent(event);
updateWindows;
end;
end;
pool.release;
end;
procedure cocoaCreateWindow(width,height:dword;title:string;fullscreen:boolean);
var
windowFlags: NSUInteger = 0;
frame:NSRect;
bar : NSMenu;
appmenu:NSMenu;
menuItem: NSMenuItem;
begin
screenwidth:=width;
screenheight:=height;
// frame := NSMakeRect(0,0,NSScreen.mainScreen.frame.size.width,NSScreen.mainScreen.frame.size.height);
frame := NSMakeRect(0,0,screenwidth,screenheight);
if fullscreen then windowFlags:=NSBorderlessWindowMask;
if not fullscreen then windowFlags:=NSTitledWindowMask or NSClosableWindowMask; // or NSMiniaturizableWindowMask; //or NSResizableWindowMask;
window := TCocoaWindow.alloc.initWithContentRect_styleMask_backing_defer(frame,windowFlags,NSBackingStoreBuffered,false);
window.setTitle(NSSTR(title));
if fullscreen then window.setCollectionBehavior(NSWindowCollectionBehaviorStationary or NSWindowCollectionBehaviorCanJoinAllSpaces or NSWindowCollectionBehaviorFullScreenPrimary );
if not fullscreen then window.setCollectionBehavior(NSWindowCollectionBehaviorManaged);
if fullscreen then window.toggleFullscreen(nil);
if fullscreen then NSApp.setPresentationOptions(NSApplicationPresentationFullScreen or NSApplicationPresentationHideDock or NSApplicationPresentationHideMenuBar);
if not fullscreen then NSApp.setPresentationOptions(NSApplicationPresentationDefault);
window.makeKeyAndOrderFront(nil);
window.setAcceptsMouseMovedEvents(true);
end;
procedure Cocoa_PollEvents;
begin
TCocoaApp(TCocoaApp.sharedApplication).poll;
//NSApp.performSelectorOnMainThread_withObject_waitUntilDone(objcselector('poll'), nil, true);
end;
procedure CocoaInit;
var
app: TCocoaApp;
delegate: TCocoaAppDelegate;
begin
app := TCocoaApp(TCocoaApp.sharedApplication);
delegate := TCocoaAppDelegate.alloc.init;
app.setDelegate(delegate);
NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
NSApp.activateIgnoringOtherApps(true);
NSApp.finishLaunching;
end;
procedure CocoaClose;
begin
window.close;
Cocoa_PollEvents;
end;