//! The smallest ltk app in a browser: a counter and a text field on a canvas over the whole page, on the default theme the page serves under `ltk/`. `make web-example` builds it and its resources into `target/web/`, `make web-serve` serves it at .
//!
//! A web app is a library the page loads, so this example builds as a `cdylib` whose entry point is the `start` function wasm-bindgen runs when the module is loaded.
use ltk::{ App, ButtonVariant, Element, button, column, spacer, text, text_edit };
use wasm_bindgen::prelude::wasm_bindgen;
#[ derive( Clone ) ]
enum Msg
{
Press,
Edit( String ),
Submit,
Fullscreen,
}
#[ derive( Default ) ]
struct Hello
{
presses: u32,
typed: String,
submitted: String,
fullscreen: bool,
}
impl App for Hello
{
type Message = Msg;
fn app_id( &self ) -> &str { "net.liberux.ltk.Hello" }
fn save_state( &self ) -> Option> { None }
fn restore_state( &mut self, _state: Vec ) {}
fn fullscreen( &self ) -> Option { Some( self.fullscreen ) }
fn view( &self ) -> Element
{
let palette = ltk::theme_palette();
column::()
.spacing( 16.0 )
.push( spacer() )
.push( text( "ltk in a browser" ).size( 28.0 ).color( palette.text_primary ) )
.push( text( format!( "Pressed {} times", self.presses ) ).size( 18.0 ).color( palette.text_secondary ) )
.push( button::( "Press" ).variant( ButtonVariant::Secondary ).on_press( Msg::Press ) )
.push( text_edit::( "Type and press Enter", &self.typed ).fixed_width( 320.0 ).on_change( Msg::Edit ).on_submit( Msg::Submit ) )
.push( text( format!( "Submitted: {}", self.submitted ) ).size( 18.0 ).color( palette.text_secondary ) )
.push( button::( if self.fullscreen { "Leave fullscreen" } else { "Fullscreen" } ).variant( ButtonVariant::Secondary ).on_press( Msg::Fullscreen ) )
.push( spacer() )
.into()
}
fn update( &mut self, msg: Msg )
{
match msg
{
Msg::Press => self.presses += 1,
Msg::Edit( value ) => self.typed = value,
Msg::Submit => self.submitted = std::mem::take( &mut self.typed ),
Msg::Fullscreen => self.fullscreen = !self.fullscreen,
}
}
}
#[ wasm_bindgen( start ) ]
pub async fn start()
{
if let Err( e ) = ltk::web::load_resources( "ltk" ).await
{
web_sys::console::warn_1( &format!( "ltk resources: {e}" ).into() );
}
ltk::run( Hello::default() );
}