有没有办法使自定义NSWindow与Spaces一起工作

Zac*_*ugh 5 cocoa objective-c

我正在编写一个应用程序,它具有使用NSWindow子类创建的自定义透明NSWindow,其中包含以下内容:

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag 
{
   self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:bufferingType defer:flag];

   if (self)
   {
     [self setOpaque:NO];
     [self setBackgroundColor:[NSColor clearColor]];
   }

   return self;
}

- (BOOL)canBecomeKeyWindow
{
  return YES;
}

- (BOOL)canBecomeMainWindow
{
  return YES;
}
Run Code Online (Sandbox Code Playgroud)

我有一切都很完美,包括拖动和调整大小,除了窗口不适用于Spaces.我不能通过按住窗口同时通过键盘快捷键切换空格,或者拖动到窗口的底部/顶部/左/右来将窗口移动到另一个空间.反正有一个自定义窗口的行为与空间的普通窗口完全一样吗?

nac*_*o4d 6

很长一段时间后,我找到了解决这个烦人问题的方法.确实[window setMovableByWindowBackground:YES];与我自己的调整大小方法冲突,窗口颤抖,看起来很糟糕!

但是如下所示覆盖鼠标事件方法解决了我的问题:)

- (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];
}
Run Code Online (Sandbox Code Playgroud)

我的所有调整大小方法都从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];
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我有自定义窗口,并与Spaces玩得很好:)


Tho*_*ing 0

你重写了 isMovable 吗?
苹果文档称,它改变了 Spaces 的行为:

如果窗口返回 NO,则意味着它只能在 F8 模式下在空格之间拖动,...

另一个可能相关的方法: NSWindow setCollectionBehavior