Struct zi::app::App

source · []
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

Creates a new application runtime

To instantiate a new runtime you need three things

  • a MessageSender responsible for delivering messages sent by components using ComponentLink
  • 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);
}

Return the application’s poll state

Return true if any components currently mounted are tickable

Resizes the application’s canvas lazily

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

Resizes the application canvas. This operation is lazy and the mounted components won’t be notified until draw is called.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.