Skip to content

Commit

Permalink
Clean up the code around STInsertionPointProtocol (#41)
Browse files Browse the repository at this point in the history
This PR removes unnecessary availability checks and downcast to the
concrete implementation of `STInsertionPointProtocol` by moving the
`color` property into `STInsertionPointProtocol` and accessing it
directly over the type-erased `textInsertionIndicator:
STInsertionPointProtocol`.

Since Apple declare the `NSTextInsertionIndicator.color` as `NSColor!`,
it's impossible to reuse the same name for property as `NSColor` and
`NSColor!` are different in the eyes of compiler, therefore the `color`
had to be declared as `insertionPointColor` within the
`STInsertionPointProtocol` protocol to workaround that.
  • Loading branch information
pronebird authored Oct 22, 2023
1 parent d6dc581 commit 27f2f36
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions Sources/STTextView/STInsertionPointView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,11 @@ open class STInsertionPointView: NSView {

var insertionPointColor: NSColor {
get {
if #available(macOS 14, *), let textInsertionIndicator = textInsertionIndicator as? STTextInsertionIndicatorNew {
return textInsertionIndicator.color
} else if let textInsertionIndicator = textInsertionIndicator as? STTextInsertionIndicatorOld {
return textInsertionIndicator.color
}

return .clear
textInsertionIndicator.insertionPointColor
}

set {
if #available(macOS 14, *), let textInsertionIndicator = textInsertionIndicator as? STTextInsertionIndicatorNew {
textInsertionIndicator.color = newValue
} else if let textInsertionIndicator = textInsertionIndicator as? STTextInsertionIndicatorOld {
textInsertionIndicator.color = newValue
}
textInsertionIndicator.insertionPointColor = newValue
}
}

Expand All @@ -48,24 +38,18 @@ open class STInsertionPointView: NSView {
}

func blinkStart() {
if #available(macOS 14, *), let textInsertionIndicator = textInsertionIndicator as? STTextInsertionIndicatorNew {
textInsertionIndicator.blinkStart()
} else if let textInsertionIndicator = textInsertionIndicator as? STTextInsertionIndicatorOld {
textInsertionIndicator.blinkStart()
}
textInsertionIndicator.blinkStart()
}

func blinkStop() {
if #available(macOS 14, *), let textInsertionIndicator = textInsertionIndicator as? STTextInsertionIndicatorNew {
textInsertionIndicator.blinkStop()
} else if let textInsertionIndicator = textInsertionIndicator as? STTextInsertionIndicatorOld {
textInsertionIndicator.blinkStop()
}
textInsertionIndicator.blinkStop()
}

}

private protocol STInsertionPointProtocol: NSView {
var insertionPointColor: NSColor { get set }

func blinkStart()
func blinkStop()
}
Expand All @@ -82,6 +66,16 @@ private class STTextInsertionIndicatorNew: NSTextInsertionIndicator, STInsertion
fatalError("init(coder:) has not been implemented")
}

var insertionPointColor: NSColor {
get {
color
}

set {
color = newValue
}
}

func blinkStart() {
displayMode = .automatic
}
Expand All @@ -97,9 +91,10 @@ private class STTextInsertionIndicatorNew: NSTextInsertionIndicator, STInsertion

private class STTextInsertionIndicatorOld: NSView, STInsertionPointProtocol {
private var timer: Timer?
var color: NSColor = .defaultTextInsertionPoint {

var insertionPointColor: NSColor = .defaultTextInsertionPoint {
didSet {
layer?.backgroundColor = color.cgColor
layer?.backgroundColor = insertionPointColor.cgColor
}
}

Expand All @@ -109,7 +104,7 @@ private class STTextInsertionIndicatorOld: NSView, STInsertionPointProtocol {
super.init(frame: indicatorRect)

wantsLayer = true
layer?.backgroundColor = color.withAlphaComponent(0.9).cgColor
layer?.backgroundColor = insertionPointColor.withAlphaComponent(0.9).cgColor
layer?.cornerRadius = 1
}

Expand Down

0 comments on commit 27f2f36

Please sign in to comment.