LINE1 10 20 30 40 50 60 70 80 901mod physics;23use std::{4 sync::{Arc, Mutex},5 thread,6 time::Duration,7};89use physics::{Collision, Simulation, Snapshot};10use tauri::{11 Emitter, Manager,12 menu::{Menu, MenuItem},13 tray::TrayIconBuilder,14};1516type SharedSimulation = Arc<Mutex<Simulation>>;171819pub fn run() {20 let result = tauri::Builder::default()21 .manage(Arc::new(Mutex::new(Simulation::new())))22 .setup(|app| {23 install_tray(app)?;24 start_simulation_loop(25 app.handle().clone(),26 app.state::<SharedSimulation>().inner().clone(),27 );28 Ok(())29 })30 .on_window_event(|window, event| {31 if let tauri::WindowEvent::CloseRequested { api, .. } = event {32 api.prevent_close();33 let _ = window.hide();34 }35 })36 .invoke_handler(tauri::generate_handler![37 get_snapshot,38 set_running,39 reset_simulation40 ])41 .run(tauri::generate_context!());4243 if let Err(error) = result {44 eprintln!("rustbar failed to start: {error}");45 }46}4748#[tauri::command]49#[allow(clippy::needless_pass_by_value)]50fn get_snapshot(simulation: tauri::State<'_, SharedSimulation>) -> Result<Snapshot, String> {51 simulation52 .lock()53 .map(|state| state.snapshot())54 .map_err(|_| "simulation lock is unavailable".to_owned())55}5657#[tauri::command]58#[allow(clippy::needless_pass_by_value)]59fn set_running(60 running: bool,61 simulation: tauri::State<'_, SharedSimulation>,62) -> Result<Snapshot, String> {63 let mut state = simulation64 .lock()65 .map_err(|_| "simulation lock is unavailable".to_owned())?;66 state.set_running(running);67 Ok(state.snapshot())68}6970#[tauri::command]71#[allow(clippy::needless_pass_by_value)]72fn reset_simulation(simulation: tauri::State<'_, SharedSimulation>) -> Result<Snapshot, String> {73 let mut state = simulation74 .lock()75 .map_err(|_| "simulation lock is unavailable".to_owned())?;76 state.reset();77 Ok(state.snapshot())78}7980fn install_tray(app: &tauri::App) -> tauri::Result<()> {81 let show = MenuItem::with_id(app, "show", "Show rustbar", true, None::<&str>)?;82 let quit = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;83 let menu = Menu::with_items(app, &[&show, &quit])?;8485 TrayIconBuilder::with_id("rustbar")86 .icon(tray_image())87 .tooltip("rustbar · physics lab")88 .menu(&menu)89 .on_menu_event(|app, event| match event.id().as_ref() {90 "show" => {91 if let Some(window) = app.get_webview_window("main") {92 let _ = window.show();93 let _ = window.set_focus();94 }95 }96 "quit" => app.exit(0),97 _ => {}98 })99 .build(app)?;100 Ok(())101}102103fn tray_image() -> tauri::image::Image<'static> {104 let mut pixels = vec![0_u8; 32 * 32 * 4];105 for y in 0_i32..32 {106 for x in 0_i32..32 {107 let dx = f64::from(x) - 15.5;108 let dy = f64::from(y) - 15.5;109 let distance = dx.hypot(dy);110 let offset =111 usize::try_from((y * 32 + x) * 4).expect("tray pixel index must fit in memory");112 if (8.0..=14.0).contains(&distance) {113 pixels[offset..offset + 4].copy_from_slice(&[105, 210, 231, 255]);114 } else if distance < 4.0 {115 pixels[offset..offset + 4].copy_from_slice(&[246, 189, 96, 255]);116 }117 }118 }119 tauri::image::Image::new_owned(pixels, 32, 32)120}121122fn start_simulation_loop(app: tauri::AppHandle, simulation: SharedSimulation) {123 thread::spawn(move || {124 loop {125 thread::sleep(Duration::from_millis(25));126 let (snapshot, collisions) = match simulation.lock() {127 Ok(mut state) => {128 let collisions = state.step(0.025);129 (state.snapshot(), collisions)130 }131 Err(_) => continue,132 };133 let _ = app.emit("simulation-update", &snapshot);134 for collision in collisions {135 emit_collision(&app, &collision);136 }137 }138 });139}140141fn emit_collision(app: &tauri::AppHandle, collision: &Collision) {142 let _ = app.emit("collision", collision);143}