diff --git a/src/lib.rs b/src/lib.rs index e60655f..4182f4c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,8 +59,21 @@ pub mod prelude { }; } +/// The main screen abstraction for rendering and managing widgets and extensions. +/// +/// `Screen` holds the root widget list and registered extensions. It provides methods +/// for drawing elements, adding extensions, and running the main rendering loop. +/// +/// # Examples +/// ```rust +/// let screen = Screen::new(); +/// rsx! { "Hello" }.draw(&screen); +/// screen.run()?; +/// ``` pub struct Screen { + /// The list of widgets currently managed by the screen. pub widgets: Mutex>>, + /// Registered extensions for the screen. extensions: Mutex>>>>, } @@ -68,6 +81,10 @@ event!(RenderWrapperEvent(*mut RenderScope)); component!(NoRender); impl RenderWrapperEvent { + /// Returns a mutable reference to the underlying `RenderScope`. + /// + /// # Safety + /// The caller must ensure the pointer is valid for the lifetime of the event. pub fn get_scope(&self) -> &mut RenderScope { unsafe { &mut *self.0 } } @@ -77,6 +94,7 @@ unsafe impl Send for RenderWrapperEvent {} unsafe impl Sync for RenderWrapperEvent {} impl Screen { + /// Creates a new screen instance wrapped in an `Arc`. pub fn new() -> Arc { Arc::new(Self { widgets: Mutex::new(Vec::new()), @@ -84,22 +102,26 @@ impl Screen { }) } + /// Draws a static element and returns its widget handle. pub fn draw(self: &Arc, element: E) -> Arc { let w = Arc::new(Widget::Static(StaticWidget::new(Box::new(element)))); self.widgets.lock().unwrap().push(w.clone()); w } + /// Draws a boxed element and returns its widget handle. pub fn draw_box(self: &Arc, element: BoxedElement) -> Arc { let w = Arc::new(Widget::Static(StaticWidget::new(element))); self.widgets.lock().unwrap().push(w.clone()); w } + /// Adds an existing widget to the screen. pub fn draw_widget(self: &Arc, widget: Arc) { self.widgets.lock().unwrap().push(widget); } + /// Draws a dynamic element using a closure and returns its widget handle. pub fn draw_dyn WidgetLoad + 'static + Send + Sync>( self: &Arc, element: F, @@ -109,6 +131,7 @@ impl Screen { w } + /// Draws a dynamic element from a boxed closure and returns its widget handle. pub fn draw_box_dyn( self: &Arc, element: Box WidgetLoad + Send + Sync>, @@ -118,6 +141,7 @@ impl Screen { w } + /// Registers an extension with the screen. pub fn extension(self: &Arc, ext: E) { self.extensions .lock() @@ -125,6 +149,9 @@ impl Screen { .push(Arc::new(Mutex::new(Box::new(ext)))); } + /// Runs the main rendering loop, calling extensions and rendering widgets. + /// + /// This method blocks and repeatedly renders the screen at a fixed interval. pub fn run(self: &Arc) -> std::io::Result<()> { for ext in self.extensions.lock().unwrap().iter() { ext.lock().unwrap().init(self.clone()); @@ -138,6 +165,9 @@ impl Screen { } } + /// Renders all widgets and applies extensions. + /// + /// This method is called internally by `run`. pub fn render(self: &Arc) -> std::io::Result<()> { let mut scope = RenderScope::new(); let (w, h) = crossterm::terminal::size().unwrap();