Is there a better way of doing this? (Dragging windows)

Discussion in 'Mac Programming' started by Avicenna, Mar 13, 2011.

  1. Avicenna macrumors member

    Joined:
    Jul 16, 2009
    #1
    I am new to Cocoa programming and I am trying to create an application with similar functionality to the Cinch App in Mac App Store.
    My goal so far is to figure out if a window is being dragged.
    This is what I did so far and it works but before going further and coding what I described in the comments (it should work), I realized this could get very messy and I should try to figure out a better way of doing it...

    [​IMG]

    So, how else can this be done?

    Thanks!
     
  2. PatrickCocoa macrumors 6502a

    Joined:
    Dec 2, 2008
    #2
    Simple Method (I hope)

    I'm not sure, but I'm guessing there's an existing method that gets called when the window is being dragged. Probably windowBeingMoved or something like that.

    Just look in the docs to find that method, then put your code (for when the window is moving) there.

    Cocoa has an (invisible) run loop that calls all kinds of interesting methods (appDidFinishLaunching, awakeFromNib, drawRect, many more).
     
  3. PatrickCocoa macrumors 6502a

    Joined:
    Dec 2, 2008
    #3
    More research

    Looked in the NSWindow reference, but didn't see what I was looking for (isBeingMoved or windowDidStartMove or something like that). Here are some methods that may help, or maybe further documentation research would be fruitful:

    - (BOOL)displaysWhenScreenProfileChanges

    - (NSEvent *)currentEvent

    Indicates whether the window is being resized by the user.
    - (BOOL)inLiveResize

    Indicates whether the window can be moved by clicking in its title bar or background.
    - (BOOL)isMovable

    Marks the key view loop as dirty and in need of recalculation.
    - (void)recalculateKeyViewLoop

    Returns the flags field of the event record for the mouse-down event that initiated the resizing session.
    - (NSInteger)resizeFlags
     
  4. misee macrumors member

    Joined:
    Jul 4, 2010
    #4
    I found a NSWindowDelegate Protocol which was introduced in Mac OS X 10.6, but most methods are available as part of an informal protocol prior to 10.6: NSWindowDelegate Protocol Reference
    This method should help:
    Code:
    - (void)windowDidMove:(NSNotification *)notification
    This method is available since Mac OS X 10.0
     
  5. PatrickCocoa macrumors 6502a

    Joined:
    Dec 2, 2008
    #5
    NSNostrodamus?

    I went to NSWindowDelegate Protocol Reference based on the comment from misee and found a method that can predict the future:

    Tells the delegate that the window is about to move.
    Code:
    - (void)windowWillMove:(NSNotification *)notification
    So apparently 10.6 has some sort of mind-reading chip that can tell when you're going to move a window.

    On a more serious note to misee: what steps did you go through to find windowDidMove? I gave up after looking through NSWindow (and whatever class it derives from, I think NSResponder).
     
  6. Avicenna thread starter macrumors member

    Joined:
    Jul 16, 2009
    #6
    How does “Cinch App” do it?

    If you aren't familiar with Cinch, its an application on Mac App Store that allows you to resize ANY window to half/full screen size if you drag the window to the edge of the screen. Exactly like the functionality in windows 7.

    Now my question is, how is it done? I have looked all over cocoa apis looking for notifications/delegate methods for whenever a window is being dragged (ALL windows, not just windows owned by the app from which code is running from) but can't find it. Looked in Core Graphics API...Quartz Display Services....but can't find it.

    Any help will be greatly appreciated as I have been looking for the past week....Thanks!

    Edit: Resize the window is easy since it can be done through applescript bridge..
     
  7. misee, Mar 24, 2011
    Last edited: Mar 24, 2011

    misee macrumors member

    Joined:
    Jul 4, 2010
    #7
    This method is available since 10.0. The documentation isn't quite clear on this, but from other methods of the blaWillDoSomething family, I'm guessing that it's called right before the windows position will be updated? I've never used it so I can't say for sure.

    In 10.6 Apple moved most (or all?) delegate methods for their classes into a protocol which is named <class_name>Delegate. So I just searched for NSWindowDelegate in the documentation.
    Also, delegate methods like windowWillMove: usually have a corresponding notification, so you could also search for NSWindowWillMoveNotification. The naming conventions in Cocoa are really consistent, so it's easy to search for something. Or just look up the class in the documentation and scroll down to the notifications section.

    EDIT: @Avicenna have looked at the accessibility framework? I have never used or looked at it, but it might offer some of the functionality you are looking for.
     

Share This Page