gpt4 book ai didi

cocoa - 使 MAAttachedWindow 可调整大小而不反弹?

转载 作者:行者123 更新时间:2023-12-03 18:01:34 25 4
gpt4 key购买 nike

为了使 MAAttachedWindow 可调整大小,我使用 here 中的代码:

- (void)mouseDown:(NSEvent *)event {
NSPoint pointInView = [event locationInWindow];

BOOL resize = YES;
if (NSPointInRect(pointInView, [self resizeRect]))
{
resize = YES;
}

NSWindow *window = self;
NSPoint originalMouseLocation = [window convertBaseToScreen:[event locationInWindow]];
NSRect originalFrame = [window frame];

while (YES)
{
//
// Lock focus and take all the dragged and mouse up events until we
// receive a mouse up.
//
NSEvent *newEvent = [window
nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];

if ([newEvent type] == NSLeftMouseUp)
{
break;
}

//
// Work out how much the mouse has moved
//
NSPoint newMouseLocation = [window convertBaseToScreen:[newEvent locationInWindow]];
NSPoint delta = NSMakePoint(
newMouseLocation.x - originalMouseLocation.x,
newMouseLocation.y - originalMouseLocation.y);

NSRect newFrame = originalFrame;

if (!resize)
{
//
// Alter the frame for a drag
//
newFrame.origin.x += delta.x;
newFrame.origin.y += delta.y;
}
else
{
//
// Alter the frame for a resize
//
// newFrame.size.width += delta.x;
newFrame.size.width += delta.x*2; // customize
newFrame.size.height -= delta.y;
newFrame.origin.y += delta.y;

//
// Constrain to the window's min and max size
//
NSRect newContentRect = [window contentRectForFrameRect:newFrame];
NSSize maxSize = [window maxSize];
NSSize minSize = [window minSize];
if (newContentRect.size.width > maxSize.width)
{
newFrame.size.width -= newContentRect.size.width - maxSize.width;
}
else if (newContentRect.size.width < minSize.width)
{
newFrame.size.width += minSize.width - newContentRect.size.width;
}
if (newContentRect.size.height > maxSize.height)
{
newFrame.size.height -= newContentRect.size.height - maxSize.height;
newFrame.origin.y += newContentRect.size.height - maxSize.height;
}
else if (newContentRect.size.height < minSize.height)
{
newFrame.size.height += minSize.height - newContentRect.size.height;
newFrame.origin.y -= minSize.height - newContentRect.size.height;
}
}




[window setFrame:newFrame display:NO animate:NO];
}

}

当我拖动窗口时,我可以调整它的大小。但时不时会有一点反弹。您可以下载demo项目。运行项目并单击状态菜单上的图标打开窗口。尝试前后拉伸(stretch),这样你就可以轻松地重现它。希望有人能帮我解决这个问题。

最佳答案

我检查了您的代码,似乎错误是有时 NSPoint newMouseLocation = [window ConvertBaseToScreen:[newEvent locationInWindow]]; 会返回原始鼠标位置而不是新位置。

我不确定为什么会失败,但无论如何这都不是最佳方法。您不应根据原始鼠标位置计算新帧,而应不断更新该位置,并将窗口移动在当前跟踪迭代中

鼠标移动的量

这是一个经过清理、缩短的版本,似乎消除了该错误。

- (void) mouseDown:(NSEvent *)event
{
NSWindow *window = self;
NSPoint originalMouseLocation = [window convertBaseToScreen:[event locationInWindow]];


while (YES)
{

NSEvent *newEvent = [window
nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];

if ([newEvent type] == NSLeftMouseUp)
{
break;
}

NSPoint newMouseLocation = [window convertBaseToScreen:[newEvent locationInWindow]];

NSPoint delta = NSMakePoint(
roundf(newMouseLocation.x - originalMouseLocation.x),
roundf(newMouseLocation.y - originalMouseLocation.y));

NSRect newFrame = [window frame];
newFrame.size.width += delta.x*2; // customize
newFrame.size.height -= delta.y;
newFrame.origin.y += delta.y;

[window setFrame:newFrame display:YES animate:NO];
originalMouseLocation = newMouseLocation; // <-- added this

}

}

关于cocoa - 使 MAAttachedWindow 可调整大小而不反弹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5846024/

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