trojanfoe
trojanfoe

Reputation: 122391

Determine if child window was moved by user or because parent window moved?

How can I determine if a child window has been moved directly by the user rather than as a result of its parent window being moved?

I get NSWindowDidMoveNotification notifications in both cases.

I figured I could test the parent frame origin in the NSWindowWillMoveNotification handler and compare it against the origin when in the NSWindowDidMoveNotification handler, but is there a better way perhaps?

Here is my current solution:

- (void)windowWillMove:(NSNotification *)notification
{
    NSWindow *window = [notification object];
    _parentOrigin = [[window parentWindow] frame].origin;
}

- (void)windowDidMove:(NSNotification *)notification
{
    NSWindow *window = [notification object];
    NSPoint newParentOrigin = [[window parentWindow] frame].origin;
    if (_parentOrigin.x == newParentOrigin.x &&
        _parentOrigin.y == newParentOrigin.y)
    {
        // The parent hasn't moved, therefore the user moved the window directly...
        [window doThing];
    }
}

Upvotes: 1

Views: 692

Answers (1)

rdelmar
rdelmar

Reputation: 104082

The window that you click on to move will become the key window, while the child window doesn't become key if it's moved as a result of moving the parent window. So, if you test for whether the window sending the notification is the key window, you can tell which was moved by the user.

- (void)windowDidMove:(NSNotification *)notification {
    NSWindow *movedWindow = notification.object;
    if ([movedWindow isKeyWindow]) {
        NSLog(@"%@",movedWindow);
        [movedWindow doThing];
    }
}

Upvotes: 4

Related Questions