diff options
Diffstat (limited to 'web/src')
-rw-r--r-- | web/src/lib.rs | 39 | ||||
-rw-r--r-- | web/src/widget/container.rs | 15 |
2 files changed, 39 insertions, 15 deletions
diff --git a/web/src/lib.rs b/web/src/lib.rs index 1de00545..a0b39c4a 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -94,11 +94,6 @@ pub use executor::Executor; /// An [`Application`](trait.Application.html) can execute asynchronous actions /// by returning a [`Command`](struct.Command.html) in some of its methods. pub trait Application { - /// The type of __messages__ your [`Application`] will produce. - /// - /// [`Application`]: trait.Application.html - type Message: Send; - /// The [`Executor`] that will run commands and subscriptions. /// /// The [`executor::WasmBindgen`] can be a good choice for the Web. @@ -107,6 +102,16 @@ pub trait Application { /// [`executor::Default`]: executor/struct.Default.html type Executor: Executor; + /// The type of __messages__ your [`Application`] will produce. + /// + /// [`Application`]: trait.Application.html + type Message: Send; + + /// The data needed to initialize your [`Application`]. + /// + /// [`Application`]: trait.Application.html + type Flags; + /// Initializes the [`Application`]. /// /// Here is where you should return the initial state of your app. @@ -117,7 +122,7 @@ pub trait Application { /// request, etc. /// /// [`Application`]: trait.Application.html - fn new() -> (Self, Command<Self::Message>) + fn new(flags: Self::Flags) -> (Self, Command<Self::Message>) where Self: Sized; @@ -165,21 +170,16 @@ pub trait Application { /// Runs the [`Application`]. /// /// [`Application`]: trait.Application.html - fn run() + fn run(flags: Self::Flags) where Self: 'static + Sized, { use futures::stream::StreamExt; - let (app, command) = Self::new(); - let window = web_sys::window().unwrap(); let document = window.document().unwrap(); let body = document.body().unwrap(); - let mut title = app.title(); - document.set_title(&title); - let (sender, receiver) = iced_futures::futures::channel::mpsc::unbounded(); @@ -187,6 +187,12 @@ pub trait Application { Self::Executor::new().expect("Create executor"), sender.clone(), ); + + let (app, command) = runtime.enter(|| Self::new(flags)); + + let mut title = app.title(); + document.set_title(&title); + runtime.spawn(command); let application = Rc::new(RefCell::new(app)); @@ -199,8 +205,13 @@ pub trait Application { let vdom = dodrio::Vdom::new(&body, instance); let event_loop = receiver.for_each(move |message| { - let command = application.borrow_mut().update(message); - let subscription = application.borrow().subscription(); + let (command, subscription) = runtime.enter(|| { + let command = application.borrow_mut().update(message); + let subscription = application.borrow().subscription(); + + (command, subscription) + }); + let new_title = application.borrow().title(); runtime.spawn(command); diff --git a/web/src/widget/container.rs b/web/src/widget/container.rs index 8e4318f9..78be3543 100644 --- a/web/src/widget/container.rs +++ b/web/src/widget/container.rs @@ -8,6 +8,7 @@ pub use iced_style::container::{Style, StyleSheet}; /// It is normally used for alignment purposes. #[allow(missing_debug_implementations)] pub struct Container<'a, Message> { + padding: u16, width: Length, height: Length, max_width: u32, @@ -29,6 +30,7 @@ impl<'a, Message> Container<'a, Message> { use std::u32; Container { + padding: 0, width: Length::Shrink, height: Length::Shrink, max_width: u32::MAX, @@ -40,6 +42,14 @@ impl<'a, Message> Container<'a, Message> { } } + /// Sets the padding of the [`Container`]. + /// + /// [`Container`]: struct.Column.html + pub fn padding(mut self, units: u16) -> Self { + self.padding = units; + self + } + /// Sets the width of the [`Container`]. /// /// [`Container`]: struct.Container.html @@ -113,12 +123,15 @@ where let column_class = style_sheet.insert(bump, css::Rule::Column); + let padding_class = + style_sheet.insert(bump, css::Rule::Padding(self.padding)); + let style = self.style_sheet.style(); let node = div(bump) .attr( "class", - bumpalo::format!(in bump, "{}", column_class).into_bump_str(), + bumpalo::format!(in bump, "{} {}", column_class, padding_class).into_bump_str(), ) .attr( "style", |