1#![warn(missing_docs)]
5extern crate alloc;
14use crate::api::PlatformError;
15use crate::lengths::LogicalLength;
16use crate::Coord;
17use crate::SharedString;
18use alloc::boxed::Box;
19
20pub use euclid;
21pub type Rect = euclid::default::Rect<Coord>;
23pub type IntRect = euclid::default::Rect<i32>;
25pub type Point = euclid::default::Point2D<Coord>;
27pub type Size = euclid::default::Size2D<Coord>;
29pub type IntSize = euclid::default::Size2D<u32>;
31pub type Transform = euclid::default::Transform2D<Coord>;
33
34pub(crate) mod color;
35pub use color::*;
36
37#[cfg(feature = "std")]
38mod path;
39#[cfg(feature = "std")]
40pub use path::*;
41
42mod brush;
43pub use brush::*;
44
45pub(crate) mod image;
46pub use self::image::*;
47
48pub(crate) mod bitmapfont;
49pub use self::bitmapfont::*;
50
51pub mod rendering_metrics_collector;
52
53#[cfg(feature = "box-shadow-cache")]
54pub mod boxshadowcache;
55
56pub mod border_radius;
57pub use border_radius::*;
58
59#[cfg(feature = "unstable-wgpu-24")]
60pub mod wgpu_24;
61
62pub struct CachedGraphicsData<T> {
67 pub data: T,
69 pub dependency_tracker: Option<core::pin::Pin<Box<crate::properties::PropertyTracker>>>,
72}
73
74impl<T> CachedGraphicsData<T> {
75 pub fn new(update_fn: impl FnOnce() -> T) -> Self {
78 let dependency_tracker = Box::pin(crate::properties::PropertyTracker::default());
79 let data = dependency_tracker.as_ref().evaluate(update_fn);
80 Self { data, dependency_tracker: Some(dependency_tracker) }
81 }
82}
83
84pub struct RenderingCache<T> {
90 slab: slab::Slab<CachedGraphicsData<T>>,
91 generation: usize,
92}
93
94impl<T> Default for RenderingCache<T> {
95 fn default() -> Self {
96 Self { slab: Default::default(), generation: 1 }
97 }
98}
99
100impl<T> RenderingCache<T> {
101 pub fn generation(&self) -> usize {
104 self.generation
105 }
106
107 pub fn get_mut(&mut self, index: usize) -> Option<&mut CachedGraphicsData<T>> {
109 self.slab.get_mut(index)
110 }
111
112 pub fn contains(&self, index: usize) -> bool {
114 self.slab.contains(index)
115 }
116
117 pub fn insert(&mut self, data: CachedGraphicsData<T>) -> usize {
119 self.slab.insert(data)
120 }
121
122 pub fn get(&self, index: usize) -> Option<&CachedGraphicsData<T>> {
124 self.slab.get(index)
125 }
126
127 pub fn remove(&mut self, index: usize) -> CachedGraphicsData<T> {
129 self.slab.remove(index)
130 }
131
132 pub fn clear(&mut self) {
135 self.slab.clear();
136 self.generation += 1;
137 }
138}
139#[derive(Debug, Clone, PartialEq, Default)]
143pub struct FontRequest {
144 pub family: Option<SharedString>,
147 pub weight: Option<i32>,
149 pub pixel_size: Option<LogicalLength>,
151 pub letter_spacing: Option<LogicalLength>,
154 pub italic: bool,
156}
157
158#[cfg(feature = "shared-fontdb")]
159impl FontRequest {
160 pub fn to_fontdb_query(&self) -> i_slint_common::sharedfontdb::fontdb::Query<'_> {
162 use i_slint_common::sharedfontdb::fontdb::{Query, Style, Weight};
163 Query {
164 style: if self.italic { Style::Italic } else { Style::Normal },
165 weight: Weight(self.weight.unwrap_or(400) as _),
166 ..Default::default()
167 }
168 }
169}
170
171#[derive(Debug, Clone, PartialEq)]
174pub enum RequestedOpenGLVersion {
175 OpenGL(Option<(u8, u8)>),
177 OpenGLES(Option<(u8, u8)>),
179}
180
181#[derive(Debug, Clone)]
184pub enum RequestedGraphicsAPI {
185 OpenGL(RequestedOpenGLVersion),
187 Metal,
189 Vulkan,
191 Direct3D,
193 #[cfg(feature = "unstable-wgpu-24")]
194 WGPU24(wgpu_24::WGPUConfiguration),
196}
197
198impl TryFrom<RequestedGraphicsAPI> for RequestedOpenGLVersion {
199 type Error = PlatformError;
200
201 fn try_from(requested_graphics_api: RequestedGraphicsAPI) -> Result<Self, Self::Error> {
202 match requested_graphics_api {
203 RequestedGraphicsAPI::OpenGL(requested_open_glversion) => Ok(requested_open_glversion),
204 RequestedGraphicsAPI::Metal => {
205 Err("Metal rendering is not supported with an OpenGL renderer".into())
206 }
207 RequestedGraphicsAPI::Vulkan => {
208 Err("Vulkan rendering is not supported with an OpenGL renderer".into())
209 }
210 RequestedGraphicsAPI::Direct3D => {
211 Err("Direct3D rendering is not supported with an OpenGL renderer".into())
212 }
213 #[cfg(feature = "unstable-wgpu-24")]
214 RequestedGraphicsAPI::WGPU24(..) => {
215 Err("WGPU 24.x rendering is not supported with an OpenGL renderer".into())
216 }
217 }
218 }
219}
220
221impl From<RequestedOpenGLVersion> for RequestedGraphicsAPI {
222 fn from(version: RequestedOpenGLVersion) -> Self {
223 Self::OpenGL(version)
224 }
225}
226
227#[cfg(feature = "unstable-wgpu-24")]
230pub fn create_graphics_api_wgpu_24(
231 instance: wgpu_24::wgpu::Instance,
232 device: wgpu_24::wgpu::Device,
233 queue: wgpu_24::wgpu::Queue,
234) -> crate::api::GraphicsAPI<'static> {
235 crate::api::GraphicsAPI::WGPU24 { instance, device, queue }
236}
237
238#[cfg(feature = "ffi")]
240pub mod ffi {
241 #![allow(unsafe_code)]
242
243 #[cfg(cbindgen)]
245 #[repr(C)]
246 struct Rect {
247 x: f32,
248 y: f32,
249 width: f32,
250 height: f32,
251 }
252
253 #[cfg(cbindgen)]
255 #[repr(C)]
256 struct IntRect {
257 x: i32,
258 y: i32,
259 width: i32,
260 height: i32,
261 }
262
263 #[cfg(cbindgen)]
265 #[repr(C)]
266 struct Point {
267 x: f32,
268 y: f32,
269 }
270
271 #[cfg(cbindgen)]
273 #[repr(C)]
274 struct Box2D<T, U> {
275 min: euclid::Point2D<T>,
276 max: euclid::Point2D<T>,
277 _unit: std::marker::PhantomData<U>,
278 }
279
280 #[cfg(feature = "std")]
281 pub use super::path::ffi::*;
282
283 pub fn physical_size_from_api(
287 size: crate::api::PhysicalSize,
288 ) -> crate::graphics::euclid::default::Size2D<u32> {
289 size.to_euclid()
290 }
291
292 pub fn physical_position_from_api(
296 position: crate::api::PhysicalPosition,
297 ) -> crate::graphics::euclid::default::Point2D<i32> {
298 position.to_euclid()
299 }
300
301 pub fn physical_position_to_api(
304 position: crate::graphics::euclid::default::Point2D<i32>,
305 ) -> crate::api::PhysicalPosition {
306 crate::api::PhysicalPosition::from_euclid(position)
307 }
308}