This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #213 from aaronpearce/console-scroll-to-bottom
Add CPPodfileConsoleTextView to allow auto scrolling
- Loading branch information
Showing
4 changed files
with
38 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Cocoa | ||
|
||
class CPPodfileConsoleTextView: NSTextView { | ||
|
||
// We bind to `attributedText`, not `attributedString` in the storyboard, this is | ||
// to allow us to calculate the previous scroll position before setting the new | ||
// `attributedString` value which updates the text view | ||
var attributedText: NSAttributedString? { | ||
|
||
didSet { | ||
guard let attributedText = attributedText else { return } | ||
|
||
// Determine if we're at the tail of the output log (and should scroll) before we append more to it. | ||
|
||
let scrolledToBottom = NSMaxY(self.visibleRect) != NSMaxY(self.bounds) | ||
|
||
// Setting via `textStorage` as we cannot call `setAttributedString` directly | ||
|
||
self.textStorage?.setAttributedString(attributedText) | ||
|
||
// Keep the text view at the bottom if it was previously, otherwise restore the previous position. | ||
|
||
if (scrolledToBottom) { | ||
self.scrollToEndOfDocument(self) | ||
} else { | ||
self.scrollPoint(self.visibleRect.origin) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters