NSDocument-based app window position - objective-c

My Xcode NSDocument-based app contains a floating panel. On each launch of the app the default doc window and panel appear in their previous locations on the screen. However, if I save a document and quit the app then double click the saved file, the document window is positioned at the same origin as the panel.
Turning off cascading gets around the problem but of course I lose cascading.
A minimum Xcode example showing the issue can be downloaded here.
Run the example project.
Do a File/Save.
Quit the app. << important
Double click the saved file.
Any help appreciated.

If you want to store the state of your window before the app terminates try this:
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
// If you want to save your window position
// you can use [window saveFrameUsingName:#"someWindowName"];
//
// then use [window setFrameAutosaveName:#"someWindowName"]; at the app launch.
return NSTerminateNow;
}

I knew it had something to do with document windows cascading from the panel. Adding this to the panel controller seems to have fixed it
- (void)windowDidLoad {
[super windowDidLoad];
[self setShouldCascadeWindows:NO];
}

Related

Cocoa How to remember the window position with multiple monitors?

To save window position for one(main) monitor i'm using this code:
[[win windowController] setShouldCascadeWindows:NO];
[win setFrameAutosaveName:#"My App"];
It's works fine.
But if I connect second monitor and drag my app window to second monitor, cocoa autosave does not work - after restart, window always placed on primary(main) monitor.
Any ideas?
Thank you!
Enable restore for your app:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[[NSUserDefaults standardUserDefaults] setObject:#YES forKey:#"NSQuitAlwaysKeepsWindows"];
}
Set your window to be restorable in Interface Builder
Also make sure that you don't call invalidate on close. It is just promise -> you might end app with deleted state
[self.window invalidateRestorableState];
To verify/debug check if the content of "~/Library/Saved Application State/yourbundleidentifier" persists after restart.

Menu Bar App Never Becomes Reactivated

I'm building a Mac app that only sits in the menu bar with no dock item and no key window and no main menu (it's LSUIElement in the info.plist is set to YES). When I first launch the app, applicationDidBecomeActive: is called, as I expect. However, once another app gains focus, applicationDidBecomeActive: is never called again.
This prevents a text field I have within my app from becoming the first responder. When I first open the app, the text field is editable:
But after another app comes to the foreground, the text field is not editable:
What I've tried:
When the menu is opened, menuWillOpen: is called on the NSMenu's delegate. I've tried placing the following with no success:
[NSApp unhide];
[NSApp arrangeInFront:self];
[NSApp activateIgnoringOtherApps:YES];
[NSApp requestUserAttention:NSCriticalRequest];
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
[[NSRunningApplication currentApplication] unhide];
I think the issue is probably related to not having any windows to bring to the front. I feel like I'm grasping at straws here. Any help would be greatly appreciated.
I think the issue is with that how the runloop operates when a NSMenu is open, so you should try activating the app before you display the menu. If you're having the NSStatusItem display it, I'd suggest doing it yourself like this:
- (void)toggleMenu:(id)sender
{
// App might already be active
if ([NSApp isActive]) {
[self.statusItem popUpStatusItemMenu:self.menu];
} else {
[NSApp activateIgnoringOtherApps:YES];
}
}
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
[self.statusItem popUpStatusItemMenu:self.menu];
}
That should work, but I think though in general you'll have better luck with an actual window instead of a menu.
You probably need to allow your input to -becomeFirstResponder, maybe by overriding -canBecomeFirstResponder or by calling the become method yourself.
You'd likely have to implement/call these methods for whatever view is housing your text input, or maybe tell your input view to become the first responder.
Either way, it smells like a responder chain issue.
Try calling -makeFirstResponder: on your window. NSWindow is usually the start of the NSResponder chain.
- (void)menuWillOpen:(NSMenu *)menu {
[[NSApp mainWindow] makeFirstResponder:yourTextInputField];
}
I'm assuming your text field already accepts first responder since you said your app launches initially with it as the first responder. If not, make sure your text field overrides -acceptsFirstResponder: to return YES
- (BOOL)acceptsFirstResponder {
return YES;
}
Edit: Ah, see that you don't have a key window. It looks like NSMenu actually has a window associated with it though, and it's safe to call -makeFirstResponder:. Some discussion here suggests overriding -viewDidMoveToWindow: on your view containing your text field in the NSMenu like so:
- (void)viewDidMoveToWindow {
[super viewDidMoveToWindow];
[[self window] makeFirstResponder:yourTextInputField];
}

NSPopover - Hide when focus lost? (clicked outside of popover)

I'm using the doubleClickAction of a NSTableView to display a NSPopover. Something like this:
NSInteger selectedRow = [dataTableView clickedRow];
NSInteger selectedColumn = [dataTableView clickedColumn];
// If something was not selected, then we cannot display anything.
if(selectedRow < 0 || selectedColumn < 0)
{
NSLog(#"Invalid selected (%ld,%ld)", selectedRow, selectedColumn);
return;
} // End of something was not selected
// Setup our view controller, make sure if there was already a popover displayed, that we kill that one off first. Finally create and display our new popover.
DataInspectorViewController * controller =
[[DataInspectorViewController alloc] initWithNibName: #"DataInspectorViewController"
bundle: nil];
if(nil != dataPreviewPopover)
{
[dataPreviewPopover close];
} // End of popover was already visible
dataPreviewPopover = [[NSPopover alloc] init];
[dataPreviewPopover setContentSize:NSMakeSize(400.0f, 400.0f)];
[dataPreviewPopover setContentViewController:controller];
[dataPreviewPopover setAnimates:YES];
[dataPreviewPopover showRelativeToRect: [dataTableView frameOfCellAtColumn: selectedColumn row: selectedRow]
ofView: dataTableView
preferredEdge: NSMinYEdge];
Which works just fine. My popovers get created and removed on the cells that I double click on . The problem is, I want to the popover to go away if I click anywhere outside of it (like a single click on another cell). I have been looking around, but for the life of me cannot figure out how to do it.
This is something I would assume is built into a popover, (I'm fairly certain it was in the iOS UIPopoverController class) so I'm just wondering if im missing something simple.
You need to change the property behavior of your popover (in code or on interface builder) to:
popover.behavior = NSPopover.Behavior.transient;
NSPopover.Behavior.transient
The system will close the popover when the user interacts with a user interface element outside the popover.
Read more about this in Apple's documentation.
the .transient flag doesn't work for me.
However I can make things work by the following:
1) Whenever I show my popover I make sure I activate the app
(my app is a menu-bar app, so this doesn't happen automatically)
NSApp.activate(ignoringOtherApps: true)
2) When I click outside the app, then my app will be deactivated. I can detect this in the AppDelegate
func applicationWillResignActive(_ notification: Notification) {
print("resign active")
}
and act accordingly
After calling show(relativeTo:of:preferredEdge:) method,
Add below line
popover.contentViewController?.view.window?.makeKey()
And make sure you set
popover.behavior = .transient
Sorry, I've added solution in Swift.
While transient worked for most cases, it was an issue when the user interacted with elements outside of the application, as the popover would hide but not close.
What finally ended working for me was:
popover.behavior = .semitransient
Now the popover closes when changing app, or interacting with any other element outside of the app. But will not close when interacting with a NSMenu, and maybe won't close either with other interactions.
Quoting from the documentation for NSPopover.Behavior.semitransient:
The exact interactions that cause semi-transient popovers to close are not specified.
Similar to the documentation for NSPopover.Behavior.transient:
The exact interactions that will cause transient popovers to close are not specified.

iCloud enabled - Stop the open file displaying on application launch?

I've just added iCloud support to an app that I am working on. Its working great, except that when I open the application without a document in focus the iCloud open file dialog appears and I don't want it to!
In my app delegate I have:
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
[mainWindowController.window makeKeyAndOrderFront:self];
return NO;
}
Which I use to show my own custom window. However now, both the iCloud open file dialog and my own dialog are displayed. Any ideas on how I can get rid of the iCloud dialog?
https://developer.apple.com/library/prerelease/content/releasenotes/AppKit/RN-AppKitOlderNotes/index.html
NSDocument Support for iCloud
In 10.8, NSDocument-based applications with a ubiquity-container-identifiers entitlement gain new functionality and UI to facilitate iCloud document management.
When iCloud is enabled and an application is first launched or re-activated and no windows are visible or being restored, instead of creating a new Untitled document, NSDocumentController will display a non-modal open panel showing the user's iCloud library.
...
Applications that do not wish to use these features for any or all of their NSDocument subclasses can override +[NSDocument usesUbiquitousStorage] and return NO. If all of the application's declared NSDocument subclasses return NO from this method, then NSDocumentController will never show the new non-modal open panel.
So if you can give up using the features listed in this release note, return NO at +[NSDocument usesUbiquitousStorage].
I confirmed you can still open/save your file into iCloud storage from the normal dialog.
Putting below codes in your App Delegate lets you bypass that iCloud pop up New Document screen. Tested for High Sierra.
-(void)applicationDidFinishLaunching:(NSNotification *)notification
{
// Schedule "Checking whether document exists." into next UI Loop.
// Because document is not restored yet.
// So we don't know what do we have to create new one.
// Opened document can be identified here. (double click document file)
NSInvocationOperation* op = [[NSInvocationOperation alloc]initWithTarget:self selector:#selector(openNewDocumentIfNeeded) object:nil];
[[NSOperationQueue mainQueue] addOperation: op];
}
-(void)openNewDocumentIfNeeded
{
NSUInteger documentCount = [[[NSDocumentController sharedDocumentController] documents]count];
// Open an untitled document what if there is no document. (restored, opened).
if(documentCount == 0){
[[NSDocumentController sharedDocumentController]openUntitledDocumentAndDisplay:YES error: nil];
}
}
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
[mainWindowController.window makeKeyAndOrderFront:self];
return NO;
}
This part is correct. I've just tested it.
Just make sure your that this class is really your app delegate.
Make a new class called prefixAppDelegate
In your MainMenu.xib, drag a new object to the side and set it's custom class to the app delegate class
Right click Application and drag from Delegate down to your app delegate object.
Now just paste the code above into your app delegate class
If that still doesn't help, try logging something in applicationShouldOpenUntitledFile:.
Also, I recommend not to set [mainWindowController.window makeKeyAndOrderFront:self]; in this method. You should rather use the app delegate method applicationDidFinishLaunching: method.
My observation and fix:
[applicationShouldOpenUntitledFile:] won't be executed except you remove Key NSDocumentClass from *-info.plist. But this is harmful if your app is document based application, it won't open the document type you linked.
My fix is open my customised window directly in -(void)applicationWillFinishLaunching:(NSNotification *)notification method (Application delegate)
ETDocumentWindowController *windowController = (ETDocumentWindowController*)get your own window controller here...;
[windowController.window makeKeyAndOrderFront:nil];
I thought I would share my solution to this issue as I see others still looking for an answer. Its not a great solution but it does the trick.
Subclass NSDocumentController and add the following:
+ (void) setCanOpenUntitledDocument: (BOOL) _canOpenUntitledDocument
{
canOpenUntitledDocument = _canOpenUntitledDocument;
} // End of setCanOpenUntitledDocument:
- (void) openDocument: (id) sender
{
// With iCloud enabled, the app keeps trying to run openDocument: on first launch (before apphasfinishedlaunching gets set.
// This method lets us check and see if the app has finished launching or not. If we try to open a document before
// its finished, then don't let it.
if(!canOpenUntitledDocument)
{
return;
} // End of appHasFinishedLaunching not set
[super openDocument: sender];
} // End of openDocument:
Add the following to your app delegate:
- (void) applicationDidFinishLaunching: (NSNotification *) aNotification
{
// Finished launching. Let us open untitled documents.
[SQLProDocumentController setCanOpenUntitledDocument: true];
...
}
And the reasoning -- By setting a breakpoint in openDocument I've found that its called before applicationDidFinishLaunching, applicationShouldOpenUntitledFile or applicationShouldHandleReopen:hasVisibleWindows: get called, meaning adding those methods is useless. Again, it's not great code but it works and does the trick. (None of the other solutions have worked for me).
I ran into a similar problem -- it turned out that in my case, I had to remove the NSDocumentClass key and value from my Info.plist in the CFBundleDocumentTypes array. Only then would the applicationShouldOpenUntitledFile: method get called and thus allow me to prevent the iCloud/Document window from opening.

Closing Mac application (clicking red cross on top) and reopening by clicking dock icon

When I close my Mac application (by clicking red cross button on window top bar) the app icon stays in the dock at the bottom. Now this is normal behaviour. When user click on it again it does not fire up the application unless the user quits the application altogether and relaunches it again.
A similar example on Mac OS X is "Activity Monitor". You can close the application by clicking the red cross button at the top the but dock icon stays there. User can re-open it by clicking dock icon.
How can I achieve this in my own application ?
If you are still concerned how to reopen the window that you have closed, use this method:
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag {
[window makeKeyAndOrderFront:self];
return YES;
}
You can use this to handle clicks on the applications icon in the dock.
For further information check out the NSApplicationDelegate Protocol Reference.
Here is the documentation:
http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html
Hope this helps!
Latest Update:
In latest Xcode 11.4 on MacOS 10.15 with Swift 5.2, this same problem exists in MacOS SwiftUI app. Adding following code inside AppDelegates.swift solves the issue.
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag{
window.makeKeyAndOrderFront(nil)
}
return true
}
Implement the method
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
return NO;
}
in your app delegate
Your app will hang around after the window is closed and then if you implement
- (void)applicationDidBecomeActive:(NSNotification *)aNotification
{
//dock icon has just been clicked , or cmd-tabbed into
}
in the app delegate
You can do things when the icon is clicked such as open a new or old window if you need to
See http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html for other relevant application events
I think that the answers above aren't fully correct, to achieve this you should override applicationShouldHandleReopen(_:hasVisibleWindows:) https://developer.apple.com/reference/appkit/nsapplicationdelegate/1428638-applicationshouldhandlereopen