Obtain visibleRect without additional layout

This commit is contained in:
1024jp 2019-07-02 15:16:04 +01:00
parent e6dcabe154
commit 5d5c8d04fa
3 changed files with 12 additions and 5 deletions

View File

@ -5,6 +5,11 @@ Change Log
3.7.8 (361)
--------------------------
### Improvements
- Optimize performance of text layout calcuration.
### Fixes
- Fix an issue where the editor could not scroll horizontally under specific conditions.

View File

@ -38,12 +38,12 @@ extension NSTextView {
/// calculate visible range
var visibleRange: NSRange? {
return self.range(for: self.visibleRect)
return self.range(for: self.visibleRect, withoutAdditionalLayout: true)
}
/// calculate range of characters in rect
func range(for rect: NSRect) -> NSRange? {
func range(for rect: NSRect, withoutAdditionalLayout: Bool = false) -> NSRange? {
guard
let layoutManager = self.layoutManager,
@ -51,7 +51,9 @@ extension NSTextView {
else { return nil }
let visibleRect = rect.offset(by: -self.textContainerOrigin)
let glyphRange = layoutManager.glyphRange(forBoundingRect: visibleRect, in: textContainer)
let glyphRange = withoutAdditionalLayout
? layoutManager.glyphRange(forBoundingRectWithoutAdditionalLayout: visibleRect, in: textContainer)
: layoutManager.glyphRange(forBoundingRect: visibleRect, in: textContainer)
return layoutManager.characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil)
}

View File

@ -8,7 +8,7 @@
//
// ---------------------------------------------------------------------------
//
// © 2018 1024jp
// © 2018-2019 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -41,7 +41,7 @@ extension NSTextView {
// avoid invoking heavy-duty `range(for:)` as possible
guard
let layoutManager = self.layoutManager,
let dirtyRange = self.range(for: dirtyRect),
let dirtyRange = self.range(for: dirtyRect, withoutAdditionalLayout: true),
layoutManager.hasTemporaryAttribute(for: .roundedBackgroundColor, in: dirtyRange)
else { return }