Add: global factor controlling fps base interval

This commit is contained in:
DaZuo0122
2026-02-15 09:40:51 +08:00
parent e5417b6799
commit f20ed1fd9d
12 changed files with 289 additions and 52 deletions

View File

@@ -159,6 +159,7 @@ pub enum FrontendBackend {
pub struct FrontendConfig {
pub backend: FrontendBackend,
pub debug_overlay_visible: bool,
pub tauri_animation_slowdown_factor: u8,
}
impl Default for FrontendConfig {
@@ -166,6 +167,7 @@ impl Default for FrontendConfig {
Self {
backend: FrontendBackend::Bevy,
debug_overlay_visible: false,
tauri_animation_slowdown_factor: 3,
}
}
}
@@ -225,11 +227,13 @@ mod tests {
config.window.x = 42.0;
config.frontend.backend = super::FrontendBackend::Tauri;
config.frontend.debug_overlay_visible = true;
config.frontend.tauri_animation_slowdown_factor = 7;
save(&path, &config).expect("save");
let (_, loaded) = load_or_create_at(&path).expect("reload");
assert!((loaded.window.x - 42.0).abs() < f32::EPSILON);
assert_eq!(loaded.frontend.backend, super::FrontendBackend::Tauri);
assert!(loaded.frontend.debug_overlay_visible);
assert_eq!(loaded.frontend.tauri_animation_slowdown_factor, 7);
}
}

View File

@@ -8,6 +8,9 @@ use tokio::runtime::Runtime;
use tokio::sync::{mpsc, Mutex};
use tracing::warn;
const TAURI_ANIMATION_SLOWDOWN_FACTOR_MIN: u8 = 1;
const TAURI_ANIMATION_SLOWDOWN_FACTOR_MAX: u8 = 20;
#[derive(Debug, Error)]
pub enum RuntimeCoreError {
#[error("{0}")]
@@ -104,6 +107,32 @@ impl RuntimeCore {
self.persist_config()
}
pub fn frontend_tauri_animation_slowdown_factor(&self) -> Result<u8, RuntimeCoreError> {
let guard = self
.config
.read()
.map_err(|_| RuntimeCoreError::ConfigPoisoned)?;
Ok(clamp_tauri_animation_slowdown_factor(
guard.frontend.tauri_animation_slowdown_factor,
))
}
pub fn set_frontend_tauri_animation_slowdown_factor(
&self,
value: u8,
) -> Result<u8, RuntimeCoreError> {
let clamped = clamp_tauri_animation_slowdown_factor(value);
{
let mut guard = self
.config
.write()
.map_err(|_| RuntimeCoreError::ConfigPoisoned)?;
guard.frontend.tauri_animation_slowdown_factor = clamped;
}
self.persist_config()?;
Ok(clamped)
}
pub fn api_config(&self) -> ApiConfig {
self.api_config.clone()
}
@@ -257,6 +286,13 @@ fn default_animation_for_state(state: sprimo_protocol::v1::FrontendState) -> &'s
}
}
fn clamp_tauri_animation_slowdown_factor(value: u8) -> u8 {
value.clamp(
TAURI_ANIMATION_SLOWDOWN_FACTOR_MIN,
TAURI_ANIMATION_SLOWDOWN_FACTOR_MAX,
)
}
fn log_api_error(err: ApiServerError) {
warn!(%err, "runtime core api server exited");
}
@@ -331,4 +367,36 @@ mod tests {
core.set_frontend_debug_overlay_visible(true).expect("set");
assert!(core.frontend_debug_overlay_visible().expect("get"));
}
#[test]
fn frontend_tauri_animation_slowdown_factor_roundtrips() {
let temp = TempDir::new().expect("tempdir");
let path = temp.path().join("config.toml");
let core = RuntimeCore::new_with_config(path, AppConfig::default(), CapabilityFlags::default())
.expect("core init");
let persisted = core
.set_frontend_tauri_animation_slowdown_factor(6)
.expect("set");
assert_eq!(persisted, 6);
assert_eq!(
core.frontend_tauri_animation_slowdown_factor().expect("get"),
6
);
}
#[test]
fn frontend_tauri_animation_slowdown_factor_clamps() {
let temp = TempDir::new().expect("tempdir");
let path = temp.path().join("config.toml");
let core = RuntimeCore::new_with_config(path, AppConfig::default(), CapabilityFlags::default())
.expect("core init");
let persisted = core
.set_frontend_tauri_animation_slowdown_factor(0)
.expect("set");
assert_eq!(persisted, 1);
assert_eq!(
core.frontend_tauri_animation_slowdown_factor().expect("get"),
1
);
}
}

View File

@@ -31,6 +31,7 @@ const MENU_ID_TOGGLE_DEBUG_OVERLAY: &str = "toggle_debug_overlay";
const MENU_ID_QUIT: &str = "quit";
const EVENT_RUNTIME_SNAPSHOT: &str = "runtime:snapshot";
const EVENT_RUNTIME_DEBUG_OVERLAY_VISIBLE: &str = "runtime:debug-overlay-visible";
const EVENT_RUNTIME_ANIMATION_SLOWDOWN_FACTOR: &str = "runtime:animation-slowdown-factor";
#[derive(Debug, Clone, serde::Serialize)]
struct UiAnimationClip {
@@ -80,6 +81,7 @@ struct UiSettingsSnapshot {
scale: f32,
visible: bool,
always_on_top: bool,
tauri_animation_slowdown_factor: u8,
}
#[derive(Debug, Clone, serde::Serialize)]
@@ -183,9 +185,37 @@ fn settings_snapshot(state: tauri::State<'_, AppState>) -> Result<UiSettingsSnap
scale: snapshot.scale,
visible: snapshot.flags.visible,
always_on_top: snapshot.flags.always_on_top,
tauri_animation_slowdown_factor: state
.runtime_core
.frontend_tauri_animation_slowdown_factor()
.map_err(|err| err.to_string())?,
})
}
#[tauri::command]
fn tauri_animation_slowdown_factor(state: tauri::State<'_, AppState>) -> Result<u8, String> {
state
.runtime_core
.frontend_tauri_animation_slowdown_factor()
.map_err(|err| err.to_string())
}
#[tauri::command]
fn set_tauri_animation_slowdown_factor(
app_handle: tauri::AppHandle,
state: tauri::State<'_, AppState>,
factor: u8,
) -> Result<u8, String> {
let persisted = state
.runtime_core
.set_frontend_tauri_animation_slowdown_factor(factor)
.map_err(|err| err.to_string())?;
app_handle
.emit(EVENT_RUNTIME_ANIMATION_SLOWDOWN_FACTOR, persisted)
.map_err(|err| err.to_string())?;
Ok(persisted)
}
#[tauri::command]
fn list_sprite_packs(state: tauri::State<'_, AppState>) -> Result<Vec<UiSpritePackOption>, String> {
let root = sprite_pack_root(state.runtime_core.as_ref())?;
@@ -349,6 +379,8 @@ fn main() -> Result<(), AppError> {
debug_overlay_visible,
set_debug_overlay_visible,
settings_snapshot,
tauri_animation_slowdown_factor,
set_tauri_animation_slowdown_factor,
list_sprite_packs,
set_sprite_pack,
set_scale,