pub trait Sandbox {
type Message: Debug + Send + Sync + 'static;
// Required methods
fn new() -> Self;
fn title(&self) -> String;
fn update(&mut self, message: Self::Message);
fn view(&self) -> Element<'_, Self::Message, Theme, Renderer>;
// Provided methods
fn theme(&self) -> Theme { ... }
fn style(&self) -> Application { ... }
fn scale_factor(&self) -> f64 { ... }
fn run(settings: Settings<()>) -> Result<(), Error>
where Self: Sized + 'static { ... }
}
Expand description
A sandboxed Application
.
If you are a just getting started with the library, this trait offers a
simpler interface than Application
.
Unlike an Application
, a Sandbox
cannot run any asynchronous
actions or be initialized with some external flags. However, both traits
are very similar and upgrading from a Sandbox
is very straightforward.
Therefore, it is recommended to always start by implementing this trait and upgrade only once necessary.
§Examples
The repository has a bunch of examples that use the Sandbox
trait:
bezier_tool
, a Paint-like tool for drawing Bézier curves using the [Canvas widget
].counter
, the classic counter example explained in the overview.custom_widget
, a demonstration of how to build a custom widget that draws a circle.geometry
, a custom widget showcasing how to draw geometry with theMesh2D
primitive iniced_wgpu
.pane_grid
, a grid of panes that can be split, resized, and reorganized.progress_bar
, a simple progress bar that can be filled by using a slider.styling
, an example showcasing custom styling with a light and dark theme.svg
, an application that renders the Ghostscript Tiger by leveraging the [Svg
widget].tour
, a simple UI tour that can run both on native platforms and the web!
§A simple “Hello, world!”
If you just want to get started, here is a simple Sandbox
that
says “Hello, world!”:
use iced::{Element, Sandbox, Settings};
pub fn main() -> iced::Result {
Hello::run(Settings::default())
}
struct Hello;
impl Sandbox for Hello {
type Message = ();
fn new() -> Hello {
Hello
}
fn title(&self) -> String {
String::from("A cool application")
}
fn update(&mut self, _message: Self::Message) {
// This application has no interactions
}
fn view(&self) -> Element<Self::Message> {
"Hello, world!".into()
}
}
Required Associated Types§
Required Methods§
sourcefn new() -> Self
fn new() -> Self
Initializes the Sandbox
.
Here is where you should return the initial state of your app.
sourcefn title(&self) -> String
fn title(&self) -> String
Returns the current title of the Sandbox
.
This title can be dynamic! The runtime will automatically update the title of your application when necessary.
Provided Methods§
sourcefn theme(&self) -> Theme
fn theme(&self) -> Theme
Returns the current Theme
of the Sandbox
.
If you want to use your own custom theme type, you will have to use an
Application
.
By default, it returns Theme::default
.
sourcefn style(&self) -> Application
fn style(&self) -> Application
Returns the current style variant of theme::Application
.
By default, it returns theme::Application::default
.
sourcefn scale_factor(&self) -> f64
fn scale_factor(&self) -> f64
Returns the scale factor of the Sandbox
.
It can be used to dynamically control the size of the UI at runtime (i.e. zooming).
For instance, a scale factor of 2.0
will make widgets twice as big,
while a scale factor of 0.5
will shrink them to half their size.
By default, it returns 1.0
.