• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Snapping windows while moving
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Snapping windows while moving


  • Subject: Re: Snapping windows while moving
  • From: Tamas Nagy <email@hidden>
  • Date: Wed, 13 Mar 2013 06:11:50 +0100

I'm using this:

//http://www.cocoadev.com/index.pl?MagneticWindows
- (void)windowDidMove:(NSNotification *)notification
{

    NSEnumerator *e;
    NSWindow *theWindow;
    id edgeObject;
    NSArray *frames = [[[[NSApp windows] copy] autorelease]  arrayByAddingObjectsFromArray:[NSScreen screens]];
    //Edit 7 October 2004: this prefers window edges. change the above line to:
    //NSArray *frames = [[NSScreen screens] arrayByAddingObjectsFromArray:[NSApp windows]];
    //to give snapping priority to screen edges.


    NSRect frame, myFrame;
    BOOL hDidChange = NO, vDidChange = NO;
    float gravity = 3;

    theWindow = [notification object];
    myFrame = [theWindow frame];
    e = [frames objectEnumerator];

    if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) return;

    while ((edgeObject = [e nextObject]))
    {
		if ( (edgeObject != theWindow
			  && ([edgeObject respondsToSelector:@selector(isVisible)] && [edgeObject isVisible]))
			|| [edgeObject isKindOfClass:[NSScreen class]] )
		{
			// leave out the following if clause and just set frame to [edgeObject frame]
			//if you want windows to snap behind the dock
			if ([edgeObject isKindOfClass:[NSScreen class]])
			{
				frame = [edgeObject visibleFrame];
			}
			else
			{
				frame = [edgeObject frame];
			}
			/* code continues here with */ /* horizontal magnet */
			//NSLog(@"%f %f", NSMinX(frame) - NSMinX(myFrame), NSMinY(frame) - NSMinY(myFrame));
			if (!hDidChange && fabs(NSMinX(frame) - NSMinX(myFrame)) <= gravity)
			{
				//NSLog(@"NSMinX(frame) - NSMinX(myFrame)");
				myFrame.origin.x = frame.origin.x;
				hDidChange = YES;
			}
			if (!hDidChange && fabs(NSMinX(frame) - NSMaxX(myFrame)) <= gravity)
			{
				//NSLog(@"NSMinX(frame) - NSMaxX(myFrame)");
				myFrame.origin.x += NSMinX(frame) - NSMaxX(myFrame);
				hDidChange = YES;
			}
			if (!hDidChange && fabs(NSMaxX(frame) - NSMinX(myFrame)) <= gravity)
			{
				//NSLog(@"NSMaxX(frame) - NSMinX(myFrame)");
				myFrame.origin.x = NSMaxX(frame);
				hDidChange = YES;
			}
			if (!hDidChange && fabs(NSMaxX(frame) - NSMaxX(myFrame)) <= gravity)
			{
				//NSLog(@"NSMaxX(frame) - NSMaxX(myFrame)");
				myFrame.origin.x += NSMaxX(frame) - NSMaxX(myFrame);
				hDidChange = YES;
			}
			/* vertical magnet */
			if (!vDidChange && fabs(NSMinY(frame) - NSMinY(myFrame)) <= gravity)
			{
				//NSLog(@"NSMinY(frame) - NSMinY(myFrame)");
				myFrame.origin.y = frame.origin.y;
				vDidChange = YES;
			}
			if (!vDidChange && fabs(NSMinY(frame) - NSMaxY(myFrame)) <= gravity)
			{
				//NSLog(@"NSMinY(frame) - NSMaxY(myFrame)");
				myFrame.origin.y += NSMinY(frame) - NSMaxY(myFrame);
				vDidChange = YES;
			}
			if (!vDidChange && fabs(NSMaxY(frame) - NSMinY(myFrame)) <= gravity)
			{
				//NSLog(@"NSMaxY(frame) - NSMinY(myFrame)");
				myFrame.origin.y = NSMaxY(frame);
				vDidChange = YES;
			}
			if (!vDidChange && fabs(NSMaxY(frame) - NSMaxY(myFrame)) <= gravity)
			{
				//NSLog(@"(NSMaxY(frame) - NSMaxY(myFrame)");
				myFrame.origin.y += NSMaxY(frame) - NSMaxY(myFrame);
				vDidChange = YES;
			}
		}
		//if (v_isChanged && h_isChanged) break;
	}

    if ([[theWindow screen] isEqualTo:[NSScreen mainScreen]]) {

        if ((myFrame.size.height+myFrame.origin.y)>([[NSScreen mainScreen] frame].size.height-20)) {
            myFrame.origin.y = [[NSScreen mainScreen] frame].size.height-myFrame.size.height-20;
        }
    }

    NSDisableScreenUpdates();
    [theWindow setFrame:myFrame display:NO];
    NSEnableScreenUpdates();
}



On Mar 12, 2013, at 9:54 PM, Steve Mills <email@hidden> wrote:

> On Mar 12, 2013, at 15:29:53, Seth Willits <email@hidden> wrote:
>
>> Pretty sure you want to subclass and override constrainFrameRect:toScreen: but I can't recall off the top of my head whether this is called live for every movement. I believe it is.
>
> Nope. It only seems to be called when the window is being created and *after* the mouse has been released during a move.
>
> --
> Steve Mills
> office: 952-818-3871
> home: 952-401-6255
> cell: 612-803-6157
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Snapping windows while moving
      • From: Steve Mills <email@hidden>
References: 
 >Snapping windows while moving (From: Steve Mills <email@hidden>)
 >Re: Snapping windows while moving (From: Seth Willits <email@hidden>)
 >Re: Snapping windows while moving (From: Steve Mills <email@hidden>)

  • Prev by Date: how to implement iphoto like animation while doing drag and drop on custom listview
  • Next by Date: Re: Objective-C Question
  • Previous by thread: Re: Snapping windows while moving
  • Next by thread: Re: Snapping windows while moving
  • Index(es):
    • Date
    • Thread