NSNotification 滚动 - 用户与编程

发布于 2024-12-06 23:11:58 字数 178 浏览 1 评论 0原文

我有一个带有 NSTextView (实际上是自定义子类)的程序,可能会以编程方式插入很多行数据。 (它从 USB 端口读取串行数据流。)我有一个用于启用/禁用自动滚动的复选框。我想让用户只需尝试向上滚动即可摆脱自动滚动。因此,我需要一个通知来告诉我用户何时滚动,而不仅仅是边界何时更改,因为每次插入更多串行数据时都会发生这种情况。这可能吗?

I have a program with an NSTextView (actually, a custom subclass) into which a lot of lines of data are likely to be programmatically inserted. (It reads a stream of serial data from a USB port.) I have a checkbox for enabling/disabling autoscrolling. I want to allow the user to break out of autoscrolling simply by trying to scroll back up. So, I need a notification that tells me when the user has scrolled, not just when the bounds have changed, since this happens every time more serial data gets inserted. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱影子 2024-12-13 23:11:58

当然,您可以使用通知来告诉您何时发生任何滚动,然后检查文本视图是否完全滚动到底部?如果是,请打开自动滚动。如果没有,请将其关闭。

Surely you could use the notification that tells you when any scrolling takes place, and then check if the text view is scrolled entirely to the bottom? If it is, turn auto-scrolling on. If not, turn it off.

我爱人 2024-12-13 23:11:58

这不是最好的,但这适用于我的情况。我认为通过一些 NSEvent 操作可以做得更干净一些,但我意识到我可以通过检查当前滚动位置与总文档矩形高度来检查用户是否已开始滚动。

NSRect totalRect   = [[serialScrollView contentView] documentRect];
NSRect visibleRect = [[serialScrollView contentView] documentVisibleRect];
NSInteger totalHeight = totalRect.size.height;
NSInteger visibleHeight = visibleRect.size.height;
NSInteger position = visibleRect.origin.y;

NSInteger scrollPoint = position + visibleHeight;

if (totalHeight != scrollPoint)
    [autoscrolls setState:0];

因此,基本上,如果滚动位置变为程序期望的编程写入以外的位置,则会关闭自动滚动。此实现的一个很酷的事情是,如果您添加 else [autoscrolls setState:1];,当您向下滚动以赶上流时,它会重新打开自动滚动。当您运行具有大量输出的 shell 脚本时,这会模拟终端中的滚动行为,例如 Fedora 上的 yum install 或类似的事情。

It's not the best, but this works in my case. I think it could be done a little cleaner with some NSEvent manipulation, but I realized that I can check to see if the user has started scrolling by checking the current scroll position against the total document rectangle height.

NSRect totalRect   = [[serialScrollView contentView] documentRect];
NSRect visibleRect = [[serialScrollView contentView] documentVisibleRect];
NSInteger totalHeight = totalRect.size.height;
NSInteger visibleHeight = visibleRect.size.height;
NSInteger position = visibleRect.origin.y;

NSInteger scrollPoint = position + visibleHeight;

if (totalHeight != scrollPoint)
    [autoscrolls setState:0];

So basically, if the scroll position becomes anything other than what the program expects it to be from programmatic writes, it turns autoscrolling off. A cool thing about this implementation is that if you ad an else [autoscrolls setState:1];, it turns autoscrolling back on when you scroll back down to catch up with the stream. This emulates the scrolling behavior in the terminal when you're running a shell script with lots of output, like a yum install on Fedora or that sort of thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文