是否有一种方法使自定义NSWindow与Spaces工作 [英] Is there a way to make a custom NSWindow work with Spaces

查看:256
本文介绍了是否有一种方法使自定义NSWindow与Spaces工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个应用程序,它有一个自定义,透明的NSWindow创建使用NSWindow子类与以下:

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

- (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;
}

我有一切正常工作,包括拖动和调整大小, t与空间工作。我不能通过按住窗口同时通过键盘快捷方式切换空格,或通过拖动到窗口的底部/顶部/左/右来将窗口移动到另一个空间。

I have everything working perfectly, including dragging and resizing, except the window doesn't work with Spaces. I cannot move the window to another space by either holding the window while switching spaces via keyboard shortcut, or by dragging to the bottom/top/left/right of the window. Is there anyway to have a custom window behave exactly like a normal window with regards to Spaces?

推荐答案

经过很长时间后,我找到了一个自定义窗口解决这个恼人的问题。
确实 [window setMovableByWindowBackground:YES]; 与我自己的调整大小方法冲突,窗口颤抖,看起来很可怕!

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];
}

所有调整大小的方法都是从mouseDown开始的:

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 :)

这篇关于是否有一种方法使自定义NSWindow与Spaces工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆