Solved

Retaining an NSWindow size and state - looking for an elegant way to do this

Posted on 2010-08-13
4
645 Views
Last Modified: 2013-12-26
I have a controller class which spawns a window when a certain condition occurs. This window can be closed by the user, moved around or resized.

Ideally, when this condition occurs, I'd like the window to re-open in the same spot the user closed it last time.

Looking for an elegant way to do this. Pointers (or references) will be most welcome.
0
Comment
Question by:ronenmagid1
  • 3
4 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33430319
I'm not sure I understand. You want to know how to move? setFrame, I think. The parameter is NSRect.

0
 

Author Comment

by:ronenmagid1
ID: 33430359
No, I can do that myself.

This is why I said "elegant". I thought there might be some Cocoa class that takes the snapshot of the windows location and/or if it's currently miniaturized and saves it into a pfile or something.
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 33430432
Animation?
I attached an action method as an example.
Full example is here:
http://www.wmdeveloper.com/2010/07/cocoa-implicit-animation.html

- (IBAction)move: (id) sender
{
    // The button frame.
    NSRect senderFrame = [sender frame];
    
    //The view bounds. The button belongs to this view.
    NSRect superBounds = [[sender superview] bounds];
    
    // Calculate new position within the view bounds.
    senderFrame.origin.x = (superBounds.size.width -
                            senderFrame.size.width) * drand48();
    senderFrame.origin.y = (superBounds.size.height -
                            senderFrame.size.height) * drand48();
    
    // Move the button.
    //[sender setFrame: senderFrame];
    [[sender animator] setFrame: senderFrame];
}

Open in new window

0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33430515
Do you need to store the rectangle somewhere and then read it and restore the view position?
Use NSUserDefaults. An example attached.


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSDate date] forKey:@"Date and Time"];
    [defaults synchronize];
	
    NSDate *today = [defaults objectForKey:@"Date and Time"];
    NSLog(@"Saved data: %@", today);
	
    [pool drain];
    return 0;
}

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now