Skip to content

Instantly share code, notes, and snippets.

@giraphics
Created September 13, 2018 04:56
Show Gist options
  • Save giraphics/9cf9eef07875aed42bee61743325e3d9 to your computer and use it in GitHub Desktop.
Save giraphics/9cf9eef07875aed42bee61743325e3d9 to your computer and use it in GitHub Desktop.
Command line Cocoa application
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
@interface MyAppDelegate : NSObject <NSApplicationDelegate>
{
NSWindow *window;
}
@end
@implementation MyAppDelegate
-(id) init
{
self = [super init];
if (self)
{
// total *main* screen frame size //
NSRect mainDisplayRect = [[NSScreen mainScreen] frame];
// calculate the window rect to be half the display and be centered //
NSRect windowRect = NSMakeRect(mainDisplayRect.origin.x + (mainDisplayRect.size.width) * 0.25,
mainDisplayRect.origin.y + (mainDisplayRect.size.height) * 0.25,
mainDisplayRect.size.width * 0.5,
mainDisplayRect.size.height * 0.5);
/*
Pick your window style:
NSBorderlessWindowMask
NSTitledWindowMask
NSClosableWindowMask
NSMiniaturizableWindowMask
NSResizableWindowMask
*/
NSUInteger windowStyle = NSTitledWindowMask | NSMiniaturizableWindowMask;
// set the window level to be on top of everything else //
NSInteger windowLevel = NSMainMenuWindowLevel + 1;
// initialize the window and its properties // just examples of properties that can be changed //
window = [[NSWindow alloc] initWithContentRect:windowRect styleMask:windowStyle backing:NSBackingStoreBuffered defer:NO];
[window setLevel:windowLevel];
[window setOpaque:YES];
[window setHasShadow:YES];
[window setPreferredBackingLocation:NSWindowBackingLocationVideoMemory];
[window setHidesOnDeactivate:NO];
[window setBackgroundColor:[NSColor greenColor]];
}
return self;
}
- (void)applicationWillFinishLaunching:(NSNotification *)notification
{
// make the window visible when the app is about to finish launching //
[window makeKeyAndOrderFront:self];
/* do layout and cool stuff here */
}
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification
{
/* initialize your code stuff here */
}
- (void)dealloc
{
// release your window and other stuff //
[window release];
[super dealloc];
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSApplication *app = [NSApplication sharedApplication];
[app setDelegate:[[[MyAppDelegate alloc] init] autorelease]];
[app run];
}
return 0;
}
// Command to build in TERMINAL
/*clang -framework Foundation -framework Cocoa NSViewExperiements.mm -o NSViewExperiments.app*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment