Use `String::from_str_in` in `iced_web`

This commit is contained in:
Héctor Ramón Jiménez 2020-07-01 07:36:42 +02:00
parent d873c37e31
commit 75464ad894
6 changed files with 67 additions and 40 deletions

View File

@ -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) let mut node = button(bump)
.attr( .attr("class", class)
"class",
bumpalo::format!(in bump, "{}", padding_class).into_bump_str(),
)
.attr( .attr(
"style", "style",
bumpalo::format!( bumpalo::format!(
in bump, in bump,
"background: {}; border-radius: {}px; width:{}; min-width: {}; color: {}", "background: {}; border-radius: {}px; width:{}; \
min-width: {}; color: {}",
background, background,
style.border_radius, style.border_radius,
css::length(self.width), css::length(self.width),

View File

@ -94,9 +94,10 @@ where
style_sheet: &mut Css<'b>, style_sheet: &mut Css<'b>,
) -> dodrio::Node<'b> { ) -> dodrio::Node<'b> {
use dodrio::builder::*; use dodrio::builder::*;
use dodrio::bumpalo::collections::String;
let checkbox_label = 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 event_bus = bus.clone();
let on_toggle = self.on_toggle.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 spacing_class = style_sheet.insert(bump, css::Rule::Spacing(5));
let (label, input) = if let Some(id) = &self.id { 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)) (label(bump).attr("for", id), input(bump).attr("id", id))
} else { } else {

View File

@ -78,14 +78,19 @@ impl<Message> Widget<Message> for Image {
_style_sheet: &mut Css<'b>, _style_sheet: &mut Css<'b>,
) -> dodrio::Node<'b> { ) -> dodrio::Node<'b> {
use dodrio::builder::*; use dodrio::builder::*;
use dodrio::bumpalo::collections::String;
let src = bumpalo::format!(in bump, "{}", match self.handle.data.as_ref() { let src = String::from_str_in(
Data::Path(path) => path.to_str().unwrap_or("") 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(); },
bump,
)
.into_bump_str();
let mut image = let alt = String::from_str_in(&self.alt, bump).into_bump_str();
img(bump).attr("src", src.into_bump_str()).attr("alt", alt);
let mut image = img(bump).attr("src", src).attr("alt", alt);
match self.width { match self.width {
Length::Shrink => {} Length::Shrink => {}

View File

@ -107,15 +107,16 @@ where
_style_sheet: &mut Css<'b>, _style_sheet: &mut Css<'b>,
) -> dodrio::Node<'b> { ) -> dodrio::Node<'b> {
use dodrio::builder::*; use dodrio::builder::*;
use dodrio::bumpalo::collections::String;
let radio_label = 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 event_bus = bus.clone();
let on_click = self.on_click.clone(); let on_click = self.on_click.clone();
let (label, input) = if let Some(id) = &self.id { 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)) (label(bump).attr("for", id), input(bump).attr("id", id))
} else { } else {
@ -123,7 +124,7 @@ where
}; };
let input = if let Some(name) = &self.name { 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) dodrio::builder::input(bump).attr("name", name)
} else { } else {

View File

@ -116,7 +116,12 @@ impl<'a, Message> Widget<Message> for Text {
) -> dodrio::Node<'b> { ) -> dodrio::Node<'b> {
use dodrio::builder::*; 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 let color = self
.color .color
.map(css::color) .map(css::color)
@ -133,7 +138,8 @@ impl<'a, Message> Widget<Message> for Text {
let style = bumpalo::format!( let style = bumpalo::format!(
in bump, in bump,
"width: {}; height: {}; font-size: {}px; color: {}; text-align: {}; font-family: {}", "width: {}; height: {}; font-size: {}px; color: {}; \
text-align: {}; font-family: {}",
width, width,
height, height,
self.size.unwrap_or(20), self.size.unwrap_or(20),

View File

@ -151,8 +151,26 @@ where
use dodrio::builder::*; use dodrio::builder::*;
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;
let padding_class = let class = {
style_sheet.insert(bump, css::Rule::Padding(self.padding)); 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_change = self.on_change.clone();
let on_submit = self.on_submit.clone(); let on_submit = self.on_submit.clone();
@ -161,15 +179,14 @@ where
let style = self.style_sheet.active(); let style = self.style_sheet.active();
input(bump) input(bump)
.attr( .attr("class", class)
"class",
bumpalo::format!(in bump, "{}", padding_class).into_bump_str(),
)
.attr( .attr(
"style", "style",
bumpalo::format!( bumpalo::format!(
in bump, 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::length(self.width),
css::max_length(self.max_width), css::max_length(self.max_width),
self.size.unwrap_or(20), self.size.unwrap_or(20),
@ -181,19 +198,9 @@ where
) )
.into_bump_str(), .into_bump_str(),
) )
.attr( .attr("placeholder", placeholder)
"placeholder", .attr("value", value)
bumpalo::format!(in bump, "{}", self.placeholder) .attr("type", if self.is_secure { "password" } else { "text" })
.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(),
)
.on("input", move |_root, _vdom, event| { .on("input", move |_root, _vdom, event| {
let text_input = match event.target().and_then(|t| { let text_input = match event.target().and_then(|t| {
t.dyn_into::<web_sys::HtmlInputElement>().ok() t.dyn_into::<web_sys::HtmlInputElement>().ok()
@ -206,10 +213,13 @@ where
}) })
.on("keypress", move |_root, _vdom, event| { .on("keypress", move |_root, _vdom, event| {
if let Some(on_submit) = on_submit.clone() { if let Some(on_submit) = on_submit.clone() {
let event = event.unchecked_into::<web_sys::KeyboardEvent>(); let event =
event.unchecked_into::<web_sys::KeyboardEvent>();
match event.key_code() { match event.key_code() {
13 => { submit_event_bus.publish(on_submit); } 13 => {
submit_event_bus.publish(on_submit);
}
_ => {} _ => {}
} }
} }