BackendSelector: respect the SLINT_BACKEND env variable

... if the backend is not explicitly selected
This commit is contained in:
Olivier Goffart 2025-05-10 12:39:38 +02:00 committed by GitHub
parent 3e94d81ba0
commit 99b7b40b6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 10 deletions

View file

@ -194,6 +194,23 @@ impl BackendSelector {
fn select_internal(&mut self) -> Result<(), PlatformError> {
self.selected = true;
#[cfg(any(
feature = "i-slint-backend-qt",
feature = "i-slint-backend-winit",
feature = "i-slint-backend-linuxkms"
))]
if self.backend.is_none() || self.renderer.is_none() {
let backend_config = std::env::var("SLINT_BACKEND").unwrap_or_default();
let backend_config = backend_config.to_lowercase();
let (backend, renderer) = super::parse_backend_env_var(backend_config.as_str());
if !backend.is_empty() {
self.backend.get_or_insert_with(|| backend.to_owned());
}
if !renderer.is_empty() {
self.renderer.get_or_insert_with(|| renderer.to_owned());
}
}
let backend_name = self.backend.as_deref().unwrap_or(super::DEFAULT_BACKEND_NAME);
let backend: Box<dyn i_slint_core::platform::Platform> = match backend_name {

View file

@ -92,17 +92,8 @@ cfg_if::cfg_if! {
pub fn create_backend() -> Result<Box<dyn Platform + 'static>, PlatformError> {
let backend_config = std::env::var("SLINT_BACKEND").unwrap_or_default();
let backend_config = backend_config.to_lowercase();
let (event_loop, _renderer) = backend_config.split_once('-').unwrap_or(match backend_config.as_str() {
"qt" => ("qt", ""),
"gl" | "winit" => ("winit", ""),
"femtovg" => ("winit", "femtovg"),
"skia" => ("winit", "skia"),
"sw" | "software" => ("winit", "software"),
"linuxkms" => ("linuxkms", ""),
x => (x, ""),
});
let (event_loop, _renderer) = parse_backend_env_var(backend_config.as_str());
match event_loop {
#[cfg(all(feature = "i-slint-backend-qt", not(no_qt)))]
@ -133,6 +124,18 @@ cfg_if::cfg_if! {
}
}
pub fn parse_backend_env_var(backend_config: &str) -> (&str, &str) {
backend_config.split_once('-').unwrap_or(match backend_config {
"qt" => ("qt", ""),
"gl" | "winit" => ("winit", ""),
"femtovg" => ("winit", "femtovg"),
"skia" => ("winit", "skia"),
"sw" | "software" => ("winit", "software"),
"linuxkms" => ("linuxkms", ""),
x => (x, ""),
})
}
/// Run the callback with the platform abstraction.
/// Create the backend if it does not exist yet
pub fn with_platform<R>(