From 852d59752e209d4f3cdc38e164f5eeb31e164700 Mon Sep 17 00:00:00 2001 From: Tom Pridham Date: Sun, 12 Apr 2020 00:51:17 -0600 Subject: add some accessibility features to web widgets --- web/src/widget/checkbox.rs | 20 +++++++++++++++++--- web/src/widget/image.rs | 16 +++++++++++++++- web/src/widget/radio.rs | 31 +++++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 6 deletions(-) (limited to 'web/src') diff --git a/web/src/widget/checkbox.rs b/web/src/widget/checkbox.rs index 5ebc26c8..10a46661 100644 --- a/web/src/widget/checkbox.rs +++ b/web/src/widget/checkbox.rs @@ -28,6 +28,7 @@ pub struct Checkbox { is_checked: bool, on_toggle: Rc Message>, label: String, + id: String, width: Length, style: Box, } @@ -51,6 +52,7 @@ impl Checkbox { is_checked, on_toggle: Rc::new(f), label: label.into(), + id: Default::default(), width: Length::Shrink, style: Default::default(), } @@ -71,6 +73,14 @@ impl Checkbox { self.style = style.into(); self } + + /// Sets the id of the [`Checkbox`]. + /// + /// [`Checkbox`]: struct.Checkbox.html + pub fn id(mut self, id: impl Into) -> Self { + self.id = id.into(); + self + } } impl Widget for Checkbox @@ -85,7 +95,10 @@ where ) -> dodrio::Node<'b> { use dodrio::builder::*; - let checkbox_label = bumpalo::format!(in bump, "{}", self.label); + let checkbox_label = + bumpalo::format!(in bump, "{}", self.label).into_bump_str(); + let checkbox_id = + bumpalo::format!(in bump, "{}", self.id).into_bump_str(); let event_bus = bus.clone(); let on_toggle = self.on_toggle.clone(); @@ -96,6 +109,7 @@ where let spacing_class = style_sheet.insert(bump, css::Rule::Spacing(5)); label(bump) + .attr("for", checkbox_id) .attr( "class", bumpalo::format!(in bump, "{} {}", row_class, spacing_class) @@ -110,6 +124,7 @@ where // TODO: Checkbox styling input(bump) .attr("type", "checkbox") + .attr("id", checkbox_id) .bool_attr("checked", self.is_checked) .on("click", move |_root, vdom, _event| { let msg = on_toggle(!is_checked); @@ -118,8 +133,7 @@ where vdom.schedule_render(); }) .finish(), - span(bump).children(vec![ - text(checkbox_label.into_bump_str())]).finish(), + text(checkbox_label), ]) .finish() } diff --git a/web/src/widget/image.rs b/web/src/widget/image.rs index 029ab352..a20bebea 100644 --- a/web/src/widget/image.rs +++ b/web/src/widget/image.rs @@ -22,6 +22,9 @@ pub struct Image { /// The image path pub handle: Handle, + /// The alt text of the image + pub alt: String, + /// The width of the image pub width: Length, @@ -36,6 +39,7 @@ impl Image { pub fn new>(handle: T) -> Self { Image { handle: handle.into(), + alt: Default::default(), width: Length::Shrink, height: Length::Shrink, } @@ -56,6 +60,14 @@ impl Image { self.height = height; self } + + /// Sets the alt text of the [`Image`]. + /// + /// [`Image`]: struct.Image.html + pub fn alt(mut self, alt: impl Into) -> Self { + self.alt = alt.into(); + self + } } impl Widget for Image { @@ -70,8 +82,10 @@ impl Widget for Image { let src = bumpalo::format!(in bump, "{}", match self.handle.data.as_ref() { Data::Path(path) => path.to_str().unwrap_or("") }); + let alt = bumpalo::format!(in bump, "{}", self.alt).into_bump_str(); - let mut image = img(bump).attr("src", src.into_bump_str()); + let mut image = + img(bump).attr("src", src.into_bump_str()).attr("alt", alt); match self.width { Length::Shrink => {} diff --git a/web/src/widget/radio.rs b/web/src/widget/radio.rs index 520b24cd..acbac3b6 100644 --- a/web/src/widget/radio.rs +++ b/web/src/widget/radio.rs @@ -35,6 +35,8 @@ pub struct Radio { is_selected: bool, on_click: Message, label: String, + id: String, + name: String, style: Box, } @@ -63,6 +65,8 @@ impl Radio { is_selected: Some(value) == selected, on_click: f(value), label: label.into(), + id: Default::default(), + name: Default::default(), style: Default::default(), } } @@ -74,6 +78,22 @@ impl Radio { self.style = style.into(); self } + + /// Sets the name attribute of the [`Radio`] button. + /// + /// [`Radio`]: struct.Radio.html + pub fn name(mut self, name: impl Into) -> Self { + self.name = name.into(); + self + } + + /// Sets the id of the [`Radio`] button. + /// + /// [`Radio`]: struct.Radio.html + pub fn id(mut self, id: impl Into) -> Self { + self.id = id.into(); + self + } } impl Widget for Radio @@ -88,7 +108,11 @@ where ) -> dodrio::Node<'b> { use dodrio::builder::*; - let radio_label = bumpalo::format!(in bump, "{}", self.label); + let radio_label = + bumpalo::format!(in bump, "{}", self.label).into_bump_str(); + let radio_name = + bumpalo::format!(in bump, "{}", self.name).into_bump_str(); + let radio_id = bumpalo::format!(in bump, "{}", self.id).into_bump_str(); let event_bus = bus.clone(); let on_click = self.on_click.clone(); @@ -96,16 +120,19 @@ where // TODO: Complete styling label(bump) .attr("style", "display: block; font-size: 20px") + .attr("for", radio_id) .children(vec![ input(bump) .attr("type", "radio") + .attr("id", radio_id) + .attr("name", radio_name) .attr("style", "margin-right: 10px") .bool_attr("checked", self.is_selected) .on("click", move |_root, _vdom, _event| { event_bus.publish(on_click.clone()); }) .finish(), - text(radio_label.into_bump_str()), + text(radio_label), ]) .finish() } -- cgit From c3643eaf6d90dc8b60c0b762200bf1f8097cdfe9 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Tue, 2 Jun 2020 20:34:13 +0200 Subject: Add `step` member to slider widgets Both the native and the web slider now have a member `step` to control the least possible change of the slider's value. It defaults to 1.0 for all sliders and can be adjusted with the step method. --- web/src/widget/slider.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'web/src') diff --git a/web/src/widget/slider.rs b/web/src/widget/slider.rs index 5aa6439e..60d69798 100644 --- a/web/src/widget/slider.rs +++ b/web/src/widget/slider.rs @@ -16,6 +16,8 @@ use std::{ops::RangeInclusive, rc::Rc}; /// /// A [`Slider`] will try to fill the horizontal space of its container. /// +/// The step size defaults to 1.0. +/// /// [`Slider`]: struct.Slider.html /// /// # Example @@ -37,6 +39,7 @@ use std::{ops::RangeInclusive, rc::Rc}; pub struct Slider<'a, Message> { _state: &'a mut State, range: RangeInclusive, + step: f32, value: f32, on_change: Rc Message>>, width: Length, @@ -69,6 +72,7 @@ impl<'a, Message> Slider<'a, Message> { _state: state, value: value.max(*range.start()).min(*range.end()), range, + step: 1.0, on_change: Rc::new(Box::new(on_change)), width: Length::Fill, style: Default::default(), @@ -90,6 +94,14 @@ impl<'a, Message> Slider<'a, Message> { self.style = style.into(); self } + + /// Sets the step size of the [`Slider`]. + /// + /// [`Slider`]: struct.Slider.html + pub fn step(mut self, step: f32) -> Self { + self.step = step; + self + } } impl<'a, Message> Widget for Slider<'a, Message> @@ -110,15 +122,15 @@ where let min = bumpalo::format!(in bump, "{}", start); let max = bumpalo::format!(in bump, "{}", end); let value = bumpalo::format!(in bump, "{}", self.value); + let step = bumpalo::format!(in bump, "{}", self.step); let on_change = self.on_change.clone(); let event_bus = bus.clone(); - // TODO: Make `step` configurable // TODO: Styling input(bump) .attr("type", "range") - .attr("step", "0.01") + .attr("step", step.into_bump_str()) .attr("min", min.into_bump_str()) .attr("max", max.into_bump_str()) .attr("value", value.into_bump_str()) -- cgit From 6b4a4655c1e533c789308b180f4d3596e62b0179 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 13 Jun 2020 14:59:09 +0200 Subject: Make `Slider` value generic in `iced_web` --- web/src/widget/slider.rs | 61 +++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 21 deletions(-) (limited to 'web/src') diff --git a/web/src/widget/slider.rs b/web/src/widget/slider.rs index 60d69798..afeedb2d 100644 --- a/web/src/widget/slider.rs +++ b/web/src/widget/slider.rs @@ -36,17 +36,20 @@ use std::{ops::RangeInclusive, rc::Rc}; /// /// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true) #[allow(missing_debug_implementations)] -pub struct Slider<'a, Message> { +pub struct Slider<'a, T, Message> { _state: &'a mut State, - range: RangeInclusive, - step: f32, - value: f32, - on_change: Rc Message>>, + range: RangeInclusive, + step: T, + value: T, + on_change: Rc Message>>, width: Length, style: Box, } -impl<'a, Message> Slider<'a, Message> { +impl<'a, T, Message> Slider<'a, T, Message> +where + T: Copy + From + std::cmp::PartialOrd, +{ /// Creates a new [`Slider`]. /// /// It expects: @@ -61,18 +64,30 @@ impl<'a, Message> Slider<'a, Message> { /// [`State`]: struct.State.html pub fn new( state: &'a mut State, - range: RangeInclusive, - value: f32, + range: RangeInclusive, + value: T, on_change: F, ) -> Self where - F: 'static + Fn(f32) -> Message, + F: 'static + Fn(T) -> Message, { + let value = if value >= *range.start() { + value + } else { + *range.start() + }; + + let value = if value <= *range.end() { + value + } else { + *range.end() + }; + Slider { _state: state, - value: value.max(*range.start()).min(*range.end()), + value, range, - step: 1.0, + step: T::from(1), on_change: Rc::new(Box::new(on_change)), width: Length::Fill, style: Default::default(), @@ -98,14 +113,15 @@ impl<'a, Message> Slider<'a, Message> { /// Sets the step size of the [`Slider`]. /// /// [`Slider`]: struct.Slider.html - pub fn step(mut self, step: f32) -> Self { + pub fn step(mut self, step: T) -> Self { self.step = step; self } } -impl<'a, Message> Widget for Slider<'a, Message> +impl<'a, T, Message> Widget for Slider<'a, T, Message> where + T: 'static + Copy + Into + num_traits::FromPrimitive, Message: 'static, { fn node<'b>( @@ -119,10 +135,10 @@ where let (start, end) = self.range.clone().into_inner(); - let min = bumpalo::format!(in bump, "{}", start); - let max = bumpalo::format!(in bump, "{}", end); - let value = bumpalo::format!(in bump, "{}", self.value); - let step = bumpalo::format!(in bump, "{}", self.step); + let min = bumpalo::format!(in bump, "{}", start.into()); + let max = bumpalo::format!(in bump, "{}", end.into()); + let value = bumpalo::format!(in bump, "{}", self.value.into()); + let step = bumpalo::format!(in bump, "{}", self.step.into()); let on_change = self.on_change.clone(); let event_bus = bus.clone(); @@ -143,19 +159,22 @@ where Some(slider) => slider, }; - if let Ok(value) = slider.value().parse::() { - event_bus.publish(on_change(value)); + if let Ok(value) = slider.value().parse::() { + if let Some(value) = T::from_f64(value) { + event_bus.publish(on_change(value)); + } } }) .finish() } } -impl<'a, Message> From> for Element<'a, Message> +impl<'a, T, Message> From> for Element<'a, Message> where + T: 'static + Copy + Into + num_traits::FromPrimitive, Message: 'static, { - fn from(slider: Slider<'a, Message>) -> Element<'a, Message> { + fn from(slider: Slider<'a, T, Message>) -> Element<'a, Message> { Element::new(slider) } } -- cgit From f5e16312bfa02eac13ce46aa97831c554151e2f8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 13 Jun 2020 15:04:49 +0200 Subject: Update `Slider` docs in `iced_web` --- web/src/widget/slider.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'web/src') diff --git a/web/src/widget/slider.rs b/web/src/widget/slider.rs index afeedb2d..a0d9df00 100644 --- a/web/src/widget/slider.rs +++ b/web/src/widget/slider.rs @@ -16,7 +16,8 @@ use std::{ops::RangeInclusive, rc::Rc}; /// /// A [`Slider`] will try to fill the horizontal space of its container. /// -/// The step size defaults to 1.0. +/// The [`Slider`] range of numeric values is generic and its step size defaults +/// to 1 unit. /// /// [`Slider`]: struct.Slider.html /// -- cgit From ffd195cdb543dfbff42327c516d6082cb2df51ef Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 1 Jul 2020 06:52:13 +0200 Subject: Fix empty `id` and `name` attributes in `iced_web` --- web/src/widget/checkbox.rs | 22 +++++++++++++--------- web/src/widget/radio.rs | 38 ++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 23 deletions(-) (limited to 'web/src') diff --git a/web/src/widget/checkbox.rs b/web/src/widget/checkbox.rs index 10a46661..4d0c7c17 100644 --- a/web/src/widget/checkbox.rs +++ b/web/src/widget/checkbox.rs @@ -28,7 +28,7 @@ pub struct Checkbox { is_checked: bool, on_toggle: Rc Message>, label: String, - id: String, + id: Option, width: Length, style: Box, } @@ -52,7 +52,7 @@ impl Checkbox { is_checked, on_toggle: Rc::new(f), label: label.into(), - id: Default::default(), + id: None, width: Length::Shrink, style: Default::default(), } @@ -78,7 +78,7 @@ impl Checkbox { /// /// [`Checkbox`]: struct.Checkbox.html pub fn id(mut self, id: impl Into) -> Self { - self.id = id.into(); + self.id = Some(id.into()); self } } @@ -97,8 +97,6 @@ where let checkbox_label = bumpalo::format!(in bump, "{}", self.label).into_bump_str(); - let checkbox_id = - bumpalo::format!(in bump, "{}", self.id).into_bump_str(); let event_bus = bus.clone(); let on_toggle = self.on_toggle.clone(); @@ -108,8 +106,15 @@ where let spacing_class = style_sheet.insert(bump, css::Rule::Spacing(5)); - label(bump) - .attr("for", checkbox_id) + let (label, input) = if let Some(id) = &self.id { + let id = bumpalo::format!(in bump, "{}", id).into_bump_str(); + + (label(bump).attr("for", id), input(bump).attr("id", id)) + } else { + (label(bump), input(bump)) + }; + + label .attr( "class", bumpalo::format!(in bump, "{} {}", row_class, spacing_class) @@ -122,9 +127,8 @@ where ) .children(vec![ // TODO: Checkbox styling - input(bump) + input .attr("type", "checkbox") - .attr("id", checkbox_id) .bool_attr("checked", self.is_checked) .on("click", move |_root, vdom, _event| { let msg = on_toggle(!is_checked); diff --git a/web/src/widget/radio.rs b/web/src/widget/radio.rs index acbac3b6..fae67cd8 100644 --- a/web/src/widget/radio.rs +++ b/web/src/widget/radio.rs @@ -35,8 +35,8 @@ pub struct Radio { is_selected: bool, on_click: Message, label: String, - id: String, - name: String, + id: Option, + name: Option, style: Box, } @@ -65,8 +65,8 @@ impl Radio { is_selected: Some(value) == selected, on_click: f(value), label: label.into(), - id: Default::default(), - name: Default::default(), + id: None, + name: None, style: Default::default(), } } @@ -83,7 +83,7 @@ impl Radio { /// /// [`Radio`]: struct.Radio.html pub fn name(mut self, name: impl Into) -> Self { - self.name = name.into(); + self.name = Some(name.into()); self } @@ -91,7 +91,7 @@ impl Radio { /// /// [`Radio`]: struct.Radio.html pub fn id(mut self, id: impl Into) -> Self { - self.id = id.into(); + self.id = Some(id.into()); self } } @@ -110,22 +110,32 @@ where let radio_label = bumpalo::format!(in bump, "{}", self.label).into_bump_str(); - let radio_name = - bumpalo::format!(in bump, "{}", self.name).into_bump_str(); - let radio_id = bumpalo::format!(in bump, "{}", self.id).into_bump_str(); let event_bus = bus.clone(); let on_click = self.on_click.clone(); + let (label, input) = if let Some(id) = &self.id { + let id = bumpalo::format!(in bump, "{}", id).into_bump_str(); + + (label(bump).attr("for", id), input(bump).attr("id", id)) + } else { + (label(bump), input(bump)) + }; + + let input = if let Some(name) = &self.name { + let name = bumpalo::format!(in bump, "{}", name).into_bump_str(); + + dodrio::builder::input(bump).attr("name", name) + } else { + input + }; + // TODO: Complete styling - label(bump) + label .attr("style", "display: block; font-size: 20px") - .attr("for", radio_id) .children(vec![ - input(bump) + input .attr("type", "radio") - .attr("id", radio_id) - .attr("name", radio_name) .attr("style", "margin-right: 10px") .bool_attr("checked", self.is_selected) .on("click", move |_root, _vdom, _event| { -- cgit From d873c37e31fb052fb376caada6780137e176a6e7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 1 Jul 2020 07:19:51 +0200 Subject: Update `dodrio` dependency in `iced_web` --- web/src/lib.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'web/src') diff --git a/web/src/lib.rs b/web/src/lib.rs index 53b54b7e..b7970c56 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -238,28 +238,25 @@ struct Instance { bus: Bus, } -impl dodrio::Render for Instance +impl<'a, A> dodrio::Render<'a> for Instance where A: Application, { - fn render<'a, 'bump>( - &'a self, - bump: &'bump bumpalo::Bump, - ) -> dodrio::Node<'bump> - where - 'a: 'bump, - { + fn render( + &self, + context: &mut dodrio::RenderContext<'a>, + ) -> dodrio::Node<'a> { use dodrio::builder::*; let mut ui = self.application.borrow_mut(); let element = ui.view(); let mut css = Css::new(); - let node = element.widget.node(bump, &self.bus, &mut css); + let node = element.widget.node(context.bump, &self.bus, &mut css); - div(bump) + div(context.bump) .attr("style", "width: 100%; height: 100%") - .children(vec![css.node(bump), node]) + .children(vec![css.node(context.bump), node]) .finish() } } -- cgit From 75464ad89422884e0718eb0429586a9d77f61c71 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 1 Jul 2020 07:36:42 +0200 Subject: Use `String::from_str_in` in `iced_web` --- web/src/widget/button.rs | 14 ++++++++---- web/src/widget/checkbox.rs | 5 ++-- web/src/widget/image.rs | 17 +++++++++----- web/src/widget/radio.rs | 7 +++--- web/src/widget/text.rs | 10 ++++++-- web/src/widget/text_input.rs | 54 ++++++++++++++++++++++++++------------------ 6 files changed, 67 insertions(+), 40 deletions(-) (limited to 'web/src') diff --git a/web/src/widget/button.rs b/web/src/widget/button.rs index 3a5afe60..a4bcc33d 100644 --- a/web/src/widget/button.rs +++ b/web/src/widget/button.rs @@ -154,16 +154,20 @@ where }, }; + let class = { + use dodrio::bumpalo::collections::String; + + String::from_str_in(&padding_class, bump).into_bump_str() + }; + let mut node = button(bump) - .attr( - "class", - bumpalo::format!(in bump, "{}", padding_class).into_bump_str(), - ) + .attr("class", class) .attr( "style", bumpalo::format!( in bump, - "background: {}; border-radius: {}px; width:{}; min-width: {}; color: {}", + "background: {}; border-radius: {}px; width:{}; \ + min-width: {}; color: {}", background, style.border_radius, css::length(self.width), diff --git a/web/src/widget/checkbox.rs b/web/src/widget/checkbox.rs index 4d0c7c17..21801e39 100644 --- a/web/src/widget/checkbox.rs +++ b/web/src/widget/checkbox.rs @@ -94,9 +94,10 @@ where style_sheet: &mut Css<'b>, ) -> dodrio::Node<'b> { use dodrio::builder::*; + use dodrio::bumpalo::collections::String; let checkbox_label = - bumpalo::format!(in bump, "{}", self.label).into_bump_str(); + String::from_str_in(&self.label, bump).into_bump_str(); let event_bus = bus.clone(); let on_toggle = self.on_toggle.clone(); @@ -107,7 +108,7 @@ where let spacing_class = style_sheet.insert(bump, css::Rule::Spacing(5)); let (label, input) = if let Some(id) = &self.id { - let id = bumpalo::format!(in bump, "{}", id).into_bump_str(); + let id = String::from_str_in(id, bump).into_bump_str(); (label(bump).attr("for", id), input(bump).attr("id", id)) } else { diff --git a/web/src/widget/image.rs b/web/src/widget/image.rs index a20bebea..a595c29a 100644 --- a/web/src/widget/image.rs +++ b/web/src/widget/image.rs @@ -78,14 +78,19 @@ impl Widget for Image { _style_sheet: &mut Css<'b>, ) -> dodrio::Node<'b> { use dodrio::builder::*; + use dodrio::bumpalo::collections::String; - let src = bumpalo::format!(in bump, "{}", match self.handle.data.as_ref() { - Data::Path(path) => path.to_str().unwrap_or("") - }); - let alt = bumpalo::format!(in bump, "{}", self.alt).into_bump_str(); + let src = String::from_str_in( + match self.handle.data.as_ref() { + Data::Path(path) => path.to_str().unwrap_or(""), + }, + bump, + ) + .into_bump_str(); - let mut image = - img(bump).attr("src", src.into_bump_str()).attr("alt", alt); + let alt = String::from_str_in(&self.alt, bump).into_bump_str(); + + let mut image = img(bump).attr("src", src).attr("alt", alt); match self.width { Length::Shrink => {} diff --git a/web/src/widget/radio.rs b/web/src/widget/radio.rs index fae67cd8..c9d0a00e 100644 --- a/web/src/widget/radio.rs +++ b/web/src/widget/radio.rs @@ -107,15 +107,16 @@ where _style_sheet: &mut Css<'b>, ) -> dodrio::Node<'b> { use dodrio::builder::*; + use dodrio::bumpalo::collections::String; let radio_label = - bumpalo::format!(in bump, "{}", self.label).into_bump_str(); + String::from_str_in(&self.label, bump).into_bump_str(); let event_bus = bus.clone(); let on_click = self.on_click.clone(); let (label, input) = if let Some(id) = &self.id { - let id = bumpalo::format!(in bump, "{}", id).into_bump_str(); + let id = String::from_str_in(id, bump).into_bump_str(); (label(bump).attr("for", id), input(bump).attr("id", id)) } else { @@ -123,7 +124,7 @@ where }; let input = if let Some(name) = &self.name { - let name = bumpalo::format!(in bump, "{}", name).into_bump_str(); + let name = String::from_str_in(name, bump).into_bump_str(); dodrio::builder::input(bump).attr("name", name) } else { diff --git a/web/src/widget/text.rs b/web/src/widget/text.rs index 3ec565a8..2f7308ee 100644 --- a/web/src/widget/text.rs +++ b/web/src/widget/text.rs @@ -116,7 +116,12 @@ impl<'a, Message> Widget for Text { ) -> dodrio::Node<'b> { use dodrio::builder::*; - let content = bumpalo::format!(in bump, "{}", self.content); + let content = { + use dodrio::bumpalo::collections::String; + + String::from_str_in(&self.content, bump) + }; + let color = self .color .map(css::color) @@ -133,7 +138,8 @@ impl<'a, Message> Widget for Text { let style = bumpalo::format!( in bump, - "width: {}; height: {}; font-size: {}px; color: {}; text-align: {}; font-family: {}", + "width: {}; height: {}; font-size: {}px; color: {}; \ + text-align: {}; font-family: {}", width, height, self.size.unwrap_or(20), diff --git a/web/src/widget/text_input.rs b/web/src/widget/text_input.rs index 3fa458bd..0049a553 100644 --- a/web/src/widget/text_input.rs +++ b/web/src/widget/text_input.rs @@ -151,8 +151,26 @@ where use dodrio::builder::*; use wasm_bindgen::JsCast; - let padding_class = - style_sheet.insert(bump, css::Rule::Padding(self.padding)); + let class = { + use dodrio::bumpalo::collections::String; + + let padding_class = + style_sheet.insert(bump, css::Rule::Padding(self.padding)); + + String::from_str_in(&padding_class, bump).into_bump_str() + }; + + let placeholder = { + use dodrio::bumpalo::collections::String; + + String::from_str_in(&self.placeholder, bump).into_bump_str() + }; + + let value = { + use dodrio::bumpalo::collections::String; + + String::from_str_in(&self.value, bump).into_bump_str() + }; let on_change = self.on_change.clone(); let on_submit = self.on_submit.clone(); @@ -161,15 +179,14 @@ where let style = self.style_sheet.active(); input(bump) - .attr( - "class", - bumpalo::format!(in bump, "{}", padding_class).into_bump_str(), - ) + .attr("class", class) .attr( "style", bumpalo::format!( in bump, - "width: {}; max-width: {}; font-size: {}px; background: {}; border-width: {}px; border-color: {}; border-radius: {}px; color: {}", + "width: {}; max-width: {}; font-size: {}px; \ + background: {}; border-width: {}px; border-color: {}; \ + border-radius: {}px; color: {}", css::length(self.width), css::max_length(self.max_width), self.size.unwrap_or(20), @@ -181,19 +198,9 @@ where ) .into_bump_str(), ) - .attr( - "placeholder", - bumpalo::format!(in bump, "{}", self.placeholder) - .into_bump_str(), - ) - .attr( - "value", - bumpalo::format!(in bump, "{}", self.value).into_bump_str(), - ) - .attr( - "type", - bumpalo::format!(in bump, "{}", if self.is_secure { "password" } else { "text" }).into_bump_str(), - ) + .attr("placeholder", placeholder) + .attr("value", value) + .attr("type", if self.is_secure { "password" } else { "text" }) .on("input", move |_root, _vdom, event| { let text_input = match event.target().and_then(|t| { t.dyn_into::().ok() @@ -206,10 +213,13 @@ where }) .on("keypress", move |_root, _vdom, event| { if let Some(on_submit) = on_submit.clone() { - let event = event.unchecked_into::(); + let event = + event.unchecked_into::(); match event.key_code() { - 13 => { submit_event_bus.publish(on_submit); } + 13 => { + submit_event_bus.publish(on_submit); + } _ => {} } } -- cgit