2015-12-30 06:41:46 +00:00
|
|
|
#if defined(Hiro_Application)
|
|
|
|
|
2013-03-15 13:11:33 +00:00
|
|
|
@implementation CocoaDelegate : NSObject
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*)sender {
|
2015-12-30 06:41:46 +00:00
|
|
|
using hiro::Application;
|
|
|
|
if(Application::state.cocoa.onQuit) Application::Cocoa::doQuit();
|
2013-03-15 13:11:33 +00:00
|
|
|
else Application::quit();
|
|
|
|
return NSTerminateCancel;
|
|
|
|
}
|
|
|
|
|
2013-03-21 12:59:01 +00:00
|
|
|
-(BOOL) applicationShouldHandleReopen:(NSApplication*)application hasVisibleWindows:(BOOL)flag {
|
2015-12-30 06:41:46 +00:00
|
|
|
using hiro::Application;
|
|
|
|
if(Application::state.cocoa.onActivate) Application::Cocoa::doActivate();
|
2013-03-21 12:59:01 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) run:(NSTimer*)timer {
|
2015-12-30 06:41:46 +00:00
|
|
|
using hiro::Application;
|
|
|
|
if(Application::state.onMain) Application::doMain();
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2013-04-09 13:31:46 +00:00
|
|
|
-(void) updateInDock:(NSTimer*)timer {
|
2013-05-02 11:25:45 +00:00
|
|
|
NSArray* windows = [NSApp windows];
|
2015-12-30 06:41:46 +00:00
|
|
|
for(uint n = 0; n < [windows count]; n++) {
|
2013-05-02 11:25:45 +00:00
|
|
|
NSWindow* window = [windows objectAtIndex:n];
|
2013-04-09 13:31:46 +00:00
|
|
|
if([window isMiniaturized]) {
|
|
|
|
[window updateInDock];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-15 13:11:33 +00:00
|
|
|
@end
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
CocoaDelegate* cocoaDelegate = nullptr;
|
2017-10-07 07:28:12 +00:00
|
|
|
NSTimer* applicationTimer = nullptr;
|
2013-03-15 13:11:33 +00:00
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
namespace hiro {
|
2013-03-15 13:11:33 +00:00
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pApplication::run() -> void {
|
2017-10-07 07:28:12 +00:00
|
|
|
//applicationTimer = [NSTimer scheduledTimerWithTimeInterval:0.1667 target:cocoaDelegate selector:@selector(updateInDock:) userInfo:nil repeats:YES];
|
2013-04-09 13:31:46 +00:00
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
if(Application::state.onMain) {
|
2017-10-07 07:28:12 +00:00
|
|
|
applicationTimer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:cocoaDelegate selector:@selector(run:) userInfo:nil repeats:YES];
|
2013-03-21 12:59:01 +00:00
|
|
|
|
|
|
|
//below line is needed to run application during window resize; however it has a large performance penalty on the resize smoothness
|
2017-10-07 07:28:12 +00:00
|
|
|
//[[NSRunLoop currentRunLoop] addTimer:applicationTimer forMode:NSEventTrackingRunLoopMode];
|
2013-03-15 13:11:33 +00:00
|
|
|
}
|
2013-04-09 13:31:46 +00:00
|
|
|
|
2013-03-15 13:11:33 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
[NSApp run];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pApplication::pendingEvents() -> bool {
|
2013-03-15 13:11:33 +00:00
|
|
|
bool result = false;
|
|
|
|
@autoreleasepool {
|
2013-05-02 11:25:45 +00:00
|
|
|
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:NO];
|
2013-03-15 13:11:33 +00:00
|
|
|
if(event != nil) result = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pApplication::processEvents() -> void {
|
2013-03-15 13:11:33 +00:00
|
|
|
@autoreleasepool {
|
2015-12-30 06:41:46 +00:00
|
|
|
while(!Application::state.quit) {
|
2013-05-02 11:25:45 +00:00
|
|
|
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
|
2013-03-15 13:11:33 +00:00
|
|
|
if(event == nil) break;
|
|
|
|
[event retain];
|
|
|
|
[NSApp sendEvent:event];
|
|
|
|
[event release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pApplication::quit() -> void {
|
2013-03-15 13:11:33 +00:00
|
|
|
@autoreleasepool {
|
2017-10-07 07:28:12 +00:00
|
|
|
[applicationTimer invalidate];
|
2013-03-15 13:11:33 +00:00
|
|
|
[NSApp stop:nil];
|
2013-05-02 11:25:45 +00:00
|
|
|
NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined location:NSMakePoint(0, 0) modifierFlags:0 timestamp:0.0 windowNumber:0 context:nil subtype:0 data1:0 data2:0];
|
2013-03-15 13:11:33 +00:00
|
|
|
[NSApp postEvent:event atStart:true];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 13:41:41 +00:00
|
|
|
auto pApplication::setScreenSaver(bool screenSaver) -> void {
|
|
|
|
//TODO: not implemented
|
|
|
|
}
|
|
|
|
|
2015-12-30 06:41:46 +00:00
|
|
|
auto pApplication::initialize() -> void {
|
2013-03-15 13:11:33 +00:00
|
|
|
@autoreleasepool {
|
|
|
|
[NSApplication sharedApplication];
|
|
|
|
cocoaDelegate = [[CocoaDelegate alloc] init];
|
|
|
|
[NSApp setDelegate:cocoaDelegate];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-12-30 06:41:46 +00:00
|
|
|
|
|
|
|
#endif
|