pub struct Engine<S = ()> { /* private fields */ }Expand description
The run loop: owns the window’s widget tree, routes input to Widgets, calls the application’s
callbacks, and draws.
S is the application state type. Engine::run takes ownership of a value of S, passes
it to every callback, and returns it when the Engine stops. Applications that prefer their
own loop use Engine::event_loop and Engine::next_event instead of callbacks.
Implementations§
Source§impl<S> Engine<S>
impl<S> Engine<S>
Sourcepub fn new(size: Size, frame_rate: u32) -> Self
pub fn new(size: Size, frame_rate: u32) -> Self
Creates an Engine for a window of the given size.
A white RootWidget is created as Widget 0 and fills the window.
§Arguments
size- The size of the window in pixels.frame_rate- The maximum number of frames drawn per second (for example30or60).0is treated as1.
§Returns
A new Engine with no Widgets other than the root, and no callbacks.
Sourcepub fn shutdown(&mut self)
pub fn shutdown(&mut self)
Stops the Engine.
The run loop exits after the current batch of events; in the event-loop style,
Engine::next_event returns None.
Sourcepub fn set_event_mask(&mut self, mask: EventMask)
pub fn set_event_mask(&mut self, mask: EventMask)
Sets the run loop’s event mask.
Events whose class is not in the mask are dropped before routing and callbacks, and the
backend is told the mask so it can stop delivering the rest. Close requests are always delivered. The change applies from the next event
and emits EngineEvent::EventMaskChanged with MaskScope::RunLoop.
With EventMask::TIMER left out and nothing to repaint, the loop blocks waiting for
input instead of polling, so an idle application uses almost no CPU.
§Arguments
mask- The classes of events to handle. The default isEventMask::ALL.
§Examples
use pushrod::engine::Engine;
use pushrod::event::EventMask;
use pushrod::geometry::size;
let mut engine: Engine = Engine::new(size(320, 200), 60);
// No hover effects: stop pointer motion at the source.
engine.set_event_mask(EventMask::ALL & !EventMask::POINTER_MOTION & !EventMask::HOVER);
assert!(!engine.event_mask().contains(EventMask::HOVER));Sourcepub fn event_mask(&self) -> EventMask
pub fn event_mask(&self) -> EventMask
Returns the run loop’s event mask.
§Returns
The mask set with Engine::set_event_mask; EventMask::ALL by default.
Sourcepub fn on_timer<F>(
&mut self,
interval: Duration,
repeat: bool,
callback: F,
) -> TimerId
pub fn on_timer<F>( &mut self, interval: Duration, repeat: bool, callback: F, ) -> TimerId
Starts a timer.
The callback runs from the run loop (or Engine::next_event_with) once interval has
passed, within one frame of it. A repeating timer runs again every interval; if the
application falls behind it runs once and keeps its rhythm instead of catching up in a
burst. Each firing emits EngineEvent::TimerFired. The callback receives the
application state and a Ui; call Ui::cancel_timer to stop a timer, even from its
own callback.
§Arguments
interval- The time until the timer fires, and between firings if it repeats.repeat-trueto fire everyinterval;falseto fire once.callback- Runs each time the timer fires.
§Returns
The timer’s TimerId, for cancelling it.
§Examples
use pushrod::engine::Engine;
use pushrod::geometry::size;
use std::time::Duration;
let mut engine: Engine<u32> = Engine::new(size(320, 200), 60);
// Counts seconds; the application reads the count from its state.
let clock = engine.on_timer(Duration::from_secs(1), true, |seconds, _ui| *seconds += 1);
assert!(engine.cancel_timer(clock));Sourcepub fn cancel_timer(&mut self, id: TimerId) -> bool
pub fn cancel_timer(&mut self, id: TimerId) -> bool
Cancels a timer started with Engine::on_timer. Emits EngineEvent::TimerCancelled
when a waiting timer was stopped.
§Arguments
id- The timer to cancel.
§Returns
true if the timer was still scheduled.
Sourcepub fn on_idle<F>(&mut self, callback: F)
pub fn on_idle<F>(&mut self, callback: F)
Registers a callback that runs when the loop is idle: once per loop iteration in which no
events were waiting. Use it for work that should yield to input, such as deferred layout
or incremental loading. While any idle callback is registered the loop polls once per
frame instead of blocking, so keep the callback short. Each run emits
EngineEvent::IdleRan.
§Arguments
callback- Receives the application state and aUi.
§Examples
use pushrod::engine::Engine;
use pushrod::geometry::size;
let mut engine: Engine<Vec<u32>> = Engine::new(size(320, 200), 60);
// Loads one item per idle iteration.
engine.on_idle(|loaded, _ui| loaded.push(loaded.len() as u32));Sourcepub fn advance(&mut self, now: Instant, state: &mut S)
pub fn advance(&mut self, now: Instant, state: &mut S)
Advances the engine’s clock to now: runs the timers that are due, then delivers one
Event::Tick to the Widgets that requested animation, and delivers everything they
cause to the callbacks. Engine::run calls this once per frame; call it directly to
test an application’s timers headless, with times of your own choosing.
§Arguments
now- The current time.state- The application state passed to the callbacks.
Sourcepub fn run_idle(&mut self, state: &mut S)
pub fn run_idle(&mut self, state: &mut S)
Runs the idle callbacks and delivers the events they cause. Engine::run calls this
when a frame had no events to process; call it directly to test idle callbacks headless.
§Arguments
state- The application state passed to the callbacks.
Sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
Sourcepub fn add_widget<W: Widget>(&mut self, widget: W) -> WidgetId
pub fn add_widget<W: Widget>(&mut self, widget: W) -> WidgetId
Sourcepub fn add_child_widget<W: Widget>(
&mut self,
parent: WidgetId,
widget: W,
) -> WidgetId
pub fn add_child_widget<W: Widget>( &mut self, parent: WidgetId, widget: W, ) -> WidgetId
Adds a Widget as a child of another Widget.
Children move with their parent, are hidden with it, and are drawn on top of it.
§Arguments
parent- The ID of the parentWidget.widget- TheWidgetto add. Its origin is relative to the parent.
§Returns
The ID assigned to the Widget.
§Panics
Panics if parent does not exist.
Sourcepub fn try_add_child_widget<W: Widget>(
&mut self,
parent: WidgetId,
widget: W,
) -> Result<WidgetId>
pub fn try_add_child_widget<W: Widget>( &mut self, parent: WidgetId, widget: W, ) -> Result<WidgetId>
Adds a Widget as a child of another, reporting an unknown parent as an error instead of
panicking. Behaves like add_child_widget otherwise.
§Arguments
parent- The ID of the parentWidget.widget- TheWidgetto add. Its origin is relative to the parent.
§Returns
The ID assigned to the Widget.
§Errors
Returns Error::UnknownWidget if parent does not exist.
Sourcepub fn widgets(&mut self) -> &mut WidgetCache
pub fn widgets(&mut self) -> &mut WidgetCache
Sourcepub fn on_click<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
pub fn on_click<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
Registers a callback that runs when a Widget is clicked.
Several callbacks may be registered for the same Widget; they run in the order registered,
before any on_event callbacks. Callbacks run with
Engine::run, Engine::dispatch and Engine::next_event_with. Every on_*
method below works the same way: it fires only for its Widget and event kind, in
registration order, after the Widget has updated itself.
§Arguments
id- The ID of theWidget.callback- Called with the application state and aUihandle.
§Returns
The CallbackId of the registered callback.
§Examples
use pushrod::engine::Engine;
use pushrod::geometry::size;
let mut engine: Engine<u32> = Engine::new(size(320, 200), 30);
engine.on_click(0, |clicks, _ui| *clicks += 1);Sourcepub fn on_press<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
pub fn on_press<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
Registers a callback that runs when a mouse button goes down on the Widget.
§Arguments
id- The ID of theWidget.callback- Called with the application state and aUihandle.
§Returns
The CallbackId of the registered callback.
Sourcepub fn on_release<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
pub fn on_release<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
Registers a callback that runs when a mouse button goes up, inside or outside the Widget (it is sent to the Widget that captured the press).
§Arguments
id- The ID of theWidget.callback- Called with the application state and aUihandle.
§Returns
The CallbackId of the registered callback.
Sourcepub fn on_activate<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
pub fn on_activate<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
Registers a callback that runs when a Widget is activated.
§Arguments
id- The ID of theWidget.callback- Called with the application state and aUihandle.
§Returns
The CallbackId of the registered callback.
Sourcepub fn on_value_change<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
pub fn on_value_change<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
Registers a callback that runs when a Widget’s value changes.
§Arguments
id- The ID of theWidget.callback- Called with the application state and aUihandle.
§Returns
The CallbackId of the registered callback.
Sourcepub fn on_selection_change<F>(
&mut self,
id: WidgetId,
callback: F,
) -> CallbackId
pub fn on_selection_change<F>( &mut self, id: WidgetId, callback: F, ) -> CallbackId
Registers a callback that runs when a Widget’s selection changes.
§Arguments
id- The ID of theWidget.callback- Called with the application state and aUihandle.
§Returns
The CallbackId of the registered callback.
Sourcepub fn on_text_change<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
pub fn on_text_change<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
Registers a callback that runs when a Widget’s text changes.
§Arguments
id- The ID of theWidget.callback- Called with the application state and aUihandle.
§Returns
The CallbackId of the registered callback.
Sourcepub fn on_editing_finished<F>(
&mut self,
id: WidgetId,
callback: F,
) -> CallbackId
pub fn on_editing_finished<F>( &mut self, id: WidgetId, callback: F, ) -> CallbackId
Registers a callback that runs when editing in a Widget finishes.
§Arguments
id- The ID of theWidget.callback- Called with the application state and aUihandle.
§Returns
The CallbackId of the registered callback.
Sourcepub fn on_toggle<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
pub fn on_toggle<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
Registers a callback that runs when a Widget is toggled.
§Arguments
id- The ID of theWidget.callback- Called with the application state, aUihandle, and the newcheckedstate.
§Returns
The CallbackId of the registered callback.
Sourcepub fn on_viewport_change<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
pub fn on_viewport_change<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
Registers a callback that runs when a scrolling Widget’s visible area moves.
§Arguments
id- The ID of theWidget.callback- Called with the application state, aUihandle, and the new scroll offset.
§Returns
The CallbackId of the registered callback.
Sourcepub fn on_key_press<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
pub fn on_key_press<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
Registers a callback that runs when a key goes down (or repeats) while the Widget has the keyboard.
§Arguments
id- The ID of theWidget.callback- Called with the application state, aUihandle, the key, and the modifiers held.
§Returns
The CallbackId of the registered callback.
Sourcepub fn on_focus_change<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
pub fn on_focus_change<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
Registers a callback that runs when a Widget gains or loses keyboard focus.
§Arguments
id- The ID of theWidget.callback- Called with the application state, aUihandle, andtrueif focus was gained.
§Returns
The CallbackId of the registered callback.
Sourcepub fn on_hover<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
pub fn on_hover<F>(&mut self, id: WidgetId, callback: F) -> CallbackId
Registers a callback that runs when the pointer enters or leaves a Widget.
§Arguments
id- The ID of theWidget.callback- Called with the application state, aUihandle, andtrueif the pointer is inside.
§Returns
The CallbackId of the registered callback.
Sourcepub fn on_engine_event<F>(&mut self, callback: F)
pub fn on_engine_event<F>(&mut self, callback: F)
Registers a callback that receives every EngineEvent.
Engine events are emitted after an internal action completes (an event mask change, a
Widget added, …). They run before on_event callbacks. Engine
events never trigger further internal actions by themselves.
§Arguments
callback- Called with the application state, aUihandle, and the engine event.
§Examples
use pushrod::engine::Engine;
use pushrod::event::EngineEvent;
use pushrod::geometry::size;
let mut engine: Engine<Vec<String>> = Engine::new(size(320, 200), 30);
engine.on_engine_event(|log, _ui, event| log.push(format!("{:?}", event)));Sourcepub fn on_engine_event_filtered<F>(
&mut self,
kinds: &[EngineEventKind],
callback: F,
)
pub fn on_engine_event_filtered<F>( &mut self, kinds: &[EngineEventKind], callback: F, )
Registers a callback that receives only the listed kinds of EngineEvent.
§Arguments
kinds- The kinds of engine event to receive.callback- Called with the application state, aUihandle, and the engine event.
Sourcepub fn on_custom<T, F>(&mut self, callback: F)
pub fn on_custom<T, F>(&mut self, callback: F)
Registers a callback for custom events whose payload is a T.
Custom events are delivered to the targeted Widgets first, then to these callbacks, then
to on_event. Events carrying any other payload type are ignored by
this callback.
§Arguments
callback- Called with the application state, aUihandle, the postingWidget(Noneif the application or another thread posted), and the payload.
§Examples
use pushrod::engine::Engine;
use pushrod::geometry::size;
struct DocumentSaved;
let mut engine: Engine<u32> = Engine::new(size(320, 200), 30);
engine.on_custom::<DocumentSaved, _>(|saves, _ui, _source, _payload| *saves += 1);Sourcepub fn set_background(&mut self, color: Color)
pub fn set_background(&mut self, color: Color)
Sets the window’s background color.
This is the color of the RootWidget (ID 0), which fills the window behind every other
Widget.
§Arguments
color- The new background color.
Sourcepub fn dispatch(&mut self, event: InputEvent, state: &mut S)
pub fn dispatch(&mut self, event: InputEvent, state: &mut S)
Processes one raw input event, as the run loop does.
The event is routed to the Widgets, and every event they generate is delivered to the
registered callbacks, in order. Useful for testing an application without opening a window.
§Arguments
event- The input event, as a backend would deliver it.state- The application state passed to the callbacks.
Sourcepub fn run(&mut self, backend: &mut dyn Backend, state: S) -> S
pub fn run(&mut self, backend: &mut dyn Backend, state: S) -> S
Runs the application on a backend until the window is closed or a callback calls shutdown.
Each frame, the loop processes waiting input events (routing them to Widgets and calling
callbacks), repaints changed Widgets, presents the frame if anything changed, then sleeps for
the rest of the frame time. Applications normally start this through a backend crate,
such as pushrod_sdl2::run, which builds the window and calls this method.
§Arguments
backend- The backend that owns the window.state- The application state, passed to every callback.
§Returns
The application state, after the last callback has run.
§Examples
use pushrod::backend::headless::HeadlessBackend;
use pushrod::engine::Engine;
use pushrod::geometry::size;
let mut engine: Engine<u32> = Engine::new(size(100, 100), 30);
let mut backend = HeadlessBackend::new(size(100, 100));
// With nothing scripted, the headless window closes at once.
assert_eq!(engine.run(&mut backend, 7), 7);Sourcepub fn event_loop(&mut self, backend: Box<dyn Backend>) -> EventLoop
pub fn event_loop(&mut self, backend: Box<dyn Backend>) -> EventLoop
Starts an application-driven event loop, as an alternative to Engine::run and callbacks.
Call Engine::next_event repeatedly to receive the events generated by the Widgets.
Applications normally start this through a backend crate, such as pushrod_sdl2::event_loop.
§Arguments
backend- The backend that owns the window; the loop keeps it.
§Returns
The EventLoop to pass to Engine::next_event.
§Examples
use pushrod::backend::headless::HeadlessBackend;
use pushrod::engine::Engine;
use pushrod::event::Event;
use pushrod::geometry::size;
let mut engine: Engine = Engine::new(size(400, 200), 60);
let mut events = engine.event_loop(Box::new(HeadlessBackend::new(size(400, 200))));
while let Some(event) = engine.next_event(&mut events) {
if let Event::Clicked { id, .. } = event {
println!("widget {} clicked", id);
}
}Sourcepub fn next_event(&mut self, events: &mut EventLoop) -> Option<Event>
pub fn next_event(&mut self, events: &mut EventLoop) -> Option<Event>
Returns the next event generated by the Widgets, waiting for input as needed.
While waiting, the screen is kept up to date: invalidated Widgets are repainted and the
frame is presented, so changes made to Widgets between calls appear immediately. When nothing
happens, the call blocks without using the CPU. Callbacks registered with on_click and
on_event are not called by this method; use Engine::next_event_with to run them too.
§Arguments
events- The loop state returned byEngine::event_loop.
§Returns
The next event, or None once the window has been closed or shutdown
has been called.
Sourcepub fn next_event_with(
&mut self,
events: &mut EventLoop,
state: &mut S,
) -> Option<Event>
pub fn next_event_with( &mut self, events: &mut EventLoop, state: &mut S, ) -> Option<Event>
Returns the next event, after running the registered callbacks for it.
This is Engine::next_event plus the callback style: the on_click, on_engine_event
and on_event callbacks run for the event, with the application state, and then the event
is returned. Use it to mix styles, for example callbacks for most Widgets and a loop for
the main window’s logic. Every event is delivered to both, in the same order as
Engine::run delivers it. Events emitted by a callback’s own actions are returned by
later calls.
§Arguments
events- The loop state returned byEngine::event_loop.state- The application state passed to the callbacks.
§Returns
The next event, or None once the window has been closed or shutdown
has been called.
§Examples
let mut engine: Engine<u32> = Engine::new(size(400, 200), 60);
let mut events = engine.event_loop(Box::new(HeadlessBackend::new(size(400, 200))));
let mut clicks = 0;
engine.on_click(0, |clicks, _ui| *clicks += 1);
// The callback has already run when the event is returned.
while let Some(event) = engine.next_event_with(&mut events, &mut clicks) {
println!("{:?}, {} clicks so far", event, clicks);
}