fix(node): prevent 100% CPU usage on Wayland by enforcing minimum event timeout (#7550)

This commit is contained in:
Bedis Nbiba 2025-02-06 17:00:50 +01:00 committed by GitHub
parent 4b8287bd0f
commit 417eaeb38a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,6 +3,7 @@
mod interpreter;
use std::path::PathBuf;
use std::sync::LazyLock;
pub use interpreter::*;
@ -14,6 +15,8 @@ use napi::{Env, JsFunction, JsObject};
#[macro_use]
extern crate napi_derive;
static IS_WAYLAND: LazyLock<bool> = LazyLock::new(|| std::env::var("WAYLAND_DISPLAY").is_ok());
#[napi]
pub fn mock_elapsed_time(ms: f64) {
i_slint_core::tests::slint_mock_elapsed_time(ms as _);
@ -32,8 +35,13 @@ pub enum ProcessEventsResult {
#[napi]
pub fn process_events() -> napi::Result<ProcessEventsResult> {
// Using std::time::Duration::Zero with the winit backend in wayland causes slint to consume 100% of a cpu core
// https://github.com/slint-ui/slint/issues/5780
let min_timeout = std::time::Duration::from_millis(16);
let timeout = if *IS_WAYLAND { min_timeout } else { std::time::Duration::ZERO };
i_slint_backend_selector::with_platform(|b| {
b.process_events(std::time::Duration::ZERO, i_slint_core::InternalToken)
b.process_events(timeout, i_slint_core::InternalToken)
})
.map_err(|e| napi::Error::from_reason(e.to_string()))
.and_then(|result| {