use alloc::boxed::Box;
use alloc::{format, string::String};
use i_slint_core::api::PlatformError;
use i_slint_core::graphics::{RequestedGraphicsAPI, RequestedOpenGLVersion};
#[i_slint_core_macros::slint_doc]
pub struct BackendSelector {
requested_graphics_api: Option<RequestedGraphicsAPI>,
backend: Option<String>,
renderer: Option<String>,
selected: bool,
}
impl BackendSelector {
#[must_use]
pub fn new() -> BackendSelector {
BackendSelector {
requested_graphics_api: None,
backend: None,
renderer: None,
selected: false,
}
}
#[must_use]
pub fn require_opengl_es_with_version(mut self, major: u8, minor: u8) -> Self {
self.requested_graphics_api =
Some(RequestedOpenGLVersion::OpenGLES(Some((major, minor))).into());
self
}
#[must_use]
pub fn require_opengl_es(mut self) -> Self {
self.requested_graphics_api = Some(RequestedOpenGLVersion::OpenGLES(None).into());
self
}
#[must_use]
pub fn require_opengl(mut self) -> Self {
self.requested_graphics_api = Some(RequestedOpenGLVersion::OpenGL(None).into());
self
}
#[must_use]
pub fn require_opengl_with_version(mut self, major: u8, minor: u8) -> Self {
self.requested_graphics_api =
Some(RequestedOpenGLVersion::OpenGL(Some((major, minor))).into());
self
}
#[must_use]
pub fn require_metal(mut self) -> Self {
self.requested_graphics_api = Some(RequestedGraphicsAPI::Metal);
self
}
#[must_use]
pub fn require_vulkan(mut self) -> Self {
self.requested_graphics_api = Some(RequestedGraphicsAPI::Vulkan);
self
}
#[must_use]
pub fn require_d3d(mut self) -> Self {
self.requested_graphics_api = Some(RequestedGraphicsAPI::Direct3D);
self
}
#[must_use]
pub fn renderer_name(mut self, name: String) -> Self {
self.renderer = Some(name);
self
}
#[must_use]
pub fn backend_name(mut self, name: String) -> Self {
self.backend = Some(name);
self
}
pub fn select(mut self) -> Result<(), PlatformError> {
self.select_internal()
}
fn select_internal(&mut self) -> Result<(), PlatformError> {
self.selected = true;
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 {
#[cfg(all(feature = "i-slint-backend-linuxkms", target_os = "linux"))]
"linuxkms" => {
if self.requested_graphics_api.is_some() {
return Err("The linuxkms backend does not implement renderer selection by graphics API".into());
}
Box::new(i_slint_backend_linuxkms::Backend::new_with_renderer_by_name(
self.renderer.as_deref(),
)?)
}
#[cfg(feature = "i-slint-backend-winit")]
"winit" => {
let builder = i_slint_backend_winit::Backend::builder();
let builder = match self.requested_graphics_api.as_ref() {
Some(api) => builder.request_graphics_api(api.clone()),
None => builder,
};
let builder = match self.renderer.as_ref() {
Some(name) => builder.with_renderer_name(name),
None => builder,
};
Box::new(builder.build()?)
}
#[cfg(feature = "i-slint-backend-qt")]
"qt" => {
if self.requested_graphics_api.is_some() {
return Err(
"The qt backend does not implement renderer selection by graphics API"
.into(),
);
}
if self.renderer.is_some() {
return Err(
"The qt backend does not implement renderer selection by name".into()
);
}
Box::new(i_slint_backend_qt::Backend::new())
}
requested_backend @ _ => {
return Err(format!(
"{requested_backend} backend requested but it is not available"
)
.into());
}
};
i_slint_core::platform::set_platform(backend)
.map_err(|set_platform_error| PlatformError::SetPlatformError(set_platform_error))
}
}
impl Drop for BackendSelector {
fn drop(&mut self) {
if !self.selected {
self.select_internal().unwrap();
}
}
}