Skip to content

Instantly share code, notes, and snippets.

@zrzka
Last active July 31, 2020 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zrzka/3316be23624400f349ecc1de0a8d139d to your computer and use it in GitHub Desktop.
Save zrzka/3316be23624400f349ecc1de0a8d139d to your computer and use it in GitHub Desktop.
class ClipView: NSClipView {
var isScrollAnimationEnabled: Bool = true
override func scroll(to newOrigin: NSPoint) {
if isScrollAnimationEnabled {
super.scroll(to: newOrigin)
} else {
setBoundsOrigin(newOrigin)
}
}
}
class OutlineView: NSOutlineView {
private var arrowKeysDown = Set<UInt16>()
override func keyDown(with event: NSEvent) {
if (125...126).contains(event.keyCode) {
arrowKeysDown.insert(event.keyCode)
(enclosingScrollView?.contentView as? ClipView)?.isScrollAnimationEnabled = false
}
super.keyDown(with: event)
}
override func keyUp(with event: NSEvent) {
super.keyUp(with: event)
arrowKeysDown.remove(event.keyCode)
(enclosingScrollView?.contentView as? ClipView)?.isScrollAnimationEnabled = arrowKeysDown.isEmpty
}
}
@tesch
Copy link

tesch commented Jul 31, 2020

Cool, this is useful! If I may ask, what is the rational for having arrowKeysDown? Wouldn't it be equivalent to just explicitly toggle isScrollAnimationEnabled in the key up / down methods?

@zrzka
Copy link
Author

zrzka commented Jul 31, 2020

@tesch

First - You'd like to disable/enable scrolling only when Up / Down arrow keys are pressed / released.
Second - imagine this:

  1. Up arrow key down (scrolling explicitly disabled)
  2. Down arrow key down (scrolling explicitly disabled)
  3. Up arrow key up (scrolling explicitly enabled, but the Down arrow key is still down)

Sometimes, especially on non full sized keyboards, I press both Up & Down arrow keys down, realize this, move my finger a bit and one key is released then (up), but another one is still down. In this case, you do not want to enable scrolling again until you release both of them, because the table/outline view is still scrolling.

It's an edge case, but can happen.

@zrzka
Copy link
Author

zrzka commented Jul 31, 2020

Did update the gist. We can set it to false explicitly in the keyDown, but we have to keep it in the keyUp to be sure that both Up / Down arrow keys are released.

@tesch
Copy link

tesch commented Jul 31, 2020

@zrzka

I see, thanks. In practice I wasn't able to re-enable the scrolling animation with the scenario you described, as the down arrow keeps calling key down, which immediately disables it again. But it sure is best practice to not rely on that.

@zrzka
Copy link
Author

zrzka commented Jul 31, 2020

Yeah, you're probably right, feel free to modify it in any way :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment