mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 23:13:42 +03:00
feat(app): scale sidebar motion by distance (#58520)
This commit is contained in:
committed by
GitHub
Unverified
parent
146ed0e1fb
commit
c1e311c9ac
@@ -55,12 +55,19 @@ export function createSessionScreenLayout(session: SessionModel, input: { wide:
|
||||
return sessionPanelWidthMax({ available: width, split: splitSide(), resized: true })
|
||||
})
|
||||
|
||||
const sideContentWidth = createMemo<string>((previous) => {
|
||||
// The side panel's open width, kept through a close as the far end of its travel.
|
||||
const sideWidth = createMemo<number | undefined>((previous) => {
|
||||
const width = available()
|
||||
|
||||
if (tabsOpen() && width !== undefined) return `${Math.max(0, width - resizedWidth())}px`
|
||||
if (tabsOpen() && width !== undefined) return Math.max(0, width - resizedWidth())
|
||||
|
||||
return previous
|
||||
})
|
||||
|
||||
const sideContentWidth = createMemo<string>((previous) => {
|
||||
const width = sideWidth()
|
||||
|
||||
return width === undefined ? previous : `${width}px`
|
||||
}, "100%")
|
||||
|
||||
return {
|
||||
@@ -76,6 +83,7 @@ export function createSessionScreenLayout(session: SessionModel, input: { wide:
|
||||
},
|
||||
side: {
|
||||
contentWidth: sideContentWidth,
|
||||
width: sideWidth,
|
||||
open: tabsOpen,
|
||||
tabs: { open: tabsOpen },
|
||||
},
|
||||
|
||||
@@ -197,6 +197,7 @@ function SessionScreenContent(props: {
|
||||
},
|
||||
element: () => sidePresence,
|
||||
offset: jamesPanelShift,
|
||||
distance: screen.side.width,
|
||||
})
|
||||
|
||||
replayPanelToggle({
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { expect, it } from "bun:test"
|
||||
import { panelSpring } from "./panel-motion"
|
||||
|
||||
const shared = { duration: 0.3, bounce: 0 }
|
||||
|
||||
const scale = { min: 0.15, max: 0.45, reference: 480 }
|
||||
|
||||
it("scales the shared spring's time by the square root of the distance against the reference width", () => {
|
||||
expect(panelSpring(480, shared, scale)).toEqual(shared)
|
||||
expect(panelSpring(260, shared, scale).duration).toBeCloseTo(0.2208, 3)
|
||||
expect(panelSpring(260, shared, scale).bounce).toBe(0)
|
||||
expect(panelSpring(260, shared, { ...scale, reference: 260 })).toEqual(shared)
|
||||
})
|
||||
|
||||
it("clamps the scaled time between the min and max durations", () => {
|
||||
expect(panelSpring(40, shared, scale).duration).toBe(0.15)
|
||||
expect(panelSpring(2000, shared, scale).duration).toBe(0.45)
|
||||
expect(panelSpring(40, shared, { ...scale, min: 0.2 }).duration).toBe(0.2)
|
||||
expect(panelSpring(2000, shared, { ...scale, max: 0.6 }).duration).toBe(0.6)
|
||||
})
|
||||
|
||||
it("stops a min dragged past the max at the max", () => {
|
||||
const crossed = { ...scale, min: 0.5, max: 0.3 }
|
||||
expect(panelSpring(40, shared, crossed).duration).toBe(0.3)
|
||||
expect(panelSpring(480, shared, crossed).duration).toBe(0.3)
|
||||
expect(panelSpring(2000, shared, crossed).duration).toBe(0.3)
|
||||
expect(panelSpring(40, shared, { ...scale, min: 0.3, max: 0.3 }).duration).toBe(0.3)
|
||||
})
|
||||
|
||||
it("keeps the shared spring when scaling is off or the distance is unknown", () => {
|
||||
expect(panelSpring(260, shared, null)).toEqual(shared)
|
||||
expect(panelSpring(undefined, shared, scale)).toEqual(shared)
|
||||
expect(panelSpring(0, shared, scale)).toEqual(shared)
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createEffect, createSignal, on, onCleanup, type Accessor } from "solid-js"
|
||||
import { ease, frameLoop, prefersReducedMotion, Spring, type DurationOptions } from "@opencode/ui/motion"
|
||||
import { motionTuning } from "@opencode/ui/motion-tuning"
|
||||
import { motionGlobals, motionTuning } from "@opencode/ui/motion-tuning"
|
||||
|
||||
const group = "Sidebars"
|
||||
|
||||
@@ -29,8 +29,61 @@ const spring = motionTuning.spring("sidebars", {
|
||||
},
|
||||
})
|
||||
|
||||
/** The shared spring, for motion that must read as the sidebar's own, like its peek. */
|
||||
export const panelSpring = spring
|
||||
// Each panel's flight takes the shared spring's time scaled by the square root of how far it travels against a 480px
|
||||
// panel, so a narrow sidebar doesn't crawl and a wide panel doesn't snap.
|
||||
const scaleByDistance = motionTuning.toggle("sidebars.scaleByDistance", {
|
||||
label: "Scale by distance",
|
||||
group: "Experiments",
|
||||
value: true,
|
||||
})
|
||||
|
||||
const distanceKnob = (
|
||||
id: string,
|
||||
meta: { label: string; value: number; min: number; max: number; step: number; unit: string },
|
||||
) => motionTuning.number(`sidebars.scaleByDistance.${id}`, { ...meta, group: "Experiments", when: scaleByDistance })
|
||||
|
||||
const minDuration = distanceKnob("min", {
|
||||
label: "Min duration",
|
||||
value: 0.15,
|
||||
min: 0.05,
|
||||
max: 0.5,
|
||||
step: 0.01,
|
||||
unit: "s",
|
||||
})
|
||||
|
||||
const maxDuration = distanceKnob("max", { label: "Max duration", value: 0.45, min: 0.2, max: 1, step: 0.01, unit: "s" })
|
||||
|
||||
// The width whose flight takes the shared spring's own time.
|
||||
const referenceWidth = distanceKnob("reference", {
|
||||
label: "Reference width",
|
||||
value: 480,
|
||||
min: 200,
|
||||
max: 900,
|
||||
step: 10,
|
||||
unit: "px",
|
||||
})
|
||||
|
||||
/** How flights scale by distance, or null while they don't. */
|
||||
export type DistanceScale = { min: number; max: number; reference: number }
|
||||
|
||||
const distanceScale = (): DistanceScale | null =>
|
||||
scaleByDistance() ? { min: minDuration(), max: maxDuration(), reference: referenceWidth() } : null
|
||||
|
||||
/**
|
||||
* The shared spring for a panel that travels `distance` pixels, for motion that must read as the sidebar's own, like its
|
||||
* peek. Without a distance, or with scaling off, it is the shared spring as tuned.
|
||||
*/
|
||||
export function panelSpring(distance?: number, base = spring(), scale = distanceScale()): DurationOptions {
|
||||
if (!scale || !distance || distance <= 0) return base
|
||||
// The bounds stretch with the global speed and slow motion, like the spring itself. A min dragged past the max stops
|
||||
// at it.
|
||||
const time = (motionGlobals.slow.value ? 4 : 1) / motionGlobals.speed.value
|
||||
const max = scale.max * time
|
||||
const min = Math.min(scale.min * time, max)
|
||||
const duration = base.duration * Math.sqrt(distance / scale.reference)
|
||||
|
||||
return { ...base, duration: Math.min(max, Math.max(min, duration)) }
|
||||
}
|
||||
|
||||
const style = motionTuning.choice("sidebars.style", {
|
||||
label: "Motion",
|
||||
@@ -331,6 +384,8 @@ export function createPanelMotion(input: {
|
||||
*/
|
||||
element?: Accessor<HTMLElement | undefined>
|
||||
offset?: () => number
|
||||
/** The width the panel travels across, in pixels, read once as each flight starts; it scales the flight's time. */
|
||||
distance?: () => number | undefined
|
||||
}) {
|
||||
const [present, setPresent] = createSignal(input.open())
|
||||
// Percent closed, so the spring's rest threshold stays well under a pixel.
|
||||
@@ -459,7 +514,7 @@ export function createPanelMotion(input: {
|
||||
|
||||
setPinned(james())
|
||||
|
||||
const tuning = james() ? (open ? jamesLayoutOpen() : jamesLayoutClose()) : spring()
|
||||
const tuning = james() ? (open ? jamesLayoutOpen() : jamesLayoutClose()) : panelSpring(input.distance?.())
|
||||
motion.omega = (2 * Math.PI) / tuning.duration
|
||||
motion.zeta = 1 - tuning.bounce
|
||||
// Retargeting keeps the current position and velocity, so a second toggle mid-flight turns around smoothly.
|
||||
|
||||
@@ -121,6 +121,7 @@ export default function Layout(props: ParentProps) {
|
||||
// A dock from a peek already shows the sidebar, so reduced motion has nothing to fade in.
|
||||
element: () => (peek.handedOff() ? undefined : state.tabsPanel),
|
||||
offset: () => -10,
|
||||
distance: () => state.tabsWidth,
|
||||
})
|
||||
|
||||
const peek = createSidebarPeek({
|
||||
@@ -128,6 +129,7 @@ export default function Layout(props: ParentProps) {
|
||||
open: preferences.appearance.sidebarOpen,
|
||||
aside: () => state.tabsAside,
|
||||
panel: () => state.tabsPanel,
|
||||
width: () => state.tabsWidth,
|
||||
paint: (shown) => {
|
||||
sidebar.peek = shown
|
||||
paintSidebarContents()
|
||||
|
||||
@@ -108,6 +108,8 @@ export function createSidebarPeek(input: {
|
||||
aside: Accessor<HTMLElement | undefined>
|
||||
/** The sidebar's box while it peeks, which doesn't move while the content inside it slides. */
|
||||
panel: Accessor<HTMLElement | undefined>
|
||||
/** The sidebar's width, which the matched spring scales its time by like the docked sidebar's. */
|
||||
width: Accessor<number>
|
||||
/** Paints how far in the peek has the sidebar, from 0 to 1, on every frame it moves; undefined once it isn't peeking. */
|
||||
paint: (shown: number | undefined) => void
|
||||
/** Whether docking continues from the peek; otherwise the docked sidebar's own motion takes over at once. */
|
||||
@@ -151,7 +153,7 @@ export function createSidebarPeek(input: {
|
||||
|
||||
// Retargeting keeps the current position and velocity, so a peek that turns around mid-flight stays smooth.
|
||||
const run = (target: number) => {
|
||||
const tuning = prefersReducedMotion() ? fade : peekMatched() ? panelSpring() : peekSpring()
|
||||
const tuning = prefersReducedMotion() ? fade : peekMatched() ? panelSpring(input.width()) : peekSpring()
|
||||
motion.omega = (2 * Math.PI) / tuning.duration
|
||||
motion.zeta = 1 - tuning.bounce
|
||||
motion.set(target)
|
||||
|
||||
@@ -34,6 +34,7 @@ function mount() {
|
||||
open: () => false,
|
||||
aside: () => aside,
|
||||
panel: () => panel,
|
||||
width: () => 260,
|
||||
paint: () => {},
|
||||
continues: () => true,
|
||||
whenDocked: (run) => run(),
|
||||
|
||||
Reference in New Issue
Block a user