Lupurus
Lupurus

Reputation: 4229

Make NSWindow with transparent titlebar partly unmovable

I have a NSWindow with a splitted screen like in Reminders. Therefore I use this code:

self.window.titlebarAppearsTransparent = true
self.window.styleMask |= NSFullSizeContentViewWindowMask

This works perfectly. But inside the window I have a SplitView (like in the Reminders App) and a NSOutlineView at the right side. The OutlineView goes up to the top of the window corner.

The problem now is: Clicking and dragging at the top of the OutlineView makes the window movable. Any way, I can disable this but still keeping the moving ability at the left side of the app?

Upvotes: 4

Views: 1570

Answers (1)

mangerlahn
mangerlahn

Reputation: 4966

Ok, there are two things you need to do:

First you need to set your window to be not movable. To do so, subclass your Window and override isMovable and return no. Or you call setMovable: and set it to no.

After that, you have to manually reenable dragging by adding a view that has the exact size and position of the area you want to draggable. Alternatively you can set up a NSTrackingArea. Either way, you need to override mouseDown: and insert some code to move the window.

My words in code:

Objective-C

[self.window setMovable:false];

// OR (in NSWindow subclass)

- (BOOL)isMovable {
    return false;
}

//Mouse Down
- (void)mouseDown:(NSEvent *)theEvent {
    _initialLocation = [theEvent locationInWindow];

    NSPoint point;
    while (1) {
        theEvent = [[self window] nextEventMatchingMask: (NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
        point =[theEvent locationInWindow];

        NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame];
        NSRect windowFrame = [self.window frame];
        NSPoint newOrigin = windowFrame.origin;

        // Get the mouse location in window coordinates.
        NSPoint currentLocation = point;
        // Update the origin with the difference between the new mouse location and the old mouse location.
        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) > (screenVisibleFrame.origin.y + screenVisibleFrame.size.height)) {
            newOrigin.y = screenVisibleFrame.origin.y + (screenVisibleFrame.size.height - windowFrame.size.height);
        }

        // Move the window to the new location
        [self.window setFrameOrigin:newOrigin];
        if ([theEvent type] == NSLeftMouseUp) {
            break;
        }
    }
}

initialLocation is a NSPoint property

Note: I looked up some things here and here

Upvotes: 4

Related Questions