Skip to content

Instantly share code, notes, and snippets.

@bjhomer
Last active July 14, 2021 18:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjhomer/2a0035fa516dd8672fe7 to your computer and use it in GitHub Desktop.
Save bjhomer/2a0035fa516dd8672fe7 to your computer and use it in GitHub Desktop.
Why not -[NSView mouseDownCanMoveWindow]?
// You might think that a view could trigger window movement by overriding -[NSView mouseDownCanMoveWindow]
@interface DraggyView : NSView
@end
@implementation DraggyView
- (BOOL)mouseDownCanMoveWindow {
return YES;
}
@end
// This probably doesn't do what you think it does, though. Specifically, it does not cause
// a mouseDragged: event to be turned into a window drag. The real API for allowing events to
// drag the window is this one:
@interface NSWindow : NSReponder
@property (getter=isMovableByWindowBackground) BOOL movableByWindowBackground;
@end
// This allows mouse events that propagate all the way up to the window (because they are
// not intercepted by some other view) to initiate a window drag. By default, opaque windows
// block mouse events from propagating up, and non-opaque views allow them to propagate.
//
// This is the behavior that is controlled by -mouseDownCanMoveWindow. If that returns YES
// (as it does by default from non-opaque views), then the mouse event will move up to the
// next view under the mouse. If NONE of the views that were under the mouse blocked it (by
// returning NO from -mouseDownCanMoveWindow), then the mouse event will reach the window
// background, and that can trigger dragging the window.
//
// However, note that this depends on every view in the view hierarchy cooperating to allow
// the event to propagate. If any view in the way handles those events for itself, then the
// event will not reach the window background, and the window drag will not be initiated.
//
// This is difficult in the Yosemite world where developers are allowed to create their own
// titlebar-like views (using NSVisualEffectMaterialTitlebar), because even though the view
// *looks* like a titlebar, it still propagates its events up the view hierarchy. There is
// no way for a view to say "I don't care what other views are under the mouse right now --
// I just want this event to initiate a window drag."
//
// Or, at least, if such an API exists, I haven't found it.
@bjhomer
Copy link
Author

bjhomer commented Jul 29, 2014

Bug report filed: rdar://17850137

@orta
Copy link

orta commented Aug 21, 2014

did you get a response, same issue right here

@bjhomer
Copy link
Author

bjhomer commented Sep 26, 2014

No response.

@Wevah
Copy link

Wevah commented May 1, 2019

In case anyone finds this during a search like I did, NSWindow now has an API for this (since 10.11):

- (void)performWindowDragWithEvent:(NSEvent *)event;

@matt-curtis
Copy link

In case anyone finds this during a search like I did, NSWindow now has an API for this (since 10.11):

- (void)performWindowDragWithEvent:(NSEvent *)event;

Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment