Skip to content

Instantly share code, notes, and snippets.

@mattpat
Created October 30, 2011 03:04
Show Gist options
  • Save mattpat/1325412 to your computer and use it in GitHub Desktop.
Save mattpat/1325412 to your computer and use it in GitHub Desktop.
How Bowtie accomplishes a movable window with a WebView.
// BTOverlayView.h
@interface BTOverlayView : NSView {
NSWindow *mainWindow;
NSPoint initialLocation;
NSTrackingArea *tArea;
BOOL frameChanged;
}
@property(assign, readwrite) IBOutlet NSWindow *mainWindow;
- (void)mouseDragged:(NSEvent *)theEvent;
- (void)mouseDown:(NSEvent *)theEvent;
- (void)mouseUp:(NSEvent *)theEvent;
@end
// BTOverlayView.m
@implementation BTOverlayView
@synthesize mainWindow;
- (id)initWithFrame:(NSRect)rect
{
if (self = [super initWithFrame:rect])
{
frameChanged = NO;
tArea = [[NSTrackingArea alloc] initWithRect:rect options:NSTrackingMouseEnteredAndExited|NSTrackingActiveAlways|NSTrackingInVisibleRect owner:self userInfo:nil];
[self addTrackingArea:tArea];
}
return self;
}
- (void)dealloc
{
[tArea release];
[super dealloc];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"lockWindowPosition"])
{
NSPoint newOrigin;
NSRect screenFrame = [[NSScreen mainScreen] frame];
NSRect windowFrame = [mainWindow frame];
NSPoint currentLocation = [mainWindow convertBaseToScreen:[mainWindow mouseLocationOutsideOfEventStream]];
newOrigin.x = currentLocation.x - initialLocation.x;
newOrigin.y = currentLocation.y - initialLocation.y;
// Don't let window get dragged up under the menu bar
if ((newOrigin.y + windowFrame.size.height) > (screenFrame.origin.y + screenFrame.size.height))
newOrigin.y = screenFrame.origin.y + (screenFrame.size.height - windowFrame.size.height);
//go ahead and move the window to the new location
[mainWindow setFrameOrigin:newOrigin];
frameChanged = YES;
}
}
- (void)mouseDown:(NSEvent *)theEvent
{
// grab the mouse location in global coordinates
initialLocation = [theEvent locationInWindow];
[super mouseDown:theEvent];
}
- (void)mouseUp:(NSEvent *)theEvent
{
if (frameChanged)
{
// you could do something here to notify your theme that the window moved
}
frameChanged = NO;
[super mouseUp:theEvent];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment