1use alloc::boxed::Box;
5use alloc::rc::Rc;
6use core::pin::Pin;
7
8use crate::api::PlatformError;
9use crate::graphics::{Rgba8Pixel, SharedPixelBuffer};
10use crate::item_tree::ItemTreeRef;
11use crate::items::{ItemRc, TextWrap};
12use crate::lengths::{LogicalLength, LogicalPoint, LogicalRect, LogicalSize, ScaleFactor};
13use crate::window::WindowAdapter;
14
15#[must_use]
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum DrawOutcome {
21 Success,
22 Occluded,
23 Timeout,
24}
25
26pub trait Renderer: RendererSealed {}
32impl<T: RendererSealed> Renderer for T {}
33
34pub trait RendererSealed {
38 fn text_size(
42 &self,
43 text_item: Pin<&dyn crate::item_rendering::RenderString>,
44 item_rc: &crate::item_tree::ItemRc,
45 max_width: Option<LogicalLength>,
46 text_wrap: TextWrap,
47 ) -> LogicalSize;
48
49 fn char_size(
51 &self,
52 text_item: Pin<&dyn crate::item_rendering::HasFont>,
53 item_rc: &crate::item_tree::ItemRc,
54 ch: char,
55 ) -> LogicalSize;
56
57 fn font_metrics(&self, font_request: crate::graphics::FontRequest)
59 -> crate::items::FontMetrics;
60
61 fn text_input_byte_offset_for_position(
66 &self,
67 text_input: Pin<&crate::items::TextInput>,
68 item_rc: &ItemRc,
69 pos: LogicalPoint,
70 ) -> usize;
71
72 fn text_input_cursor_rect_for_byte_offset(
76 &self,
77 text_input: Pin<&crate::items::TextInput>,
78 item_rc: &ItemRc,
79 byte_offset: usize,
80 ) -> LogicalRect;
81
82 fn free_graphics_resources(
84 &self,
85 _component: ItemTreeRef,
86 _items: &mut dyn Iterator<Item = Pin<crate::items::ItemRef<'_>>>,
87 ) -> Result<(), crate::platform::PlatformError> {
88 Ok(())
89 }
90
91 fn mark_dirty_region(&self, _region: crate::partial_renderer::DirtyRegion) {}
95
96 #[cfg(feature = "std")] fn register_font_from_memory(
101 &self,
102 _data: &'static [u8],
103 ) -> Result<(), Box<dyn std::error::Error>> {
104 Err("This renderer does not support registering custom fonts.".into())
105 }
106
107 #[cfg(feature = "std")]
108 fn register_font_from_path(
112 &self,
113 _path: &std::path::Path,
114 ) -> Result<(), Box<dyn std::error::Error>> {
115 Err("This renderer does not support registering custom fonts.".into())
116 }
117
118 fn register_bitmap_font(&self, _font_data: &'static crate::graphics::BitmapFont) {
119 crate::debug_log!(
120 "Internal error: The current renderer cannot load fonts build with the `EmbedForSoftwareRenderer` option. Please use the software Renderer, or disable that option when building your slint files"
121 );
122 }
123
124 fn set_rendering_notifier(
127 &self,
128 _callback: Box<dyn crate::api::RenderingNotifier>,
129 ) -> Result<(), crate::api::SetRenderingNotifierError> {
130 Err(crate::api::SetRenderingNotifierError::Unsupported)
131 }
132
133 fn set_window_adapter(&self, _window_adapter: &Rc<dyn WindowAdapter>);
134
135 fn window_adapter(&self) -> Option<Rc<dyn WindowAdapter>>;
136
137 fn scale_factor(&self) -> Option<ScaleFactor> {
138 self.window_adapter()
139 .map(|window_adapter| ScaleFactor::new(window_adapter.window().scale_factor()))
140 }
141
142 #[cfg(feature = "shared-parley")]
143 fn slint_context(&self) -> Option<crate::SlintContext> {
144 self.window_adapter()
145 .map(|wa| crate::window::WindowInner::from_pub(wa.window()).context().clone())
146 }
147
148 fn resize(&self, _size: crate::api::PhysicalSize) -> Result<(), PlatformError> {
149 Ok(())
150 }
151
152 fn take_snapshot(&self) -> Result<SharedPixelBuffer<Rgba8Pixel>, PlatformError> {
155 Err("WindowAdapter::take_snapshot is not implemented by the platform".into())
156 }
157
158 fn supports_transformations(&self) -> bool;
160}