summaryrefslogtreecommitdiffstats
path: root/web
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2020-07-01 07:09:27 +0200
committerLibravatar GitHub <noreply@github.com>2020-07-01 07:09:27 +0200
commit08e13e00f15fb54ced21c0375e7efba4bfbf89a0 (patch)
tree29d0b9d82307d98fdf7248a9ad59a824dda0c2d1 /web
parent1bc69e7a8a8ea59efc14fd765889895662c9ba46 (diff)
parentffd195cdb543dfbff42327c516d6082cb2df51ef (diff)
downloadiced-08e13e00f15fb54ced21c0375e7efba4bfbf89a0.tar.gz
iced-08e13e00f15fb54ced21c0375e7efba4bfbf89a0.tar.bz2
iced-08e13e00f15fb54ced21c0375e7efba4bfbf89a0.zip
Merge pull request #292 from TomPridham/feature/accessibility-web
add some accessibility features to web widgets
Diffstat (limited to 'web')
-rw-r--r--web/src/widget/checkbox.rs28
-rw-r--r--web/src/widget/image.rs16
-rw-r--r--web/src/widget/radio.rs45
3 files changed, 79 insertions, 10 deletions
diff --git a/web/src/widget/checkbox.rs b/web/src/widget/checkbox.rs
index 5ebc26c8..4d0c7c17 100644
--- a/web/src/widget/checkbox.rs
+++ b/web/src/widget/checkbox.rs
@@ -28,6 +28,7 @@ pub struct Checkbox<Message> {
is_checked: bool,
on_toggle: Rc<dyn Fn(bool) -> Message>,
label: String,
+ id: Option<String>,
width: Length,
style: Box<dyn StyleSheet>,
}
@@ -51,6 +52,7 @@ impl<Message> Checkbox<Message> {
is_checked,
on_toggle: Rc::new(f),
label: label.into(),
+ id: None,
width: Length::Shrink,
style: Default::default(),
}
@@ -71,6 +73,14 @@ impl<Message> Checkbox<Message> {
self.style = style.into();
self
}
+
+ /// Sets the id of the [`Checkbox`].
+ ///
+ /// [`Checkbox`]: struct.Checkbox.html
+ pub fn id(mut self, id: impl Into<String>) -> Self {
+ self.id = Some(id.into());
+ self
+ }
}
impl<Message> Widget<Message> for Checkbox<Message>
@@ -85,7 +95,8 @@ 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 event_bus = bus.clone();
let on_toggle = self.on_toggle.clone();
@@ -95,7 +106,15 @@ where
let spacing_class = style_sheet.insert(bump, css::Rule::Spacing(5));
- label(bump)
+ 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)
@@ -108,7 +127,7 @@ where
)
.children(vec![
// TODO: Checkbox styling
- input(bump)
+ input
.attr("type", "checkbox")
.bool_attr("checked", self.is_checked)
.on("click", move |_root, vdom, _event| {
@@ -118,8 +137,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<T: Into<Handle>>(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<String>) -> Self {
+ self.alt = alt.into();
+ self
+ }
}
impl<Message> Widget<Message> for Image {
@@ -70,8 +82,10 @@ impl<Message> Widget<Message> 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..fae67cd8 100644
--- a/web/src/widget/radio.rs
+++ b/web/src/widget/radio.rs
@@ -35,6 +35,8 @@ pub struct Radio<Message> {
is_selected: bool,
on_click: Message,
label: String,
+ id: Option<String>,
+ name: Option<String>,
style: Box<dyn StyleSheet>,
}
@@ -63,6 +65,8 @@ impl<Message> Radio<Message> {
is_selected: Some(value) == selected,
on_click: f(value),
label: label.into(),
+ id: None,
+ name: None,
style: Default::default(),
}
}
@@ -74,6 +78,22 @@ impl<Message> Radio<Message> {
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<String>) -> Self {
+ self.name = Some(name.into());
+ self
+ }
+
+ /// Sets the id of the [`Radio`] button.
+ ///
+ /// [`Radio`]: struct.Radio.html
+ pub fn id(mut self, id: impl Into<String>) -> Self {
+ self.id = Some(id.into());
+ self
+ }
}
impl<Message> Widget<Message> for Radio<Message>
@@ -88,16 +108,33 @@ 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 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")
.children(vec![
- input(bump)
+ input
.attr("type", "radio")
.attr("style", "margin-right: 10px")
.bool_attr("checked", self.is_selected)
@@ -105,7 +142,7 @@ where
event_bus.publish(on_click.clone());
})
.finish(),
- text(radio_label.into_bump_str()),
+ text(radio_label),
])
.finish()
}