Created
September 19, 2011 18:54
-
-
Save jarek-foksa/1227267 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Boxy_CSS.h" | |
@implementation Boxy_CSS | |
@synthesize window; | |
@synthesize webView; | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { | |
} | |
- (void)awakeFromNib { | |
[self startServer]; | |
[window setDelegate:self]; // close the app when window is closed | |
} | |
- (void)windowWillClose:(NSNotification *)aNotification { | |
// The code from this function fires when user clicks red button. | |
// TODO: do not close the application here, instead send message to client | |
// that user requested window close [WebView trigger('Cocoa->clickedCloseButton')] | |
// and let client decide when to close the window | |
[self closeApplication]; | |
} | |
- (void)closeApplication { | |
[self stopServer]; | |
[NSApp terminate: nil]; | |
NSLog ( @"Application terminated properly"); | |
} | |
//############################################################################################## | |
//############################################################################################## | |
// SERVER | |
//############################################################################################## | |
//############################################################################################## | |
// | |
// Start server process | |
// | |
- (void) startServer { | |
serverProcess = [[NSTask alloc] init]; | |
// Path to the resources directory in app bundle | |
NSString *resourcesPath = [[NSBundle mainBundle] resourcePath]; | |
// Path to Node.js binary in app bundle | |
NSString *nodeJsPath = [resourcesPath stringByAppendingString:@"/node"]; | |
// Path to server directory in app bundle | |
NSString *serverPath = [resourcesPath stringByAppendingString:@"/server"]; | |
// Path to server.js file in app bundle | |
NSString *serverJsPath = [resourcesPath stringByAppendingString:@"/server/server.js"]; | |
[serverProcess setCurrentDirectoryPath:serverPath]; | |
[serverProcess setLaunchPath:nodeJsPath]; | |
[serverProcess setArguments:[NSArray arrayWithObjects:serverJsPath,nil]]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleServerOutput:) | |
name:NSFileHandleReadCompletionNotification object:nil]; | |
pipe=[NSPipe pipe]; | |
[serverProcess setStandardOutput:pipe]; | |
[serverProcess setStandardError:pipe]; | |
[[pipe fileHandleForReading] readInBackgroundAndNotify]; | |
[serverProcess launch]; | |
} | |
// | |
// Stop server process | |
// | |
- (void) stopServer { | |
[serverProcess terminate]; | |
serverProcess = nil; | |
} | |
// | |
// Handle output from server process | |
// | |
-(void)handleServerOutput:(NSNotification*)notification{ | |
NSString* str = [[NSString alloc] initWithData:[[notification userInfo] objectForKey:NSFileHandleNotificationDataItem] | |
encoding:NSASCIIStringEncoding]; | |
NSFileHandle *fileHandle = (NSFileHandle*)[notification object]; | |
if([str length] > 0){ | |
NSLog ( @"%@", str); | |
} | |
// Start the Webview only after "Server started" message was found in server output | |
if ([str rangeOfString:@"Server started"].location==0) { | |
[self serverStarted]; | |
} | |
[fileHandle readInBackgroundAndNotify]; | |
} | |
// | |
// Server started event | |
// | |
- (void) serverStarted { | |
[self startWebview]; | |
} | |
//############################################################################################## | |
//############################################################################################## | |
// VEWBVIEW | |
//############################################################################################## | |
//############################################################################################## | |
// | |
// Start Webview | |
// | |
- (void)startWebview { | |
[webView setFrameLoadDelegate:self]; | |
WebPreferences *prefs = [webView preferences]; | |
// Set font size | |
[prefs setDefaultFontSize: 16]; | |
[prefs setDefaultFixedFontSize: 16]; | |
[prefs setMinimumFontSize: 0]; | |
[prefs setMinimumLogicalFontSize: 0]; | |
// Enabled hardware accelaration | |
[prefs setAcceleratedDrawingEnabled: true]; | |
[prefs setAcceleratedCompositingEnabled:true]; | |
[prefs setAccelerated2dCanvasEnabled:true]; | |
[prefs setCanvasUsesAcceleratedDrawing: true]; | |
[prefs setWebGLEnabled:true]; | |
//[prefs setShowDebugBorders:true]; | |
// Enable developer tools | |
[prefs setDeveloperExtrasEnabled:true]; | |
// Make it possible to access iframes from top level document | |
[prefs setAllowUniversalAccessFromFileURLs:true]; | |
// Make it possible to access files stored in the client directory with XHR | |
[prefs setAllowFileAccessFromFileURLs:true]; | |
[prefs setJavaScriptCanAccessClipboard:true]; | |
[prefs setDOMPasteAllowed:true]; | |
// Load XHTML file | |
NSString *resourcesPath = [[NSBundle mainBundle] resourcePath]; | |
NSString *htmlPath = [resourcesPath stringByAppendingString:@"/client/index.xhtml"]; | |
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]]; | |
// Fire windowLostFocus() function when window loses focus | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowLostFocus) name:NSWindowDidResignKeyNotification object:nil]; | |
// Fire windowRecievedFocus() function when window recieves focus | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowRecievedFocus) name:NSWindowDidBecomeKeyNotification object:nil]; | |
NSLog ( @"WebView started"); | |
} | |
// | |
// Trigger client event: 'cocoa->windowFocusLost' | |
// | |
- (void) windowLostFocus { | |
[scriptObject evaluateWebScript:@"trigger('cocoa->windowLostFocus')"]; | |
} | |
- (void) windowRecievedFocus { | |
[scriptObject evaluateWebScript:@"trigger('cocoa->windowRecievedFocus')"]; | |
} | |
// | |
// Trigger client event: 'cocoa->clickedMenuItem->Copy' | |
// | |
- (void) clickedMenuItemCopy { | |
[scriptObject evaluateWebScript:@"console.log('Triggering event: cocoa->clickedMenuItem->Copy')"]; | |
[scriptObject evaluateWebScript:@"trigger('cocoa->clickedMenuItem->Copy')"]; | |
} | |
// | |
// Trigger client event: 'cocoa->clickedMenuItem->Paste' | |
// | |
- (void) clickedMenuItemPaste { | |
[scriptObject evaluateWebScript:@"console.log('Triggering event: cocoa->clickedMenuItem->Paste')"]; | |
[scriptObject evaluateWebScript:@"trigger('cocoa->clickedMenuItem->Paste')"]; | |
} | |
// | |
// DOM loaded VebView event (the same as window.onload in JS) | |
// | |
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { | |
if (frame == [frame findFrameNamed:@"_top"]) { | |
NSLog( @"WebView loaded"); | |
scriptObject = [sender windowScriptObject]; | |
[scriptObject setValue:self forKey:@"cocoa"]; | |
[self clickedMenuItemCopy]; | |
[self clickedMenuItemPaste]; | |
} | |
} | |
- (void)consoleLog { | |
NSLog(@"JSLog"); | |
} | |
+(NSString*)webScriptNameForSelector:(SEL)sel { | |
if(sel == @selector(consoleLog:)) | |
return @"log"; | |
return nil; | |
} | |
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel { | |
if(sel == @selector(consoleLog:)) | |
return NO; | |
return YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment