Skip to content

Commit

Permalink
Refactor drawingInfo lnitialization
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Oct 25, 2023
1 parent f26bc82 commit f280e74
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions CotEditor/Sources/LineNumberView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ final class LineNumberView: NSView {
didSet {
if !self.isHiddenOrHasHiddenAncestor {
self.invalidateThickness()
self.invalidateIntrinsicContentSize()
}
}
}
Expand Down Expand Up @@ -100,8 +99,8 @@ final class LineNumberView: NSView {

private let textView: NSTextView

private var drawingInfo: DrawingInfo?
private var thickness = 32.0
private var drawingInfo: DrawingInfo
@Invalidating(.intrinsicContentSize) private var thickness = 32.0

@Invalidating(.display) private var textColor: NSColor = .textColor
@Invalidating(.display) private var backgroundColor: NSColor = .textBackgroundColor
Expand All @@ -120,10 +119,10 @@ final class LineNumberView: NSView {
init(textView: NSTextView) {

self.textView = textView
self.drawingInfo = DrawingInfo(fontSize: textView.font!.pointSize, scale: textView.scale)

super.init(frame: .zero)

self.invalidateDrawingInfo()
self.observeTextView(textView)
}

Expand Down Expand Up @@ -250,7 +249,6 @@ final class LineNumberView: NSView {
private func drawNumbers(in rect: NSRect) {

guard
let drawingInfo = self.drawingInfo,
let layoutManager = self.textView.layoutManager as? LayoutManager,
let context = NSGraphicsContext.current?.cgContext
else { return assertionFailure() }
Expand All @@ -262,6 +260,7 @@ final class LineNumberView: NSView {
context.setFillColor(self.foregroundColor().cgColor)
context.setStrokeColor(self.foregroundColor(.stroke).cgColor)

let drawingInfo = self.drawingInfo
let textView = self.textView
let isVerticalText = textView.layoutOrientation == .vertical
let scale = textView.scale
Expand Down Expand Up @@ -344,27 +343,20 @@ final class LineNumberView: NSView {
/// Update receiver's thickness based on drawingInfo and textView's status.
private func invalidateThickness() {

guard let drawingInfo = self.drawingInfo else { return assertionFailure() }

let thickness: CGFloat = {
self.thickness = {
switch self.orientation {
case .horizontal:
let requiredNumberOfDigits = max(self.numberOfLines.digits.count, self.minNumberOfDigits)
let thickness = CGFloat(requiredNumberOfDigits) * drawingInfo.charWidth + 2 * drawingInfo.padding
let thickness = CGFloat(requiredNumberOfDigits) * self.drawingInfo.charWidth + 2 * self.drawingInfo.padding
return max(thickness.rounded(.up), self.minVerticalThickness)

case .vertical:
let thickness = drawingInfo.fontSize + 4 * drawingInfo.tickLength
let thickness = self.drawingInfo.fontSize + 4 * self.drawingInfo.tickLength
return max(thickness.rounded(.up), self.minHorizontalThickness)

@unknown default: fatalError()
}
}()

guard thickness != self.thickness else { return }

self.thickness = thickness
self.invalidateIntrinsicContentSize()
}


Expand Down

0 comments on commit f280e74

Please sign in to comment.