diff --git a/AUTHORS.md b/AUTHORS.md index a8bb3ec..1410575 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -13,3 +13,4 @@ - Reed Stoner - Aleksandr Kostyutchenko - Edward Patel +- Jurriaan Pruis diff --git a/resources/MainMenu.xib b/resources/MainMenu.xib index 69f11eb..6000e6b 100644 --- a/resources/MainMenu.xib +++ b/resources/MainMenu.xib @@ -12,7 +12,7 @@ YES - + YES @@ -903,6 +903,14 @@ + + + Autosave after losing focus + + 2147483647 + + + @@ -1597,6 +1605,14 @@ 619 + + + toggleAutoSaveOnLosingFocus: + + + + 621 + @@ -2315,6 +2331,7 @@ YES + @@ -2404,6 +2421,11 @@ + + 620 + + + @@ -2607,6 +2629,7 @@ 611.IBPluginDependency 613.IBPluginDependency 616.IBPluginDependency + 620.IBPluginDependency 72.IBPluginDependency 72.ImportedFromIB2 73.IBPluginDependency @@ -2815,7 +2838,7 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{355, 813}, {265, 23}} + {{537, 793}, {274, 43}} com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -2834,6 +2857,7 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -2877,7 +2901,7 @@ - 619 + 621 @@ -3055,6 +3079,7 @@ focusLocationBar: goToLine: reloadStyle: + toggleAutoSaveOnLosingFocus: toggleSplitView: toggleStatusBarVisibility: @@ -3065,6 +3090,7 @@ id id id + id @@ -3074,6 +3100,7 @@ focusLocationBar: goToLine: reloadStyle: + toggleAutoSaveOnLosingFocus: toggleSplitView: toggleStatusBarVisibility: @@ -3091,6 +3118,10 @@ reloadStyle: id + + toggleAutoSaveOnLosingFocus: + id + toggleSplitView: id @@ -3343,20 +3374,6 @@ YES - - NSObject - - IBFrameworkSource - FScript.framework/Headers/FSNSNumber.h - - - - NSObject - - IBFrameworkSource - FScript.framework/Headers/FSNSObject.h - - NSObject diff --git a/src/KAppDelegate.mm b/src/KAppDelegate.mm index 8eea8c5..5b9926c 100644 --- a/src/KAppDelegate.mm +++ b/src/KAppDelegate.mm @@ -329,6 +329,20 @@ - (void)applicationWillTerminate:(NSNotification *)aNotification { // TODO: terminate node thread } +- (void)applicationWillResignActive:(NSNotification *)notification { + DLOG("Not Active Anymore"); + if(kconf_bool(@"editor/save/onlosefocus", NO)) { + DLOG("Saving Documents"); + KDocumentController* controller = [KDocumentController kodController]; + NSArray* documents = controller.documents; + for(KDocument* document in documents) { + if ([document isDirty] && [document canQuietlySaveDocument]) { + [document saveDocument:nil]; + } + } + } +} + - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames { //DLOG("application:openFiles:%@", filenames); diff --git a/src/KBrowserWindowController.h b/src/KBrowserWindowController.h index 427b258..e70e917 100644 --- a/src/KBrowserWindowController.h +++ b/src/KBrowserWindowController.h @@ -28,6 +28,7 @@ - (IBAction)toggleSplitView:(id)sender; - (IBAction)reloadStyle:(id)sender; - (IBAction)goToLine:(id)sender; +- (IBAction)toggleAutoSaveOnLosingFocus:(id)sender; - (BOOL)openFileDirectoryAtURL:(NSURL *)absoluteURL error:(NSError **)outError; diff --git a/src/KBrowserWindowController.mm b/src/KBrowserWindowController.mm index 14f92e2..958ecc3 100644 --- a/src/KBrowserWindowController.mm +++ b/src/KBrowserWindowController.mm @@ -155,6 +155,15 @@ - (IBAction)toggleSplitView:(id)sender { [splitView_ toggleCollapse:sender]; } +- (IBAction)toggleAutoSaveOnLosingFocus:(id)sender { + NSMenuItem *menuItem = (NSMenuItem*) sender; + if([menuItem state] == NSOffState) { + [menuItem setState:NSOnState]; + } else { + [menuItem setState:NSOffState]; + } + kconf_set_bool(@"editor/save/onlosefocus", [menuItem state] == NSOnState); +} - (IBAction)reloadStyle:(id)sender { [[KStyle sharedStyle] reload]; @@ -251,6 +260,9 @@ - (BOOL)validateMenuItem:(NSMenuItem *)item { [item setState:!statusBarController_.isHidden]; return YES; } + } else if (item.action == @selector(toggleAutoSaveOnLosingFocus:)) { + [item setState: kconf_bool(@"editor/save/onlosefocus", NO)]; + return YES; } else { y = [super validateMenuItem:item]; #if 0 diff --git a/src/KDocument.h b/src/KDocument.h index 27b87ec..f31a6c5 100644 --- a/src/KDocument.h +++ b/src/KDocument.h @@ -48,6 +48,7 @@ extern NSString *const KDocumentWillCloseNotification; @property(assign, nonatomic) BOOL isDirty; @property BOOL hasMetaRuler; +@property(readonly) BOOL canQuietlySaveDocument; @property(readonly) BOOL canSaveDocument; @property(readonly) BOOL hasRemoteSource; @property(assign) NSStringEncoding textEncoding; diff --git a/src/KDocument.mm b/src/KDocument.mm index c4e32dc..fea9cb0 100644 --- a/src/KDocument.mm +++ b/src/KDocument.mm @@ -1627,11 +1627,18 @@ - (BOOL)readFromData:(NSData *)data // Returns true to indicate a saveDocument: message is allowed, saving the // document to it's current URL -- (BOOL)canSaveDocument { +- (BOOL)canQuietlySaveDocument { NSURL *url = self.fileURL; KURLHandler *urlHandler = - [[KDocumentController kodController] urlHandlerForURL:url]; - return ( (urlHandler && [urlHandler canWriteURL:url]) || !url ); + [[KDocumentController kodController] urlHandlerForURL:url]; + return ( (urlHandler && [urlHandler canWriteURL:url]) ); +} + +// Returns true to indicate a saveDocument: message is allowed, saving the +// document to it's current URL or that it's OK save to a new file +- (BOOL)canSaveDocument { + NSURL *url = self.fileURL; + return ( [self canQuietlySaveDocument] || !url ); }