From 62d52bad2cd38971b59b7e3d48920fd1936a0e10 Mon Sep 17 00:00:00 2001 From: "smfr%smfr.org" Date: Sun, 11 Dec 2005 19:22:13 +0000 Subject: [PATCH] Fix bug 308714: location of prefs window moves around because of window height changes. Changed to center the window the first time, and thereafter store the window top-left point in user defaults. --- .../preferences/MVPreferencesController.mm | 50 +++++++++++++++++-- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/camino/src/preferences/MVPreferencesController.mm b/camino/src/preferences/MVPreferencesController.mm index 89f80df1a76c..a8019e1cccc7 100644 --- a/camino/src/preferences/MVPreferencesController.mm +++ b/camino/src/preferences/MVPreferencesController.mm @@ -59,11 +59,12 @@ static MVPreferencesController *gSharedInstance = nil; NSString* const MVPreferencesWindowNotification = @"MVPreferencesWindowNotification"; +static NSString* const kPrefsWindowLocationDefaultsKey = @"CaminoWindow TopLeftLocation PreferencesWindow"; + static NSString* const CacheInfoPaneImageKey = @"MVPreferencePaneImage"; static NSString* const CacheInfoPaneLabelKey = @"MVPreferencePaneLabel"; static NSString* const CacheInfoPaneSeenKey = @"MVPreferencePaneSeen"; // NSNumber with bool - @interface NSToolbar (NSToolbarPrivate) - (NSView *) _toolbarView; @@ -76,6 +77,9 @@ static NSString* const CacheInfoPaneSeenKey = @"MVPreferencePaneSeen"; // N + (NSString*)applicationSupportPathForDomain:(short)inDomain; ++ (NSDictionary*)dictionaryWithPoint:(NSPoint)inPoint; ++ (BOOL)point:(NSPoint*)outPoint fromDictionary:(NSDictionary*)inDict; + - (void)loadPreferencePanesAtPath:(NSString*)inFolderPath; - (void)buildPrefPaneIdentifierList; - (void)doUnselect:(NSNotification *) notification; @@ -161,11 +165,9 @@ static NSString* const CacheInfoPaneSeenKey = @"MVPreferencePaneSeen"; // N - (void) awakeFromNib { - NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:@"preferences.toolbar.1"] autorelease]; - [mWindow setDelegate:self]; - [mWindow setFrameAutosaveName:@"CaminoPreferenceWindowFrame"]; + NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:@"preferences.toolbar.1"] autorelease]; [toolbar setAllowsUserCustomization:NO]; [toolbar setAutosavesConfiguration:NO]; [toolbar setDelegate:self]; @@ -173,6 +175,15 @@ static NSString* const CacheInfoPaneSeenKey = @"MVPreferencePaneSeen"; // N [toolbar setShowsContextMenu:NO]; [mWindow setToolbar:toolbar]; [toolbar setDisplayMode:NSToolbarDisplayModeIconAndLabel]; + + // save/restore the top-left window frame (because our size changes confuse the standard frame saving) + // (Cocoa will ensure that the window isn't placed totally offscreen) + id prefsLocation = [[NSUserDefaults standardUserDefaults] dictionaryForKey:kPrefsWindowLocationDefaultsKey]; + NSPoint topLeftPoint; + if ([[self class] point:&topLeftPoint fromDictionary:prefsLocation]) + [[self window] setFrameTopLeftPoint:topLeftPoint]; + else + [[self window] center]; } - (NSWindow *)window @@ -287,7 +298,13 @@ static NSString* const CacheInfoPaneSeenKey = @"MVPreferencePaneSeen"; // N // the current pref pane selected per Apple's recommendation. [[self currentPane] willUnselect]; [[self currentPane] didUnselect]; - + + // save frame location + NSRect windowFrame = [[self window] frame]; + NSPoint topLeftPoint = windowFrame.origin; // bottom left + topLeftPoint.y += NSHeight(windowFrame); // top left + [[NSUserDefaults standardUserDefaults] setObject:[[self class] dictionaryWithPoint:topLeftPoint] forKey:kPrefsWindowLocationDefaultsKey]; + // write out prefs and user defaults nsCOMPtr prefService ( do_GetService(NS_PREF_CONTRACTID) ); NS_ASSERTION(prefService, "Could not get pref service, prefs unsaved"); @@ -371,6 +388,29 @@ static NSString* const CacheInfoPaneSeenKey = @"MVPreferencePaneSeen"; // N return nil; } + ++ (NSDictionary*)dictionaryWithPoint:(NSPoint)inPoint +{ + return [NSDictionary dictionaryWithObjectsAndKeys: + [NSNumber numberWithFloat:inPoint.x], @"x", + [NSNumber numberWithFloat:inPoint.y], @"y", + nil]; +} + ++ (BOOL)point:(NSPoint*)outPoint fromDictionary:(NSDictionary*)inDict +{ + *outPoint = NSZeroPoint; + id xValue, yValue; + if ((xValue = [inDict objectForKey:@"x"]) && (yValue = [inDict objectForKey:@"y"]) && + ([xValue isKindOfClass:[NSNumber class]] && [yValue isKindOfClass:[NSNumber class]])) + { + *outPoint = NSMakePoint([xValue floatValue], [yValue floatValue]); + return YES; + } + + return NO; +} + - (IBAction)selectPreferencePane:(id) sender { [self selectPreferencePaneByIdentifier:[sender itemIdentifier]];