Skip to main content

EventLoop

Struct EventLoop 

Source
pub struct EventLoop<'a> { /* private fields */ }
Expand description

Runs mounted components until every one has finished. See the module docs.

Implementations§

Source§

impl<'a> EventLoop<'a>

Source

pub fn new(backend: impl Backend + 'a, options: LoopOptions) -> EventLoop<'a>

Source

pub fn terminal( session: SessionOptions, options: LoopOptions, ) -> Result<EventLoop<'a>>

A loop on the real terminal, starting a Session.

Source

pub fn mount<C: Component + 'a>(&mut self, component: C) -> Handle<C::Output>

Add a component. Its outcome is in the handle once it finishes.

Source

pub fn graphics(&mut self, source: Arc<dyn PlacementSource>)

Draw the graphics source places on the views’ cells (micro assets through Kitty, iTerm2 or Sixel). The loop wakes when an animation among them shows its next frame. Give it only on a terminal that showed it can draw them.

Source

pub fn every(&mut self, interval: Duration, callback: impl FnMut() -> bool + 'a)

Call callback every interval while the loop runs, until it returns false.

Source

pub fn run(&mut self) -> Result<()>

Run until every mounted component has finished. Ctrl+C finishes all of them as interrupted.

Source

pub fn step(&mut self, event: Event) -> Result<bool>

Push driving, for a host that cannot block in Backend::read (a browser tab, another program’s event loop): deliver event, as run does with one it read, then fire the ticks and timers due by the backend’s elapsed time and paint. Returns whether a component is still running. The first call (of this or pump) starts the components and paints them first; once none is running, events are dropped. Call finish at the end.

Steps give the same results as run given the same events at the same times. PushBackend is a backend made for it: the host sets its time and takes what was painted.

use rich_interact::{EventLoop, Event, Key, KeyCode, LoopOptions, Outcome, PushBackend, Select};

let backend = PushBackend::new(30, 6);
let port = backend.port();
let mut event_loop = EventLoop::new(backend, LoopOptions::default());
let handle = event_loop.mount(Select::new("Pick", vec!["red".to_string(), "green".to_string()]));
event_loop.pump().unwrap();
assert!(port.take_output().contains("red"));
event_loop.step(Event::Key(Key::new(KeyCode::Down))).unwrap();
let running = event_loop.step(Event::Key(Key::new(KeyCode::Enter))).unwrap();
assert!(!running);
event_loop.finish().unwrap();
assert_eq!(handle.take(), Some(Outcome::Done("green".to_string())));
Source

pub fn pump(&mut self) -> Result<bool>

Push driving without an event: the turn run takes when a read times out. Fires the ticks and timers due by the backend’s time, starts what was mounted, and paints; returns whether a component is still running. Call it when timeout has passed (after moving the backend’s clock), and once at the start to paint the first view.

Source

pub fn timeout(&self) -> Option<Duration>

How long until a tick or timer is due, by the backend’s time: when to pump next. None: only an event changes anything.

Source

pub fn is_running(&self) -> bool

Whether a mounted component is still running.

Source

pub fn finish(&mut self) -> Result<()>

End painting: leave the last view (or clear it, when transient) and give the cursor back. run calls it; a host driving the loop with step calls it once at the end.

Auto Trait Implementations§

§

impl<'a> !Freeze for EventLoop<'a>

§

impl<'a> !RefUnwindSafe for EventLoop<'a>

§

impl<'a> !Send for EventLoop<'a>

§

impl<'a> !Sync for EventLoop<'a>

§

impl<'a> !UnwindSafe for EventLoop<'a>

§

impl<'a> Unpin for EventLoop<'a>

§

impl<'a> UnsafeUnpin for EventLoop<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.