使 MAAttachedWindow 可调整大小而不反弹?
为了使 MAAttachedWindow 可调整大小,我使用 此处的代码:
- (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];
}
}
当我拖动窗口时,我可以调整它的大小。但时不时会有一点反弹。 您可以下载演示项目。 运行项目并单击状态菜单上的图标打开窗口。尝试前后伸展,这样你就可以轻松地重现它。 希望有人能帮我解决这个问题。
To make the MAAttachedWindow resizable, I use code from 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];
}
}
When I drag the window, I can resize it. But there is a little bounce every now and then.
You can download the demo project.
Run the project and click the icon on the status menu to open the window. Try to stretch back and forward so you can reproduce it easily.
Hope someone can help me fixed this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我检查了您的代码,似乎错误是有时
NSPoint newMouseLocation = [window ConvertBaseToScreen:[newEvent locationInWindow]];
会返回原始鼠标位置而不是新位置。我不确定为什么会失败,但无论如何这都不是最佳方法。您不应根据原始鼠标位置计算新帧,而应不断更新该位置并仅将窗口移动鼠标在当前跟踪迭代中移动的量
这是一个清理、缩短的版本这似乎消除了这个错误。
}
I checked out you code and it seemed like the bug was that occasionally
NSPoint newMouseLocation = [window convertBaseToScreen:[newEvent locationInWindow]];
would return the original mouse location instead of the new one.I'm not sure why that would fail but regardless that's not the optimal way to do it. Instead of calculating the new frame from the original mouse location, you should continually update that location and just move the window by the amount that the mouse has moved in the current tracking iteration
Here's a cleaned up, shortened version that seems to eliminate the bug.
}