Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rainbowsynthi

macrumors newbie
Original poster
Apr 26, 2010
3
0
Hello..
Im new to cocoa/xcode..
hoping someone can help with a reasonably simple task.
I have an application with a fullscreen window and a separate preferences window for settings.
I want the cursor to disappear when I click the x button to close the preferences window, and come back when I call it back to front.

The code to check if the preferences page is visible or not and control the cursor is simple enough..

if ([settingsWindow isVisible])
CGDisplayShowCursor (kCGDirectMainDisplay);
else
CGDisplayHideCursor (kCGDirectMainDisplay);

This works on application startup if I place it after
- (void) applicationDidFinishLaunching:(NSNotification*)aNotification
or
- (void)awakeFromNib

however I'm not sure the correct code to have this react during runtime when I hide/orderfront the window..

The orderfront action is set up in interface builder just by control dragging from the preferences line in the main menu to the settingsWindow and choosing orderfront, and I use apple-P to bring it back to front..

How should I be going about this?
 
One method would be to listen for NSWindowDidBecomeKeyNotification and NSWindowDidResignKeyNotification notifications, and show/hide the cursor appropriately.
 
Thanks very much..
I don't suppose you could post a basic way of implementing this code..

ie.. at the moment I can do this:

- (void)awakeFromNib {
if ([settingsWindow isVisible])
CGDisplayShowCursor (kCGDirectMainDisplay);
else
CGDisplayHideCursor (kCGDirectMainDisplay);
}

which will call the code at awakefromnib..

how would I call in the same manner from the WindowDidBecomeKeyNotification notification?

any help much appreciated!
 
Another possibility would be to use a transparent cursor for your main window. This could get a little unpleasant, though, if it causes the cursor to vanish when it exits the frame of the preferences window.
 
Something like this (untested):

Code:
- (void)awakeFromNib {
    NSNotificationCenter *notifCenter = [NSNotificationCenter defaultCenter];
    [notifCenter addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:settingsWindow];
    [notifCenter addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:settingsWindow];
}

- (void)windowDidBecomeKey:(NSNotification *)notif {
    [NSCursor unhide];
}

- (void)windowDidResignKey:(NSNotification *)notif {
    [NSCursor hide];
}

You may want to use NSWindowWillCloseNotification for catching when the window closes to hide the cursor, instead of NSWindowDidResignKeyNotification, but it depends.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.