@@ -3,7 +3,7 @@ import { clamp } from './math'
33
44import type { ScrollAcceleration } from '@opentui/core'
55
6- const SCROLL_MODE_OVERRIDE = 'CODEBUFF_SCROLL_MODE '
6+ const SCROLL_MULTIPLIER = 'CODEBUFF_SCROLL_MULTIPLIER '
77
88const INERTIAL_HINT_VARS = [
99 'TERM_PROGRAM',
@@ -23,7 +23,7 @@ type ScrollEnvironment =
2323 | {
2424 enabled: true
2525 hint?: (typeof ENVIRONMENTS)[number]
26- override?: 'slow'
26+ override?: 'slow' | 'fast'
2727 }
2828 | {
2929 enabled: false
@@ -32,14 +32,17 @@ type ScrollEnvironment =
3232 }
3333
3434const resolveScrollEnvironment = (): ScrollEnvironment => {
35- const override = process.env[SCROLL_MODE_OVERRIDE ]?.toLowerCase()
35+ const override = process.env[SCROLL_MULTIPLIER ]?.toLowerCase()
3636
37- if (override === 'slow' || override === 'inertial' ) {
37+ if (override === 'slow') {
3838 return { enabled: true, override: 'slow' }
3939 }
4040 if (override === 'default' || override === 'off') {
4141 return { enabled: false, override: 'default' }
4242 }
43+ if (override === 'fast') {
44+ return { enabled: true, override: 'fast' }
45+ }
4346
4447 for (const hintVar of INERTIAL_HINT_VARS) {
4548 const value = process.env[hintVar]
@@ -61,6 +64,33 @@ const ENV_MULTIPLIERS = {
6164 default: 0.3,
6265} satisfies Record<(typeof ENVIRONMENTS)[number] | 'default', number>
6366
67+ type LinearScrollAccelOptions = {
68+ /** How fast to scale the scrolling. */
69+ multiplier?: number
70+ }
71+
72+ /** Always scrolls at a constant speed per tick. */
73+ export class LinearScrollAccel implements ScrollAcceleration {
74+ private multiplier: number
75+ private buffer: number
76+
77+ constructor(private opts: LinearScrollAccelOptions = {}) {
78+ this.buffer = 0
79+ this.multiplier = opts.multiplier ?? 1
80+ }
81+
82+ tick(): number {
83+ this.buffer += this.multiplier
84+ const rows = Math.floor(this.buffer)
85+ this.buffer -= rows
86+ return rows
87+ }
88+
89+ reset(): void {
90+ this.buffer = 0
91+ }
92+ }
93+
6494type QuadraticScrollAccelOptions = {
6595 /** How fast to scale the scrolling. */
6696 multiplier?: number
@@ -118,23 +148,28 @@ export class QuadraticScrollAccel implements ScrollAcceleration {
118148 }
119149}
120150
121- export const createChatScrollAcceleration = ():
122- | ScrollAcceleration
123- | undefined => {
151+ export const createChatScrollAcceleration = (): ScrollAcceleration => {
124152 const environment = resolveScrollEnvironment()
125153
126- let environmentTunedOptions: QuadraticScrollAccelOptions = {}
154+ // MacOS handles the scroll acceleration differently
155+ const ScrollAccel =
156+ process.platform === 'darwin' ? LinearScrollAccel : QuadraticScrollAccel
157+
158+ let environmentTunedOptions: { multiplier?: number } = {}
127159
128160 if (!environment.enabled) {
129161 // No environment detected
130- environmentTunedOptions.multiplier = 0.2
162+ environmentTunedOptions.multiplier = ENV_MULTIPLIERS.default
131163 } else {
132164 environmentTunedOptions.multiplier =
133165 ENV_MULTIPLIERS[environment.hint ?? 'default']
134166 if (environment.override === 'slow') {
135167 environmentTunedOptions.multiplier *= 0.5
136168 }
169+ if (environment.override === 'fast') {
170+ environmentTunedOptions.multiplier *= 2
171+ }
137172 }
138173
139- return new QuadraticScrollAccel (environmentTunedOptions)
174+ return new ScrollAccel (environmentTunedOptions)
140175}
0 commit comments