Skip to main content

run

Function run 

Source
pub fn run<E: IntoElement + 'static>(
    options: WindowOptions,
    build: impl FnOnce() -> E + 'static,
) -> Result<(), PlatformError>
Expand description

Opens a window and runs frames until the window closes.

build runs once, when the window first appears, and mounts the element it returns. A frame runs after a reactive signal wakes an effect, after a message from another thread arrives, after a resize or a scale change, and once per frame while a tick is installed. Otherwise the window waits for the next event.

Call this from the main thread. The event loop takes over the calling thread until the window closes.

Platform-specific:

  • Web: the loop runs on the browser’s own event loop, so this returns Ok(()) as soon as the loop is started, before the window opens. See run_with for the details.

§Errors

Returns PlatformError::ClearColor when WindowOptions::clear_color is not opaque, PlatformError::Renderer when the WindowOptions::renderer choice or its environment override cannot be used, PlatformError::EventLoop when the event loop cannot be created or run, PlatformError::Window when the window cannot be created, and PlatformError::Surface when the presentation surface cannot be created, resized or presented.

§Examples

use smudge_core::geometry::{Color, Size};
use smudge_core::style::px;
use smudge_core::widget::panel;
use smudge_platform::{WindowOptions, run};

run(
    WindowOptions {
        title: String::from("Smudge"),
        size: Size {
            width: 320.0,
            height: 200.0,
        },
        clear_color: Color::WHITE,
        ..WindowOptions::default()
    },
    || panel().width(px(120.0)).height(px(40.0)).background(Color::BLACK),
)?;