Getting window restored bounds - objective-c

When you maximize a window in Macos, the window fills the screen.
What I want to be able to do is get the windows restored position and size, like when you press the maximize button again to restore the original position. How can you do this?
I need this for saving the window position on exit.

There is a method prepared for this task in NSWindow.
You could ask your ViewController for its NSWindow and set an AutosaveName for its Frame like..
[self.view.window setFrameAutosaveName:#"VerySpecialWindowAutoSaveName"];
which will end up in NSUserDefaults of your App as entry like...
"NSWindow Frame VerySpecialWindowAutoSaveName" = "300 100 1200 1005 0 0 2560 1289"
But the best place for this code is ... there is no best place because it depends on your apps approach.
The whole process can be challenging when you have multiple windows in a document based application but as you can set the AutosaveName per documents window you are able to recover the frame if needed, at least for the last document. Should be mentioned that you can set the AutosaveName in InterfaceBuilder and in Code as well - so keep an eye on it they follow the same name if you use both IB & code for one and the same window.
and an example in swift you can find in this gist

Related

'Pause' Display while control elements are updated (VB.Net)

So, I am kind of asking a question for a hypothetical situation here.
I am producing a Windows Form App made to show multiple windows of data on the screen at the same time. To accomplish this, I have decided to put a Table Layout Panel in the main form to act as a container for all of the data windows I will open.
The width and height of each cell of the Table Layout Panel will change depending on how many windows are open. For example, if just one window is open, it takes up 100% of the width and height of the container area. Alternatively if 3 windows are open, all the windows will have a size equal to 50% of the container's width and height.
Now let's assume each of these Windows have 20 different control elements, which are all used to help the user search through the data shown in each window. Additionally, all of these elements dynamically resize and relocate themselves as the dimensions of the window they are in change.
With all of these moving elements, when a screen is added or removed it can be assumed that there will be a good deal of glitching going on on the screen that may alarm the user. Thus we get to my question.
Is there a way to 'pause' the display of a Windows Form Application so that the user doesn't see everything on change modifying itself? I can't seem to find anything like while looking online so that is why I am asking here.

What Cocoa Views and Controls Will Create Something like Part of the Network Prefs Display (Mac OS)? [duplicate]

This question already has an answer here:
NSTableView with +/- buttons like in System Preferences using only Interface Builder
(1 answer)
Closed 7 years ago.
I'm building an OSX app and want to create a set of controls similar to what's found at bottom of the standard Network Preferences configuration panel. I'm running into some layout problems that I wouldn't have expected.
These are my specific questions:
What contains the 3 buttons so there's similar shading all they way across the row where the buttons are positioned? In particular, what's causing the area without buttons to have shading?
How do you do this without getting a double border where the row of buttons meets up with the table?
I want to do this with an xib file. This may be incredibly simple, but I'm missing something I guess.
I find that if you make a button with style "Gradient" and type "Momentary Change", then it looks like the other buttons but does not respond to clicks, so you can use that as the area after the last button. (The NSMomentaryChangeButton is documented as changing the image and title when clicked, so if you don't use an image or title, nothing should change.)
If you check Refuses First Responder in the attributes inspector, then it will not be possible to highlight this blank button using Full Keyboard Access.
Ken Thomases also brings up the issue of the blank button being shown as a button to Accessibility. One can fix that by using a subclass of NSButtonCell that has just one method:
- (BOOL)accessibilityIsIgnored
{
return YES;
}
I think that's easier than writing a custom view.
As d00dle says, avoid double borders by slightly overlapping things.
Since you want the slack space to have the same background as the buttons, and since the buttons can change appearance from release to release of the OS, the best thing to do is to get the frameworks to draw it like it would the buttons.
Rather than using an actual button as JWWalker suggests, I have used a custom view that leverages NSButtonCell to draw the background. The advantage is that you can be sure there's no chance of getting undesirable behavior. For example, a button could get focus (for users who have All Controls selected in System Preferences > Keyboard > Shortcuts > Full Keyboard Access) so that the user could Tab to it. Accessibility will report the presence of the button through VoiceOver. Etc.
Configure the button cell just like the buttons (set buttonType and bezelStyle). In the view's -drawRect: call [buttonCell drawWithFrame:rect inView:self];, where rect is similar to the frames of the buttons. Since one way to avoid double borders is to make the buttons larger than the view's bounds, you may need to do the same for rect. For example, you might want to use NSInsetRect(self.bounds, -1, -1).
The buttons are buttons... This can be accomplished with a custom view drawing border and the background "shading".
To avoid the double border where the table and the custom view meet you simply align it so they overlap by 1 point (pixel) or avoid drawing the top border in your custom view.
I don't know of any standard object capable of doing this.

How can I save NSWindow programmatically?

Basically I got an array of allocated windows and want to save their state (position/size/subviews) when the app is closed. Is there any way to do this?
When I try to use a NSUserDefault i get this error message: Attempt to set a non-property-list. Which turns out to that you can't save NSWindows in a user default. Should i use this instead?
//customView
[self setFrameAutosaveName:[NSString stringWithFormat:#"%d",self.tag]];
It's not usually useful to save the positions and size of subviews in a window.
Using setFrameAutosaveName: will save the window's position and size for a given name. The next time your app opens a document and creates a window with that autosave name, its state should be restored.
As for the size and positions of subviews, they are usually automatically laid out based on the auto layout system, or using Struts and Springs. Either that, or their positions and sizes are derived from data in your document model. Either way, you don't generally need to save them yourself. Why do you think you should be saving them? What are you trying to accomplish by saving them?

Save NSWindow Size on Resize & Close For User

I've noticed that all applications on OS X seem to save the size you set it at. The next time you open it it's typically in the same position and size.
I'm making an app and I've noticed that after resizing, if I launch the application again it's just the size of what I've set in Xcode 4's IB and not the size that I resized it to on launch.
Do I have to manually save the window size each time its changed? Or is there an easier way to do this through IB? (My window does have a minimum size set if that changes anything.)
Apple makes it easy. In interface builder, for your window, just type in a unique name in the Autosave field and it will save it under that name in the global user defaults. E.G.
When you select your main window in the Interface Builder, there should be a property called Autosave in the attributes inspector. Put any name in there does the trick for me.
After setting the Autosave name in IB I could see that my window's frame was being saved to its preferences file, but still the auto restore would not work. Then I remembered to uncheck the Close windows when quitting an app checkbox in the System Preferences under the General settings, and the window position restored as expected.
If you did not use IB to create your window, you can set this property in Swift using:
setFrameAutosaveName(_ name: NSWindow.FrameAutosaveName)
(the frameAutosaveName property is get only)
For example:
windowController.window?.setFrameAutosaveName("appWindow")

Getting and setting the size of any window visible on screen

I'd like to be able to select (with the mouse), some window that's visible on the screen, store it's size. I also want to be able to modify the size of some window that's visible on the screen. The window is not a window within my own app.
How can I do this in Cocoa?
You can use the Accessibility API to control other apps' windows (and more).
I don't know how you can interface with windows in other applications. You would probably have to do something with Apple Script which can be run from Cocoa. As far as in your own app you can look at the documentation for NSWindow at Apples Site. You can get the window size with the frame property like [window frame] and save that either in mememory as an NSRect or if you need to save it in a file with NSNumbers or NSData. You can then resize a window by setting its frame property. with [window setFrame:] good luck hopefully this well get you in the right direction!