LINE1 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 2201import { useEffect, useMemo, useState } from 'react'2import { invoke } from '@tauri-apps/api/core'3import { listen } from '@tauri-apps/api/event'4import type { Body, Collision, Snapshot } from './types'56const colors = ['#69d2e7', '#f6bd60', '#f28482', '#a8dadc', '#cdb4db', '#90be6d']78function formatClock(tick: number): string {9 const seconds = Math.floor(tick / 40)10 return `${String(Math.floor(seconds / 60)).padStart(2, '0')}:${String(seconds % 60).padStart(2, '0')}`11}1213function Arena({ snapshot, collision }: { snapshot: Snapshot; collision: Collision | null }) {14 const bodies = useMemo(() => snapshot.bodies, [snapshot.bodies])15 return (16 <div className="simulation-grid relative aspect-[1.55] overflow-hidden rounded-box border border-base-300 bg-base-200/70">17 <div className="absolute left-3 top-3 z-10 flex items-center gap-2 rounded-full border border-base-300 bg-base-100/80 px-2.5 py-1 text-[10px] font-medium uppercase tracking-[0.16em] text-base-content/60 backdrop-blur">18 <span className={`size-1.5 rounded-full ${snapshot.running ? 'bg-success' : 'bg-base-content/30'}`} />19 {snapshot.running ? 'Live simulation' : 'Paused'}20 </div>21 <svg className="absolute inset-0 size-full" viewBox={`0 0 ${snapshot.width} ${snapshot.height}`} role="img" aria-label={`${bodies.length} bodies moving in a physics simulation`}>22 {bodies.map((body: Body) => (23 <g key={body.id}>24 <circle cx={body.x} cy={body.y} r={body.radius + 2} fill={colors[(body.id - 1) % colors.length]} opacity="0.12" />25 <circle cx={body.x} cy={body.y} r={body.radius} fill={colors[(body.id - 1) % colors.length]} stroke="white" strokeOpacity="0.65" strokeWidth="0.7" />26 <circle cx={body.x - body.radius * 0.28} cy={body.y - body.radius * 0.3} r={body.radius * 0.2} fill="white" opacity="0.65" />27 </g>28 ))}29 {collision && (30 <g key={collision.tick} className="pointer-events-none">31 <circle cx={collision.x} cy={collision.y} r="7" fill="none" stroke="#ffffff" strokeWidth="1" opacity="0.8">32 <animate attributeName="r" from="3" to="13" dur="0.45s" fill="freeze" />33 <animate attributeName="opacity" from="0.8" to="0" dur="0.45s" fill="freeze" />34 </circle>35 </g>36 )}37 </svg>38 <span className="absolute bottom-3 right-3 font-mono text-[10px] text-base-content/35">100 × 70</span>39 </div>40 )41}4243export default function App() {44 const [snapshot, setSnapshot] = useState<Snapshot | null>(null)45 const [lastCollision, setLastCollision] = useState<Collision | null>(null)46 const [recent, setRecent] = useState<Collision[]>([])47 const [error, setError] = useState<string | null>(null)4849 useEffect(() => {50 let active = true51 let unlistenUpdate = () => {}52 let unlistenCollision = () => {}53 void Promise.all([54 listen<Snapshot>('simulation-update', ({ payload }) => active && setSnapshot(payload)),55 listen<Collision>('collision', ({ payload }) => {56 if (!active) return57 setLastCollision(payload)58 setRecent((items) => [payload, ...items].slice(0, 4))59 }),60 invoke<Snapshot>('get_snapshot'),61 ]).then(([stopUpdate, stopCollision, initial]) => {62 if (!active) {63 stopUpdate()64 stopCollision()65 return66 }67 unlistenUpdate = stopUpdate68 unlistenCollision = stopCollision69 setSnapshot(initial)70 }).catch((reason: unknown) => {71 if (active) setError(String(reason))72 })73 return () => {74 active = false75 unlistenUpdate()76 unlistenCollision()77 }78 }, [])7980 async function toggleRunning() {81 if (!snapshot) return82 try {83 setSnapshot(await invoke<Snapshot>('set_running', { running: !snapshot.running }))84 setError(null)85 } catch (reason) {86 setError(String(reason))87 }88 }8990 async function reset() {91 try {92 setSnapshot(await invoke<Snapshot>('reset_simulation'))93 setRecent([])94 setLastCollision(null)95 setError(null)96 } catch (reason) {97 setError(String(reason))98 }99 }100101 return (102 <main className="mx-auto flex min-h-screen w-full max-w-2xl flex-col gap-5 px-5 py-5 text-sm sm:px-7 sm:py-7">103 <header className="drag-region flex items-center justify-between">104 <div className="flex items-center gap-3">105 <div className="grid size-10 place-items-center rounded-xl bg-primary text-primary-content shadow-lg shadow-primary/20">106 <svg viewBox="0 0 24 24" className="size-5" fill="none" stroke="currentColor" strokeWidth="1.8" aria-hidden="true">107 <circle cx="12" cy="12" r="7.5" /><circle cx="12" cy="12" r="2" fill="currentColor" />108 <path d="M12 2v2m0 16v2M2 12h2m16 0h2M4.93 4.93l1.42 1.42m11.3 11.3 1.42 1.42m0-14.14-1.42 1.42m-11.3 11.3-1.42 1.42" />109 </svg>110 </div>111 <div>112 <h1 className="text-base font-semibold tracking-tight">rustbar <span className="text-base-content/40">/</span> physics lab</h1>113 <p className="text-xs text-base-content/50">A tiny desktop-sized universe</p>114 </div>115 </div>116 <span className="badge badge-outline gap-1.5 border-base-300 text-[10px] uppercase tracking-wider text-base-content/55">117 <span className="size-1.5 rounded-full bg-success" /> Rust engine118 </span>119 </header>120121 {error && <div role="alert" className="alert alert-error py-2 text-xs">{error}</div>}122123 <section className="card border border-base-300 bg-base-100 shadow-xl shadow-black/10">124 <div className="card-body gap-4 p-4 sm:p-5">125 <div className="flex items-end justify-between">126 <div>127 <p className="text-[10px] font-semibold uppercase tracking-[0.2em] text-base-content/40">Realtime playground</p>128 <h2 className="mt-1 text-lg font-semibold">Particle motion</h2>129 </div>130 <span className="font-mono text-xs tabular-nums text-base-content/50">{snapshot ? formatClock(snapshot.tick) : '--:--'}</span>131 </div>132 {snapshot ? <Arena snapshot={snapshot} collision={lastCollision} /> : <div className="skeleton aspect-[1.55] w-full rounded-box" />}133 <div className="flex flex-wrap items-center justify-between gap-3">134 <div className="flex gap-2">135 <button className="btn btn-sm btn-primary min-w-24" onClick={() => void toggleRunning()} disabled={!snapshot}>136 {snapshot?.running ? 'Pause' : 'Resume'}137 </button>138 <button className="btn btn-sm btn-ghost border border-base-300" onClick={() => void reset()}>Reset</button>139 </div>140 <div className="flex items-center gap-4 text-xs text-base-content/55">141 <span><strong className="font-mono text-base-content">{snapshot?.bodies.length ?? '—'}</strong> bodies</span>142 <span><strong className="font-mono text-base-content">{snapshot?.collisions ?? '—'}</strong> collisions</span>143 </div>144 </div>145 </div>146 </section>147148 <section className="grid gap-4 sm:grid-cols-[1fr_1.15fr]">149 <div className="card border border-base-300 bg-base-100">150 <div className="card-body p-4">151 <div className="flex items-center justify-between">152 <h2 className="text-xs font-semibold uppercase tracking-widest text-base-content/50">Engine status</h2>153 <span className="badge badge-success badge-sm badge-soft">Connected</span>154 </div>155 <div className="mt-1 grid grid-cols-2 gap-3">156 <div><p className="text-[10px] uppercase tracking-wider text-base-content/40">Update rate</p><p className="mt-1 font-mono text-sm">40 <span className="text-xs text-base-content/45">Hz</span></p></div>157 <div><p className="text-[10px] uppercase tracking-wider text-base-content/40">Runtime</p><p className="mt-1 font-mono text-sm">Tauri <span className="text-xs text-base-content/45">v2</span></p></div>158 </div>159 </div>160 </div>161 <div className="card border border-base-300 bg-base-100">162 <div className="card-body gap-2 p-4">163 <div className="flex items-center justify-between">164 <h2 className="text-xs font-semibold uppercase tracking-widest text-base-content/50">Recent impacts</h2>165 <span className="text-[10px] text-base-content/35">LIVE EVENTS</span>166 </div>167 {recent.length ? recent.map((event, index) => (168 <div key={`${event.tick}-${index}`} className="flex items-center justify-between text-xs">169 <span className="flex items-center gap-2 text-base-content/70"><span className="size-1.5 rounded-full bg-warning" />Body {event.first} <span className="text-base-content/35">×</span> {event.second}</span>170 <span className="font-mono text-[10px] text-base-content/40">{formatClock(event.tick)}</span>171 </div>172 )) : <p className="py-2 text-xs text-base-content/40">Waiting for the next collision…</p>}173 </div>174 </div>175 </section>176177 <footer className="mt-auto flex items-center justify-between border-t border-base-300/70 pt-3 text-[10px] text-base-content/35">178 <span>Rust computes · React renders</span>179 <span>Open from the system tray anytime</span>180 </footer>181 </main>182 )183}