Is there a way to make a custom NSWindow work with Spaces

后端 未结 3 1892
别跟我提以往
别跟我提以往 2021-02-03 15:07

I\'m writing an app that has a custom, transparent NSWindow created using a NSWindow subclass with the following:

- (id)initWithContentRect:(NSRect)contentRect s         


        
3条回答
  •  [愿得一人]
    2021-02-03 15:28

    After a long time I found a solution to this annoying problem. Indeed [window setMovableByWindowBackground:YES]; conflicts with my own resizing methods, the window trembles, it looks awful!

    But overriding mouse event methods like below solved the problem in my case :)

    - (void)mouseMoved:(NSEvent *)event
    {
        //set movableByWindowBackground to YES **ONLY** when the mouse is on the title bar
        NSPoint mouseLocation = [event locationInWindow];
        if (NSPointInRect(mouseLocation, [titleBar frame])){
            [self setMovableByWindowBackground:YES];
        }else{
            [self setMovableByWindowBackground:NO];
        }
    
        //This is a good place to set the appropriate cursor too
    }
    
    - (void)mouseDown:(NSEvent *)event
    {
        //Just in case there was no mouse movement before the click AND
        //is inside the title bar frame then setMovableByWindowBackground:YES
        NSPoint mouseLocation = [event locationInWindow];
        if (NSPointInRect(mouseLocation, [titleBar frame])){
            [self setMovableByWindowBackground:YES];
        }else if (NSPointInRect(mouseLocation, bottomRightResizingCornerRect)){
            [self doBottomRightResize:event];
        }//... do all other resizings here. There are 6 more in OSX 10.7!
    }
    
    - (void)mouseUp:(NSEvent *)event
    {
        //movableByBackground must be set to YES **ONLY**
        //when the mouse is inside the titlebar.
        //Disable it here :)
        [self setMovableByWindowBackground:NO];
    }
    

    All my resizing methods start in mouseDown:

    - (void)doBottomRightResize:(NSEvent *)event {
        //This is a good place to push the appropriate cursor
    
        NSRect r = [self frame];
        while ([event type] != NSLeftMouseUp) {
            event = [self nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
            //do a little bit of maths and adjust rect r
            [self setFrame:r display:YES];
        }
    
        //This is a good place to pop the cursor :)
    
        //Dispatch unused NSLeftMouseUp event object
        if ([event type] == NSLeftMouseUp) {
            [self mouseUp:event];
        }
    }
    

    Now I have my Custom window and plays nice with Spaces :)

提交回复
热议问题