4
votes

I have a borderless NSPanel, when I first launch it and it has focus I can drag it around and the mousedragged method gets triggered correctly, however when I switch focus to another app and then return to the NSPanel (which is set with an NSNonactivatingPanelMask) I no longer receive the mousedragged events.

I do still receive mouseup and mousedown events, so I'm at a loss of why the mousedragged event isn't executed.

Any ideas?

Here's how it gets initialized:

   _panel  = [[MyPanel alloc] initWithContentRect:frame
              styleMask:NSBorderlessWindowMask | NSNonactivatingPanelMask
              backing:NSBackingStoreBuffered
              defer:NO];

I've also tried adding all these methods to my panel class:

- (BOOL)canBecomeKeyWindow {
    return YES;
}

- (BOOL)canBecomeMainWindow
{
    return YES;
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (BOOL)acceptsFirstResponder
{
    return YES;
}

- (BOOL)acceptsFirstMouse
{
    return YES;
}

And making it first responder in the mouse down (which it still receives):

- (void)mouseDown:(NSEvent *)theEvent
{
    [self makeFirstResponder:self];
    [self makeKeyWindow];
    [self setBackgroundColor:[NSColor redColor]];
    [self display];
}

The mousedragged simply contains this:

- (void)mouseDragged:(NSEvent *)theEvent
{
    [self setBackgroundColor:[NSColor greenColor]];
    [self display];
    NSLog(@"dragged");
}

I do not want the window to get focus. (The focus should remain on the third party app below).

Update: I've added a sample project, download here: http://users.telenet.be/prullen/MovingPanel.zip

As you will see, when you first run the app, and drag it will output "dragged" in the console.log continuously (and the background color will be green). If you then switch to another app and then back to the movingpanel app, dragging will not output anything any more. (and the background color will be red, which is set in the mousedown event handler).

Without the NSNonactivatingPanelMask this works as it should, but it is vital that the window below my panel remains active. If there is another way to accomplish this, please do share.

One thing I have also noticed, if you double click on the panel (fast), and then drag, it turns green (so the mousedragged: is being called), but it's not moving... Not sure what to think of that.

1

1 Answers

0
votes

I also noticed that if I do [self setMovableByWindowBackground:NO]; then it will also work correctly. I am betting that the way setMovableByWindowBackground works is interfering with the mouseDragged being called. It's probably a bug that it is called at all.

I would guess one possible solution would be to implement your own window dragging.

If what you are really interested in is responding to when the window is moving, this question and answer may provide what you need.

How to receive notifications when moving Window by mouse?