Hide cursor on window orderout

Discussion in 'Mac Programming' started by rainbowsynthi, Apr 26, 2010.

  1. rainbowsynthi macrumors newbie

    Joined:
    Apr 26, 2010
    #1
    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?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    One method would be to listen for NSWindowDidBecomeKeyNotification and NSWindowDidResignKeyNotification notifications, and show/hide the cursor appropriately.
     
  3. rainbowsynthi thread starter macrumors newbie

    Joined:
    Apr 26, 2010
    #3
    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!
     
  4. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #4
    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.
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    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.
     
  6. rainbowsynthi thread starter macrumors newbie

    Joined:
    Apr 26, 2010
    #6
    Thank-you so, so much..
    thats magnificent, worked perfect first go..
    many thanks!!
     

Share This Page