#![warn(missing_docs)]
extern crate alloc;
use crate::api::PlatformError;
use crate::lengths::LogicalLength;
use crate::Coord;
use crate::SharedString;
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::format;
pub use euclid;
pub type Rect = euclid::default::Rect<Coord>;
pub type IntRect = euclid::default::Rect<i32>;
pub type Point = euclid::default::Point2D<Coord>;
pub type Size = euclid::default::Size2D<Coord>;
pub type IntSize = euclid::default::Size2D<u32>;
pub type Transform = euclid::default::Transform2D<Coord>;
pub(crate) mod color;
pub use color::*;
#[cfg(feature = "std")]
mod path;
#[cfg(feature = "std")]
pub use path::*;
mod brush;
pub use brush::*;
pub(crate) mod image;
pub use self::image::*;
pub(crate) mod bitmapfont;
pub use self::bitmapfont::*;
pub mod rendering_metrics_collector;
#[cfg(feature = "box-shadow-cache")]
pub mod boxshadowcache;
pub mod border_radius;
pub use border_radius::*;
pub struct CachedGraphicsData<T> {
pub data: T,
pub dependency_tracker: Option<core::pin::Pin<Box<crate::properties::PropertyTracker>>>,
}
impl<T> CachedGraphicsData<T> {
pub fn new(update_fn: impl FnOnce() -> T) -> Self {
let dependency_tracker = Box::pin(crate::properties::PropertyTracker::default());
let data = dependency_tracker.as_ref().evaluate(update_fn);
Self { data, dependency_tracker: Some(dependency_tracker) }
}
}
pub struct RenderingCache<T> {
slab: slab::Slab<CachedGraphicsData<T>>,
generation: usize,
}
impl<T> Default for RenderingCache<T> {
fn default() -> Self {
Self { slab: Default::default(), generation: 1 }
}
}
impl<T> RenderingCache<T> {
pub fn generation(&self) -> usize {
self.generation
}
pub fn get_mut(&mut self, index: usize) -> Option<&mut CachedGraphicsData<T>> {
self.slab.get_mut(index)
}
pub fn contains(&self, index: usize) -> bool {
self.slab.contains(index)
}
pub fn insert(&mut self, data: CachedGraphicsData<T>) -> usize {
self.slab.insert(data)
}
pub fn get(&self, index: usize) -> Option<&CachedGraphicsData<T>> {
self.slab.get(index)
}
pub fn remove(&mut self, index: usize) -> CachedGraphicsData<T> {
self.slab.remove(index)
}
pub fn clear(&mut self) {
self.slab.clear();
self.generation += 1;
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct FontRequest {
pub family: Option<SharedString>,
pub weight: Option<i32>,
pub pixel_size: Option<LogicalLength>,
pub letter_spacing: Option<LogicalLength>,
pub italic: bool,
}
#[cfg(feature = "shared-fontdb")]
impl FontRequest {
pub fn to_fontdb_query(&self) -> i_slint_common::sharedfontdb::fontdb::Query<'_> {
use i_slint_common::sharedfontdb::fontdb::{Query, Style, Weight};
Query {
style: if self.italic { Style::Italic } else { Style::Normal },
weight: Weight(self.weight.unwrap_or( 400) as _),
..Default::default()
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum RequestedOpenGLVersion {
OpenGL(Option<(u8, u8)>),
OpenGLES(Option<(u8, u8)>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum RequestedGraphicsAPI {
OpenGL(RequestedOpenGLVersion),
Metal,
Vulkan,
Direct3D,
}
impl TryFrom<RequestedGraphicsAPI> for RequestedOpenGLVersion {
type Error = PlatformError;
fn try_from(requested_graphics_api: RequestedGraphicsAPI) -> Result<Self, Self::Error> {
match requested_graphics_api {
RequestedGraphicsAPI::OpenGL(requested_open_glversion) => Ok(requested_open_glversion),
RequestedGraphicsAPI::Metal => {
Err(format!("Metal rendering is not supported with an OpenGL renderer").into())
}
RequestedGraphicsAPI::Vulkan => {
Err(format!("Vulkan rendering is not supported with an OpenGL renderer").into())
}
RequestedGraphicsAPI::Direct3D => {
Err(format!("Direct3D rendering is not supported with an OpenGL renderer").into())
}
}
}
}
impl From<RequestedOpenGLVersion> for RequestedGraphicsAPI {
fn from(version: RequestedOpenGLVersion) -> Self {
Self::OpenGL(version)
}
}
#[cfg(feature = "ffi")]
pub mod ffi {
#![allow(unsafe_code)]
#[cfg(cbindgen)]
#[repr(C)]
struct Rect {
x: f32,
y: f32,
width: f32,
height: f32,
}
#[cfg(cbindgen)]
#[repr(C)]
struct IntRect {
x: i32,
y: i32,
width: i32,
height: i32,
}
#[cfg(cbindgen)]
#[repr(C)]
struct Point {
x: f32,
y: f32,
}
#[cfg(cbindgen)]
#[repr(C)]
struct Box2D<T, U> {
min: euclid::Point2D<T>,
max: euclid::Point2D<T>,
_unit: std::marker::PhantomData<U>,
}
#[cfg(feature = "std")]
pub use super::path::ffi::*;
pub fn physical_size_from_api(
size: crate::api::PhysicalSize,
) -> crate::graphics::euclid::default::Size2D<u32> {
size.to_euclid()
}
pub fn physical_position_from_api(
position: crate::api::PhysicalPosition,
) -> crate::graphics::euclid::default::Point2D<i32> {
position.to_euclid()
}
pub fn physical_position_to_api(
position: crate::graphics::euclid::default::Point2D<i32>,
) -> crate::api::PhysicalPosition {
crate::api::PhysicalPosition::from_euclid(position)
}
}