Cocoa topmost window - objective-c

How could I get one of my Cocoa application windows to get (and maintain) on top all desktop windows while my apllication stay running?

Have a look at NSWindow's -setLevel: method:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWindow/setLevel:

You have to make the window key and order it to front. Use this
[[myWindowController window] makeKeyAndOrderFront:self];

Related

Where is my closed NSWindow?

I have an Application delegate that holds the reference to a NSWindow and the program (small test program so far) is generally working with Main Menu and views in the window.
However I discovered that if I close the window holding the views it's gone although the program is still running. As far as i can see the window reference is not nil but how do I restore it so it's visible and shown under the Windows menu again?
The program is not document based. All actions are performed in the window in question.
I created the window in the MainMenu.xib that was auto created by Xcode (this was in Xcode7 or 8 but now I've upgraded to 9).
I'm new to windows handling on Mac so I understand this is a very basic question but I'm totally stuck here. Having a window that is supposed to hold all functionality disappear without the user being able to restore it is bad I believe.
If you have a reference to the NSWindow, you just need to show it. For example:
[window makeKeyAndOrderFront:self];
But if the user closed the single window of your app, than the question is: How will the user trigger that code? The simplest answer is that the user will click your app's icon on the dock. To handle that click, implement the following method on you app delegate.
- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)hasVisibleWindows
{
if (hasVisibleWindows) {
return YES;
}
[window makeKeyAndOrderFront:self];
return NO;
}

Show all NSWindows

Pretty simple idea here, I want to show all the NSWindows in an app. The idea being that there are two windows in the app, one his hidden the other is vissable. I want to show all the windows in the app and then hide one. I can hide the window I want to hide but I cant show the windows because I am unable to obtain a reference to it. is there anyway of getting a list of all the nswindows in the app then iterating through it and hiding them or something similar, I can use [NSApp windows] however trying to use
NSArray *windowArray = [NSApp windows];
[windowArray[0] makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
does not work, nor does:
NSArray *windowArray = [NSApp windows];
NSWindow *tempWindow = windowArray[0];
[tempWindow makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
any suggestions?
Well in hindsight I've been a bit stupid. I had two seep rate controller objects each running one window. One was hardly doing anything as the view inside it was being handled by a different object so I just removed that one and made the first controller object take care of both windows. Problem solved.

Creating a HUD on top of another application with Cocoa

Is it possible to create a HUD with Cocoa on top of a window from another application? For example, if I were to create a Poker HUD, which would display information about opponents on top of a Poker Client window, how would I implement it in OS X (with Cocoa?).
For example how would I recreate the following Windows program (PokerTracker HUD: displays a HUD on top of PokerStars)?
I'm not sure if this is what you're asking, but I think the HUD elements would be controls or views within an NSWindow set up as an overlay. Here are some key steps to configure it:
[myWindow setOpaque: NO];
[myWindow setBackgroundColor: [NSColor clearColor]];
[myWindow setLevel: NSStatusWindowLevel];
And then you'd need to track which app is frontmost, so that your overlay would only be visible when the target app is in front. I think you could do this by finding the NSRunningApplication for the target app and then key-value-observing its active property.

Re-show my main Cocoa application Window

Ok, as simple and silly as that may seem, this is my scenario :
The application window (there is just ONE main window) is minimized
The user clicks something on the dock menu
We want to bring that main window back
Simple?
Not quite.
I've tried accessing my main window via :
[[NSApplication sharedApplication] mainWindow]
[[NSApplication sharedApplication] windows]
and then sending a makeKeyAndOrderFront: message but it's simply NOT showing up.
I've even tried showing all windows (yep, it still sees as "windows" one-or-two (hidden) sheets I'm using...), but still nothing.
Could you suggest a way, I could finally show that window, WITHOUT having to set an outlet anywhere?
I don't know; I'm probably too tired to notice, but this thing is almost nerve-wrecking...
You can un-minimize a window like so:
if([aWindow isMiniaturized])
{
[aWindow deminiaturize:self];
}
However, the problem is that the window will lose main status when it is minimized, so you'll need some other way of identifying the window.
Why can't you just use an outlet?
You can make the app un-minimize all of its minimized windows like so:
for(NSWindow* win in [NSApp windows])
{
if([win isMiniaturized])
{
[win deminiaturize:self];
}
}
When the app launches, you could store a reference to the main window and register for the NSWindowDidMiniaturizeNotification. That will tell you when the main window minimizes, which might also help you.

Is it possible to bring window to front without taking focus?

I am working on an application for my personal usage that will remind me of stuff at regular intervals and/or will require text entry. Hence this popup window has an NSTextField.
If the window pop's up when I am in the middle of typing, my typing transfers to the popup window which is very annoying! Is there any way to stop this, currently I am using:
[NSApp activateIgnoringOtherApps:YES];
[hudWindow makeKeyAndOrderFront:nil];
I have also tried:
[NSApp activateIgnoringOtherApps:YES];
[hudWindow orderFrontRegardless];
Is there any other way to do it?
[hudWindow orderFront: nil];
Moves the window to the front of its level in the screen list, without changing either the key window or the main window.
Did you try using setLevel:? Setting it to one of the higher levels should do the trick.
like
[hudWindow
setLevel:NSFloatingWindowLevel];