Hello World!
Topics: logging, app, run
Lets get started with a simple message box. Source.
extern crate kas; use kas::widgets::dialog::MessageBox; fn main() -> kas::app::Result<()> { env_logger::init(); let window = MessageBox::new("Message").into_window("Hello world"); kas::app::Default::new(())?.with(window).run() }
cargo run --example hello
Logging
Enabling a logger is optional, but can be very useful for debugging:
#![allow(unused)] fn main() { env_logger::init(); }
Kas uses the log
facade internally. To see the
output, we need an implementation, such as
env_logger
.
Trace level can be a bit chatty; to get a reasonable level of output you might try this:
export RUST_LOG=warn,naga=error,kas=debug
cargo run --example hello
A window, a shell
Next, we construct a MessageBox
widget, then wrap with a Window
:
#![allow(unused)] fn main() { extern crate kas; use kas::widgets::dialog::MessageBox; let window = MessageBox::new("Message") .into_window("Hello world"); let _: kas::Window<()> = window; }
Finally, we construct a default app, add this window, and run:
extern crate kas; use kas::widgets::dialog::MessageBox; fn main() -> kas::app::Result<()> { let window = MessageBox::new("Message").into_window("Hello world"); kas::app::Default::new(())? .with(window) .run() }
kas::app::Default
is just a parameterisation of kas::app::Application
which selects a sensible graphics backend and theme.
If you wanted to select your own theme instead, you could do so as follows:
extern crate kas; use kas::widgets::dialog::MessageBox; fn main() -> kas::app::Result<()> { let window = MessageBox::new("Message").into_window("Hello world"); let theme = kas::theme::SimpleTheme::new(); kas::app::Default::with_theme(theme) .build(())? .with(window) .run() }
Or, if you wanted to specify the graphics backend and theme:
extern crate kas; use kas::widgets::dialog::MessageBox; fn main() -> kas::app::Result<()> { let window = MessageBox::new("Message").into_window("Hello world"); kas_wgpu::WgpuBuilder::new(()) .with_theme(kas_wgpu::ShadedTheme::new()) .build(())? .with(window) .run() }
Finally, Application::run
starts our UI. This method runs the event-loop internally, returning Ok(())
once all windows have closed successfully.