mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
wii - gettings specs on App.open. Window closing not working quite right.
This commit is contained in:
@@ -6,9 +6,6 @@
|
||||
|
||||
@property (nonatomic, retain) NSMutableArray *controllers;
|
||||
|
||||
- (AtomController *)createController:(NSString *)path;
|
||||
- (AtomController *)createController:(NSString *)path;
|
||||
- (void)removeController:(AtomController *)controller;
|
||||
- (void)reloadController:(AtomController *)controller;
|
||||
|
||||
@end
|
||||
|
||||
@@ -11,46 +11,31 @@
|
||||
|
||||
@synthesize controllers = _controllers;
|
||||
|
||||
- (AtomController *)createController:(NSString *)path {
|
||||
AtomController *controller = [[AtomController alloc] initWithURL:path];
|
||||
[self.controllers addObject:controller];
|
||||
return controller;
|
||||
}
|
||||
|
||||
- (AtomController *)createSpecController {
|
||||
AtomController *controller = [[AtomController alloc] initForSpecs];
|
||||
[self.controllers addObject:controller];
|
||||
return controller;
|
||||
}
|
||||
|
||||
- (void)removeController:(AtomController *)controller {
|
||||
[self.controllers removeObject:controller];
|
||||
[controller.jscocoa callJSFunctionNamed:@"triggerEvent" withArguments:@"window:close", nil, false, nil];
|
||||
}
|
||||
|
||||
- (void)reloadController:(AtomController *)controller {
|
||||
[controller createWebView];
|
||||
}
|
||||
|
||||
- (void)open:(NSString *)path {
|
||||
if (!path) {
|
||||
NSOpenPanel *panel =[NSOpenPanel openPanel];
|
||||
panel.canChooseDirectories = YES;
|
||||
if (panel.runModal != NSFileHandlingPanelOKButton) return;
|
||||
|
||||
path = [[[panel URLs] lastObject] path];
|
||||
}
|
||||
|
||||
[self createController:path];
|
||||
AtomController *controller = [[AtomController alloc] initWithURL:path];
|
||||
[self.controllers addObject:controller];
|
||||
}
|
||||
|
||||
// Events in the "app:*" namespace get sent to all controllers
|
||||
- (void)triggerGlobalEvent:(NSString *)name data:(id)data {
|
||||
- (void)triggerGlobalAtomEvent:(NSString *)name data:(id)data {
|
||||
for (AtomController *controller in self.controllers) {
|
||||
[controller.jscocoa callJSFunctionNamed:@"triggerEvent" withArguments:name, data, false, nil];
|
||||
[controller triggerAtomEventWithName:name data:data];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)shouldRunSpecsOnEvent:(NSEvent *)event {
|
||||
return [event modifierFlags] & (NSAlternateKeyMask | NSControlKeyMask | NSCommandKeyMask) && [[event charactersIgnoringModifiers] hasPrefix:@"s"];
|
||||
}
|
||||
|
||||
// Overridden
|
||||
- (void)sendEvent:(NSEvent *)event {
|
||||
if ([event type] != NSKeyDown) {
|
||||
@@ -58,32 +43,22 @@
|
||||
return;
|
||||
}
|
||||
|
||||
BOOL handeled = NO;
|
||||
AtomController *controller = [[self keyWindow] windowController];
|
||||
if ([self shouldRunSpecsOnEvent:event]) {
|
||||
[self createSpecController];
|
||||
return;
|
||||
}
|
||||
|
||||
// The keyWindow could be a Cocoa Dialog or something, ignore those.
|
||||
if ([controller isKindOfClass:[AtomController class]]) {
|
||||
// cmd-r should always reload the current controller, so it needs to be here
|
||||
if ([event modifierFlags] & NSCommandKeyMask && [[event charactersIgnoringModifiers] hasPrefix:@"r"]) {
|
||||
[self reloadController:controller];
|
||||
handeled = YES;
|
||||
}
|
||||
else if ([event modifierFlags] & (NSAlternateKeyMask | NSControlKeyMask | NSCommandKeyMask) && [[event charactersIgnoringModifiers] hasPrefix:@"s"]) {
|
||||
[self createSpecController];
|
||||
handeled = YES;
|
||||
}
|
||||
else {
|
||||
JSValueRef value = [controller.jscocoa callJSFunctionNamed:@"handleKeyEvent" withArguments:event, nil];
|
||||
handeled = [controller.jscocoa toBool:value];
|
||||
}
|
||||
AtomController *controller = [[self keyWindow] windowController];
|
||||
if ([controller isKindOfClass:[AtomController class]]) { // ensure its not a dialog
|
||||
if ([controller handleInputEvent:event]) return;
|
||||
}
|
||||
|
||||
if (!handeled) [super sendEvent:event];
|
||||
[super sendEvent:event];
|
||||
}
|
||||
|
||||
- (void)terminate:(id)sender {
|
||||
for (AtomController *controller in self.controllers) {
|
||||
[controller.jscocoa callJSFunctionNamed:@"shutdown" withArguments:nil];
|
||||
for (AtomController *controller in self.controllers) {
|
||||
[controller close];
|
||||
}
|
||||
|
||||
[super terminate:sender];
|
||||
@@ -98,7 +73,6 @@
|
||||
}
|
||||
|
||||
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
|
||||
[self createController:nil];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#import "JSCocoa.h"
|
||||
|
||||
@class JSCocoa;
|
||||
@class WebView;
|
||||
|
||||
struct JSGlobalContextRef;
|
||||
|
||||
@interface AtomController : NSWindowController <NSWindowDelegate> {
|
||||
}
|
||||
|
||||
@property (assign) WebView *webView;
|
||||
@property (nonatomic, retain) JSCocoa *jscocoa;
|
||||
|
||||
@property (nonatomic, retain, readonly) NSString *url;
|
||||
@property (nonatomic, retain, readonly) NSString *bootstrapScript;
|
||||
@@ -15,6 +17,9 @@
|
||||
- (id)initForSpecs;
|
||||
- (id)initWithURL:(NSString *)url;
|
||||
|
||||
- (void)createWebView;
|
||||
- (BOOL)handleInputEvent:(NSEvent *)event;
|
||||
- (void)triggerAtomEventWithName:(NSString *)name data:(id)data;
|
||||
- (void)reload;
|
||||
- (JSValueRefAndContextRef)jsWindow;
|
||||
|
||||
@end
|
||||
|
||||
@@ -11,6 +11,19 @@
|
||||
|
||||
@end
|
||||
|
||||
@interface WebView (Atom)
|
||||
- (id)inspector;
|
||||
- (void)showConsole:(id)sender;
|
||||
- (void)startDebuggingJavaScript:(id)sender;
|
||||
@end
|
||||
|
||||
@interface AtomController ()
|
||||
- (void)createWebView;
|
||||
|
||||
@property (nonatomic, retain) JSCocoa *jscocoa;
|
||||
|
||||
@end
|
||||
|
||||
@implementation AtomController
|
||||
|
||||
@synthesize
|
||||
@@ -47,10 +60,31 @@
|
||||
return [self initWithBootstrapScript:@"bootstrap" url:url];
|
||||
}
|
||||
|
||||
- (void)createWebView {
|
||||
[self.webView removeFromSuperview];
|
||||
- (BOOL)shouldReloadOnEvent:(NSEvent *)event {
|
||||
return [event modifierFlags] & NSCommandKeyMask && [[event charactersIgnoringModifiers] hasPrefix:@"r"];
|
||||
}
|
||||
|
||||
- (BOOL)handleInputEvent:(NSEvent *)event {
|
||||
if ([self shouldReloadOnEvent:event]) {
|
||||
[self reload];
|
||||
return YES;
|
||||
}
|
||||
|
||||
if ([self.jscocoa hasJSFunctionNamed:@"handleKeyEvent"]) {
|
||||
JSValueRef handled = [self.jscocoa callJSFunctionNamed:@"handleKeyEvent" withArguments:event, nil];
|
||||
return [self.jscocoa toBool:handled];
|
||||
}
|
||||
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (void)triggerAtomEventWithName:(NSString *)name data:(id)data {
|
||||
[self.jscocoa callJSFunctionNamed:@"triggerEvent" withArguments:name, data, false, nil];
|
||||
}
|
||||
|
||||
- (void)createWebView {
|
||||
self.webView = [[WebView alloc] initWithFrame:[self.window.contentView frame]];
|
||||
|
||||
[self.webView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
|
||||
[self.window.contentView addSubview:self.webView];
|
||||
[self.webView setUIDelegate:self];
|
||||
@@ -64,6 +98,19 @@
|
||||
|
||||
NSURLRequest *request = [NSURLRequest requestWithURL:indexURL];
|
||||
[[self.webView mainFrame] loadRequest:request];
|
||||
|
||||
[[self.webView inspector] showConsole:self];
|
||||
}
|
||||
|
||||
- (JSValueRefAndContextRef)jsWindow {
|
||||
JSValueRef window = [self.jscocoa evalJSString:@"window"];
|
||||
JSValueRefAndContextRef windowWithContext = {window, self.jscocoa.ctx};
|
||||
return windowWithContext;
|
||||
}
|
||||
|
||||
- (void)reload {
|
||||
[self.webView removeFromSuperview];
|
||||
[self createWebView];
|
||||
}
|
||||
|
||||
- (void)windowDidLoad {
|
||||
@@ -79,7 +126,8 @@
|
||||
}
|
||||
|
||||
- (void)close {
|
||||
[(AtomApp *)NSApp removeController:self];
|
||||
[(AtomApp *)NSApp removeController:self];
|
||||
[self.jscocoa callJSFunctionNamed:@"triggerEvent" withArguments:@"window:close", nil, false, nil];
|
||||
[super close];
|
||||
}
|
||||
|
||||
@@ -87,7 +135,13 @@
|
||||
return PROJECT_DIR;
|
||||
}
|
||||
|
||||
// WebUIDelegate
|
||||
#pragma mark NSWindowDelegate
|
||||
- (BOOL)windowShouldClose:(id)sender {
|
||||
[self close];
|
||||
return YES;
|
||||
}
|
||||
|
||||
#pragma mark WebUIDelegate
|
||||
- (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element defaultMenuItems:(NSArray *)defaultMenuItems {
|
||||
return defaultMenuItems;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user