gpt4 book ai didi

objective-c - 有没有办法让自定义 NSWindow 与 Spaces 一起工作

转载 作者:太空狗 更新时间:2023-10-30 03:51:02 26 4
gpt4 key购买 nike

我正在编写一个应用程序,它有一个自定义的、透明的 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;
}

我的一切都运行良好,包括拖动和调整大小,但窗口不适用于 Spaces。我无法通过在通过键盘快捷键切换空间时按住窗口或拖动到窗口的底部/顶部/左侧/右侧来将窗口移动到另一个空间。无论如何,自定义窗口在空间方面的行为与普通窗口完全一样吗?

最佳答案

很长一段时间后,我找到了解决这个恼人问题的方法。确实[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];
}

我所有的调整大小方法都从 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];
}
}

现在我有了自己的自定义窗口,并且可以很好地使用 Spaces :)

关于objective-c - 有没有办法让自定义 NSWindow 与 Spaces 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1694582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com