Allow click and dragging a view to drag the window itself?

后端 未结 8 1747
醉梦人生
醉梦人生 2020-12-29 06:06

I\'m using a textured window that has a tab bar along the top of it, just below the title bar.

I\'ve used -setContentBorderThickness:forEdge: on the window to make t

8条回答
  •  [愿得一人]
    2020-12-29 06:47

    I found this here:

    -(void)mouseDown:(NSEvent *)theEvent {    
        NSRect  windowFrame = [[self window] frame];
    
        initialLocation = [NSEvent mouseLocation];
    
        initialLocation.x -= windowFrame.origin.x;
        initialLocation.y -= windowFrame.origin.y;
    }
    
    - (void)mouseDragged:(NSEvent *)theEvent {
        NSPoint currentLocation;
        NSPoint newOrigin;
    
        NSRect  screenFrame = [[NSScreen mainScreen] frame];
        NSRect  windowFrame = [self frame];
    
        currentLocation = [NSEvent mouseLocation];
        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
        [[self window] setFrameOrigin:newOrigin];
    }
    

    It works fine, though I'm not 100% sure I'm doing it correctly. There's one bug I've found so far, and that's if the drag begins inside a subview (a tab itself) and then enters the superview (the tab bar). The window jumps around. Some -hitTest: magic, or possibly even just invalidating initialLocation on mouseUp should probably fix that.

提交回复
热议问题