From 019b3d5b926d50c59acbfc1f762db52c032efb23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Thu, 24 Dec 2015 21:48:56 -0800 Subject: [PATCH] [osx] Respect system window closing preference MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of forcing the system to restore windows between sessions (which confusingly switches the behavior of ⌘Q and ⌥⌘Q), honor the preference but have each new window inherit the style of the previous main window. --- platform/osx/app/AppDelegate.m | 12 +------ platform/osx/app/MapDocument.m | 61 +++++++++++++++++--------------- platform/osx/app/MapDocument.xib | 2 +- 3 files changed, 34 insertions(+), 41 deletions(-) diff --git a/platform/osx/app/AppDelegate.m b/platform/osx/app/AppDelegate.m index 76c522207..19ef9796d 100644 --- a/platform/osx/app/AppDelegate.m +++ b/platform/osx/app/AppDelegate.m @@ -1,8 +1,6 @@ #import "AppDelegate.h" -#import "LocationCoordinate2DTransformer.h" #import "MapDocument.h" -#import "NSValue+Additions.h" #import @@ -43,14 +41,6 @@ static NSString * const MGLMapboxAccessTokenDefaultsKey = @"MGLMapboxAccessToken andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL]; - - // Normally, an application should respect the “Close windows when quitting - // an application” setting in the General pane of System Preferences. But - // the map would only be restored to its last opened location if the user - // quits the application using Quit and Keep Windows. An application that - // displays only a map should restore the last viewed map, like Maps.app - // does. - [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSQuitAlwaysKeepsWindows"]; } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { @@ -79,7 +69,7 @@ static NSString * const MGLMapboxAccessTokenDefaultsKey = @"MGLMapboxAccessToken } - (MapDocument *)mainDocument { - NSDocument *mainDocument = [NSApp mainWindow].windowController.document; + NSDocument *mainDocument = NSApp.mainWindow.windowController.document; return [mainDocument isKindOfClass:[MapDocument class]] ? (MapDocument *)mainDocument : nil; } diff --git a/platform/osx/app/MapDocument.m b/platform/osx/app/MapDocument.m index b7d16e5c4..f44e5d249 100644 --- a/platform/osx/app/MapDocument.m +++ b/platform/osx/app/MapDocument.m @@ -1,6 +1,7 @@ #import "MapDocument.h" #import "DroppedPinAnnotation.h" +#import "NSValue+Additions.h" #import @@ -13,13 +14,17 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = { { .latitude = -13.15589555, .longitude = -74.2178961777998 }, }; -@interface MapDocument () +@interface MapDocument () @property (weak) IBOutlet NSMenu *mapViewContextMenu; @end @implementation MapDocument { + /// Style URL inherited from an existing document at the time this document + /// was created. + NSURL *_inheritedStyleURL; + NSPoint _mouseLocationForMapViewContextMenu; NSUInteger _droppedPinCounter; NSNumberFormatter *_spellOutNumberFormatter; @@ -35,41 +40,31 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = { return @"MapDocument"; } -- (void)windowControllerDidLoadNib:(NSWindowController *)aController { - [super windowControllerDidLoadNib:aController]; - - NSURL *savedURL = [[NSUserDefaults standardUserDefaults] URLForKey:@"MBXCurrentStyleURL"]; - if (savedURL) { - self.mapView.styleURL = savedURL; +- (void)windowControllerWillLoadNib:(NSWindowController *)windowController { + NSDocument *sourceDocument = [[NSDocumentController sharedDocumentController] documentForWindow:NSApp.mainWindow]; + if ([sourceDocument isKindOfClass:[MapDocument class]]) { + _inheritedStyleURL = [(MapDocument *)sourceDocument mapView].styleURL; } +} + +- (void)windowControllerDidLoadNib:(NSWindowController *)controller { + [super windowControllerDidLoadNib:controller]; _spellOutNumberFormatter = [[NSNumberFormatter alloc] init]; NSPressGestureRecognizer *pressGestureRecognizer = [[NSPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlePressGesture:)]; [self.mapView addGestureRecognizer:pressGestureRecognizer]; -} - -- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError { - // Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil. - // You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead. - if (outError) { - *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:nil]; + + if (_inheritedStyleURL) { + self.mapView.styleURL = _inheritedStyleURL; } - return nil; } -- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError { - // Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO. - // You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead. - // If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded. - if (outError) { - *outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:nil]; - } - return NO; -} - -+ (BOOL)autosavesInPlace { - return YES; +- (NSString *)displayName { + // Temporarily set the display name to the default center coordinate instead + // of “Untitled” until the binding kicks in. + NSValue *nullIsland = [NSValue valueWithCLLocationCoordinate2D:CLLocationCoordinate2DMake(0, 0)]; + return [[NSValueTransformer valueTransformerForName:@"LocationCoordinate2DTransformer"] transformedValue:nullIsland]; } - (NSWindow *)window { @@ -127,7 +122,6 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = { break; } self.mapView.styleURL = styleURL; - [[NSUserDefaults standardUserDefaults] setURL:self.mapView.styleURL forKey:@"MBXCurrentStyleURL"]; [self.window.toolbar validateVisibleItems]; } @@ -150,7 +144,6 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = { if ([alert runModal] == NSAlertFirstButtonReturn) { self.mapView.styleURL = [NSURL URLWithString:textField.stringValue]; [[NSUserDefaults standardUserDefaults] setURL:self.mapView.styleURL forKey:@"MBXCustomStyleURL"]; - [[NSUserDefaults standardUserDefaults] setURL:self.mapView.styleURL forKey:@"MBXCurrentStyleURL"]; [self.window.toolbar validateVisibleItems]; } } @@ -471,6 +464,16 @@ static const CLLocationCoordinate2D WorldTourDestinations[] = { return NO; } +#pragma mark NSWindowDelegate methods + +- (void)window:(NSWindow *)window willEncodeRestorableState:(NSCoder *)state { + [state encodeObject:self.mapView.styleURL forKey:@"MBXMapViewStyleURL"]; +} + +- (void)window:(NSWindow *)window didDecodeRestorableState:(NSCoder *)state { + self.mapView.styleURL = [state decodeObjectForKey:@"MBXMapViewStyleURL"]; +} + #pragma mark NSSharingServicePickerDelegate methods - (NS_ARRAY_OF(NSSharingService *) *)sharingServicePicker:(NSSharingServicePicker *)sharingServicePicker sharingServicesForItems:(NSArray *)items proposedSharingServices:(NS_ARRAY_OF(NSSharingService *) *)proposedServices { diff --git a/platform/osx/app/MapDocument.xib b/platform/osx/app/MapDocument.xib index 3adbba8c1..74743ca9e 100644 --- a/platform/osx/app/MapDocument.xib +++ b/platform/osx/app/MapDocument.xib @@ -13,7 +13,7 @@ - +