pub struct App { /* private fields */ }
Expand description
The application runtime.
The runtime encapsulates the whole state of the application. It performs layout calculations and draws the component tree to a canvas. The runtime owns all the components and manages their lifetime. It creates components when first mounted and it is responsible for delivering messages, input and layout events.
The runtime itself does not run an event loop. This is delegated to a
particular backend implementation to allow for maximum flexibility. For
instance, the crossterm
terminal backend implements an event loop using
tokio channels for component message + mio for stdin input and resize
events.
Note: This is a low-level struct useful when you are implementing a backend or for testing your application. For an end user application, you would normally use a backend that wraps an App in an event loop, see the examples.
Implementations
sourceimpl App
impl App
sourcepub fn new(sender: impl MessageSender, size: Size, root: Layout) -> Self
pub fn new(sender: impl MessageSender, size: Size, root: Layout) -> Self
Creates a new application runtime
To instantiate a new runtime you need three things
- a
MessageSender
responsible for delivering messages sent by components usingComponentLink
- the
size
of the initial canvas - the root
Layout
that will be rendered
use std::sync::mpsc;
use zi::{
app::{App, ComponentMessage, MessageSender},
components::text::{Text, TextProperties},
prelude::*,
};
#[derive(Clone, Debug)]
struct MessageQueue(mpsc::Sender<ComponentMessage>);
impl MessageSender for MessageQueue {
fn send(&self, message: ComponentMessage) {
self.0.send(message).unwrap();
}
fn clone_box(&self) -> Box<dyn MessageSender> {
Box::new(self.clone())
}
}
let (sender, receiver) = mpsc::channel();
let message_queue = MessageQueue(sender);
let mut app = App::new(
message_queue,
Size::new(10, 10),
Text::with(TextProperties::new().content("Hello")),
);
loop {
// Deliver component messages. This would block forever as no component
// sends any messages.
let message = receiver.recv().unwrap();
app.handle_message(message);
app.handle_resize(Size::new(20, 20));
// Draw
let canvas = app.draw();
eprintln!("{}", canvas);
}
sourcepub fn poll_state(&self) -> PollState
pub fn poll_state(&self) -> PollState
Return the application’s poll state
sourcepub fn is_tickable(&mut self) -> bool
pub fn is_tickable(&mut self) -> bool
Return true
if any components currently mounted are tickable
sourcepub fn draw(&mut self) -> &Canvas
pub fn draw(&mut self) -> &Canvas
Compute component layout and draw the application to a canvas
This function flushes all pending changes to the component tree,
computes the layout and redraws components where needed. After calling this
function poll_state()
will be PollState::Clean
sourcepub fn handle_resize(&mut self, size: Size)
pub fn handle_resize(&mut self, size: Size)
Resizes the application canvas. This operation is lazy and the mounted
components won’t be notified until draw
is called.
pub fn handle_message(&mut self, message: ComponentMessage)
pub fn handle_input(&mut self, event: Event)
Auto Trait Implementations
impl !RefUnwindSafe for App
impl !Send for App
impl !Sync for App
impl Unpin for App
impl !UnwindSafe for App
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more