Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion Sources/TinyHand/Ink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 18 additions & 1 deletion Sources/TinyHand/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Double> = 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") }
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions Sources/TinyHand/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Double>) -> 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 {
Expand Down