diff --git a/mxd.swift b/mxd.swift index a7f903a..d66de9c 100644 --- a/mxd.swift +++ b/mxd.swift @@ -66,14 +66,26 @@ struct Config { var smoothScroll = true var scrollStep = 33.6 // a notch counts as at least this many pixels var scrollSpeed = 2.70 // multiplier on every notch, so one slow notch travels 33.6 × 2.70 - var scrollDuration = 4.35 // 0...5, higher = longer glide. See scrollTransition. + var glideIntensity = 0.5 // 0...1, how strong the glide is: 0 = jump with no glide, 0.5 = Mos's default, 1 = Mos's longest var scrollDeadZone = 1.0 // frames at or below this many pixels aren't posted var scrollDash = 5.0 // multiplier while Option is held var reverseMouseScroll = false // invert the mouse only, keep trackpad "natural" // Shift+wheel scrolls sideways. Cmd+wheel skips smoothing, so zooming stays immediate. - // Fraction of the remaining distance consumed per frame: Mos's generateDurationTransition (0.085 at 4.35). - var scrollTransition: Double { (1000 * (1 - (scrollDuration / 5.2).squareRoot())).rounded() / 1000 } + // Fraction of the remaining distance consumed per frame, log-interpolated through 1.0 (0), 0.085 (0.5, Mos's + // default) and 0.019 (1, Mos's generateDurationTransition at its slider maximum of 5.0). + var scrollTransition: Double { + let i = min(max(glideIntensity, 0), 1) + return i < 0.5 ? pow(0.085, i * 2) : 0.085 * pow(0.019 / 0.085, i * 2 - 1) + } + // Blend factor of Mos's ScrollFilter: its own 0.23 from the default up, easing to 1.0 (no extra glide) at 0. + var scrollFilter: Double { + let i = min(max(glideIntensity, 0), 1) + return i >= 0.5 ? 0.23 : 1 - (1 - 0.23) * i * 2 + } + // A frame is remaining × transition and is only posted above the dead zone, so a smaller transition + // would drop most of the distance (or all of it); shrink the dead zone in step to keep the full travel. + var scrollEffectiveDeadZone: Double { scrollDeadZone * min(1, scrollTransition / 0.085) } } let config = Config() let probeOnly = CommandLine.arguments.contains("--probe") @@ -148,12 +160,12 @@ final class Axis { } /// Next frame's output in pixels: Mos's lerp + ScrollFilter. Its 5-point curve only ever uses - /// two of its points, which reduces to this one-frame delay and 23% blend. - func next(_ transition: Double) -> Double { + /// two of its points, which reduces to this one-frame delay and a blend of `filter` (23% by default). + func next(_ transition: Double, _ filter: Double) -> Double { let frame = (target - current) * transition current += frame let output = filtered - filtered += 0.23 * (frame - filtered) + filtered += filter * (frame - filtered) return output } @@ -166,7 +178,7 @@ final class Axis { final class Smoother { let y = Axis(), x = Axis() - let transition = config.scrollTransition, deadZone = config.scrollDeadZone + let transition = config.scrollTransition, filter = config.scrollFilter, deadZone = config.scrollEffectiveDeadZone var event: CGEvent? // copy of the latest notch, re-sent with each frame's distance var targetPID: pid_t = 0 var flags: CGEventFlags = [] @@ -193,7 +205,7 @@ final class Smoother { } func tick() { - var oy = y.next(transition), ox = x.next(transition) + var oy = y.next(transition, filter), ox = x.next(transition, filter) if flags.contains(.maskShift) && oy != 0 && ox == 0 { (oy, ox) = (0, oy) } // Shift+wheel: sideways if max(abs(oy), abs(ox)) > deadZone, let e = event { e.setDoubleValueField(.scrollWheelEventPointDeltaAxis1, value: oy)