Skip to content

Commit

Permalink
Fix Command-W behavior for main window
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheng5 committed Nov 5, 2013
1 parent 33d8f22 commit 3c7e5a1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/cpp/desktop-mac/MainFrameController.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

- (id) invokeCommand: (NSString*) command;

- (BOOL) isCommandEnabled: (NSString*) command;


// initiate a quit sequence
- (void) initiateQuit;
Expand Down
6 changes: 6 additions & 0 deletions src/cpp/desktop-mac/MainFrameController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ - (id) invokeCommand: (NSString*) command
command]];
}

- (BOOL) isCommandEnabled: (NSString*) command
{
return [[self evaluateJavaScript: [NSString stringWithFormat: @"window.desktopHooks.isCommandEnabled(\"%@\");",
command]] boolValue];
}

- (BOOL) hasDesktopObject
{
WebScriptObject* script = [webView_ windowScriptObject];
Expand Down
3 changes: 1 addition & 2 deletions src/cpp/desktop-mac/MainFrameMenu.mm
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ - (BOOL) validateMenuItem: (NSMenuItem *) item {
if (trailingSep != Nil)
[trailingSep setHidden: YES];

NSString* enabledJs = [NSString stringWithFormat: @"window.desktopHooks.isCommandEnabled(\"%@\");", command];
if ([[[MainFrameController instance] evaluateJavaScript: enabledJs] boolValue])
if ([[MainFrameController instance] isCommandEnabled: command])
return YES;
else
return NO;
Expand Down
20 changes: 20 additions & 0 deletions src/cpp/desktop-mac/MainFrameWebView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* MainFrameWebView.h
*
* Copyright (C) 2009-13 by RStudio, Inc.
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/

#import <WebKit/WebKit.h>

@interface MainFrameWebView : WebView

@end
48 changes: 48 additions & 0 deletions src/cpp/desktop-mac/MainFrameWebView.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* MainFrameWebView.mm
*
* Copyright (C) 2009-13 by RStudio, Inc.
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/

#import "MainFrameWebView.h"
#import "MainFrameController.h"

@implementation MainFrameWebView

- (id) initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}

- (BOOL) performKeyEquivalent: (NSEvent *) theEvent
{
if ([[theEvent charactersIgnoringModifiers] isEqualToString: @"w"]
&& ([theEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask) == NSCommandKeyMask)
{
if ([[MainFrameController instance] isCommandEnabled: @"closeSourceDoc"])
{
[[MainFrameController instance] invokeCommand: @"closeSourceDoc"];
}
else
{
[[self window] performClose: self];
}
return YES;
}
return [super performKeyEquivalent: theEvent];
}

@end
3 changes: 2 additions & 1 deletion src/cpp/desktop-mac/WebViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "SatelliteController.h"
#import "SecondaryWindowController.h"
#import "Utils.hpp"
#import "MainFrameWebView.h"

struct PendingSatelliteWindow
{
Expand Down Expand Up @@ -105,7 +106,7 @@ - (id)initWithURLRequest: (NSURLRequest*) request
if (self = [super initWithWindow: window])
{
// create web view, save it as a member, and register as it's delegate,
webView_ = [[WebView alloc] initWithFrame: frameRect];
webView_ = [[MainFrameWebView alloc] initWithFrame: frameRect];
[webView_ setUIDelegate: self];
[webView_ setFrameLoadDelegate: self];
[webView_ setResourceLoadDelegate: self];
Expand Down

0 comments on commit 3c7e5a1

Please sign in to comment.