diff --git a/Sources/TinyHand/Ink.swift b/Sources/TinyHand/Ink.swift index 766dbed..328df71 100644 --- a/Sources/TinyHand/Ink.swift +++ b/Sources/TinyHand/Ink.swift @@ -2,7 +2,14 @@ import AppKit final class Ink { let layer = CALayer() - var keep: Bool { Preferences.fadeSeconds <= 0 } + var keep: Bool { Preferences.fadeSeconds == 0 } + + // With the trail setting, nothing is erased while you hold the button. When you let go, an + // eraser waits `trailDelay`, then follows the line from its start at a steady speed, so long + // lines take longer to go than short ones. + private var trailing: Bool { Preferences.fadeSeconds < 0 } + private var trailDelay: CFTimeInterval { Preferences.trailDelay } + private var eraseSpeed: CGFloat { CGFloat(Preferences.trailSpeed) } static let purple = CGColor(srgbRed: 0.53, green: 0.33, blue: 0.95, alpha: 1) static let red = CGColor(srgbRed: 1.0, green: 0.16, blue: 0.12, alpha: 1) @@ -66,8 +73,13 @@ final class Ink { return } stroke = nil + let length = lineLength() + headLength(s) points = [] arrow = false + if trailing { + erase(s, length: length) + return + } freeze(s) if keep { return @@ -81,6 +93,35 @@ final class Ink { layer.sublayers?.forEach { $0.removeFromSuperlayer() } } + private func lineLength() -> CGFloat { + zip(points, points.dropFirst()).reduce(0) { $0 + hypot($1.1.x - $1.0.x, $1.1.y - $1.0.y) } + } + + // Erases the finished line from its start, `trailDelay` after release, at a steady speed. + private func erase(_ s: CAShapeLayer, length: CGFloat) { + let duration = max(0.05, CFTimeInterval(length / eraseSpeed)) + CATransaction.begin() + CATransaction.setDisableActions(true) + s.strokeStart = 1 + CATransaction.commit() + let anim = CABasicAnimation(keyPath: "strokeStart") + anim.fromValue = 0 + anim.toValue = 1 + anim.beginTime = CACurrentMediaTime() + trailDelay + anim.duration = duration + anim.timingFunction = CAMediaTimingFunction(name: .linear) + anim.fillMode = .backwards + s.add(anim, forKey: "trail") + DispatchQueue.main.asyncAfter(deadline: .now() + trailDelay + duration) { [weak s] in + s?.removeFromSuperlayer() + } + } + + // The arrowhead's two strokes are drawn after the line, so the eraser reaches them last. + private func headLength(_ s: CAShapeLayer) -> CGFloat { + arrow ? 2 * max(14, s.lineWidth * 3.2) : 0 + } + private func freeze(_ s: CAShapeLayer) { guard let path = s.path else { return diff --git a/Sources/TinyHand/Preferences.swift b/Sources/TinyHand/Preferences.swift index 6eff8f7..d7f1e7a 100644 --- a/Sources/TinyHand/Preferences.swift +++ b/Sources/TinyHand/Preferences.swift @@ -109,6 +109,23 @@ enum Preferences { set { set(newValue, "fadeSeconds") } } + /// The `fadeSeconds` value that makes lines trail the pen instead of fading as a whole. + static let trailFade: Double = -1 + + static let trailDelayRange: ClosedRange = 0.2...2 + + /// Seconds from releasing a line until the trail starts erasing it. + static var trailDelay: Double { + get { defaults.object(forKey: "trailDelay") as? Double ?? 0.6 } + set { set(newValue, "trailDelay") } + } + + /// How fast the trail erases, in points per second. + static var trailSpeed: Double { + get { defaults.object(forKey: "trailSpeed") as? Double ?? 2000 } + set { set(newValue, "trailSpeed") } + } + static var handSound: Bool { get { defaults.object(forKey: "sound.hands") as? Bool ?? true } set { set(newValue, "sound.hands") } @@ -185,7 +202,7 @@ enum Preferences { } static func resetAll() { - for key in ["shortcut.toggle", "shortcut.clear", "pointerSize", "showShadow", "fadeSeconds", + for key in ["shortcut.toggle", "shortcut.clear", "pointerSize", "showShadow", "fadeSeconds", "trailDelay", "trailSpeed", "sound.hands", "sound.laser", "interactKey", "interactKeepsHand", "doubleShiftClickThrough", "pinnedStyles", "pointerStyle", "inkChoice", "showInkButton", "showToolbar", "tapSound"] { defaults.removeObject(forKey: key) diff --git a/Sources/TinyHand/SettingsView.swift b/Sources/TinyHand/SettingsView.swift index 4b62fc9..5156892 100644 --- a/Sources/TinyHand/SettingsView.swift +++ b/Sources/TinyHand/SettingsView.swift @@ -977,6 +977,13 @@ private struct DrawingPage: View { ("After 6 seconds", 6), ("After 10 seconds", 10), ("Keep until cleared", 0), + ("Trail behind the pen", Preferences.trailFade), + ] + private static let trailSpeeds: [(String, Double)] = [ + ("Slow", 800), + ("Medium", 1400), + ("Fast", 2000), + ("Very fast", 3000), ] var body: some View { @@ -1002,8 +1009,37 @@ private struct DrawingPage: View { .fixedSize() .focusable(false) } + if Preferences.fadeSeconds < 0 { + Row("Erasing starts", detail: "Counted from when you let go.") { + HStack(spacing: 10) { + Slider(value: model.binding({ Preferences.trailDelay }, { Preferences.trailDelay = ($0 * 10).rounded() / 10 }), + in: Preferences.trailDelayRange) + .frame(width: 180) + .focusable(false) + Text(String(format: "%.1f s", Preferences.trailDelay)) + .font(.system(size: 12).monospacedDigit()) + .foregroundColor(.secondary) + .frame(width: 36, alignment: .trailing) + } + } + Row("Erasing speed", detail: "Long lines take longer to erase than short ones.") { + menu(DrawingPage.trailSpeeds, model.binding({ Preferences.trailSpeed }, { Preferences.trailSpeed = $0 })) + } + } } } + + private func menu(_ choices: [(String, Double)], _ selection: Binding) -> some View { + Picker("", selection: selection) { + ForEach(choices, id: \.1) { choice in + Text(choice.0).tag(choice.1) + } + } + .labelsHidden() + .pickerStyle(.menu) + .fixedSize() + .focusable(false) + } } private struct SoundPage: View {