use iced::gradient::Linear;
use iced::widget::canvas::Canvas;
use iced::widget::markdown;
use iced::widget::scrollable::{Direction as ScrollDir, Scrollbar};
use iced::widget::text_editor::Content;
use iced::widget::{
button, checkbox, column, container, mouse_area, pick_list, progress_bar, radio, row, rule,
scrollable, slider, stack, svg, text, text_editor, text_input, toggler, tooltip, Column, Id,
Row, Space, Stack,
};
use iced::{Alignment, Background, Color, Element, Length, Padding, Radians};
use crate::chrome::SCROLL_RAIL_WIDTH;
use crate::host_canvas::{ArcRing, SpinnerDots};
use crate::scroll::{ClipLayer, ScrollRail};
use crate::a11y::{self, A11y, Role};
use crate::collection::{
page_range, virtual_pads, window_after_scroll, window_after_scroll_var, Accordion, ListModel,
RowFace, RowHeights, Selection, Tabs, TreeNode, VisibleWindow,
};
use crate::i18n::Direction;
use crate::icon::{Icon, Icons};
use crate::style;
use crate::theme::Tokens;
use crate::toast::{Toast, ToastKind};
use crate::typo;
use crate::variant::Variant;
fn pad(tok: Tokens) -> Padding {
let p = tok.density.pad;
let v = crate::density::Density::snap(p.saturating_sub(4).max(4));
let h = crate::density::Density::snap(p + 4);
Padding::from([v as f32, h as f32])
}
fn control_height(tok: Tokens) -> f32 {
let line = f32::from(
iced::widget::text::LineHeight::default().to_absolute(iced::Pixels(typo::BODY as f32)),
);
let p = pad(tok);
line + p.top + p.bottom
}
fn icon_label<'a, M: 'a>(title: String, icons: Icons, tok: Tokens) -> Element<'a, M> {
let mut r = Row::new().spacing(8).align_y(Alignment::Center);
if let Some(ic) = icons.leading {
r = r.push(icon_svg(ic, tok, A11y::new(title.clone(), Role::Image)));
}
r = r.push(text(title.clone()).size(typo::BODY));
if let Some(ic) = icons.trailing {
r = r.push(icon_svg(ic, tok, A11y::new(title, Role::Image)));
}
r.into()
}
pub fn slider_step(range: std::ops::RangeInclusive<f32>) -> f32 {
let span = (*range.end() - *range.start()).abs();
if span == 0.0 {
return f32::EPSILON;
}
(span / 100.0).max(span * 1e-6)
}
pub fn icon_style(tok: Tokens) -> impl Fn(&iced::Theme, svg::Status) -> svg::Style {
move |_t, _s| svg::Style {
color: Some(tok.scheme().on_surface),
}
}
pub fn icon_svg<'a, M: 'a>(icon: Icon, tok: Tokens, a11y: A11y) -> Element<'a, M> {
let handle = svg::Handle::from_memory(icon.bytes());
a11y::attach(
svg(handle)
.width(16.0)
.height(16.0)
.style(icon_style(tok))
.into(),
&a11y,
)
}
pub fn label<'a, M: 'a>(s: impl Into<String>, tok: Tokens, a11y: A11y) -> Element<'a, M> {
let s = a11y.apply_name(s);
a11y::attach(
text(s)
.size(typo::BODY)
.color(tok.scheme().on_surface)
.font(typo::UI)
.into(),
&a11y,
)
}
pub fn display_line<'a, M: 'a>(s: impl Into<String>, tok: Tokens, a11y: A11y) -> Element<'a, M> {
let s = a11y.apply_name(s);
a11y::attach(
text(s)
.size(typo::META)
.color(tok.scheme().on_surface_variant)
.font(typo::UI)
.width(Length::Fill)
.align_x(Alignment::End)
.into(),
&a11y,
)
}
pub fn figure_display<'a, M: 'a>(s: impl Into<String>, tok: Tokens, a11y: A11y) -> Element<'a, M> {
let s = a11y.apply_name(s);
let mut r = Row::new().spacing(8).align_y(Alignment::Center);
for ch in s.chars() {
r = r.push(
text(ch.to_string())
.size(typo::DISPLAY)
.font(typo::UI_BOLD)
.color(tok.scheme().on_surface),
);
}
a11y::attach(r.into(), &a11y)
}
pub fn meta<'a, M: 'a>(s: impl Into<String>, tok: Tokens, a11y: A11y) -> Element<'a, M> {
let s = a11y.apply_name(s);
a11y::attach(
text(s)
.size(typo::META)
.color(tok.scheme().on_surface_variant)
.into(),
&a11y,
)
}
pub fn code_block<'a, M: Clone + 'a>(
content: &'a Content,
on_action: impl Fn(text_editor::Action) -> M + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let e = text_editor(content)
.height(Length::Shrink)
.padding(12)
.font(typo::MONO)
.size(typo::CODE)
.wrapping(iced::widget::text::Wrapping::Word)
.style(editor_style(tok))
.on_action(move |a| on_action(select_only(a)));
container(e)
.width(Length::Fill)
.style(move |_| editor_frame(tok))
.id(Id::from(a11y.node_id()))
.into()
}
pub fn hyperlink<'a, M: Clone + 'a>(
title: impl Into<String>,
msg: M,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let title = a11y.apply_name(title);
let mut b = button(text(title).size(typo::BODY).color(tok.scheme().primary))
.padding(0)
.style(style::button_style(tok, Variant::Ghost));
if let Some(m) = a11y.apply_message(Some(msg)) {
b = b.on_press(m);
}
a11y::attach(b.into(), &a11y)
}
pub fn themed_button<'a, M: Clone + 'a>(
title: impl Into<String>,
msg: Option<M>,
tok: Tokens,
variant: Variant,
icons: Icons,
a11y: A11y,
) -> Element<'a, M> {
themed_button_sized(
title,
msg,
tok,
variant,
icons,
Length::Shrink,
Length::Shrink,
a11y,
)
}
#[allow(clippy::too_many_arguments)]
pub fn themed_button_sized<'a, M: Clone + 'a>(
title: impl Into<String>,
msg: Option<M>,
tok: Tokens,
variant: Variant,
icons: Icons,
width: Length,
height: Length,
a11y: A11y,
) -> Element<'a, M> {
let label = a11y.apply_name(title);
let face: Element<'a, M> = if icons == Icons::NONE {
text(label)
.size(typo::BODY)
.width(Length::Fill)
.align_x(Alignment::Center)
.into()
} else {
icon_label(label, icons, tok)
};
let mut b = button(face)
.padding(pad(tok))
.width(width)
.height(height)
.style(style::button_style(tok, variant));
if let Some(m) = a11y.apply_message(msg) {
b = b.on_press(m);
}
a11y::attach(b.into(), &a11y)
}
pub fn split_button<'a, M: Clone + 'a>(
title: impl Into<String>,
primary: M,
overflow: impl IntoIterator<Item = (String, M)>,
tok: Tokens,
icons: Icons,
a11y: A11y,
) -> Element<'a, M> {
let title = a11y.apply_name(title);
let primary_msg = (!a11y.disabled).then_some(primary);
let items: Vec<(String, M)> = overflow.into_iter().collect();
let h = control_height(tok);
a11y::attach(
row![
themed_button_sized(
title.clone(),
primary_msg,
tok,
Variant::Primary,
icons,
Length::Shrink,
Length::Fixed(h),
A11y::button(&title).with_disabled(a11y.disabled),
),
crate::menubar::split_more(items, tok, a11y.disabled, h),
]
.spacing(2)
.align_y(Alignment::Center)
.into(),
&a11y,
)
}
pub fn toggle_button<'a, M: Clone + 'a>(
title: impl Into<String>,
pressed: bool,
msg: M,
tok: Tokens,
icons: Icons,
a11y: A11y,
) -> Element<'a, M> {
let title = title.into();
let a11y = A11y {
checked: Some(pressed),
..a11y
};
themed_button(
title,
(!a11y.disabled).then_some(msg),
tok,
if a11y.apply_checked(pressed) {
Variant::Primary
} else {
Variant::Quiet
},
icons,
a11y,
)
}
pub fn themed_checkbox<'a, M: Clone + 'a>(
label_s: impl Into<String>,
checked: bool,
msg: impl Fn(bool) -> M + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let name = a11y.apply_name(label_s);
let is_on = a11y.apply_checked(checked);
let mut c = checkbox(is_on)
.label(name)
.style(style::checkbox_style(tok));
if !a11y.disabled {
c = c.on_toggle(msg);
}
a11y::attach(c.into(), &a11y)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CheckState {
#[default]
Unchecked,
Checked,
Indeterminate,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cell {
pub label: String,
pub icon: Option<Icon>,
}
impl Cell {
pub fn new(label: impl Into<String>) -> Self {
Self {
label: label.into(),
icon: None,
}
}
pub fn with_icon(mut self, icon: Icon) -> Self {
self.icon = Some(icon);
self
}
}
impl From<String> for Cell {
fn from(label: String) -> Self {
Self::new(label)
}
}
impl From<&str> for Cell {
fn from(label: &str) -> Self {
Self::new(label)
}
}
impl From<&String> for Cell {
fn from(label: &String) -> Self {
Self::new(label.clone())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FieldFace {
#[default]
Filled,
Outlined,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CardFace {
#[default]
Elevated,
Filled,
Outlined,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BadgeSize {
Small,
#[default]
Large,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ChipKind {
#[default]
Assist,
Filter,
Input,
Suggestion,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TooltipAnchor {
#[default]
Follow,
Top,
Bottom,
Start,
}
impl TooltipAnchor {
pub fn position(self) -> tooltip::Position {
match self {
Self::Follow => tooltip::Position::FollowCursor,
Self::Top => tooltip::Position::Top,
Self::Bottom => tooltip::Position::Bottom,
Self::Start => tooltip::Position::Left,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ControlSize {
Compact,
#[default]
Default,
Comfortable,
}
impl ControlSize {
pub fn pad(self) -> u16 {
match self {
Self::Compact => 4,
Self::Default => 8,
Self::Comfortable => 12,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FieldOpts<'a> {
pub face: FieldFace,
pub icons: Icons,
pub label: &'a str,
pub max_len: Option<usize>,
}
impl FieldOpts<'static> {
pub const NONE: Self = Self {
face: FieldFace::Filled,
icons: Icons::NONE,
label: "",
max_len: None,
};
}
impl CheckState {
pub fn toggle(self) -> Self {
match self {
Self::Checked => Self::Unchecked,
Self::Unchecked | Self::Indeterminate => Self::Checked,
}
}
}
pub fn check_state_from_bool(on: bool) -> CheckState {
if on {
CheckState::Checked
} else {
CheckState::Unchecked
}
}
pub fn checkbox_indeterminate<'a, M: Clone + 'a>(
label_s: impl Into<String>,
state: CheckState,
msg: impl Fn(CheckState) -> M + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let name = a11y.apply_name(label_s);
match state {
CheckState::Checked | CheckState::Unchecked => {
let on = matches!(state, CheckState::Checked);
themed_checkbox(
name,
on,
move |next| msg(check_state_from_bool(next)),
tok,
a11y.with_checked(on),
)
}
CheckState::Indeterminate => {
let s = tok.scheme();
let box_face = container(
text("−")
.size(typo::META)
.color(s.on_primary)
.width(Length::Fill)
.align_x(Alignment::Center),
)
.width(16)
.height(16)
.center_x(16)
.center_y(16)
.style(move |_| indeterminate_box_face(tok));
let row = row![
box_face,
text(name.clone()).size(typo::BODY).color(s.on_surface)
]
.spacing(8)
.align_y(Alignment::Center);
if a11y.disabled {
return a11y::attach(row.into(), &a11y);
}
let next = state.toggle();
a11y::attach(mouse_area(row).on_press(msg(next)).into(), &a11y)
}
}
}
fn indeterminate_box_face(tok: Tokens) -> iced::widget::container::Style {
let s = tok.scheme();
let mut st = style::fill(s.primary, s.on_primary);
st.border = iced::border::Border {
color: s.primary,
width: 2.0,
radius: crate::m3::shape::Component::Field.radius(),
};
st
}
pub fn segmented_button<'a, M: Clone + 'a>(
cells: impl IntoIterator<Item = impl Into<Cell>>,
selected: usize,
on_select: impl Fn(usize) -> M + Copy + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let cells: Vec<Cell> = cells.into_iter().map(Into::into).collect();
let mut r = Row::new().spacing(0).align_y(Alignment::Center);
for (i, cell) in cells.iter().enumerate() {
let on = i == selected;
let icons = cell.icon.map(Icons::leading).unwrap_or(Icons::NONE);
let face = themed_button_sized(
cell.label.clone(),
if a11y.disabled {
None
} else {
Some(on_select(i))
},
tok,
if on { Variant::Primary } else { Variant::Quiet },
icons,
Length::Shrink,
Length::Fixed(control_height(tok)),
A11y::button(cell.label.clone())
.with_checked(on)
.with_disabled(a11y.disabled),
);
r = r.push(face);
}
a11y::attach(r.into(), &a11y)
}
pub fn button_group<'a, M: Clone + 'a>(
cells: impl IntoIterator<Item = impl Into<Cell>>,
on_press: impl Fn(usize) -> M + Copy + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let cells: Vec<Cell> = cells.into_iter().map(Into::into).collect();
let mut r = Row::new().spacing(0).align_y(Alignment::Center);
for (i, cell) in cells.iter().enumerate() {
if i > 0 {
r = r.push(
container(Space::new().width(1).height(control_height(tok))).style(move |_| {
style::fill(tok.scheme().outline_variant, tok.scheme().on_surface)
}),
);
}
let icons = cell.icon.map(Icons::leading).unwrap_or(Icons::NONE);
let face = themed_button_sized(
cell.label.clone(),
if a11y.disabled {
None
} else {
Some(on_press(i))
},
tok,
Variant::Quiet,
icons,
Length::Shrink,
Length::Fixed(control_height(tok)),
A11y::button(cell.label.clone()).with_disabled(a11y.disabled),
);
r = r.push(face);
}
a11y::attach(
container(r)
.style(move |_| {
let s = tok.scheme();
let mut st = style::fill(Color::TRANSPARENT, s.on_surface);
st.border = iced::border::Border {
color: s.outline_variant,
width: 1.0,
radius: crate::m3::shape::Component::Button.radius(),
};
st
})
.into(),
&a11y,
)
}
pub fn icon_button<'a, M: Clone + 'a>(
icon: Icon,
msg: Option<M>,
tok: Tokens,
variant: Variant,
size: ControlSize,
a11y: A11y,
) -> Element<'a, M> {
let mut b = button(icon_svg(
icon,
tok,
A11y::new(a11y.name.clone(), Role::Image),
))
.padding(size.pad())
.style(style::button_style(tok, variant));
if let Some(m) = a11y.apply_message(msg) {
b = b.on_press(m);
}
a11y::attach(b.into(), &a11y)
}
pub fn icon_button_toggle<'a, M: Clone + 'a>(
icon: Icon,
pressed: bool,
msg: M,
tok: Tokens,
variant: Variant,
size: ControlSize,
a11y: A11y,
) -> Element<'a, M> {
let a11y = A11y {
checked: Some(pressed),
..a11y
};
icon_button(
icon,
(!a11y.disabled).then_some(msg),
tok,
if a11y.apply_checked(pressed) {
variant
} else {
Variant::Ghost
},
size,
a11y,
)
}
pub fn themed_switch<'a, M: Clone + 'a>(
label_s: impl Into<String>,
on: bool,
msg: impl Fn(bool) -> M + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let name = a11y.apply_name(label_s);
let on = a11y.apply_checked(on);
let mut t = toggler(on).label(name).style(style::switch_style(tok));
if !a11y.disabled {
t = t.on_toggle(msg);
}
a11y::attach(t.into(), &a11y)
}
pub fn themed_radio<'a, V, M: Clone + 'a>(
label_s: impl Into<String>,
value: V,
selected: Option<V>,
msg: impl Fn(V) -> M + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M>
where
V: Copy + Eq,
{
let name = a11y.apply_name(label_s);
if a11y.disabled {
let on = a11y.apply_checked(selected == Some(value));
return a11y::attach(
row![
container(Space::new().width(8).height(8))
.width(16)
.height(16)
.center_x(16)
.center_y(16)
.style(move |_| radio_idle_face(tok, on)),
text(name.clone())
.size(typo::BODY)
.color(tok.scheme().on_surface_variant),
]
.spacing(8)
.align_y(Alignment::Center)
.into(),
&a11y,
);
}
a11y::attach(
radio(name, value, selected, msg)
.style(style::radio_style(tok))
.into(),
&a11y,
)
}
fn radio_idle_face(tok: Tokens, on: bool) -> iced::widget::container::Style {
let s = tok.scheme();
iced::widget::container::Style {
background: Some(iced::Background::Color(if on {
s.primary
} else {
Color::TRANSPARENT
})),
border: iced::border::Border {
color: s.outline,
width: 2.0,
radius: crate::m3::shape::Shape::Full.radius(),
},
..iced::widget::container::Style::default()
}
}
pub fn scroll_delta_pixels(delta: iced::mouse::ScrollDelta, row_h: f32) -> f32 {
match delta {
iced::mouse::ScrollDelta::Lines { y, .. } => -y * row_h.max(0.0),
iced::mouse::ScrollDelta::Pixels { y, .. } => -y,
}
}
pub fn scroll_delta_x(delta: iced::mouse::ScrollDelta) -> f32 {
match delta {
iced::mouse::ScrollDelta::Lines { x, .. } => -x * 32.0,
iced::mouse::ScrollDelta::Pixels { x, .. } => -x,
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct SliderMarks<'a> {
pub ticks: usize,
pub min: &'a str,
pub max: &'a str,
pub vertical: bool,
pub thumb: &'a str,
}
impl SliderMarks<'static> {
pub const NONE: Self = Self {
ticks: 0,
min: "",
max: "",
vertical: false,
thumb: "",
};
}
pub fn themed_slider<'a, M: Clone + 'a>(
range: std::ops::RangeInclusive<f32>,
value: f32,
msg: impl Fn(f32) -> M + 'a,
marks: SliderMarks<'a>,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let slider_el: Element<'a, M> = if a11y.disabled {
let _ = (range, value, msg);
let (w, h) = if marks.vertical {
(Length::Fixed(18.0), Length::Fill)
} else {
(Length::Fill, Length::Fixed(18.0))
};
container(Space::new().width(4).height(4))
.width(w)
.height(h)
.style(move |_| disabled_slider_face(tok))
.into()
} else {
let step = slider_step(range.clone());
if marks.vertical {
iced::widget::vertical_slider(range, value, msg)
.step(step)
.style(style::slider_style(tok))
.height(Length::Fill)
.into()
} else {
slider(range, value, msg)
.step(step)
.style(style::slider_style(tok))
.width(Length::Fill)
.into()
}
};
let s = tok.scheme();
let mut col = Column::new().spacing(2).width(if marks.vertical {
Length::Shrink
} else {
Length::Fill
});
if !marks.thumb.is_empty() {
col = col.push(
text(marks.thumb)
.size(typo::META)
.color(s.on_surface)
.width(Length::Fill),
);
}
if marks.ticks > 1 && !marks.vertical {
let mut ticks = Row::new().width(Length::Fill);
for i in 0..marks.ticks {
if i > 0 {
ticks = ticks.push(Space::new().width(Length::Fill));
}
ticks = ticks.push(
container(Space::new().width(1).height(6))
.style(move |_| style::fill(s.outline_variant, s.on_surface)),
);
}
col = col.push(ticks);
}
if marks.vertical {
col = col.push(container(slider_el).height(Length::Fixed(160.0)).width(32));
} else {
col = col.push(slider_el);
}
if !marks.min.is_empty() || !marks.max.is_empty() {
col = col.push(
row![
text(marks.min)
.size(typo::META)
.color(s.on_surface_variant)
.width(Length::Fill),
text(marks.max).size(typo::META).color(s.on_surface_variant),
]
.width(Length::Fill),
);
}
a11y::attach(col.into(), &a11y)
}
fn disabled_slider_face(tok: Tokens) -> iced::widget::container::Style {
let s = tok.scheme();
style::fill(s.surface_container_highest, s.on_surface_variant)
}
pub fn clamp_range_pair(range: std::ops::RangeInclusive<f32>, low: f32, high: f32) -> (f32, f32) {
let (min, max) = (*range.start(), *range.end());
let low = low.clamp(min, max).min(high.clamp(min, max));
let high = high.clamp(min, max).max(low);
(low, high)
}
pub fn range_pair_after_low(low: f32, high: f32) -> (f32, f32) {
(low.min(high), high)
}
pub fn range_pair_after_high(low: f32, high: f32) -> (f32, f32) {
(low, high.max(low))
}
pub fn range_slider<'a, M: Clone + 'a>(
range: std::ops::RangeInclusive<f32>,
low: f32,
high: f32,
msg: impl Fn((f32, f32)) -> M + Copy + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let (low, high) = clamp_range_pair(range.clone(), low, high);
let lo = themed_slider(
range.clone(),
low,
move |v| msg(range_pair_after_low(v, high)),
SliderMarks::NONE,
tok,
A11y::new(format!("{} low", a11y.name), Role::Slider).with_disabled(a11y.disabled),
);
let hi = themed_slider(
range,
high,
move |v| msg(range_pair_after_high(low, v)),
SliderMarks::NONE,
tok,
A11y::new(format!("{} high", a11y.name), Role::Slider).with_disabled(a11y.disabled),
);
let s = tok.scheme();
a11y::attach(
column![
lo,
text(format!("{low:.0} – {high:.0}"))
.size(typo::META)
.color(s.on_surface_variant),
hi,
]
.spacing(4)
.width(Length::Fill)
.into(),
&a11y,
)
}
pub fn progress_label(value: f32, remaining: Option<&str>) -> String {
let pct = (value.clamp(0.0, 1.0) * 100.0).round() as i32;
match remaining {
Some(r) if !r.is_empty() => format!("{pct}% · {r}"),
_ => format!("{pct}%"),
}
}
pub fn progress<'a, M: 'a>(
value: f32,
buffer: Option<f32>,
copy: Option<&str>,
indeterminate: bool,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let value = value.clamp(0.0, 1.0);
let s = tok.scheme();
let seg = |w: u16, fill: iced::Color, ink: iced::Color| {
container(Space::new().height(8))
.width(Length::FillPortion(w.max(1)))
.style(move |_| style::fill(fill, ink))
};
let mut parts = Row::new().width(Length::Fill);
if indeterminate {
let lead = (value * 70.0).round() as u16;
let mid = 30u16;
let tail = 100u16.saturating_sub(lead.saturating_add(mid)).max(1);
parts = parts
.push(seg(lead, s.surface_container_highest, s.on_surface))
.push(seg(mid, s.primary, s.on_primary))
.push(seg(tail, s.surface_container_highest, s.on_surface));
} else {
let buf = buffer.unwrap_or(value).clamp(0.0, 1.0).max(value);
let v = (value * 100.0).round() as u16;
let b = ((buf * 100.0).round() as u16).saturating_sub(v);
let rest = 100u16.saturating_sub(v.saturating_add(b)).max(1);
parts = parts
.push(seg(v, s.primary, s.on_primary))
.push(seg(b, s.secondary_container, s.on_secondary_container))
.push(seg(rest, s.surface_container_highest, s.on_surface));
};
let bar: Element<'a, M> = container(parts).width(Length::Fill).height(8).into();
let el = if let Some(c) = copy.filter(|t| !t.is_empty()) {
Column::new()
.spacing(4)
.width(Length::Fill)
.push(bar)
.push(meta(c, tok, A11y::new(c, Role::Status)))
.into()
} else {
bar
};
a11y::attach(el, &a11y)
}
pub fn ring_angles(value: f32) -> (f32, f32) {
let v = value.clamp(0.0, 1.0);
let start = -std::f32::consts::FRAC_PI_2;
(start, start + v * std::f32::consts::TAU)
}
pub fn spinner_angles(phase: f32) -> (f32, f32) {
let p = phase.rem_euclid(1.0);
let start = p * std::f32::consts::TAU - std::f32::consts::FRAC_PI_2;
(start, start + std::f32::consts::PI * 1.5)
}
pub fn ring_should_stroke(start: f32, end: f32) -> bool {
(end - start).abs() > 0.001
}
pub fn progress_ring<'a, M: 'a>(
value: f32,
copy: Option<&str>,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let (start, end) = ring_angles(value);
let s = tok.scheme();
let track = s.surface_container_highest;
let ring = Canvas::new(ArcRing {
start,
end,
color: s.primary,
track,
})
.width(56)
.height(56);
let el = if let Some(c) = copy.filter(|s| !s.is_empty()) {
column![ring, meta(c, tok, A11y::new(c, Role::Status))]
.spacing(4)
.width(Length::Fill)
.into()
} else {
ring.into()
};
a11y::attach(el, &a11y)
}
pub fn busy_overlay<'a, M: Clone + 'a>(
child: Element<'a, M>,
busy: bool,
phase: f32,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
if !busy {
return child;
}
a11y::attach(
stack![
child,
container(spinner(tok, phase, A11y::new("busy", Role::Progress)))
.width(Length::Fill)
.height(Length::Fill)
.center_x(Length::Fill)
.center_y(Length::Fill)
.style(move |_| {
let scrim = tok.scheme().scrim;
style::fill(
Color::from_rgba(scrim.r, scrim.g, scrim.b, 0.32),
tok.scheme().on_surface,
)
}),
]
.into(),
&a11y,
)
}
pub fn spinner<'a, M: 'a>(tok: Tokens, phase: f32, a11y: A11y) -> Element<'a, M> {
a11y::attach(
Canvas::new(SpinnerDots {
phase: phase.rem_euclid(1.0),
color: tok.scheme().primary,
})
.width(56)
.height(56)
.into(),
&a11y,
)
}
#[derive(Clone)]
pub enum ImageSlot {
Ready {
handle: iced::widget::image::Handle,
fit: iced::ContentFit,
},
Loading,
Error(String),
}
pub fn image_slot<'a, M: Clone + 'a>(
slot: ImageSlot,
width: impl Into<Length>,
height: impl Into<Length>,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let width = width.into();
let height = height.into();
let box_up = |child: Element<'a, M>| container(child).width(width).height(height);
match slot {
ImageSlot::Ready { handle, fit } => a11y::attach(
container(
iced::widget::image(handle)
.content_fit(fit)
.width(Length::Fill)
.height(Length::Fill),
)
.width(width)
.height(height)
.style(move |_| {
let s = tok.scheme();
style::fill(s.surface_container_high, s.on_surface)
})
.into(),
&a11y,
),
ImageSlot::Loading => a11y::attach(
box_up(
column![
container(Space::new().width(Length::Fill).height(14))
.style(move |_| style::skeleton(tok)),
container(Space::new().width(Length::Fill).height(14))
.style(move |_| style::skeleton(tok)),
]
.spacing(8)
.padding(12)
.into(),
)
.style(move |_| {
let s = tok.scheme();
style::fill(s.surface_container_high, s.on_surface)
})
.into(),
&a11y,
),
ImageSlot::Error(msg) => a11y::attach(
box_up(meta(msg.clone(), tok, A11y::new(msg, Role::Status)))
.center_x(width)
.center_y(height)
.style(move |_| style::callout(tok, ToastKind::Warning))
.into(),
&a11y,
),
}
}
pub fn number_input<'a, M: Clone + 'a>(
value: f64,
on_change: impl Fn(String) -> M + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let shown = format!("{value}");
let mut i = text_input("0", &shown)
.style(style::search_style(tok))
.padding(pad(tok))
.size(typo::BODY);
if !a11y.disabled {
i = i.on_input(on_change);
}
a11y::attach(i.into(), &a11y)
}
pub fn step_number(value: f64, step: f64, min: f64, max: f64, dir: i32) -> f64 {
(value + step * f64::from(dir)).clamp(min, max)
}
#[allow(clippy::too_many_arguments)]
pub fn themed_text_input<'a, M: Clone + 'a>(
placeholder: &str,
value: &str,
on_input: impl Fn(String) -> M + 'a,
on_submit: Option<M>,
opts: FieldOpts<'a>,
tok: Tokens,
a11y: A11y,
input_id: Option<Id>,
) -> Element<'a, M> {
let outlined = matches!(opts.face, FieldFace::Outlined);
let mut i = text_input(placeholder, value)
.style(style::field_style(tok, outlined))
.padding(pad(tok))
.size(typo::BODY);
if let Some(id) = input_id {
i = i.id(id);
}
if !a11y.disabled {
i = i.on_input(on_input);
if let Some(m) = a11y.apply_message(on_submit) {
i = i.on_submit(m);
}
}
let mut field: Element<'a, M> = i.into();
if opts.icons != Icons::NONE {
let mut r = Row::new().spacing(8).align_y(Alignment::Center);
if let Some(ic) = opts.icons.leading {
r = r.push(icon_svg(ic, tok, A11y::new("prefix", Role::Image)));
}
r = r.push(field);
if let Some(ic) = opts.icons.trailing {
r = r.push(icon_svg(ic, tok, A11y::new("suffix", Role::Image)));
}
field = r.width(Length::Fill).into();
}
let mut col = Column::new().spacing(4).width(Length::Fill);
if !opts.label.is_empty() && !value.is_empty() {
col = col.push(
text(opts.label)
.size(typo::META)
.color(tok.scheme().on_surface_variant),
);
}
col = col.push(field);
if let Some(max) = opts.max_len {
let n = value.chars().count();
col = col.push(
text(format!("{n}/{max}"))
.size(typo::META)
.color(tok.scheme().on_surface_variant)
.width(Length::Fill),
);
}
a11y::attach(col.into(), &a11y)
}
pub fn field_support<'a, M: 'a>(
child: Element<'a, M>,
support: Option<&str>,
error: Option<&str>,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let s = tok.scheme();
let mut col = column![child].spacing(4).width(Length::Fill);
if let Some(err) = error.filter(|t| !t.is_empty()) {
col = col.push(
text(err.to_string())
.size(typo::META)
.color(s.error)
.width(Length::Fill),
);
} else if let Some(help) = support.filter(|t| !t.is_empty()) {
col = col.push(
text(help.to_string())
.size(typo::META)
.color(s.on_surface_variant)
.width(Length::Fill),
);
}
a11y::attach(col.into(), &a11y)
}
pub fn suggest_field<'a, M: Clone + 'a>(
placeholder: &str,
value: &str,
on_input: impl Fn(String) -> M + 'a,
suggestions: &'a [String],
on_pick: impl Fn(usize) -> M + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let mut col = column![themed_text_input(
placeholder,
value,
on_input,
None,
FieldOpts::NONE,
tok,
a11y.child(Role::TextBox),
None,
)]
.spacing(2);
for (i, s) in suggestions.iter().enumerate() {
col = col.push(themed_button(
s.clone(),
a11y.apply_message(Some(on_pick(i))),
tok,
Variant::Ghost,
Icons::NONE,
A11y::new(s.clone(), Role::ListItem).with_disabled(a11y.disabled),
));
}
a11y::attach(col.into(), &a11y)
}
pub fn password_input<'a, M: Clone + 'a>(
placeholder: &str,
value: &str,
on_input: impl Fn(String) -> M + 'a,
tok: Tokens,
a11y: A11y,
masked: bool,
) -> Element<'a, M> {
let mut i = text_input(placeholder, value)
.secure(masked)
.style(style::search_style(tok))
.padding(pad(tok))
.size(typo::BODY);
if !a11y.disabled {
i = i.on_input(on_input);
}
a11y::attach(i.into(), &a11y)
}
#[allow(clippy::too_many_arguments)]
pub fn secret_field<'a, M: Clone + 'a>(
placeholder: &str,
value: &str,
on_input: impl Fn(String) -> M + 'a,
revealed: bool,
on_toggle: M,
copy: &crate::action::Action<M>,
tok: Tokens,
dir: Direction,
a11y: A11y,
) -> Element<'a, M> {
let toggle_title = if revealed { "Hide" } else { "Show" };
let field = password_input(
placeholder,
value,
on_input,
tok,
a11y.child(Role::TextBox),
!revealed,
);
let toggle = themed_button(
toggle_title,
Some(on_toggle),
tok,
Variant::Quiet,
Icons::NONE,
A11y::button(toggle_title).with_disabled(a11y.disabled),
);
let copy_btn = themed_button(
copy.title.clone(),
copy.invoke(),
tok,
Variant::Quiet,
Icons::NONE,
A11y::button(copy.title.clone()).with_disabled(!copy.enabled || a11y.disabled),
);
let kids = crate::i18n::order(dir, [field, toggle, copy_btn]);
let mut r = Row::new().spacing(8).align_y(Alignment::Center);
for k in kids {
r = r.push(k);
}
a11y::attach(r.into(), &a11y)
}
#[allow(clippy::too_many_arguments)]
pub fn value_field<'a, M: Clone + 'a>(
title: impl Into<String>,
content: &'a Content,
on_action: impl Fn(text_editor::Action) -> M + 'a,
copy: Option<&crate::action::Action<M>>,
face: typo::FontFace,
label_width: f32,
tok: Tokens,
dir: Direction,
a11y: A11y,
) -> Element<'a, M> {
let title = title.into();
let label = container(meta(
title.clone(),
tok,
a11y.child(Role::Status).with_value(title.clone()),
))
.width(Length::Fixed(label_width.max(1.0)));
let value = container(selectable(
content,
on_action,
tok,
face,
a11y.child(Role::TextBox),
))
.width(Length::Fill);
let mut kids: Vec<Element<'a, M>> = vec![label.into(), value.into()];
if let Some(copy) = copy {
kids.push(themed_button(
copy.title.clone(),
copy.invoke(),
tok,
Variant::Quiet,
Icons::NONE,
A11y::button(copy.title.clone()).with_disabled(!copy.enabled || a11y.disabled),
));
}
let kids = crate::i18n::order(dir, kids);
let mut r = Row::new()
.spacing(8)
.align_y(Alignment::Center)
.width(Length::Fill);
for k in kids {
r = r.push(k);
}
a11y::attach(r.into(), &a11y)
}
pub fn textarea<'a, M: Clone + 'a>(
content: &'a Content,
on_action: impl Fn(text_editor::Action) -> M + 'a,
tok: Tokens,
height: Length,
a11y: A11y,
) -> Element<'a, M> {
let mut e = text_editor(content)
.height(height)
.padding(8)
.size(typo::BODY)
.style(editor_style(tok));
if !a11y.disabled {
e = e.on_action(on_action);
}
container(e)
.width(Length::Fill)
.height(height)
.style(move |_| editor_frame(tok))
.id(Id::from(a11y.node_id()))
.into()
}
pub use crate::select::select_only;
pub fn selectable<'a, M: Clone + 'a>(
content: &'a Content,
on_action: impl Fn(text_editor::Action) -> M + 'a,
tok: Tokens,
face: typo::FontFace,
a11y: A11y,
) -> Element<'a, M> {
let e = text_editor(content)
.height(Length::Shrink)
.padding(0)
.font(face.font())
.size(match face {
typo::FontFace::Mono => typo::CODE,
typo::FontFace::Ui => typo::BODY,
})
.wrapping(iced::widget::text::Wrapping::Word)
.style(selectable_style(tok))
.on_action(move |a| on_action(select_only(a)));
a11y::attach(e.into(), &a11y)
}
fn selectable_style(
tok: Tokens,
) -> impl Fn(&iced::Theme, iced::widget::text_editor::Status) -> iced::widget::text_editor::Style {
move |_t, _s| {
let s = tok.scheme();
iced::widget::text_editor::Style {
background: iced::Background::Color(Color::TRANSPARENT),
border: iced::Border {
color: Color::TRANSPARENT,
width: 0.0,
radius: 0.0.into(),
},
placeholder: s.on_surface_variant,
value: s.on_surface,
selection: s.secondary_container,
}
}
}
pub fn highlighted_code<'a, M: Clone + 'a>(
content: &'a Content,
syntax: &str,
on_action: impl Fn(text_editor::Action) -> M + 'a,
tok: Tokens,
theme_name: &str,
height: Length,
a11y: A11y,
) -> Element<'a, M> {
let theme = crate::theme::code_highlight(theme_name);
let e = text_editor(content)
.height(height)
.padding(8)
.style(editor_style(tok))
.highlight(syntax, theme)
.font(typo::MONO)
.size(typo::CODE)
.on_action(move |a| on_action(select_only(a)));
container(e)
.width(Length::Fill)
.height(height)
.style(move |_| editor_frame(tok))
.id(Id::from(a11y.node_id()))
.into()
}
fn editor_frame(tok: Tokens) -> iced::widget::container::Style {
let s = tok.scheme();
iced::widget::container::Style {
background: Some(iced::Background::Color(s.surface_container_highest)),
border: iced::Border {
color: s.outline_variant,
width: 1.0,
radius: crate::m3::shape::Component::Field.radius(),
},
..iced::widget::container::Style::default()
}
}
pub fn editor_style(
tok: Tokens,
) -> impl Fn(&iced::Theme, iced::widget::text_editor::Status) -> iced::widget::text_editor::Style {
move |_t, _s| {
let s = tok.scheme();
iced::widget::text_editor::Style {
background: iced::Background::Color(s.surface_container_highest),
border: iced::Border {
color: s.outline_variant,
width: 1.0,
radius: crate::m3::shape::Component::Field.radius(),
},
placeholder: s.on_surface_variant,
value: s.on_surface,
selection: s.secondary_container,
}
}
}
pub fn search_input<'a, M: Clone + 'a>(
value: &str,
on_input: impl Fn(String) -> M + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
search_input_clear(value, on_input, None, tok, a11y)
}
pub fn search_input_clear<'a, M: Clone + 'a>(
value: &str,
on_input: impl Fn(String) -> M + 'a,
on_clear: Option<M>,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let mut r = row![
icon_svg(Icon::Search, tok, A11y::new("search", Role::Image)),
themed_text_input(
"Search",
value,
on_input,
None,
FieldOpts::NONE,
tok,
a11y.child(Role::TextBox),
None,
),
]
.spacing(8)
.align_y(Alignment::Center);
if !value.is_empty() {
if let Some(clear) = on_clear {
r = r.push(icon_button(
Icon::Close,
if a11y.disabled { None } else { Some(clear) },
tok,
Variant::Ghost,
ControlSize::Default,
A11y::button("Clear search").with_disabled(a11y.disabled),
));
}
}
a11y::attach(r.into(), &a11y)
}
#[allow(clippy::too_many_arguments)]
pub fn search_view<'a, M: Clone + 'a>(
query: &str,
hits: impl IntoIterator<Item = impl Into<String>>,
on_input: impl Fn(String) -> M + 'a,
on_pick: impl Fn(usize) -> M + Copy + 'a,
on_clear: Option<M>,
empty: &'a str,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let hits: Vec<String> = hits.into_iter().map(Into::into).collect();
let field = search_input_clear(
query,
on_input,
if a11y.disabled { None } else { on_clear },
tok,
A11y::new(a11y.name.clone(), Role::TextBox).with_disabled(a11y.disabled),
);
let body: Element<'a, M> = if hits.is_empty() {
meta(empty, tok, A11y::new(empty, Role::Status))
} else {
let mut col = Column::new().spacing(0).width(Length::Fill);
for (i, hit) in hits.iter().enumerate() {
let hit_a11y = A11y::button(hit.clone()).with_disabled(a11y.disabled);
let mut b = button(
text(hit_a11y.apply_name(hit.clone()))
.size(typo::BODY)
.width(Length::Fill)
.align_x(Alignment::Start),
)
.padding(pad(tok))
.width(Length::Fill)
.style(style::button_style(tok, Variant::Ghost));
if let Some(m) = hit_a11y.apply_message(if a11y.disabled {
None
} else {
Some(on_pick(i))
}) {
b = b.on_press(m);
}
col = col.push(a11y::attach(b.into(), &hit_a11y));
}
col.into()
};
a11y::attach(
column![field, body].spacing(4).width(Length::Fill).into(),
&a11y,
)
}
pub fn themed_pick_list<'a, T, M: Clone + 'a>(
options: impl std::borrow::Borrow<[T]> + 'a,
selected: Option<T>,
on_select: impl Fn(T) -> M + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M>
where
T: ToString + PartialEq + Clone + 'a,
{
if a11y.disabled {
let _ = on_select;
let shown = selected
.as_ref()
.map(ToString::to_string)
.unwrap_or_default();
return a11y::attach(
container(
text(shown)
.size(typo::BODY)
.color(tok.scheme().on_surface_variant),
)
.padding(pad(tok))
.style(move |_| style::panel(tok))
.into(),
&a11y,
);
}
a11y::attach(
pick_list(options, selected, on_select)
.style(style::picker_style(tok))
.padding(pad(tok))
.into(),
&a11y,
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DateValue {
pub year: i32,
pub month: u32,
pub day: u32,
}
impl DateValue {
pub fn clamp(mut self) -> Self {
self.month = self.month.clamp(1, 12);
let max_d = days_in_month(self.year, self.month);
self.day = self.day.clamp(1, max_d);
self
}
}
pub fn days_in_month(year: i32, month: u32) -> u32 {
match month {
2 if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) => 29,
2 => 28,
4 | 6 | 9 | 11 => 30,
_ => 31,
}
}
pub fn date_picker<'a, M: Clone + 'a>(
value: DateValue,
on_prev: M,
on_next: M,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let v = value.clamp();
let shown = format!("{:04}-{:02}-{:02}", v.year, v.month, v.day);
a11y::attach(
row![
themed_button(
"<",
a11y.apply_message(Some(on_prev)),
tok,
Variant::Quiet,
Icons::NONE,
A11y::button("previous-day").with_disabled(a11y.disabled),
),
label(
shown.clone(),
tok,
a11y.child(Role::Status).with_value(shown),
),
themed_button(
">",
a11y.apply_message(Some(on_next)),
tok,
Variant::Quiet,
Icons::NONE,
A11y::button("next-day").with_disabled(a11y.disabled),
),
]
.spacing(8)
.align_y(Alignment::Center)
.into(),
&a11y,
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct TimeValue {
pub hour: u8,
pub minute: u8,
pub second: u8,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeClock {
pub hour12: bool,
pub seconds: bool,
}
impl TimeClock {
pub const HOURS_MINUTES: Self = Self {
hour12: false,
seconds: false,
};
pub const HOURS_MINUTES_SECONDS: Self = Self {
hour12: false,
seconds: true,
};
pub const HOUR12: Self = Self {
hour12: true,
seconds: false,
};
pub const HOUR12_SECONDS: Self = Self {
hour12: true,
seconds: true,
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimeField {
Hour,
Minute,
Second,
Period,
}
impl TimeValue {
pub fn hm(hour: u8, minute: u8) -> Self {
Self {
hour,
minute,
second: 0,
}
.clamp()
}
pub fn hms(hour: u8, minute: u8, second: u8) -> Self {
Self {
hour,
minute,
second,
}
.clamp()
}
pub fn clamp(self) -> Self {
Self {
hour: self.hour.min(23),
minute: self.minute.min(59),
second: self.second.min(59),
}
}
pub fn hour12(self) -> u8 {
match self.clamp().hour % 12 {
0 => 12,
n => n,
}
}
pub fn afternoon(self) -> bool {
self.clamp().hour >= 12
}
pub fn step_hour(self, dir: i32) -> Self {
let v = self.clamp();
let hour = (i32::from(v.hour) + dir).rem_euclid(24) as u8;
Self { hour, ..v }
}
pub fn step_minute(self, dir: i32) -> Self {
let v = self.clamp();
let minute = (i32::from(v.minute) + dir).rem_euclid(60) as u8;
Self { minute, ..v }
}
pub fn step_second(self, dir: i32) -> Self {
let v = self.clamp();
let second = (i32::from(v.second) + dir).rem_euclid(60) as u8;
Self { second, ..v }
}
pub fn step_hour12(self, dir: i32) -> Self {
let v = self.clamp();
let index = (i32::from(v.hour12() % 12) + dir).rem_euclid(12) as u8;
let hour = if v.afternoon() { index + 12 } else { index };
Self { hour, ..v }
}
pub fn toggle_period(self) -> Self {
let v = self.clamp();
let hour = if v.afternoon() {
v.hour - 12
} else {
v.hour + 12
};
Self { hour, ..v }
}
pub fn step_field(self, field: TimeField, clock: TimeClock) -> Self {
match field {
TimeField::Hour if clock.hour12 => self.step_hour12(1),
TimeField::Hour => self.step_hour(1),
TimeField::Minute => self.step_minute(1),
TimeField::Second => self.step_second(1),
TimeField::Period => self.toggle_period(),
}
}
}
fn time_colon<'a, M: 'a>(tok: Tokens) -> Element<'a, M> {
container(
text(":")
.size(typo::BODY)
.font(typo::UI_BOLD)
.color(tok.scheme().on_surface),
)
.padding(Padding {
top: 8.0,
right: 2.0,
bottom: 8.0,
left: 2.0,
})
.into()
}
pub fn time_picker<'a, M: Clone + 'a>(
value: TimeValue,
clock: TimeClock,
on_field: impl Fn(TimeField) -> M + Copy + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let v = value.clamp();
let hour = if clock.hour12 {
format!("{:02}", v.hour12())
} else {
format!("{:02}", v.hour)
};
let mut row = Row::new().spacing(4).align_y(Alignment::Center);
row = row.push(themed_button(
hour,
a11y.apply_message(Some(on_field(TimeField::Hour))),
tok,
Variant::Quiet,
Icons::NONE,
A11y::button("hour").with_disabled(a11y.disabled),
));
row = row.push(time_colon(tok));
row = row.push(themed_button(
format!("{:02}", v.minute),
a11y.apply_message(Some(on_field(TimeField::Minute))),
tok,
Variant::Quiet,
Icons::NONE,
A11y::button("minute").with_disabled(a11y.disabled),
));
if clock.seconds {
row = row.push(time_colon(tok));
row = row.push(themed_button(
format!("{:02}", v.second),
a11y.apply_message(Some(on_field(TimeField::Second))),
tok,
Variant::Quiet,
Icons::NONE,
A11y::button("second").with_disabled(a11y.disabled),
));
}
if clock.hour12 {
row = row.push(themed_button(
if v.afternoon() { "PM" } else { "AM" },
a11y.apply_message(Some(on_field(TimeField::Period))),
tok,
Variant::Quiet,
Icons::NONE,
A11y::button("period").with_disabled(a11y.disabled),
));
}
a11y::attach(row.into(), &a11y)
}
#[derive(Debug, Clone)]
pub struct MarkdownDoc {
pub source: String,
pub hash: u64,
pub items: Vec<markdown::Item>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MdHeading {
pub index: usize,
pub level: u8,
pub title: String,
}
impl MarkdownDoc {
pub fn parse(source: impl Into<String>) -> Self {
parse(&source.into())
}
pub fn headings(&self) -> Vec<MdHeading> {
let style = markdown_measure_style();
self.items
.iter()
.enumerate()
.filter_map(|(index, item)| {
let markdown::Item::Heading(level, text) = item else {
return None;
};
let title: String = text.spans(style).iter().map(|s| s.text.as_ref()).collect();
let level = match level {
markdown::HeadingLevel::H1 => 1,
markdown::HeadingLevel::H2 => 2,
markdown::HeadingLevel::H3 => 3,
markdown::HeadingLevel::H4 => 4,
markdown::HeadingLevel::H5 => 5,
markdown::HeadingLevel::H6 => 6,
};
Some(MdHeading {
index,
level,
title,
})
})
.collect()
}
pub fn item_offset(&self, index: usize) -> f32 {
self.items
.iter()
.take(index)
.map(crate::select::markdown_item_extent)
.sum()
}
}
fn markdown_measure_style() -> markdown::Style {
markdown::Style::from_palette(iced::theme::Palette {
background: iced::Color::BLACK,
text: iced::Color::WHITE,
primary: iced::Color::WHITE,
success: iced::Color::WHITE,
warning: iced::Color::WHITE,
danger: iced::Color::WHITE,
})
}
pub fn markdown_outline<'a, M: Clone + 'a>(
headings: &'a [MdHeading],
selected: Option<usize>,
on_jump: impl Fn(usize) -> M + Copy + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let mut col = Column::new().spacing(2);
for h in headings {
let pad = 8 + u16::from(h.level.saturating_sub(1)) * 8;
let on = selected == Some(h.index);
col = col.push(
container(themed_button(
h.title.clone(),
a11y.apply_message(Some(on_jump(h.index))),
tok,
if on { Variant::Quiet } else { Variant::Ghost },
Icons::NONE,
A11y::button(h.title.clone())
.with_checked(on)
.with_disabled(a11y.disabled),
))
.padding(Padding {
top: 0.0,
right: 0.0,
bottom: 0.0,
left: f32::from(pad),
}),
);
}
if headings.is_empty() {
col = col.push(meta(
"No headings",
tok,
A11y::new("No headings", Role::Status),
));
}
a11y::attach(col.into(), &a11y)
}
pub fn parse(source: &str) -> MarkdownDoc {
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
source.hash(&mut hasher);
MarkdownDoc {
source: source.to_string(),
hash: hasher.finish(),
items: markdown::parse(source).collect(),
}
}
pub fn markdown_view<'a, M: Clone + 'a>(
items: &'a [markdown::Item],
span: Option<&'a crate::select::MarkdownSpan>,
on_pointer: impl Fn(crate::select::MarkdownPointer) -> M + Copy + 'a,
tok: Tokens,
on_link: impl Fn(markdown::Uri) -> M + Copy + 'a,
a11y: A11y,
) -> Element<'a, M> {
let settings = crate::select::markdown_paint_settings(markdown_style(tok));
let body: Element<'a, M> = match span.copied().filter(|s| !s.is_empty()) {
None => iced_selection::markdown::view(items, settings).map(on_link),
Some(span) => {
let mut col = Column::new().spacing(settings.spacing).width(Length::Fill);
for (i, item) in items.iter().enumerate() {
let block = iced_selection::markdown::view(std::slice::from_ref(item), settings)
.map(on_link);
let block: Element<'a, M> = if span.covers(i) {
container(block)
.width(Length::Fill)
.style(move |_| style::list_row(tok, true))
.into()
} else {
block
};
col = col.push(block);
}
col.into()
}
};
a11y::attach(
mouse_area(body)
.on_move(move |p| on_pointer(crate::select::MarkdownPointer::Move(p.y)))
.on_press(on_pointer(crate::select::MarkdownPointer::Press))
.on_release(on_pointer(crate::select::MarkdownPointer::Release))
.into(),
&a11y,
)
}
fn markdown_style(tok: Tokens) -> markdown::Style {
let s = tok.scheme();
let mut style = markdown::Style::from_palette(iced::theme::Palette {
background: s.surface,
text: s.on_surface,
primary: s.primary,
success: s.success,
warning: s.warning,
danger: s.error,
});
style.font = typo::UI;
style.inline_code_color = s.on_surface;
style.inline_code_font = typo::MONO;
style.code_block_font = typo::MONO;
style.link_color = s.primary;
style.inline_code_highlight.background = iced::Background::Color(s.surface_container_high);
style
}
pub fn tooltip_wrap<'a, M: 'a>(
child: Element<'a, M>,
tip: impl Into<String>,
anchor: TooltipAnchor,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let tip = a11y.apply_name(tip);
a11y::attach(
tooltip(
child,
container(meta(tip.clone(), tok, A11y::new(tip, Role::Tooltip)))
.padding(6)
.style(tip_style(tok)),
anchor.position(),
)
.into(),
&a11y,
)
}
pub fn tooltip_rich<'a, M: Clone + 'a>(
child: Element<'a, M>,
title: impl Into<String>,
body: impl Into<String>,
action: Option<(String, M)>,
anchor: TooltipAnchor,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let title = title.into();
let body = body.into();
if title.is_empty() && body.is_empty() && action.is_none() {
return child;
}
let mut col = Column::new().spacing(2);
if !title.is_empty() {
col = col.push(label(title.clone(), tok, A11y::new(title, Role::Header)));
}
if !body.is_empty() {
col = col.push(meta(body.clone(), tok, A11y::new(body, Role::Status)));
}
if let Some((t, m)) = action {
col = col.push(themed_button(
t.clone(),
Some(m),
tok,
Variant::Ghost,
Icons::NONE,
A11y::button(t),
));
}
a11y::attach(
tooltip(
child,
container(col).padding(8).style(tip_style(tok)),
anchor.position(),
)
.into(),
&a11y,
)
}
pub fn rule_h<'a, M: 'a>(tok: Tokens, a11y: A11y) -> Element<'a, M> {
a11y::attach(
rule::horizontal(1).style(style::rule_style(tok)).into(),
&a11y,
)
}
pub fn dismiss_button<'a, M: Clone + 'a>(msg: M, tok: Tokens, a11y: A11y) -> Element<'a, M> {
let mut b = button(icon_svg(Icon::Close, tok, A11y::new("close", Role::Image)))
.padding(4)
.style(style::button_style(tok, Variant::Ghost));
if let Some(m) = a11y.apply_message(Some(msg)) {
b = b.on_press(m);
}
a11y::attach(b.into(), &a11y)
}
fn chip_wash(tok: Tokens, variant: Variant) -> iced::Color {
chip_face(tok, variant).0
}
pub fn chip_face(
tok: Tokens,
variant: Variant,
) -> (iced::Color, iced::Color, iced::border::Border) {
let s = tok.scheme();
let r = crate::m3::shape::Component::Chip.radius();
match variant {
Variant::Primary | Variant::Chip => (
s.primary,
s.on_primary,
iced::border::Border {
color: Color::TRANSPARENT,
width: 0.0,
radius: r,
},
),
Variant::Quiet | Variant::Ghost => (
Color::TRANSPARENT,
s.on_surface,
iced::border::Border {
color: s.outline,
width: 1.0,
radius: r,
},
),
Variant::Danger => (
s.error_container,
s.on_error_container,
iced::border::Border {
color: Color::TRANSPARENT,
width: 0.0,
radius: r,
},
),
Variant::Success => (
crate::theme::mix(s.success, s.surface, 0.20),
s.on_surface,
iced::border::Border {
color: Color::TRANSPARENT,
width: 0.0,
radius: r,
},
),
Variant::Warning => (
crate::theme::mix(s.warning, s.surface, 0.20),
s.on_surface,
iced::border::Border {
color: Color::TRANSPARENT,
width: 0.0,
radius: r,
},
),
Variant::Outlined | Variant::Elevated => (
Color::TRANSPARENT,
s.on_surface,
iced::border::Border {
color: s.outline,
width: 1.0,
radius: r,
},
),
}
}
#[allow(clippy::too_many_arguments)]
pub fn chip<'a, M: Clone + 'a>(
title: impl Into<String>,
press: Option<M>,
dismiss: Option<M>,
tok: Tokens,
variant: Variant,
kind: ChipKind,
icons: Icons,
a11y: A11y,
) -> Element<'a, M> {
let title = a11y.apply_name(title);
let variant = match kind {
ChipKind::Assist => Variant::Chip,
ChipKind::Filter => variant,
ChipKind::Input | ChipKind::Suggestion => Variant::Quiet,
};
let (wash, ink, border) = chip_face(tok, variant);
let mut line = Row::new().spacing(4).align_y(Alignment::Center);
if let Some(ic) = icons.leading {
line = line.push(icon_svg(ic, tok, A11y::new(title.clone(), Role::Image)));
}
line = line.push(text(title.clone()).size(typo::META).color(ink));
if let Some(msg) = dismiss {
line = line.push(dismiss_button(
msg,
tok,
A11y::button(format!("dismiss {title}")).with_disabled(a11y.disabled),
));
}
let face = container(line).padding([8, 12]).style(move |_| {
let mut st = style::fill(wash, ink);
st.border = border;
st
});
let body: Element<'a, M> = if let Some(msg) = a11y.apply_message(press) {
mouse_area(face).on_press(msg).into()
} else {
face.into()
};
a11y::attach(body, &a11y)
}
pub fn filter_chips<'a, M: Clone + 'a>(
labels: &[String],
selected: &[bool],
on_toggle: impl Fn(usize) -> M + Copy + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let mut r = Row::new().spacing(8).align_y(Alignment::Center);
for (i, label) in labels.iter().enumerate() {
let on = selected.get(i).copied().unwrap_or(false);
r = r.push(chip(
label.clone(),
if a11y.disabled {
None
} else {
Some(on_toggle(i))
},
None,
tok,
if on { Variant::Primary } else { Variant::Quiet },
ChipKind::Filter,
Icons::NONE,
A11y::button(label.clone())
.with_checked(on)
.with_disabled(a11y.disabled),
));
}
a11y::attach(r.into(), &a11y)
}
pub fn badge<'a, M: 'a>(
title: impl Into<String>,
host: Option<Element<'a, M>>,
tok: Tokens,
variant: Variant,
size: BadgeSize,
a11y: A11y,
) -> Element<'a, M> {
let title = a11y.apply_name(title);
let s = tok.scheme();
let ink = match variant {
Variant::Danger => s.error,
Variant::Primary => s.primary,
Variant::Success => s.success,
Variant::Warning => s.warning,
_ => s.on_surface_variant,
};
let wash = chip_wash(tok, variant);
let pad = match size {
BadgeSize::Small => [2, 5],
BadgeSize::Large => [4, 8],
};
let type_size = match size {
BadgeSize::Small => typo::META.saturating_sub(2).max(10),
BadgeSize::Large => typo::META,
};
let mark: Element<'a, M> = container(text(title).size(type_size).color(ink))
.padding(pad)
.style(move |_| style::fill(wash, ink))
.into();
let body = if let Some(child) = host {
Stack::new()
.push(child)
.push(container(mark).width(Length::Fill).align_x(Alignment::End))
.into()
} else {
mark
};
a11y::attach(body, &a11y)
}
pub fn group_box<'a, M: 'a>(
title: impl Into<String>,
child: Element<'a, M>,
tok: Tokens,
face: CardFace,
a11y: A11y,
) -> Element<'a, M> {
let title = a11y.apply_name(title);
a11y::attach(
container(
column![
meta(title.clone(), tok, A11y::new(title, Role::Header)),
child
]
.spacing(8),
)
.padding(16)
.width(Length::Fill)
.style(move |_| match face {
CardFace::Elevated => style::raised_card(tok),
CardFace::Filled => style::card(tok, false),
CardFace::Outlined => style::outlined_card(tok),
})
.into(),
&a11y,
)
}
pub fn banner<'a, M: Clone + 'a>(
text_s: impl Into<String>,
action: Option<(String, M)>,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let text_s = a11y.apply_name(text_s);
let mut r = row![label(text_s.clone(), tok, A11y::new(text_s, Role::Status))]
.spacing(12)
.align_y(Alignment::Center);
if let Some((t, m)) = action {
r = r.push(themed_button(
t.clone(),
a11y.apply_message(Some(m)),
tok,
Variant::Quiet,
Icons::NONE,
A11y::button(t).with_disabled(a11y.disabled),
));
}
a11y::attach(
container(r)
.width(Length::Fill)
.padding(12)
.style(move |_| style::callout(tok, ToastKind::Info))
.into(),
&a11y,
)
}
pub fn info_bar<'a, M: Clone + 'a>(
kind: ToastKind,
text_s: impl Into<String>,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let text_s = a11y.apply_name(text_s);
a11y::attach(
container(label(text_s.clone(), tok, A11y::new(text_s, Role::Status)))
.width(Length::Fill)
.padding(10)
.style(move |_| style::callout(tok, kind))
.into(),
&a11y,
)
}
pub fn breadcrumb<'a, M: Clone + 'a>(
parts: &[(String, Option<M>)],
tok: Tokens,
dir: Direction,
a11y: A11y,
) -> Element<'a, M> {
let parts = crate::i18n::order(dir, parts.iter().cloned());
let mut r = Row::new().spacing(6).align_y(Alignment::Center);
for (i, (name, msg)) in parts.iter().enumerate() {
if i > 0 {
r = r.push(meta("/", tok, A11y::new("/", Role::Separator)));
}
if let Some(m) = msg.clone() {
r = r.push(hyperlink(
name.clone(),
m,
tok,
A11y::new(name.clone(), Role::Link).with_disabled(a11y.disabled),
));
} else {
r = r.push(label(
name.clone(),
tok,
A11y::new(name.clone(), Role::Header),
));
}
}
a11y::attach(r.into(), &a11y)
}
pub fn toast_view<'a, M: Clone + 'a>(
toast: &Toast,
dismiss: M,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let kind = toast.kind;
let text_s = toast.text.clone();
a11y::attach(
container(
row![
label(text_s.clone(), tok, A11y::new(text_s, Role::Status),),
Space::new().width(Length::Fill),
dismiss_button(
dismiss,
tok,
A11y::button("dismiss").with_disabled(a11y.disabled),
),
]
.spacing(8)
.align_y(Alignment::Center),
)
.width(Length::Fill)
.padding([8, 12])
.style(toast_style(tok, kind))
.into(),
&a11y,
)
}
fn tip_style(tok: Tokens) -> impl Fn(&iced::Theme) -> iced::widget::container::Style {
move |_| style::raised_card(tok)
}
fn toast_style(
tok: Tokens,
kind: ToastKind,
) -> impl Fn(&iced::Theme) -> iced::widget::container::Style {
move |_| style::callout(tok, kind)
}
pub fn themed_scroll<'a, M, F>(
child: Element<'a, M>,
tok: Tokens,
a11y: A11y,
stick: bool,
scroll_id: Option<Id>,
on_scroll: Option<F>,
) -> Element<'a, M>
where
M: 'a,
F: Fn(iced::widget::scrollable::Viewport) -> M + 'a,
{
let mut s = scrollable(child)
.height(Length::Fill)
.direction(ScrollDir::Vertical(
Scrollbar::new()
.width(SCROLL_RAIL_WIDTH)
.scroller_width(SCROLL_RAIL_WIDTH),
))
.style(style::scroll_style(tok));
if stick {
s = s.anchor_bottom();
}
if let Some(id) = scroll_id {
s = s.id(id);
}
if let Some(f) = on_scroll {
s = s.on_scroll(f);
}
a11y::attach(s.into(), &a11y)
}
#[allow(clippy::too_many_arguments)]
pub fn log_view<'a, M: Clone + 'a>(
lines: &'a [String],
window: VisibleWindow,
row_h: f32,
overscan: usize,
on_scroll: impl Fn(VisibleWindow) -> M + Copy + 'a,
scroll_id: Option<Id>,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let n = lines.len();
let h = row_h.max(0.0);
let viewport = window.viewport.max(1.0);
let scroll = if window.end == 0 {
stick_scroll_snapped(n, h, viewport)
} else {
window.scroll.max(0.0)
};
let (top, win, bot) = virtual_pads(n, h, scroll, viewport, overscan, None);
let align_pad = stick_align_pad(n, h, viewport);
let mut col = Column::new().spacing(0);
if n == 0 {
col = col.push(meta("No lines", tok, A11y::new("No lines", Role::Status)));
} else {
col = col.push(Space::new().height(Length::Fixed(top)));
for i in win.range() {
let line = lines.get(i).map(String::as_str).unwrap_or("");
col = col.push(
container(
text(line.to_string())
.size(typo::META)
.color(tok.scheme().on_surface)
.font(typo::MONO),
)
.width(Length::Fill)
.height(h)
.padding([2, 8])
.clip(true),
);
}
col = col.push(Space::new().height(Length::Fixed(bot + align_pad)));
}
let prev = window;
themed_scroll(
col.into(),
tok,
a11y,
true,
scroll_id,
Some(move |vp: iced::widget::scrollable::Viewport| {
on_scroll(window_after_scroll(
prev,
vp.absolute_offset_reversed().y,
vp.bounds().height,
h,
n,
overscan,
None,
))
}),
)
}
#[allow(clippy::too_many_arguments)]
fn virtual_clip<'a, M, F, G>(
prev: VisibleWindow,
heights: RowHeights<'a>,
len: usize,
overscan: usize,
cover: Option<usize>,
on_scroll: F,
scroll_id: Option<Id>,
tok: Tokens,
rows: G,
) -> Element<'a, M>
where
M: Clone + 'a,
F: Fn(VisibleWindow) -> M + Copy + 'a,
G: Fn(VisibleWindow) -> Column<'a, M> + 'a,
{
let content = heights.total(len);
let step = heights.at(0).max(1.0);
iced::widget::responsive(move |size| {
let viewport = if size.height > 0.0 {
size.height
} else {
prev.viewport.max(1.0)
};
let win = match heights {
RowHeights::Uniform(h) => window_after_scroll(
prev,
prev.scroll,
viewport,
h.max(0.0),
len,
overscan,
cover,
),
RowHeights::PerRow(hs) => {
window_after_scroll_var(prev, prev.scroll, viewport, hs, overscan, cover)
}
};
let scroll = win.scroll;
let shift = heights.offset(win.start) - scroll;
let emit = move |y: f32| {
on_scroll(match heights {
RowHeights::Uniform(h) => {
window_after_scroll(prev, y, viewport, h.max(0.0), len, overscan, cover)
}
RowHeights::PerRow(hs) => {
window_after_scroll_var(prev, y, viewport, hs, overscan, cover)
}
})
};
let mut inner = container(rows(win))
.width(crate::layout::FILL)
.padding(Padding {
top: shift,
right: 0.0,
bottom: 0.0,
left: 0.0,
});
if let Some(id) = scroll_id.clone() {
inner = inner.id(id);
}
let frame = container(inner)
.width(crate::layout::FILL)
.height(crate::layout::FILL);
let pane = mouse_area(ClipLayer::new(frame)).on_scroll(move |delta| {
let max_s = (content - viewport).max(0.0);
emit((scroll + scroll_delta_pixels(delta, step)).clamp(0.0, max_s))
});
row![
pane,
Element::from(ScrollRail::new(content, viewport, scroll, emit, tok)),
]
.spacing(4)
.width(crate::layout::FILL)
.height(crate::layout::FILL)
.into()
})
.into()
}
fn stick_scroll_snapped(n: usize, row_h: f32, viewport: f32) -> f32 {
let max_s = crate::layout::end_offset(n as f32 * row_h, viewport);
if row_h > 0.0 {
(max_s / row_h).floor() * row_h
} else {
max_s
}
}
fn stick_align_pad(n: usize, row_h: f32, viewport: f32) -> f32 {
if row_h <= 0.0 {
return 0.0;
}
let max_s = crate::layout::end_offset(n as f32 * row_h, viewport);
if max_s <= 0.0 {
return 0.0;
}
let r = max_s % row_h;
if r < 1e-3 {
0.0
} else {
row_h - r
}
}
fn ellipsize_line(s: &str, max_chars: usize) -> String {
let chars: Vec<char> = s.chars().collect();
if max_chars == 0 || chars.len() <= max_chars {
return s.to_string();
}
let keep = max_chars.saturating_sub(1);
format!("{}…", chars[..keep].iter().collect::<String>())
}
fn row_slot_el<'a, M: Clone + 'a>(
slot: crate::collection::RowSlot,
index: usize,
on_check: impl Fn(usize) -> M + Copy + 'a,
tok: Tokens,
disabled: bool,
) -> Element<'a, M> {
match slot {
crate::collection::RowSlot::Empty => Space::new().width(0).height(16).into(),
crate::collection::RowSlot::Icon(icon) => {
icon_svg(icon, tok, A11y::new("row-icon", Role::Image))
}
crate::collection::RowSlot::Check(on) => {
let s = tok.scheme();
let face = container(Space::new().width(10).height(10))
.width(16)
.height(16)
.center_x(16)
.center_y(16)
.style(move |_| {
if on {
style::fill(s.primary, s.on_primary)
} else {
let mut st = style::fill(Color::TRANSPARENT, s.on_surface);
st.border = iced::border::Border {
color: s.outline,
width: 2.0,
radius: crate::m3::shape::Component::Field.radius(),
};
st
}
});
if disabled {
face.into()
} else {
mouse_area(face).on_press(on_check(index)).into()
}
}
}
}
fn two_line_row<'a, M: 'a>(
title: &str,
meta_s: Option<&str>,
meta_color: iced::Color,
selected: bool,
row_h: f32,
tok: Tokens,
) -> Element<'a, M> {
let title = ellipsize_line(title, 42);
let on = tok.scheme().on_surface;
let mut col = column![text(title)
.size(typo::BODY)
.color(on)
.font(typo::UI)
.width(Length::Fill)
.wrapping(iced::widget::text::Wrapping::None)]
.spacing(2)
.width(Length::Fill);
if let Some(m) = meta_s.filter(|s| !s.is_empty()) {
col = col.push(
text(ellipsize_line(m, 48))
.size(typo::META)
.color(meta_color)
.width(Length::Fill)
.wrapping(iced::widget::text::Wrapping::None),
);
}
container(col)
.width(Length::Fill)
.height(row_h)
.padding(Padding {
top: 8.0,
right: 12.0,
bottom: 8.0,
left: 8.0,
})
.clip(true)
.style(move |_| style::list_row(tok, selected))
.into()
}
fn card_row<'a, M: 'a>(
title: &str,
meta_s: Option<&str>,
meta_color: iced::Color,
selected: bool,
row_h: f32,
meter: Option<f32>,
tok: Tokens,
) -> Element<'a, M> {
let on = tok.scheme().on_surface;
let mut col = column![text(title.to_string())
.size(typo::BODY)
.color(on)
.font(if selected { typo::UI_BOLD } else { typo::UI })
.width(Length::Fill)
.wrapping(iced::widget::text::Wrapping::Word)]
.spacing(2)
.width(Length::Fill);
if let Some(m) = meta_s.filter(|s| !s.is_empty()) {
col = col.push(
text(m.to_string())
.size(typo::META)
.color(meta_color)
.width(Length::Fill),
);
}
if let Some(v) = meter {
col = col.push(
progress_bar(0.0..=1.0, v.clamp(0.0, 1.0))
.girth(3)
.style(style::progress_style(tok)),
);
}
container(col)
.width(Length::Fill)
.height(row_h)
.padding(Padding {
top: 8.0,
right: 12.0,
bottom: 8.0,
left: 12.0,
})
.clip(true)
.style(move |_| style::card(tok, selected))
.into()
}
#[allow(clippy::too_many_arguments)]
pub fn virtual_column<'a, M: Clone + 'a>(
heights: &'a [f32],
window: VisibleWindow,
overscan: usize,
cover: Option<usize>,
on_scroll: impl Fn(VisibleWindow) -> M + Copy + 'a,
scroll_id: Option<Id>,
tok: Tokens,
row: impl Fn(usize) -> Element<'a, M> + 'a,
a11y: A11y,
) -> Element<'a, M> {
let len = heights.len();
let prev = window;
a11y::attach(
virtual_clip(
prev,
RowHeights::PerRow(heights),
len,
overscan,
cover,
on_scroll,
scroll_id,
tok,
move |win| {
let mut col = Column::new();
for i in win.range() {
let h = heights.get(i).copied().unwrap_or(0.0);
col = col.push(container(row(i)).width(Length::Fill).height(h).clip(true));
}
col
},
),
&a11y,
)
}
#[allow(clippy::too_many_arguments)]
pub fn list_view<'a, M, L>(
model: &'a L,
selection: &'a Selection,
on_select: impl Fn(usize) -> M + Copy + 'a,
tok: Tokens,
window: VisibleWindow,
row_h: impl Into<RowHeights<'a>>,
overscan: usize,
on_scroll: impl Fn(VisibleWindow) -> M + Copy + 'a,
empty: &'a str,
meta_color: impl Fn(usize) -> iced::Color + Copy + 'a,
scroll_id: Option<Id>,
face: RowFace<impl Fn(usize) -> f32 + Copy + 'a>,
on_check: impl Fn(usize) -> M + Copy + 'a,
a11y: A11y,
) -> Element<'a, M>
where
M: Clone + 'a,
L: ListModel + ?Sized,
{
let cover = selection.primary();
let heights = row_h.into();
let len = model.len();
let prev = window;
let disabled = a11y.disabled;
a11y::attach(
virtual_clip(
prev,
heights,
len,
overscan,
cover,
on_scroll,
scroll_id,
tok,
move |win| {
let gap = match face {
RowFace::Flush => 0.0,
RowFace::Card { .. } => 2.0,
};
let mut col = Column::new().spacing(gap);
if model.is_empty() {
col = col.push(meta(empty, tok, A11y::new(empty, Role::Status)));
} else {
for i in win.range() {
let h = heights.at(i);
if model.is_separator(i) {
col = col.push(
container(rule_h(tok, A11y::new("sep", Role::Separator)))
.width(Length::Fill)
.height(h)
.center_y(h),
);
continue;
}
let selected = selection.contains(i);
let title = model.title(i);
let meta_s = model.meta(i);
let name = title.to_string();
let painted = match face {
RowFace::Flush => {
two_line_row(title, meta_s, meta_color(i), selected, h, tok)
}
RowFace::Card { meter } => card_row(
title,
meta_s,
meta_color(i),
selected,
h,
meter.map(|m| m(i)),
tok,
),
};
let body: Element<'a, M> = if disabled {
painted
} else {
mouse_area(painted)
.on_press(on_select(i))
.on_right_press(on_select(i))
.into()
};
let row: Element<'a, M> = row![
row_slot_el(model.leading(i), i, on_check, tok, disabled),
body,
row_slot_el(model.trailing(i), i, on_check, tok, disabled),
]
.spacing(4)
.align_y(Alignment::Center)
.into();
col = col.push(a11y::attach(
row,
&A11y::new(name, Role::ListItem)
.with_checked(selected)
.with_disabled(disabled),
));
}
}
col
},
),
&a11y,
)
}
pub fn item_grid<'a, M: Clone + 'a>(
labels: &[String],
on_select: impl Fn(usize) -> M + Copy + 'a,
selected: Option<usize>,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let cols = 3;
let mut rows = iced::widget::Column::new()
.spacing(8)
.width(Length::Fill)
.height(Length::Fill);
let mut i = 0;
while i < labels.len() {
let mut r = iced::widget::Row::new()
.spacing(8)
.width(Length::Fill)
.height(Length::Fill);
for _ in 0..cols {
if i < labels.len() {
let s = labels[i].clone();
let on = selected == Some(i);
let tile = themed_button_sized(
s.clone(),
a11y.apply_message(Some(on_select(i))),
tok,
if on { Variant::Quiet } else { Variant::Ghost },
Icons::NONE,
Length::Fill,
Length::Fill,
A11y::new(s.clone(), Role::ListItem)
.with_checked(on)
.with_disabled(a11y.disabled),
);
r = r.push(if a11y.disabled {
tile
} else {
mouse_area(tile).on_right_press(on_select(i)).into()
});
i += 1;
} else {
r = r.push(Space::new().width(Length::Fill).height(Length::Fill));
}
}
rows = rows.push(r);
}
a11y::attach(rows.into(), &a11y)
}
#[allow(clippy::too_many_arguments)]
pub fn data_table<'a, M, T>(
model: &'a T,
selection: &'a Selection,
cursor: Option<(usize, usize)>,
columns: &'a crate::collection::ColumnLayout,
zebra: bool,
window: VisibleWindow,
row_h: f32,
overscan: usize,
on_cell: impl Fn(usize, usize) -> M + Copy + 'a,
on_sort: impl Fn(usize) -> M + Copy + 'a,
on_scroll: impl Fn(VisibleWindow) -> M + Copy + 'a,
on_h_scroll: impl Fn(f32) -> M + Copy + 'a,
on_check: impl Fn(usize) -> M + Copy + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M>
where
M: Clone + 'a,
T: crate::collection::TableSource + ?Sized,
{
let cover = selection.primary();
let n = model.row_count();
let order = columns.display();
let last = order.last().copied();
let h = row_h.max(0.0);
let prev = window;
let disabled = a11y.disabled;
let show_checks = (0..n.max(1)).any(|r| model.row_checked(r).is_some());
let frozen_n = columns.frozen.min(order.len());
let pin = order[..frozen_n].to_vec();
let rest = order[frozen_n..].to_vec();
let h_scroll = columns.h_scroll.max(0.0);
let last_col = last;
let col_w = move |c: usize| {
if Some(c) == last_col {
Length::Fill
} else {
Length::Fixed(columns.width(c))
}
};
let mut pin_head = Row::new().spacing(0);
if show_checks {
pin_head = pin_head.push(
container(Space::new().width(16).height(16))
.width(36)
.center_x(36)
.center_y(32),
);
}
for c in &pin {
let c = *c;
let title = model.header(c).to_string();
pin_head = pin_head.push(
container(themed_button(
title.clone(),
a11y.apply_message(Some(on_sort(c))),
tok,
Variant::Ghost,
Icons::NONE,
A11y::button(title).with_disabled(disabled),
))
.width(col_w(c)),
);
}
let mut rest_head = Row::new().spacing(0);
for c in &rest {
let c = *c;
let title = model.header(c).to_string();
rest_head = rest_head.push(
container(themed_button(
title.clone(),
a11y.apply_message(Some(on_sort(c))),
tok,
Variant::Ghost,
Icons::NONE,
A11y::button(title).with_disabled(disabled),
))
.width(col_w(c)),
);
}
let rest_head = mouse_area(
container(rest_head)
.width(Length::Fill)
.padding(Padding {
top: 0.0,
right: 0.0,
bottom: 0.0,
left: -h_scroll,
})
.clip(true),
)
.on_scroll(move |delta| on_h_scroll((h_scroll + scroll_delta_x(delta)).max(0.0)));
let header = row![pin_head, rest_head].width(crate::layout::FILL);
a11y::attach(
column![
header,
virtual_clip(
prev,
RowHeights::Uniform(h),
n,
overscan,
cover,
on_scroll,
None,
tok,
move |win| {
let mut body = Column::new().spacing(0);
for i in win.range() {
let selected = selection.contains(i);
let stripe = zebra && i % 2 == 1;
let paint_cell = |line: Row<'a, M>, c: usize| {
let focused = cursor == Some((i, c));
let value = model.cell(i, c).to_string();
let w = col_w(c);
let cell_style = style::table_cell(tok, selected, focused, stripe);
let ink = cell_style.text_color.unwrap_or(tok.scheme().on_surface);
let face = container(text(value.clone()).size(typo::BODY).color(ink))
.width(w)
.height(h)
.padding([8, 8])
.style(move |_| cell_style);
let cell: Element<'a, M> = if disabled {
face.into()
} else {
mouse_area(face)
.on_press(on_cell(i, c))
.on_right_press(on_cell(i, c))
.into()
};
line.push(a11y::attach(
cell,
&A11y::new(format!("{i}:{c}"), Role::ListItem)
.with_value(value)
.with_checked(focused || selected)
.with_disabled(disabled),
))
};
let mut pin_line = Row::new().spacing(0);
if show_checks {
let on = model.row_checked(i).unwrap_or(false);
pin_line = pin_line.push(
container(row_slot_el(
crate::collection::RowSlot::Check(on),
i,
on_check,
tok,
disabled,
))
.width(36)
.center_x(36)
.center_y(h),
);
}
for c in &pin {
pin_line = paint_cell(pin_line, *c);
}
let mut rest_line = Row::new().spacing(0);
for c in &rest {
rest_line = paint_cell(rest_line, *c);
}
let rest_line = container(rest_line)
.width(Length::Fill)
.padding(Padding {
top: 0.0,
right: 0.0,
bottom: 0.0,
left: -h_scroll,
})
.clip(true);
body = body.push(row![pin_line, rest_line].width(crate::layout::FILL));
}
body
},
)
]
.spacing(4)
.width(crate::layout::FILL)
.height(crate::layout::FILL)
.into(),
&a11y,
)
}
pub fn tree_view<'a, M: Clone + 'a>(
root: &TreeNode,
selected: Option<u64>,
on_toggle: impl Fn(u64) -> M + Copy + 'a,
on_select: impl Fn(u64) -> M + Copy + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let mut col = Column::new().spacing(2);
for (depth, id, label_s, expanded, has_children) in root.flatten() {
let is_sel = selected == Some(id);
let mut line = Row::new().spacing(4).align_y(Alignment::Center);
line = line.push(Space::new().width(Length::Fixed(depth as f32 * 16.0)));
if has_children {
let mark = if expanded { "▾" } else { "▸" };
line = line.push(themed_button(
mark,
a11y.apply_message(Some(on_toggle(id))),
tok,
Variant::Ghost,
Icons::NONE,
A11y::button(format!("toggle {label_s}"))
.with_checked(expanded)
.with_disabled(a11y.disabled),
));
} else {
line = line.push(Space::new().width(28.0));
}
let title = container(label(
label_s.clone(),
tok,
A11y::new(label_s.clone(), Role::Tree).with_checked(is_sel),
))
.width(Length::Fill)
.padding([6, 8]);
let title: Element<'a, M> = if is_sel {
title.style(move |_| style::list_row(tok, true)).into()
} else {
title.into()
};
let pick: Element<'a, M> = if a11y.disabled {
title
} else {
mouse_area(title)
.on_press(on_select(id))
.on_right_press(on_select(id))
.into()
};
line = line.push(a11y::attach(
pick,
&A11y::new(label_s, Role::Tree).with_checked(is_sel),
));
col = col.push(line);
}
a11y::attach(
themed_scroll(
col.into(),
tok,
A11y::new("tree-scroll", Role::Group),
false,
None,
None::<fn(_) -> M>,
),
&a11y,
)
}
pub fn tab_overflow_pick<M: Clone>(
titles: Vec<String>,
on_select: impl Fn(usize) -> M + Copy,
) -> impl Fn(String) -> M {
move |title: String| on_select(tab_overflow_index(&titles, &title))
}
pub fn tab_overflow_index(titles: &[String], chosen: &str) -> usize {
titles.iter().position(|t| t == chosen).unwrap_or(0)
}
pub fn tab_visible_count(titles: &[String], max_width: f32) -> usize {
if titles.is_empty() {
return 0;
}
if max_width <= 0.0 {
return titles.len();
}
let mut used = 0.0;
let mut visible = 0;
for title in titles {
let w = (title.len() as f32) * 7.0 + 48.0;
if visible > 0 && used + w > max_width - 72.0 {
break;
}
used += w;
visible += 1;
}
visible.max(1).min(titles.len())
}
pub fn tab_bar<'a, M: Clone + 'a>(
tabs: &Tabs,
on_select: impl Fn(usize) -> M + Copy + 'a,
on_close: impl Fn(usize) -> M + Copy + 'a,
max_width: f32,
secondary: bool,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let visible = tab_visible_count(&tabs.titles, max_width);
let mut r = Row::new().spacing(0).align_y(Alignment::End);
for (i, title) in tabs.titles.iter().enumerate().take(visible) {
let active = i == tabs.active;
let badge = tabs.badges.get(i).filter(|s| !s.is_empty()).cloned();
let mut label_row = Row::new().spacing(6).align_y(Alignment::Center);
if let Some(Some(ic)) = tabs.icons.get(i) {
label_row = label_row.push(icon_svg(*ic, tok, A11y::new(title.clone(), Role::Image)));
}
label_row = label_row.push(text(title.clone()).size(typo::META));
if let Some(b) = badge {
label_row = label_row.push(self::badge(
b,
None,
tok,
Variant::Primary,
BadgeSize::Small,
A11y::new("tab-badge", Role::Status),
));
}
let mut tab = button(label_row)
.padding([12, 16])
.style(style::tab_style(tok, active));
if !a11y.disabled {
tab = tab.on_press(on_select(i));
}
let bar_h = if secondary { 1.0 } else { 3.0 };
let indicator = if active {
container(Space::new().height(bar_h))
.width(Length::Fill)
.style(move |_| style::tab_indicator(tok))
} else {
container(Space::new().height(0.0)).width(Length::Fill)
};
let label_col = column![tab, indicator].spacing(0).width(Length::Shrink);
let cell: Element<'a, M> = if tabs.closable {
row![
label_col,
dismiss_button(
on_close(i),
tok,
A11y::button(format!("close {title}")).with_disabled(a11y.disabled),
)
]
.spacing(2)
.align_y(Alignment::Center)
.width(Length::Shrink)
.into()
} else {
label_col.into()
};
r = r.push(a11y::attach(
cell,
&A11y::new(title.clone(), Role::Tab).with_checked(active),
));
}
if visible < tabs.titles.len() {
let all = tabs.titles.clone();
let hidden: Vec<String> = all[visible..].to_vec();
r = r.push(themed_pick_list(
hidden,
None,
tab_overflow_pick(all, on_select),
tok,
A11y::new("more-tabs", Role::ComboBox).with_disabled(a11y.disabled),
));
}
let strip = column![
r,
container(Space::new().width(Length::Fill).height(1)).style(move |_| style::hairline(tok)),
];
a11y::attach(
container(strip)
.width(Length::Fill)
.style(move |_| style::app_bar(tok))
.into(),
&a11y,
)
}
fn disclosure_header<'a, M: Clone + 'a>(
title: impl Into<String>,
open: bool,
msg: Option<M>,
tok: Tokens,
a11y: A11y,
inset: Padding,
) -> Element<'a, M> {
let title = title.into();
let mark = if open { "▾" } else { "▸" };
let s = tok.scheme();
let face = row![
text(title.clone())
.size(typo::BODY)
.color(s.on_surface)
.width(Length::Fill),
text(mark).size(typo::BODY).color(s.on_surface_variant),
]
.spacing(8)
.align_y(Alignment::Center)
.width(Length::Fill);
let mut b = button(face)
.padding(inset)
.width(Length::Fill)
.style(style::button_style(tok, Variant::Ghost));
if let Some(m) = a11y.apply_message(msg) {
b = b.on_press(m);
}
a11y::attach(b.into(), &a11y)
}
pub fn accordion_view<'a, M: Clone + 'a>(
titles: &[String],
bodies: Vec<Element<'a, M>>,
state: &Accordion,
on_toggle: impl Fn(usize) -> M + Copy + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let mut col = Column::new().spacing(0).width(Length::Fill);
for (i, (title, body)) in titles.iter().zip(bodies).enumerate() {
let open = state.open == Some(i);
col = col.push(disclosure_header(
title.clone(),
open,
a11y.apply_message(Some(on_toggle(i))),
tok,
A11y::button(title.clone())
.with_checked(open)
.with_disabled(a11y.disabled),
pad(tok),
));
if open {
col = col.push(
container(body)
.width(Length::Fill)
.padding(12)
.style(move |_| style::panel(tok)),
);
}
}
a11y::attach(col.into(), &a11y)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Peek {
Pixels(f32),
Lines(u16),
}
impl Peek {
pub fn body_line() -> f32 {
typo::BODY as f32 * 1.3
}
pub const DESCENT: f32 = 6.0;
pub fn height(self) -> f32 {
let raw = match self {
Self::Pixels(px) => px.max(0.0),
Self::Lines(n) => {
let n = u32::from(n.max(1));
n as f32 * Self::body_line() + Self::DESCENT
}
};
crate::density::Density::snap(raw.ceil() as u32).max(4) as f32
}
}
impl From<f32> for Peek {
fn from(px: f32) -> Self {
Self::Pixels(px)
}
}
fn peek_clip<'a, M: 'a>(child: Element<'a, M>, h: f32, tok: Tokens) -> Element<'a, M> {
let fade_h = 12.0_f32.min(h * 0.4).max(4.0);
let surface = tok.scheme().surface;
let mut clear = surface;
clear.a = 0.0;
let grad = Linear::new(Radians(std::f32::consts::FRAC_PI_2))
.add_stop(0.0, clear)
.add_stop(1.0, surface);
let fade = container(Space::new().width(Length::Fill).height(fade_h))
.width(Length::Fill)
.height(fade_h)
.style(move |_| container::Style {
background: Some(Background::from(grad)),
snap: false,
..container::Style::default()
});
stack![
container(child)
.width(Length::Fill)
.height(Length::Fixed(h))
.clip(true),
column![Space::new().height(Length::Fill), fade].height(Length::Fixed(h)),
]
.into()
}
pub fn expander<'a, M: Clone + 'a>(
title: impl Into<String>,
child: Element<'a, M>,
collapsed: impl Into<Peek>,
open: bool,
on_toggle: impl Fn(bool) -> M + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let title = a11y.apply_name(title);
let header = disclosure_header(
title.clone(),
open,
a11y.apply_message(Some(on_toggle(!open))),
tok,
A11y::button(title.clone())
.with_checked(open)
.with_disabled(a11y.disabled),
Padding {
top: 8.0,
right: 0.0,
bottom: 8.0,
left: 0.0,
},
);
let h = collapsed.into().height();
let body: Element<'a, M> = if open {
child
} else {
peek_clip(child, h, tok)
};
a11y::attach(
container(column![header, body].spacing(8))
.padding(12)
.width(Length::Fill)
.style(move |_| style::card(tok, false))
.into(),
&a11y,
)
}
pub fn pagination<'a, M: Clone + 'a>(
len: usize,
page: usize,
per_page: usize,
on_page: impl Fn(usize) -> M + Copy + 'a,
tok: Tokens,
a11y: A11y,
) -> Element<'a, M> {
let pages = crate::collection::page_count(len, per_page);
let range = page_range(len, page, per_page);
let status = format!("{}–{} / {len}", range.start, range.end);
a11y::attach(
row![
themed_button(
"Prev",
a11y.apply_message((page > 0).then(|| on_page(page - 1))),
tok,
Variant::Quiet,
Icons::NONE,
A11y::button("Prev").with_disabled(a11y.disabled || page == 0),
),
meta(status.clone(), tok, A11y::new(status, Role::Status)),
themed_button(
"Next",
a11y.apply_message((page + 1 < pages).then(|| on_page(page + 1))),
tok,
Variant::Quiet,
Icons::NONE,
A11y::button("Next").with_disabled(a11y.disabled || page + 1 >= pages),
),
]
.spacing(8)
.align_y(Alignment::Center)
.into(),
&a11y,
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::collection::{Selection as Sel, TableModel, VecList};
use crate::density::Density;
use crate::theme::named;
fn must(ok: bool, msg: impl std::fmt::Display) {
if !ok {
panic!("{msg}");
}
}
#[test]
#[should_panic(expected = "cover-must")]
fn must_rejects_a_failed_check() {
must(false, "cover-must");
}
fn draw_once<M: Clone>(el: &mut Element<'_, M>) {
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::renderer::Style;
use iced::advanced::widget::Tree;
use iced::mouse;
use iced::{Font, Pixels, Point, Rectangle, Size, Theme};
let mut tree = Tree::new(el.as_widget());
let mut renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(400.0, 400.0));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let viewport = Rectangle::new(Point::ORIGIN, Size::new(400.0, 400.0));
el.as_widget().draw(
&tree,
&mut renderer,
&Theme::Dark,
&Style::default(),
layout,
mouse::Cursor::Unavailable,
&viewport,
);
}
const TEST_PNG: &[u8] = &[
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44,
0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90,
0x77, 0x53, 0xDE, 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x08, 0xD7, 0x63, 0xF8,
0xCF, 0xC0, 0x00, 0x00, 0x03, 0x01, 0x01, 0x00, 0x18, 0xDD, 0x8D, 0xB0, 0x00, 0x00, 0x00,
0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,
];
#[test]
fn date_time_step_and_suggest() {
assert_eq!(days_in_month(2024, 2), 29);
assert_eq!(days_in_month(2023, 2), 28);
assert_eq!(days_in_month(2000, 2), 29);
assert_eq!(days_in_month(1900, 2), 28);
assert_eq!(days_in_month(2023, 4), 30);
assert_eq!(days_in_month(2023, 1), 31);
let d = DateValue {
year: 2023,
month: 13,
day: 40,
}
.clamp();
assert_eq!(d.month, 12);
assert_eq!(d.day, 31);
let t = TimeValue {
hour: 30,
minute: 90,
second: 90,
}
.clamp();
assert_eq!(t.hour, 23);
assert_eq!(t.minute, 59);
assert_eq!(t.second, 59);
let noon = TimeValue::hms(23, 59, 59);
assert_eq!(noon.step_hour(1).hour, 0);
assert_eq!(noon.step_minute(1).minute, 0);
assert_eq!(noon.step_second(1).second, 0);
assert_eq!(TimeValue::hm(0, 0).step_hour(-1).hour, 23);
assert_eq!(TimeValue::hm(0, 0).step_minute(-1).minute, 59);
assert_eq!(TimeValue::hms(0, 0, 0).step_second(-1).second, 59);
assert_eq!(TimeValue::hm(0, 0).hour12(), 12);
assert!(!TimeValue::hm(0, 0).afternoon());
assert_eq!(TimeValue::hm(12, 0).hour12(), 12);
assert!(TimeValue::hm(12, 0).afternoon());
assert_eq!(TimeValue::hm(13, 5).hour12(), 1);
assert_eq!(TimeValue::hm(11, 0).step_hour12(1).hour, 0);
assert_eq!(TimeValue::hm(23, 0).step_hour12(1).hour, 12);
assert_eq!(TimeValue::hm(0, 0).toggle_period().hour, 12);
assert_eq!(TimeValue::hm(15, 0).toggle_period().hour, 3);
assert_eq!(
TimeValue::hm(9, 30)
.step_field(TimeField::Hour, TimeClock::HOUR12)
.hour,
10
);
assert_eq!(
TimeValue::hm(23, 0)
.step_field(TimeField::Hour, TimeClock::HOURS_MINUTES)
.hour,
0
);
assert_eq!(
TimeValue::hm(9, 0)
.step_field(TimeField::Period, TimeClock::HOUR12)
.hour,
21
);
assert_eq!(
TimeValue::hm(9, 30)
.step_field(TimeField::Minute, TimeClock::HOURS_MINUTES)
.minute,
31
);
assert_eq!(
TimeValue::hms(9, 30, 4)
.step_field(TimeField::Second, TimeClock::HOURS_MINUTES_SECONDS)
.second,
5
);
assert_eq!(step_number(5.0, 1.0, 0.0, 10.0, 1), 6.0);
assert_eq!(step_number(0.0, 1.0, 0.0, 10.0, -1), 0.0);
assert!(A11y::button("x").with_disabled(true).disabled);
assert_eq!(
A11y::new("y", Role::Checkbox).with_checked(false).checked,
Some(false)
);
}
#[test]
fn widgets_build_elements() {
let tok = named("dark").tokens;
let btn = |n: &str| A11y::button(n);
let role = |n: &str, r: Role| A11y::new(n, r);
let _: Element<'_, ()> = icon_svg(Icon::Search, tok, role("search", Role::Image));
let _: Element<'_, ()> = label("Hi", tok, role("Hi", Role::Header));
let _: Element<'_, ()> = meta("m", tok, role("m", Role::Status));
let snippet = Content::with_text("fn");
let _: Element<'_, ()> = code_block(&snippet, |_| (), tok, role("fn", Role::Group));
let _: Element<'_, ()> = hyperlink("l", (), tok, role("l", Role::Link));
let _: Element<'_, ()> =
themed_button("B", Some(()), tok, Variant::Primary, Icons::NONE, btn("B"));
let _: Element<'_, ()> = themed_button_sized(
"7",
Some(()),
tok,
Variant::Quiet,
Icons::NONE,
Length::Fill,
Length::Fixed(Density::default().tile() as f32),
btn("7"),
);
let _: Element<'_, ()> = display_line("6 × 4 =", tok, role("expr", Role::Status));
let _: Element<'_, ()> = figure_display("12:40", tok, role("clock", Role::Status));
let glyph = A11y::button("Backspace");
let _: Element<'_, ()> = themed_button_sized(
"⌫",
Some(()),
tok,
Variant::Quiet,
Icons::NONE,
Length::Fill,
Length::Fixed(48.0),
glyph,
);
let _: Element<'_, ()> = themed_button(
"D",
None,
tok,
Variant::Danger,
Icons::NONE,
btn("D").with_disabled(true),
);
let _: Element<'_, i32> =
split_button("S", 0, vec![("As…".into(), 1)], tok, Icons::NONE, btn("S"));
let _: Element<'_, i32> = split_button(
"S",
0,
vec![],
tok,
Icons::NONE,
btn("S").with_disabled(true),
);
assert!((slider_step(0.0..=1.0) - 0.01).abs() < f32::EPSILON);
assert!(slider_step(0.0..=1.0) < 1.0);
assert!((slider_step(10.0..=10.0) - f32::EPSILON).abs() < 1e-12);
let _: Element<'_, ()> =
toggle_button("T", true, (), tok, Icons::NONE, btn("T").with_checked(true));
let _: Element<'_, ()> = toggle_button(
"T",
false,
(),
tok,
Icons::NONE,
btn("T").with_checked(false),
);
let _: Element<'_, ()> = toggle_button(
"T",
true,
(),
tok,
Icons::NONE,
btn("T").with_checked(true).with_disabled(true),
);
let _: Element<'_, ()> = themed_checkbox(
"c",
true,
|_| (),
tok,
role("c", Role::Checkbox).with_checked(true),
);
let _: Element<'_, ()> = themed_switch(
"s",
false,
|_| (),
tok,
role("s", Role::Switch).with_disabled(true),
);
let _: Element<'_, ()> = themed_switch(
"s2",
true,
|_| (),
tok,
role("s2", Role::Switch).with_checked(true),
);
let _: Element<'_, ()> =
themed_radio("r", 1u8, Some(1u8), |_| (), tok, role("r", Role::Radio));
let _: Element<'_, ()> = themed_radio(
"off",
2u8,
Some(1u8),
|_| (),
tok,
role("off", Role::Radio).with_disabled(true),
);
let _: Element<'_, ()> = themed_radio(
"on",
1u8,
Some(1u8),
|_| (),
tok,
role("on", Role::Radio).with_disabled(true),
);
let idle = radio_idle_face(tok, false);
assert_eq!(idle.border.color, tok.scheme().outline);
assert!(idle.border.radius.top_left >= 8.0);
let on = radio_idle_face(tok, true);
assert_eq!(
on.background,
Some(iced::Background::Color(tok.scheme().primary))
);
assert_eq!(
scroll_delta_pixels(iced::mouse::ScrollDelta::Lines { x: 0.0, y: -2.0 }, 20.0),
40.0
);
assert_eq!(
scroll_delta_pixels(iced::mouse::ScrollDelta::Pixels { x: 0.0, y: 8.0 }, 20.0),
-8.0
);
assert_eq!(
scroll_delta_x(iced::mouse::ScrollDelta::Lines { x: 1.0, y: 0.0 }),
-32.0
);
assert_eq!(
scroll_delta_x(iced::mouse::ScrollDelta::Pixels { x: 8.0, y: 0.0 }),
-8.0
);
let _: Element<'_, ()> = themed_slider(
0.0..=1.0,
0.5,
|_| (),
SliderMarks::NONE,
tok,
role("s", Role::Slider).with_value("0.5"),
);
let _: Element<'_, ()> = range_slider(
0.0..=100.0,
10.0,
90.0,
|_| (),
tok,
role("rs", Role::Slider),
);
assert_eq!(CheckState::Indeterminate.toggle(), CheckState::Checked);
assert_eq!(CheckState::Checked.toggle(), CheckState::Unchecked);
assert_eq!(CheckState::Unchecked.toggle(), CheckState::Checked);
let tri_face = indeterminate_box_face(tok);
assert_eq!(
tri_face.background,
Some(iced::Background::Color(tok.scheme().primary))
);
assert_eq!(tri_face.border.color, tok.scheme().primary);
assert_eq!(check_state_from_bool(true), CheckState::Checked);
assert_eq!(check_state_from_bool(false), CheckState::Unchecked);
let off_slide = disabled_slider_face(tok);
assert_eq!(
off_slide.background,
Some(iced::Background::Color(
tok.scheme().surface_container_highest
))
);
let _: Element<'_, ()> = checkbox_indeterminate(
"all",
CheckState::Indeterminate,
|_| (),
tok,
role("tri", Role::Checkbox),
);
let _: Element<'_, ()> = checkbox_indeterminate(
"on",
CheckState::Checked,
|_| (),
tok,
role("tri-on", Role::Checkbox),
);
let _: Element<'_, ()> = checkbox_indeterminate(
"off",
CheckState::Unchecked,
|_| (),
tok,
role("tri-off", Role::Checkbox).with_disabled(true),
);
let _: Element<'_, ()> = checkbox_indeterminate(
"ind-d",
CheckState::Indeterminate,
|_| (),
tok,
role("tri-d", Role::Checkbox).with_disabled(true),
);
let _: Element<'_, ()> =
segmented_button(["A", "B"], 1, |_| (), tok, role("seg", Role::Group));
let _: Element<'_, ()> = segmented_button(
["A"],
0,
|_| (),
tok,
role("seg-d", Role::Group).with_disabled(true),
);
let _: Element<'_, ()> = icon_button(
Icon::Search,
Some(()),
tok,
Variant::Ghost,
ControlSize::Default,
A11y::button("s"),
);
let _: Element<'_, ()> = icon_button(
Icon::Close,
None,
tok,
Variant::Quiet,
ControlSize::Default,
A11y::button("c").with_disabled(true),
);
let field = themed_text_input(
"x",
"",
|_| (),
None,
FieldOpts::NONE,
tok,
role("f", Role::TextBox),
None,
);
let _: Element<'_, ()> = field_support(
field,
Some("help"),
Some("err"),
tok,
role("fs", Role::Group),
);
let field2 = themed_text_input(
"y",
"v",
|_| (),
None,
FieldOpts::NONE,
tok,
role("f2", Role::TextBox),
None,
);
let _: Element<'_, ()> =
field_support(field2, Some("only"), None, tok, role("fs2", Role::Group));
let field3 = themed_text_input(
"z",
"",
|_| (),
None,
FieldOpts::NONE,
tok,
role("f3", Role::TextBox),
None,
);
let _: Element<'_, ()> = field_support(field3, None, None, tok, role("fs3", Role::Group));
let _: Element<'_, ()> = filter_chips(
&["a".into(), "b".into()],
&[true, false],
|_| (),
tok,
role("fc", Role::Group),
);
let _: Element<'_, ()> = filter_chips(
&["a".into()],
&[],
|_| (),
tok,
role("fc-d", Role::Group).with_disabled(true),
);
let (on_bg, on_ink, on_border) = chip_face(tok, Variant::Primary);
let (off_bg, off_ink, off_border) = chip_face(tok, Variant::Quiet);
assert_ne!(on_bg, off_bg);
assert_ne!(on_ink, off_ink);
assert_ne!(on_border.color, off_border.color);
assert!(on_border.width < off_border.width || off_border.width >= 1.0);
for v in Variant::ALL {
let (bg, ink, border) = chip_face(tok, v);
assert_ne!(bg, ink);
let _ = border.width;
}
let _: Element<'_, ()> =
search_input_clear("q", |_| (), Some(()), tok, role("sc", Role::TextBox));
let _: Element<'_, ()> =
search_input_clear("", |_| (), Some(()), tok, role("sc0", Role::TextBox));
let _: Element<'_, ()> = search_input_clear(
"q",
|_| (),
None,
tok,
role("sc-n", Role::TextBox).with_disabled(true),
);
let _: Element<'_, ()> = range_slider(
0.0..=10.0,
2.0,
8.0,
|_| (),
tok,
role("rs-d", Role::Slider).with_disabled(true),
);
assert_eq!(clamp_range_pair(0.0..=100.0, -10.0, 200.0), (0.0, 100.0));
assert_eq!(clamp_range_pair(0.0..=100.0, 80.0, 20.0), (20.0, 20.0));
assert_eq!(range_pair_after_low(50.0, 40.0), (40.0, 40.0));
assert_eq!(range_pair_after_low(10.0, 40.0), (10.0, 40.0));
assert_eq!(range_pair_after_high(10.0, 5.0), (10.0, 10.0));
assert_eq!(range_pair_after_high(10.0, 50.0), (10.0, 50.0));
let _: Element<'_, ()> = progress(
0.2,
None,
None,
false,
tok,
role("p", Role::Progress).with_value("0.2"),
);
let _: Element<'_, ()> = progress(
0.5,
Some(0.8),
Some("50% · 1 min"),
false,
tok,
role("pc", Role::Progress).with_value("0.5"),
);
let _: Element<'_, ()> =
progress_ring(0.4, None, tok, role("pr", Role::Progress).with_value("0.4"));
let _: Element<'_, ()> = progress_ring(
0.5,
Some("50%"),
tok,
role("prc", Role::Progress).with_value("0.5"),
);
assert_eq!(progress_label(0.5, None), "50%");
assert_eq!(progress_label(0.5, Some("1 min")), "50% · 1 min");
let _: Element<'_, ()> = image_slot(
ImageSlot::Ready {
handle: iced::widget::image::Handle::from_bytes(TEST_PNG),
fit: iced::ContentFit::Cover,
},
48.0,
48.0,
tok,
role("cover", Role::Image),
);
let _: Element<'_, ()> = image_slot(
ImageSlot::Loading,
48.0,
48.0,
tok,
role("load", Role::Image),
);
let _: Element<'_, ()> = image_slot(
ImageSlot::Error("missing".into()),
48.0,
48.0,
tok,
role("err", Role::Image),
);
let _: Element<'_, ()> = spinner(tok, 0.25, role("spin", Role::Progress));
let _: Element<'_, ()> = busy_overlay(
label("Body", tok, role("Body", Role::Status)),
true,
0.2,
tok,
role("busy", Role::Group),
);
let _: Element<'_, ()> = busy_overlay(
label("Idle", tok, role("Idle", Role::Status)),
false,
0.0,
tok,
role("idle", Role::Group),
);
assert!(ring_angles(1.0).1 > ring_angles(0.2).1);
assert!(
(spinner_angles(0.0).1 - spinner_angles(0.0).0 - std::f32::consts::PI * 1.5).abs()
< 0.01
);
assert!(ring_should_stroke(0.0, 1.0));
assert!(!ring_should_stroke(0.0, 0.0));
let a11y = A11y::button("Nope").with_disabled(true);
let _: Element<'_, ()> =
themed_button("Nope", Some(()), tok, Variant::Primary, Icons::NONE, a11y);
let unnamed = A11y::button("");
let _: Element<'_, ()> = themed_button(
"Shown",
Some(()),
tok,
Variant::Primary,
Icons::NONE,
unnamed,
);
let unnamed_c = A11y::new("", Role::Checkbox);
let _: Element<'_, ()> = themed_checkbox("box", true, |_| (), tok, unnamed_c);
let ca = A11y::new("off", Role::Checkbox)
.with_checked(true)
.with_disabled(true);
let _: Element<'_, ()> = themed_checkbox("off", false, |_| (), tok, ca);
let _: Element<'_, ()> = number_input(
3.0,
|_| (),
tok,
role("n", Role::SpinButton).with_value("3"),
);
let _: Element<'_, ()> = themed_text_input(
"p",
"v",
|_| (),
Some(()),
FieldOpts::NONE,
tok,
role("v", Role::TextBox),
Some(Id::new("name")),
);
let _: Element<'_, ()> = themed_text_input(
"p",
"",
|_| (),
None,
FieldOpts::NONE,
tok,
role("Name", Role::TextBox),
None,
);
let _: Element<'_, ()> = themed_text_input(
"p",
"",
|_| (),
Some(()),
FieldOpts::NONE,
tok,
role("Name", Role::TextBox).with_disabled(true),
None,
);
let _: Element<'_, ()> = password_input(
"p",
"v",
|_| (),
tok,
role("pw", Role::TextBox).with_disabled(true),
true,
);
let _: Element<'_, ()> =
password_input("p", "v", |_| (), tok, role("pw2", Role::TextBox), true);
let copy = crate::action::Action::new("secret.copy", "Copy", ());
let _: Element<'_, ()> = secret_field(
"Secret",
"v",
|_| (),
false,
(),
©,
tok,
Direction::Ltr,
role("secret", Role::Group),
);
let mut dead = crate::action::Action::new("secret.copy", "Copy", ());
dead.enabled = false;
let _: Element<'_, ()> = secret_field(
"Secret",
"v",
|_| (),
true,
(),
&dead,
tok,
Direction::Rtl,
role("secret-off", Role::Group).with_disabled(true),
);
let content = Content::new();
let _: Element<'_, ()> = textarea(
&content,
|_| (),
tok,
crate::layout::FILL,
role("ta", Role::TextBox),
);
let _: Element<'_, ()> = textarea(
&content,
|_| (),
tok,
crate::layout::fixed(120.0),
role("ta", Role::TextBox),
);
let _: Element<'_, ()> = selectable(
&content,
|_| (),
tok,
typo::FontFace::Ui,
role("body", Role::TextBox),
);
let _: Element<'_, ()> = selectable(
&content,
|_| (),
tok,
typo::FontFace::Mono,
role("path", Role::TextBox).with_disabled(true),
);
let copy = crate::action::Action::new("value.copy", "Copy", ());
let _: Element<'_, ()> = value_field(
"Path",
&content,
|_| (),
Some(©),
typo::FontFace::Mono,
crate::layout::FORM_LABEL,
tok,
Direction::Ltr,
role("vf", Role::Group),
);
let _: Element<'_, ()> = value_field(
"Id",
&content,
|_| (),
None,
typo::FontFace::Ui,
crate::layout::FORM_LABEL,
tok,
Direction::Rtl,
role("vf-off", Role::Group).with_disabled(true),
);
let _: Element<'_, ()> = search_input("q", |_| (), tok, role("q", Role::TextBox));
let mut sv: Element<'_, ()> = search_view(
"in",
["Inbox", "Sent"],
|_| (),
|_| (),
Some(()),
"No matches",
tok,
role("find", Role::Group),
);
draw_once(&mut sv);
let mut empty_sv: Element<'_, ()> = search_view(
"zz",
Vec::<String>::new(),
|_| (),
|_| (),
None,
"No matches",
tok,
role("find-empty", Role::Group).with_disabled(true),
);
draw_once(&mut empty_sv);
let mut disabled_hits: Element<'_, ()> = search_view(
"in",
["Inbox", "Sent"],
|_| (),
|_| (),
Some(()),
"No matches",
tok,
role("find-off", Role::Group).with_disabled(true),
);
draw_once(&mut disabled_hits);
let mut group: Element<'_, usize> =
button_group(["Cut", "Copy"], |i| i, tok, role("edit", Role::Group));
draw_once(&mut group);
let mut group_off: Element<'_, usize> = button_group(
["Cut"],
|i| i,
tok,
role("edit-off", Role::Group).with_disabled(true),
);
draw_once(&mut group_off);
let _: Element<'_, usize> = button_group(
std::iter::empty::<Cell>(),
|i| i,
tok,
role("edit-empty", Role::Group).with_disabled(true),
);
let mut tip: Element<'_, ()> = tooltip_rich(
label("Save", tok, role("Save", Role::Header)),
"Save",
"Write the buffer.",
None,
TooltipAnchor::Follow,
tok,
role("tip", Role::Tooltip),
);
draw_once(&mut tip);
let _: Element<'_, ()> = tooltip_rich(
label("x", tok, role("x", Role::Header)),
"",
"",
None,
TooltipAnchor::Follow,
tok,
role("tip-empty", Role::Tooltip),
);
let hints = ["save".into(), "open".into()];
let _: Element<'_, ()> = suggest_field(
"Command",
"s",
|_| (),
&hints,
|_| (),
tok,
role("suggest", Role::Group),
);
let opts = ["a".to_string(), "b".to_string()];
let _: Element<'_, ()> = themed_pick_list(
opts,
Some("a".into()),
|_| (),
tok,
role("a", Role::ComboBox),
);
let _: Element<'_, ()> = date_picker(
DateValue {
year: 2024,
month: 1,
day: 1,
},
(),
(),
tok,
role("date", Role::SpinButton).with_value("2024-01-01"),
);
let _: Element<'_, ()> = time_picker(
TimeValue::hm(8, 5),
TimeClock::HOURS_MINUTES,
|_| (),
tok,
role("time", Role::SpinButton),
);
let _: Element<'_, ()> = time_picker(
TimeValue::hms(13, 5, 9),
TimeClock::HOUR12_SECONDS,
|_| (),
tok,
role("time-12", Role::SpinButton).with_disabled(true),
);
let _: Element<'_, ()> = time_picker(
TimeValue::hms(8, 5, 1),
TimeClock::HOURS_MINUTES_SECONDS,
|_| (),
tok,
role("time-sec", Role::SpinButton),
);
let _: Element<'_, ()> = time_picker(
TimeValue::hm(9, 30),
TimeClock::HOUR12,
|_| (),
tok,
role("time-am", Role::SpinButton),
);
let items = markdown::parse("# Hi");
let items: Vec<_> = items.collect();
let _: Element<'_, ()> =
markdown_view(&items, None, |_| (), tok, |_| (), role("md", Role::Group));
let code = Content::with_text("fn main() {}\n");
let _: Element<'_, ()> = highlighted_code(
&code,
"rs",
|_| (),
tok,
"dark",
crate::layout::FILL,
role("code", Role::Group),
);
let light = named("light").tokens;
let _: Element<'_, ()> = highlighted_code(
&code,
"py",
|_| (),
light,
"solarized-light",
crate::layout::fixed(280.0),
role("code", Role::Group),
);
let mocha = named("catppuccin-mocha").tokens;
let _: Element<'_, ()> = highlighted_code(
&code,
"rs",
|_| (),
mocha,
"catppuccin-mocha",
crate::layout::FILL,
role("code", Role::Group),
);
let _: Element<'_, ()> = tooltip_wrap(
label("x", tok, role("x", Role::Header)),
"tip",
TooltipAnchor::Follow,
tok,
role("tip", Role::Tooltip),
);
let _: Element<'_, ()> = rule_h(tok, role("rule", Role::Separator));
let _: Element<'_, ()> = dismiss_button((), tok, btn("dismiss"));
let _: Element<'_, ()> = chip(
"c",
None,
Some(()),
tok,
Variant::Quiet,
ChipKind::Assist,
Icons::NONE,
btn("c"),
);
let _: Element<'_, ()> = chip(
"plain",
None,
None,
tok,
Variant::Primary,
ChipKind::Assist,
Icons::NONE,
btn("plain"),
);
let _: Element<'_, ()> = chip(
"hot",
None,
Some(()),
tok,
Variant::Danger,
ChipKind::Assist,
Icons::NONE,
btn("hot"),
);
let _: Element<'_, ()> = chip(
"g",
None,
None,
tok,
Variant::Ghost,
ChipKind::Assist,
Icons::NONE,
btn("g"),
);
let _: Element<'_, ()> = chip(
"k",
Some(()),
None,
tok,
Variant::Chip,
ChipKind::Assist,
Icons::NONE,
btn("k"),
);
let _: Element<'_, ()> = chip(
"ok",
None,
None,
tok,
Variant::Success,
ChipKind::Assist,
Icons::NONE,
btn("ok"),
);
let _: Element<'_, ()> = chip(
"warn",
None,
None,
tok,
Variant::Warning,
ChipKind::Assist,
Icons::NONE,
btn("warn"),
);
let _: Element<'_, ()> = badge(
"b",
None,
tok,
Variant::Quiet,
BadgeSize::Large,
role("b", Role::Status),
);
let _: Element<'_, ()> = badge(
"new",
None,
tok,
Variant::Primary,
BadgeSize::Large,
role("new", Role::Status),
);
let _: Element<'_, ()> = badge(
"!",
None,
tok,
Variant::Danger,
BadgeSize::Large,
role("bang", Role::Status),
);
let _: Element<'_, ()> = badge(
"g",
None,
tok,
Variant::Ghost,
BadgeSize::Large,
role("g", Role::Status),
);
let _: Element<'_, ()> = badge(
"chip",
None,
tok,
Variant::Chip,
BadgeSize::Large,
role("chip", Role::Status),
);
let _: Element<'_, ()> = badge(
"ok",
None,
tok,
Variant::Success,
BadgeSize::Large,
role("ok", Role::Status),
);
let _: Element<'_, ()> = badge(
"warn",
None,
tok,
Variant::Warning,
BadgeSize::Large,
role("warn", Role::Status),
);
let _: Element<'_, ()> = group_box(
"g",
label("x", tok, role("x", Role::Header)),
tok,
CardFace::Elevated,
role("g", Role::Group),
);
let _: Element<'_, ()> = banner("b", Some(("go".into(), ())), tok, role("b", Role::Status));
let _: Element<'_, ()> = banner("b", None, tok, role("b", Role::Status));
let _: Element<'_, ()> = info_bar(ToastKind::Warning, "w", tok, role("w", Role::Status));
let _: Element<'_, ()> = breadcrumb(
&[("Home".into(), Some(())), ("Here".into(), None)],
tok,
Direction::Ltr,
role("bc", Role::Group),
);
let _: Element<'_, ()> = breadcrumb(
&[("Home".into(), Some(())), ("Here".into(), None)],
tok,
Direction::Rtl,
role("bc", Role::Group),
);
let toast = Toast {
id: 1,
kind: ToastKind::Info,
text: "t".into(),
ttl_ms: 10,
};
let _: Element<'_, ()> = toast_view(&toast, (), tok, role("t", Role::Status));
let list = VecList {
items: vec![
crate::collection::ListRow::new("a").with_meta("meta"),
crate::collection::ListRow::new("b").with_meta(""),
],
};
let empty = VecList::default();
let with_sep = VecList {
items: vec![
crate::collection::ListRow::new("a"),
crate::collection::ListRow::separator(),
crate::collection::ListRow::new("b"),
],
};
let win = VisibleWindow::new(100.0);
let _: Element<'_, ()> = list_view(
&list,
&Sel::Single(0),
|_| (),
tok,
win,
24.0,
crate::collection::OVERSCAN,
|_| (),
"Empty",
|_| tok.muted,
Some(Id::from("list-host")),
RowFace::FLUSH,
|_| (),
role("list", Role::List),
);
let _: Element<'_, ()> = list_view(
&empty,
&Sel::None,
|_| (),
tok,
win,
24.0,
0,
|_| (),
"No sessions",
|_| tok.muted,
None,
RowFace::FLUSH,
|_| (),
role("list", Role::List),
);
let mut striped: Element<'_, ()> = list_view(
&list,
&Sel::Single(0),
|_| (),
tok,
win,
24.0,
4,
|_| (),
"Empty",
|i| {
if i == 0 {
tok.danger
} else {
tok.muted
}
},
None,
RowFace::FLUSH,
|_| (),
role("list", Role::List),
);
draw_once(&mut striped);
let _: Element<'_, ()> = list_view(
&with_sep,
&Sel::Single(0),
|_| (),
tok,
win,
24.0,
4,
|_| (),
"Empty",
|_| tok.muted,
None,
RowFace::FLUSH,
|_| (),
role("list-sep", Role::List),
);
let lines = ["boot".into(), "ready".into()];
let _: Element<'_, ()> = log_view(
&lines,
VisibleWindow::new(80.0),
18.0,
2,
|_| (),
Some(Id::from("log")),
tok,
role("log", Role::List),
);
let live = crate::collection::visible_window(20.0, 80.0, 18.0, 2, 2, None);
let _: Element<'_, ()> = log_view(
&lines,
live,
18.0,
2,
|_| (),
None,
tok,
role("log-live", Role::List),
);
let empty_log: [String; 0] = [];
let _: Element<'_, ()> = log_view(
&empty_log,
VisibleWindow::new(80.0),
18.0,
0,
|_| (),
None,
tok,
role("log-empty", Role::List),
);
let _: Element<'_, ()> = item_grid(
&["a".into(), "b".into()],
|_| (),
None,
tok,
role("grid", Role::List),
);
let table = TableModel {
headers: vec!["A".into(), "B".into()],
rows: vec![vec!["1".into(), "x".into()], vec!["2".into(), "y".into()]],
sort_col: None,
sort_asc: true,
checks: Vec::new(),
};
let widths = crate::collection::ColumnLayout::new(vec![80.0, 80.0]);
let _: Element<'_, ()> = data_table(
&table,
&Sel::Single(0),
Some((0, 1)),
&widths,
true,
VisibleWindow::new(100.0),
24.0,
crate::collection::OVERSCAN,
|_, _| (),
|_| (),
|_| (),
|_| (),
|_| (),
tok,
role("table", Role::Table),
);
let big = TableModel {
headers: vec!["N".into()],
rows: (0..50).map(|i| vec![i.to_string()]).collect(),
sort_col: None,
sort_asc: true,
checks: Vec::new(),
};
let one = crate::collection::ColumnLayout::new(vec![96.0]);
let _: Element<'_, ()> = data_table(
&big,
&Sel::None,
None,
&one,
false,
VisibleWindow {
start: 0,
end: 0,
scroll: 200.0,
viewport: 80.0,
},
20.0,
2,
|_, _| (),
|_| (),
|_| (),
|_| (),
|_| (),
tok,
role("table", Role::Table),
);
let tree = TreeNode::branch(1, "r", vec![TreeNode::leaf(2, "c")]);
let _: Element<'_, ()> = tree_view(
&tree,
Some(2),
|_| (),
|_| (),
tok,
role("tree", Role::Tree),
);
let mut tabs = Tabs::new(["A", "B"]);
tabs.closable = true;
let _: Element<'_, ()> = tab_bar(
&tabs,
|_| (),
|_| (),
120.0,
false,
tok,
role("tabs", Role::Tab),
);
let open_tabs = Tabs::new(["A"]);
let _: Element<'_, ()> = tab_bar(
&open_tabs,
|_| (),
|_| (),
0.0,
false,
tok,
role("tabs", Role::Tab),
);
let acc = Accordion { open: Some(0) };
let _: Element<'_, ()> = accordion_view(
&["A".into()],
vec![label("b", tok, role("b", Role::Header))],
&acc,
|_| (),
tok,
role("acc", Role::Group),
);
let note = label("more", tok, role("more", Role::Status));
let _: Element<'_, bool> = expander(
"Notes",
note,
48.0,
false,
|open| open,
tok,
role("exp", Role::Group),
);
let note = label("more", tok, role("more", Role::Status));
let _: Element<'_, bool> = expander(
"Notes",
note,
48.0,
true,
|open| open,
tok,
role("exp", Role::Group).with_disabled(true),
);
let _: Element<'_, ()> = pagination(100, 1, 10, |_| (), tok, role("page", Role::Group));
let _: Element<'_, ()> = pagination(10, 0, 10, |_| (), tok, role("page", Role::Group));
let theme = crate::theme::iced_theme("dark", tok);
let _ = icon_style(tok)(&theme, svg::Status::Idle);
let _ = icon_style(tok)(&theme, svg::Status::Hovered);
let _ = editor_style(tok)(&theme, iced::widget::text_editor::Status::Active);
let _ = editor_style(tok)(&theme, iced::widget::text_editor::Status::Hovered);
let _ = editor_style(tok)(
&theme,
iced::widget::text_editor::Status::Focused { is_hovered: true },
);
let _ = editor_style(tok)(&theme, iced::widget::text_editor::Status::Disabled);
let _ = editor_frame(tok);
let _ = tip_style(tok)(&theme);
let _ = toast_style(tok, ToastKind::Info)(&theme);
let collapsed = TreeNode::branch(1, "r", vec![TreeNode::leaf(2, "c")]);
let mut collapsed = collapsed;
assert!(crate::collection::tree_toggle(&mut collapsed, 1));
assert!(!collapsed.expanded);
let selected = Some(1u64);
assert_eq!(selected, Some(1));
let _: Element<'_, ()> = tree_view(
&collapsed,
selected,
|_| (),
|_| (),
tok,
role("tree", Role::Tree),
);
let src = include_str!("widget.rs");
let product = src.split("#[cfg(test)]").next().unwrap();
assert!(!product.contains(".height(120)"));
assert!(!product.contains(".height(280)"));
assert!(product.contains(".id(Id::from(a11y.node_id()))"));
assert!(btn("Save")
.with_value("idle")
.with_disabled(true)
.node_id()
.contains("button|Save|1"));
let _: Element<'_, ()> = themed_scroll(
label("log", tok, role("log", Role::Status)),
tok,
role("scroll", Role::Group),
true,
None,
None::<fn(_) -> ()>,
);
let _: Element<'_, ()> = themed_scroll(
label("body", tok, role("body", Role::Status)),
tok,
role("scroll", Role::Group),
false,
Some(Id::from("body-scroll")),
None::<fn(_) -> ()>,
);
assert!(crate::layout::stick_to_end(80.0, 100.0, 20.0, 4.0));
let input_src = src
.split("pub fn themed_text_input")
.nth(1)
.unwrap()
.split("pub fn password_input")
.next()
.unwrap();
assert!(!input_src.contains("apply_name(value)"));
assert!(input_src.contains("on_submit"));
assert!(input_src.contains(".id("));
assert!(product.contains("virtual_pads("));
assert!(product.contains("window_after_scroll("));
assert!(product.contains("ScrollRail::new"));
assert!(!product.contains("list_body_and_rail"));
let pass_src = src
.split("pub fn password_input")
.nth(1)
.unwrap()
.split("pub fn secret_field")
.next()
.unwrap();
assert!(!pass_src.contains("apply_name(value)"));
assert!(pass_src.contains("secure(masked)"));
let secret_src = src
.split("pub fn secret_field")
.nth(1)
.unwrap()
.split("pub fn textarea")
.next()
.unwrap();
assert!(secret_src.contains("password_input("));
assert!(!secret_src.contains("apply_name(value)"));
assert!(secret_src.contains("a11y.child(Role::TextBox)"));
let num_src = src
.split("pub fn number_input")
.nth(1)
.unwrap()
.split("pub fn step_number")
.next()
.unwrap();
assert!(!num_src.contains("apply_name"));
let doc = parse("# Hi\n\nBody.");
assert!(!doc.items.is_empty());
assert_eq!(doc.hash, MarkdownDoc::parse("# Hi\n\nBody.").hash);
assert_ne!(doc.hash, parse("# Other").hash);
let _: Element<'_, ()> = markdown_view(
&doc.items,
None,
|_| (),
tok,
|_| (),
role("md", Role::Group),
);
let md = markdown_style(tok);
let s = tok.scheme();
assert_eq!(md.link_color, s.primary);
assert_eq!(md.inline_code_color, s.on_surface);
assert_eq!(md.inline_code_font, typo::MONO);
let cut = parse(&"# title\n\nbody".repeat(8)[..20]);
assert!(cut.source.len() <= 20);
let outlined = parse("# Hi\n\nA paragraph.\n\n## Next\n\nMore.");
let heads = outlined.headings();
assert_eq!(heads.len(), 2);
assert_eq!(heads[0].level, 1);
assert_eq!(heads[0].title, "Hi");
assert_eq!(heads[1].level, 2);
let deep = parse("# A\n\n## B\n\n### C\n\n#### D\n\n##### E\n\n###### F\n");
let levels: Vec<u8> = deep.headings().iter().map(|h| h.level).collect();
assert_eq!(levels, vec![1, 2, 3, 4, 5, 6]);
let _: Element<'_, ()> = markdown_outline(
&heads,
Some(heads[0].index),
|_| (),
tok,
role("outline", Role::List),
);
let none: [MdHeading; 0] = [];
let _: Element<'_, ()> =
markdown_outline(&none, None, |_| (), tok, role("outline-empty", Role::List));
assert!(outlined.item_offset(heads[1].index) > outlined.item_offset(heads[0].index));
assert_eq!(outlined.item_offset(0), 0.0);
assert!(outlined.item_offset(outlined.items.len()) > outlined.item_offset(heads[1].index));
let _ = deep.item_offset(deep.items.len());
let rich = parse(
"# T\n\nA paragraph with [link](https://example.com).\n\n- bullet\n - nested\n\n1. one\n\n- [x] done\n- [ ] todo\n\n> quoted\n>\n> still quoted\n\n---\n\n```rust\nfn x() {}\n```\n\n| Name | Ready |\n| --- | --- |\n| A | yes |\n\n\n",
);
assert!(rich.items.len() >= 8);
assert!(rich.item_offset(rich.items.len()) > 100.0);
}
#[test]
fn split_and_segmented_match_button_height() {
let tok = named("dark").tokens;
let btn = |n: &str| A11y::button(n);
let max = iced::Size::new(800.0, 400.0);
let mut labeled = themed_button(
"Save",
Some(()),
tok,
Variant::Primary,
Icons::NONE,
btn("Save"),
);
let mut split = split_button(
"Save",
0,
vec![("As…".into(), 1)],
tok,
Icons::NONE,
btn("Save"),
);
let mut segmented = segmented_button(
["Day", "Week", "Month"],
0,
|i| i,
tok,
A11y::new("Range", Role::Group),
);
let labeled_h = layout_size(&mut labeled, max).height;
let split_h = layout_size(&mut split, max).height;
let segmented_h = layout_size(&mut segmented, max).height;
assert_eq!(split_h, labeled_h);
assert_eq!(segmented_h, labeled_h);
}
#[test]
fn material_knobs_draw_disabled_empty_and_outlined() {
let compact = named("dark")
.tokens
.with_density(crate::density::Density::named(
crate::density::DensityName::Compact,
));
let tok = named("dark").tokens;
let btn = |n: &str| A11y::button(n);
let role = |n: &str, r: Role| A11y::new(n, r);
let _ = Cell::from(String::from("X"));
let _ = Cell::from(&String::from("Y"));
let mut filled = themed_button(
"Save",
Some(()),
tok,
Variant::Primary,
Icons::both(Icon::Check, Icon::Chevron),
btn("Save"),
);
draw_once(&mut filled);
let mut outlined = themed_button(
"Edit",
None::<()>,
compact,
Variant::Outlined,
Icons::NONE,
btn("Edit").with_disabled(true),
);
draw_once(&mut outlined);
let mut elevated = themed_button(
"Open",
Some(()),
tok,
Variant::Elevated,
Icons::NONE,
btn("Open"),
);
draw_once(&mut elevated);
let mut toggle_ic = icon_button_toggle(
Icon::Check,
true,
(),
tok,
Variant::Primary,
ControlSize::Comfortable,
btn("Bold").with_checked(true),
);
let mut off_ic = icon_button_toggle(
Icon::Menu,
false,
(),
compact,
Variant::Quiet,
ControlSize::Default,
btn("Off").with_checked(false).with_disabled(true),
);
draw_once(&mut off_ic);
draw_once(&mut toggle_ic);
let mut cells = segmented_button(
[Cell::new("Day").with_icon(Icon::Search), Cell::new("Week")],
0,
|i| i,
tok,
role("seg", Role::Group),
);
draw_once(&mut cells);
let opts = FieldOpts {
face: FieldFace::Outlined,
icons: Icons::both(Icon::Search, Icon::Close),
label: "Find",
max_len: Some(8),
};
let mut field = themed_text_input(
"q",
"hi",
|_| (),
None,
opts,
tok,
role("q", Role::TextBox),
None,
);
draw_once(&mut field);
let empty_opts = FieldOpts {
face: FieldFace::Filled,
icons: Icons::NONE,
label: "Name",
max_len: Some(4),
};
let mut empty = themed_text_input(
"Name",
"",
|_| (),
None,
empty_opts,
compact,
role("n", Role::TextBox).with_disabled(true),
None,
);
draw_once(&mut empty);
let mut vert = themed_slider(
0.0..=1.0,
0.3,
|_| (),
SliderMarks {
vertical: true,
thumb: "30%",
..SliderMarks::NONE
},
tok,
role("v", Role::Slider),
);
draw_once(&mut vert);
let mut vert_off = themed_slider(
0.0..=1.0,
0.0,
|_| (),
SliderMarks {
vertical: true,
..SliderMarks::NONE
},
tok,
role("vd", Role::Slider).with_disabled(true),
);
draw_once(&mut vert_off);
let mut tip_b: Element<'_, ()> = tooltip_wrap(
label("H", tok, role("Hb", Role::Header)),
"tip",
TooltipAnchor::Bottom,
tok,
role("tb", Role::Tooltip),
);
draw_once(&mut tip_b);
let mut tip_s: Element<'_, ()> = tooltip_wrap(
label("H", tok, role("Hs", Role::Header)),
"tip",
TooltipAnchor::Start,
tok,
role("ts", Role::Tooltip),
);
draw_once(&mut tip_s);
let mut filled_card: Element<'_, ()> = group_box(
"F",
label("x", tok, role("xf", Role::Status)),
tok,
CardFace::Filled,
role("cf", Role::Group),
);
draw_once(&mut filled_card);
let mut suggest = chip(
"go",
Some(()),
None,
tok,
Variant::Primary,
ChipKind::Suggestion,
Icons::trailing(Icon::Chevron),
btn("go"),
);
draw_once(&mut suggest);
let mut tabs = crate::collection::Tabs::new(["A", "B"]).with_icon(0, Icon::Search);
tabs.closable = true;
let mut strip = tab_bar(
&tabs,
|i| i,
|_| 0,
480.0,
true,
tok,
role("tabs", Role::Tab),
);
draw_once(&mut strip);
tabs.select(1);
let _ = tabs.close(0);
let mut busy: Element<'_, ()> = progress(
0.2,
None,
Some("wait"),
true,
tok,
role("p", Role::Progress),
);
draw_once(&mut busy);
let mut card: Element<'_, ()> = group_box(
"Box",
label("x", tok, role("x", Role::Status)),
tok,
CardFace::Outlined,
role("c", Role::Group),
);
draw_once(&mut card);
let mut mark: Element<'_, ()> = badge(
"9",
Some(icon_svg(Icon::Menu, tok, role("i", Role::Image))),
tok,
Variant::Primary,
BadgeSize::Small,
role("b", Role::Status),
);
draw_once(&mut mark);
let mut tip: Element<'_, ()> = tooltip_rich(
label("H", tok, role("H", Role::Header)),
"Save",
"Write.",
Some(("More".into(), ())),
TooltipAnchor::Top,
tok,
role("t", Role::Tooltip),
);
draw_once(&mut tip);
let mut input_chip = chip(
"tag",
None,
Some(()),
tok,
Variant::Quiet,
ChipKind::Input,
Icons::leading(Icon::Close),
btn("tag"),
);
draw_once(&mut input_chip);
let mut filter_out = chip(
"f",
Some(()),
None,
tok,
Variant::Outlined,
ChipKind::Filter,
Icons::NONE,
btn("f"),
);
draw_once(&mut filter_out);
let mut filter_el = chip(
"e",
Some(()),
None,
tok,
Variant::Elevated,
ChipKind::Filter,
Icons::NONE,
btn("e"),
);
draw_once(&mut filter_el);
let mut ic_def = icon_button(
Icon::Search,
Some(()),
tok,
Variant::Ghost,
ControlSize::Default,
btn("s"),
);
draw_once(&mut ic_def);
assert!(compact.density.pad < tok.density.pad);
assert_eq!(ControlSize::Compact.pad(), 4);
assert_eq!(ControlSize::Default.pad(), 8);
assert_eq!(ControlSize::Comfortable.pad(), 12);
assert_eq!(
TooltipAnchor::Follow.position(),
tooltip::Position::FollowCursor
);
assert_eq!(TooltipAnchor::Top.position(), tooltip::Position::Top);
assert_eq!(TooltipAnchor::Bottom.position(), tooltip::Position::Bottom);
assert_eq!(TooltipAnchor::Start.position(), tooltip::Position::Left);
assert_eq!(FieldFace::default(), FieldFace::Filled);
assert_eq!(CardFace::default(), CardFace::Elevated);
assert_eq!(BadgeSize::default(), BadgeSize::Large);
assert_eq!(ChipKind::default(), ChipKind::Assist);
assert_eq!(TooltipAnchor::default(), TooltipAnchor::Follow);
assert_eq!(ControlSize::default(), ControlSize::Default);
let comfy = tok.with_density(crate::density::Density::named(
crate::density::DensityName::Comfortable,
));
assert!(comfy.density.pad > tok.density.pad);
let mut comfy_btn = themed_button(
"Wide",
Some(()),
comfy,
Variant::Primary,
Icons::NONE,
btn("Wide"),
);
draw_once(&mut comfy_btn);
}
#[test]
fn named_densities_produce_distinct_control_heights() {
let base = named("dark").tokens;
let height = |name| control_height(base.with_density(crate::density::Density::named(name)));
let compact = height(crate::density::DensityName::Compact);
let default = height(crate::density::DensityName::Default);
let comfortable = height(crate::density::DensityName::Comfortable);
must(
compact < default,
format!("compact {compact} must be shorter than default {default}"),
);
must(
default < comfortable,
format!("default {default} must be shorter than comfortable {comfortable}"),
);
let max = iced::Size::new(400.0, 80.0);
let mut compact_btn = themed_button(
"Save",
Some(()),
base.with_density(crate::density::Density::named(
crate::density::DensityName::Compact,
)),
Variant::Primary,
Icons::NONE,
A11y::button("Save"),
);
let mut default_btn = themed_button(
"Save",
Some(()),
base,
Variant::Primary,
Icons::NONE,
A11y::button("Save"),
);
let mut comfortable_btn = themed_button(
"Save",
Some(()),
base.with_density(crate::density::Density::named(
crate::density::DensityName::Comfortable,
)),
Variant::Primary,
Icons::NONE,
A11y::button("Save"),
);
let hc = layout_size(&mut compact_btn, max).height;
let hd = layout_size(&mut default_btn, max).height;
let hh = layout_size(&mut comfortable_btn, max).height;
assert!(hc < hd, "compact button {hc} < default {hd}");
assert!(hd < hh, "default button {hd} < comfortable {hh}");
let slider_fn = include_str!("widget.rs")
.split("pub fn themed_slider")
.nth(1)
.unwrap();
assert_eq!(
slider_fn
.matches(".style(style::slider_style(tok))")
.count(),
2
);
}
#[test]
fn widgets_paint_themed_faces() {
let tok = named("dark").tokens;
let btn = |n: &str| A11y::button(n);
let role = |n: &str, r: Role| A11y::new(n, r);
let code = Content::with_text("fn main() {}\n");
let mut painted: Vec<Element<'_, ()>> = vec![
code_block(&code, |_| (), tok, role("fn", Role::Group)),
hyperlink("l", (), tok, role("l", Role::Link)),
busy_overlay(
label("Body", tok, role("Body", Role::Status)),
true,
0.2,
tok,
role("busy", Role::Group),
),
image_slot(
ImageSlot::Error("missing".into()),
48.0,
48.0,
tok,
role("err", Role::Image),
),
image_slot(
ImageSlot::Loading,
48.0,
48.0,
tok,
role("load", Role::Image),
),
image_slot(
ImageSlot::Ready {
handle: iced::widget::image::Handle::from_bytes(TEST_PNG),
fit: iced::ContentFit::Contain,
},
48.0,
48.0,
tok,
role("img", Role::Image),
),
progress_ring(0.5, Some("half"), tok, role("pr", Role::Progress)),
number_input(3.0, |_| (), tok, role("n", Role::SpinButton)),
textarea(
&code,
|_| (),
tok,
crate::layout::FILL,
role("ta", Role::TextBox),
),
highlighted_code(
&code,
"rs",
|_| (),
tok,
"dark",
crate::layout::FILL,
role("code", Role::Group),
),
chip(
"ok",
None,
None,
tok,
Variant::Success,
ChipKind::Assist,
Icons::NONE,
btn("ok"),
),
chip(
"x",
None,
Some(()),
tok,
Variant::Quiet,
ChipKind::Assist,
Icons::NONE,
btn("x"),
),
tooltip_wrap(
label("n", tok, role("n", Role::Status)),
"tip",
TooltipAnchor::Follow,
tok,
role("tt", Role::Tooltip),
),
badge(
"ok",
None,
tok,
Variant::Success,
BadgeSize::Large,
role("ok", Role::Status),
),
group_box(
"Box",
label("in", tok, role("in", Role::Status)),
tok,
CardFace::Elevated,
role("box", Role::Group),
),
banner("Hi", None, tok, role("ban", Role::Status)),
info_bar(ToastKind::Info, "n", tok, role("ib", Role::Status)),
];
for el in &mut painted {
draw_once(el);
}
let tree = TreeNode::branch(1, "r", vec![TreeNode::leaf(2, "c")]);
let mut tv = tree_view(
&tree,
Some(1),
|_| (),
|_| (),
tok,
role("tree", Role::Tree),
);
draw_once(&mut tv);
let acc = Accordion { open: Some(0) };
let mut av = accordion_view(
&["A".into()],
vec![label("b", tok, role("b", Role::Header))],
&acc,
|_| (),
tok,
role("acc", Role::Group),
);
draw_once(&mut av);
let list = VecList {
items: vec![crate::collection::ListRow::new("a").with_meta("m")],
};
let mut lv = list_view(
&list,
&Sel::Single(0),
|_| (),
tok,
VisibleWindow::new(80.0),
24.0,
2,
|_| (),
"Empty",
|_| tok.muted,
None,
RowFace::FLUSH,
|_| (),
role("list", Role::List),
);
draw_once(&mut lv);
let empty_list = VecList::default();
let mut empty_lv = list_view(
&empty_list,
&Sel::None,
|_| (),
tok,
VisibleWindow::new(80.0),
24.0,
2,
|_| (),
"Empty",
|_| tok.muted,
None,
RowFace::FLUSH,
|_| (),
role("list-empty", Role::List),
);
draw_once(&mut empty_lv);
let two = VecList {
items: vec![
crate::collection::ListRow::new("a").with_meta("m"),
crate::collection::ListRow::new("b").with_meta("n"),
],
};
let mut color_lv = list_view(
&two,
&Sel::Single(0),
|_| (),
tok,
VisibleWindow::new(80.0),
24.0,
2,
|_| (),
"Empty",
|i| {
if i == 0 {
tok.danger
} else {
tok.muted
}
},
None,
RowFace::FLUSH,
|_| (),
role("list-color", Role::List),
);
draw_once(&mut color_lv);
let mut dead_lv = list_view(
&list,
&Sel::Single(0),
|_| (),
tok,
VisibleWindow::new(80.0),
24.0,
2,
|_| (),
"Empty",
|_| tok.muted,
None,
RowFace::FLUSH,
|_| (),
role("list", Role::List).with_disabled(true),
);
draw_once(&mut dead_lv);
let table = TableModel {
headers: vec!["A".into()],
rows: vec![vec!["1".into()]],
sort_col: None,
sort_asc: true,
checks: Vec::new(),
};
let widths = crate::collection::ColumnLayout::new(vec![80.0]);
let mut dt = data_table(
&table,
&Sel::Single(0),
Some((0, 0)),
&widths,
true,
VisibleWindow::new(80.0),
24.0,
2,
|_, _| (),
|_| (),
|_| (),
|_| (),
|_| (),
tok,
role("table", Role::Table),
);
draw_once(&mut dt);
let mut dead_dt = data_table(
&table,
&Sel::Single(0),
Some((0, 0)),
&widths,
true,
VisibleWindow::new(80.0),
24.0,
2,
|_, _| (),
|_| (),
|_| (),
|_| (),
|_| (),
tok,
role("table", Role::Table).with_disabled(true),
);
draw_once(&mut dead_dt);
let mut dead_tree = tree_view(
&tree,
Some(1),
|_| (),
|_| (),
tok,
role("tree", Role::Tree).with_disabled(true),
);
draw_once(&mut dead_tree);
let mut dead_grid = item_grid(
&["A".into(), "B".into()],
|_| (),
None,
tok,
role("grid", Role::List).with_disabled(true),
);
draw_once(&mut dead_grid);
let disabled = A11y::button("off").with_disabled(true);
let mut dead_link = hyperlink("l", (), tok, disabled.clone());
draw_once(&mut dead_link);
let mut dead_num = number_input(
1.0,
|_| (),
tok,
role("n", Role::SpinButton).with_disabled(true),
);
draw_once(&mut dead_num);
let mut dead_ta = textarea(
&code,
|_| (),
tok,
crate::layout::FILL,
role("ta", Role::TextBox).with_disabled(true),
);
draw_once(&mut dead_ta);
let mut dead_sel = selectable(
&code,
|_| (),
tok,
typo::FontFace::Ui,
role("sel", Role::TextBox).with_disabled(true),
);
draw_once(&mut dead_sel);
let copy = crate::action::Action::new("value.copy", "Copy", ());
let mut dead_vf = value_field(
"Path",
&code,
|_| (),
Some(©),
typo::FontFace::Mono,
crate::layout::FORM_LABEL,
tok,
Direction::Ltr,
role("vf", Role::Group).with_disabled(true),
);
draw_once(&mut dead_vf);
let mut dead_block = code_block(
&code,
|_| (),
tok,
role("blk", Role::TextBox).with_disabled(true),
);
draw_once(&mut dead_block);
let mut dead_code = highlighted_code(
&code,
"rs",
|_| (),
tok,
"dark",
crate::layout::FILL,
role("code", Role::Group).with_disabled(true),
);
draw_once(&mut dead_code);
let mut dead_x = dismiss_button((), tok, A11y::button("x").with_disabled(true));
draw_once(&mut dead_x);
let toast = Toast {
id: 1,
kind: ToastKind::Info,
text: "t".into(),
ttl_ms: 10,
};
let mut tvw = toast_view(&toast, (), tok, role("t", Role::Status));
draw_once(&mut tvw);
let mut closed = accordion_view(
&["A".into()],
vec![label("b", tok, role("b", Role::Header))],
&Accordion { open: None },
|_| (),
tok,
role("acc", Role::Group),
);
draw_once(&mut closed);
let mut exp_shut = expander(
"Notes",
label("more", tok, role("more", Role::Status)),
48.0,
false,
|_| (),
tok,
role("exp", Role::Group),
);
draw_once(&mut exp_shut);
let mut exp_open = expander(
"Notes",
label("more", tok, role("more", Role::Status)),
48.0,
true,
|_| (),
tok,
role("exp", Role::Group),
);
draw_once(&mut exp_open);
let mut exp_off = expander(
"Notes",
label("more", tok, role("more", Role::Status)),
48.0,
false,
|_| (),
tok,
role("exp", Role::Group).with_disabled(true),
);
draw_once(&mut exp_off);
}
#[test]
fn secret_field_does_not_name_the_editor_with_the_secret() {
let src = include_str!("widget.rs");
let secret_src = src
.split("pub fn secret_field")
.nth(1)
.unwrap()
.split("pub fn textarea")
.next()
.unwrap();
assert!(secret_src.contains("password_input("));
assert!(!secret_src.contains("apply_name(value)"));
assert!(secret_src.contains("a11y.child(Role::TextBox)"));
let tok = named("dark").tokens;
let secret = "hunter2-never-in-name";
let copy = crate::action::Action::new("secret.copy", "Copy", ());
let _: Element<'_, ()> = secret_field(
"Token",
secret,
|_| (),
false,
(),
©,
tok,
Direction::Ltr,
A11y::new("api-token", Role::Group),
);
let search_src = src
.split("pub fn search_input_clear")
.nth(1)
.unwrap()
.split("pub fn themed_pick_list")
.next()
.unwrap();
assert!(!search_src.contains("apply_name(value)"));
assert!(search_src.contains("a11y.child(Role::TextBox)"));
assert!(src.contains("pub fn search_input_clear"));
let vf_src = src
.split("pub fn value_field")
.nth(1)
.unwrap()
.split("pub fn textarea")
.next()
.unwrap();
assert!(vf_src.contains("label_width") && vf_src.contains("Length::Fixed"));
assert!(vf_src.contains("Length::Fill"));
}
#[test]
fn search_input_disabled_does_not_emit_and_keeps_stable_name() {
use iced::advanced::clipboard;
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::keyboard;
use iced::mouse;
use iced::{Event, Font, Pixels, Point, Rectangle, Size};
let tok = named("dark").tokens;
let a11y = A11y::new("find", Role::TextBox).with_disabled(true);
assert_eq!(a11y.child(Role::TextBox).node_id(), "textbox|find|1");
let mut el: Element<'_, String> = search_input("typed-query", |s| s, tok, a11y);
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(240.0, 40.0));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let viewport = Rectangle::new(Point::ORIGIN, Size::new(240.0, 40.0));
let mut clipboard = clipboard::Null;
let mut messages = Vec::new();
{
let mut shell = iced::advanced::Shell::new(&mut messages);
el.as_widget_mut().update(
&mut tree,
&Event::Keyboard(keyboard::Event::KeyPressed {
key: keyboard::Key::Character("x".into()),
modified_key: keyboard::Key::Character("x".into()),
physical_key: keyboard::key::Physical::Unidentified(
keyboard::key::NativeCode::Unidentified,
),
location: keyboard::Location::Standard,
modifiers: keyboard::Modifiers::empty(),
text: Some("x".into()),
repeat: false,
}),
layout,
mouse::Cursor::Available(Point::new(40.0, 12.0)),
&renderer,
&mut clipboard,
&mut shell,
&viewport,
);
}
assert!(messages.is_empty());
}
#[test]
fn tree_toggle_expands_and_select_is_a_distinct_id() {
let mut tree = TreeNode::branch(
1,
"root",
vec![TreeNode::leaf(2, "child"), TreeNode::leaf(3, "other")],
);
assert_eq!(tree.children[0].label, "child");
assert_eq!(tree.children[1].label, "other");
assert!(crate::collection::tree_toggle(&mut tree, 1));
assert!(!tree.expanded);
let visible: Vec<u64> = tree
.flatten()
.into_iter()
.map(|(_, id, _, _, _)| id)
.collect();
assert_eq!(visible, vec![1]);
assert!(crate::collection::tree_toggle(&mut tree, 1));
assert!(tree.expanded);
let child = tree.children[0].id;
let other = tree.children[1].id;
assert_ne!(child, other);
let selected = Some(other);
assert_eq!(selected, Some(3));
assert!(tree.expanded);
let tok = named("dark").tokens;
let _: Element<'_, u64> = tree_view(
&tree,
selected,
|id| id,
|id| id,
tok,
A11y::new("tree", Role::Tree),
);
let _: Element<'_, u64> = tree_view(
&tree,
None,
|id| id,
|id| id,
tok,
A11y::new("tree", Role::Tree),
);
}
#[test]
fn fill_editors_publish_fill_size() {
let content = Content::with_text("# hi\n");
let tok = named("dark").tokens;
let code = highlighted_code(
&content,
"md",
|_| (),
tok,
"dark",
crate::layout::FILL,
A11y::new("source", Role::TextBox),
);
let size = code.as_widget().size();
assert_eq!(size.width, Length::Fill);
assert_eq!(size.height, Length::Fill);
let area = textarea(
&content,
|_| (),
tok,
crate::layout::FILL,
A11y::new("body", Role::TextBox),
);
let size = area.as_widget().size();
assert_eq!(size.width, Length::Fill);
assert_eq!(size.height, Length::Fill);
let fixed = textarea(
&content,
|_| (),
tok,
crate::layout::fixed(120.0),
A11y::new("body", Role::TextBox),
);
assert_eq!(fixed.as_widget().size().height, Length::Fixed(120.0));
let body = selectable(
&content,
|_| (),
tok,
typo::FontFace::Ui,
A11y::new("body", Role::TextBox),
);
assert_eq!(body.as_widget().size().height, Length::Shrink);
assert_eq!(body.as_widget().size().width, Length::Fill);
let block = code_block(&content, |_| (), tok, A11y::new("src", Role::TextBox));
assert_eq!(block.as_widget().size().height, Length::Shrink);
let src = include_str!("widget.rs");
let hl = src
.split("pub fn highlighted_code")
.nth(1)
.unwrap()
.split("fn editor_frame")
.next()
.unwrap();
assert!(hl.contains("select_only"));
assert!(hl.contains("typo::CODE"));
assert!(!hl.contains("if !a11y.disabled"));
let block_src = src
.split("pub fn code_block")
.nth(1)
.unwrap()
.split("/// A text link")
.next()
.unwrap();
assert!(block_src.contains("typo::CODE"));
}
#[test]
fn select_only_keeps_selection_and_drops_edits() {
use iced::widget::text_editor::{Action, Edit, Motion};
assert_eq!(
select_only(Action::Edit(Edit::Insert('x'))),
Action::Scroll { lines: 0 }
);
assert_eq!(
select_only(Action::Edit(Edit::Delete)),
Action::Scroll { lines: 0 }
);
assert_eq!(select_only(Action::SelectAll), Action::SelectAll);
assert_eq!(
select_only(Action::Select(Motion::Right)),
Action::Select(Motion::Right)
);
assert_eq!(
select_only(Action::Scroll { lines: 3 }),
Action::Scroll { lines: 3 }
);
assert_eq!(
select_only(Action::Move(Motion::Left)),
Action::Move(Motion::Left)
);
let click = Action::Click(iced::Point::new(1.0, 2.0));
assert_eq!(select_only(click.clone()), click);
let drag = Action::Drag(iced::Point::new(4.0, 5.0));
assert_eq!(select_only(drag.clone()), drag);
let tok = named("dark").tokens;
let theme = crate::theme::iced_theme("dark", tok);
let s = tok.scheme();
let sel = selectable_style(tok)(&theme, iced::widget::text_editor::Status::Active);
assert_eq!(sel.value, s.on_surface);
assert_eq!(sel.placeholder, s.on_surface_variant);
assert_eq!(sel.selection, s.secondary_container);
let frame = editor_frame(tok);
assert_eq!(
frame.background,
Some(iced::Background::Color(s.surface_container_highest))
);
assert_eq!(frame.border.color, s.outline_variant);
let ed = editor_style(tok)(&theme, iced::widget::text_editor::Status::Active);
assert_eq!(ed.value, s.on_surface);
assert_eq!(ed.selection, s.secondary_container);
let _: Element<'_, ()> = themed_slider(
0.0..=1.0,
0.5,
|_| (),
SliderMarks::NONE,
tok,
A11y::new("off", Role::Slider).with_disabled(true),
);
}
#[test]
fn virtual_column_mounts_only_window_range() {
let tok = named("dark").tokens;
let heights = crate::collection::expand_card_heights(20, 40.0, &[(2, 100.0)]);
let win = VisibleWindow {
start: 1,
end: 5,
scroll: 40.0,
viewport: 120.0,
};
let mut el: Element<'_, ()> = virtual_column(
&heights,
win,
2,
Some(2),
|_| (),
Some(Id::from("vc-scroll")),
tok,
|i| label(format!("r{i}"), tok, A11y::new("r", Role::ListItem)),
A11y::new("vc", Role::List),
);
draw_once(&mut el);
let empty_h: [f32; 0] = [];
let mut empty: Element<'_, ()> = virtual_column(
&empty_h,
VisibleWindow::new(80.0),
0,
None,
|_| (),
None,
tok,
{
let paint = |_: usize| label("x", tok, A11y::new("x", Role::ListItem));
let _ = paint(0);
paint
},
A11y::new("vc0", Role::List),
);
draw_once(&mut empty);
let src = include_str!("widget.rs");
let body = src
.split("pub fn virtual_column")
.nth(1)
.unwrap()
.split("/// A virtualized row list.")
.next()
.unwrap();
assert!(body.contains("virtual_clip") && body.contains("RowHeights::PerRow"));
}
#[test]
fn markdown_view_uses_structured_selectable_layout() {
let tok = named("dark").tokens;
let source = "# Title\n\nFirst paragraph.\n\nSecond block.";
let doc = parse(source);
let _: Element<'_, ()> = markdown_view(
&doc.items,
None,
|_| (),
tok,
|_| (),
A11y::new("md", Role::Group),
);
let plain = crate::select::markdown_plain(&doc.items);
assert!(plain.contains("Title") && plain.contains("Second block."));
let src = include_str!("widget.rs");
let md = src
.split("pub fn markdown_view")
.nth(1)
.unwrap()
.split("fn markdown_style")
.next()
.unwrap();
assert!(md.contains("iced_selection::markdown::view"));
assert!(!md.contains("Rich::with_spans"));
assert!(!md.contains("let _ = span"));
assert!(md.contains("covers"));
let mut st = crate::select::markdown_select(
&doc.items,
crate::select::MarkdownSelect::default(),
crate::select::MarkdownPointer::Move(0.0),
);
st = crate::select::markdown_select(&doc.items, st, crate::select::MarkdownPointer::Press);
let end_y = doc
.items
.iter()
.map(crate::select::markdown_item_extent)
.sum::<f32>();
st = crate::select::markdown_select(
&doc.items,
st,
crate::select::MarkdownPointer::Move(end_y),
);
let mut painted: Element<'_, crate::select::MarkdownPointer> = markdown_view(
&doc.items,
Some(&st.span),
|ev| ev,
tok,
|_| crate::select::MarkdownPointer::Release,
A11y::new("md-span", Role::Group),
);
draw_once(&mut painted);
assert!(st.span.text(&doc.items).contains("Title"));
assert!(st.span.text(&doc.items).contains("First"));
let head = crate::select::MarkdownSpan {
start: crate::select::MarkdownPos { item: 0, offset: 0 },
end: crate::select::MarkdownPos { item: 0, offset: 5 },
};
let mut part: Element<'_, ()> = markdown_view(
&doc.items,
Some(&head),
|_| (),
tok,
|_| (),
A11y::new("md-head", Role::Group),
);
draw_once(&mut part);
}
#[test]
fn list_slots_and_table_checks_still_virtualize() {
let tok = named("dark").tokens;
let list = VecList {
items: (0..40)
.map(|i| {
crate::collection::ListRow::new(format!("r{i}"))
.with_leading(crate::collection::RowSlot::Check(i == 0))
.with_trailing(crate::collection::RowSlot::Icon(Icon::Search))
})
.collect(),
};
let win = VisibleWindow::new(120.0);
let mut el: Element<'_, usize> = list_view(
&list,
&Sel::None,
|i| i,
tok,
win,
24.0,
2,
|_| 0,
"Empty",
|_| tok.muted,
None,
RowFace::FLUSH,
|i| i,
A11y::new("slotted", Role::List),
);
draw_once(&mut el);
let empty_model = VecList::default();
let mut empty_slots: Element<'_, ()> = list_view(
&empty_model,
&Sel::None,
|_| (),
tok,
win,
24.0,
0,
|_| (),
"Empty",
|_| tok.muted,
None,
RowFace::FLUSH,
|_| (),
A11y::new("empty-slots", Role::List),
);
draw_once(&mut empty_slots);
let table = TableModel {
headers: vec!["N".into()],
rows: (0..40).map(|i| vec![format!("{i}")]).collect(),
sort_col: None,
sort_asc: true,
checks: (0..40).map(|i| i % 2 == 0).collect(),
};
let cols = crate::collection::ColumnLayout::new(vec![80.0]);
let mut dt: Element<'_, usize> = data_table(
&table,
&Sel::None,
None,
&cols,
false,
win,
24.0,
2,
|_, _| 0,
|_| 0,
|_| 0,
|_| 0,
|i| i,
tok,
A11y::new("checks", Role::Table),
);
draw_once(&mut dt);
let _: Element<'_, ()> = themed_slider(
0.0..=1.0,
0.2,
|_| (),
SliderMarks {
ticks: 3,
min: "lo",
max: "hi",
..SliderMarks::NONE
},
tok,
A11y::new("ticked", Role::Slider),
);
let _: Element<'_, ()> = themed_slider(
0.0..=1.0,
0.2,
|_| (),
SliderMarks::NONE,
tok,
A11y::new("plain", Role::Slider).with_disabled(true),
);
let mut buf: Element<'_, ()> = progress(
0.3,
Some(0.6),
Some("30% · 4s"),
false,
tok,
A11y::new("buf", Role::Progress),
);
draw_once(&mut buf);
let mut zero: Element<'_, ()> = progress(
0.0,
Some(0.0),
None,
false,
tok,
A11y::new("zero", Role::Progress),
);
draw_once(&mut zero);
let tabs = Tabs::new(["A", "B", "C", "D", "E"]).with_badge(0, "2");
let mut ov: Element<'_, usize> = tab_bar(
&tabs,
|i| i,
|_| 0,
80.0,
false,
tok,
A11y::new("ov", Role::Tab),
);
draw_once(&mut ov);
let _: Element<'_, usize> = tab_bar(
&tabs,
|i| i,
|_| 0,
0.0,
false,
tok,
A11y::new("all", Role::Tab),
);
assert_eq!(tab_overflow_index(&tabs.titles, "C"), 2);
assert_eq!(tab_overflow_pick(tabs.titles.clone(), |i| i)("C".into()), 2);
assert_eq!(tab_overflow_index(&tabs.titles, "nope"), 0);
assert_eq!(tab_visible_count(&[], 100.0), 0);
assert_eq!(tab_visible_count(&tabs.titles, 0.0), 5);
assert!(tab_visible_count(&tabs.titles, 80.0) < 5);
assert_eq!(tab_visible_count(&["A".into()], 10.0), 1);
let dead_slots = VecList {
items: vec![crate::collection::ListRow::new("x")
.with_leading(crate::collection::RowSlot::Check(true))],
};
let mut dead_check: Element<'_, ()> = list_view(
&dead_slots,
&Sel::None,
|_| (),
tok,
win,
24.0,
0,
|_| (),
"Empty",
|_| tok.muted,
None,
RowFace::FLUSH,
|_| (),
A11y::new("dead-check", Role::List).with_disabled(true),
);
draw_once(&mut dead_check);
}
#[test]
fn list_and_table_forward_scrollable_offset() {
use iced::advanced::clipboard;
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::mouse;
use iced::{Event, Font, Pixels, Point, Rectangle, Size};
let tok = named("dark").tokens;
let list = VecList {
items: (0..80)
.map(|i| crate::collection::ListRow::new(format!("r{i}")))
.collect(),
};
let window = VisibleWindow {
start: 0,
end: 0,
scroll: 0.0,
viewport: 200.0,
};
let drive = |el: &mut Element<'_, VisibleWindow>| {
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(320.0, 200.0));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let viewport = Rectangle::new(Point::ORIGIN, Size::new(320.0, 200.0));
let mut clipboard = clipboard::Null;
let mut messages = Vec::new();
{
let mut shell = iced::advanced::Shell::new(&mut messages);
el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::WheelScrolled {
delta: mouse::ScrollDelta::Lines { x: 0.0, y: -10.0 },
}),
layout,
mouse::Cursor::Available(Point::new(16.0, 40.0)),
&renderer,
&mut clipboard,
&mut shell,
&viewport,
);
}
messages
};
let mut list_el: Element<'_, VisibleWindow> = list_view(
&list,
&Sel::Single(0),
|_| window,
tok,
window,
20.0,
4,
|w| w,
"Empty",
|_| tok.muted,
Some(Id::from("list-scroll")),
RowFace::FLUSH,
|_| window,
A11y::new("list", Role::List),
);
let _ = drive(&mut list_el);
let table = TableModel {
headers: vec!["N".into(), "M".into()],
rows: (0..80)
.map(|i| vec![format!("r{i}"), format!("m{i}")])
.collect(),
sort_col: None,
sort_asc: true,
checks: Vec::new(),
};
let tw = crate::collection::ColumnLayout::new(vec![80.0, 80.0]);
let mut table_el: Element<'_, VisibleWindow> = data_table(
&table,
&Sel::Single(0),
Some((0, 0)),
&tw,
true,
window,
20.0,
4,
|_, _| window,
|_| window,
|w| w,
|_| window,
|_| window,
tok,
A11y::new("table", Role::Table),
);
let _ = drive(&mut table_el);
let after =
crate::collection::window_after_scroll(window, 4.0, 200.0, 20.0, 80, 4, Some(0));
assert_eq!(after.start, 0);
assert!((after.scroll - 4.0).abs() < 0.01);
let mut scroller: Element<'_, f32> = themed_scroll(
iced::widget::column![
label("a", tok, A11y::new("a", Role::Status)),
Space::new().height(800.0),
]
.into(),
tok,
A11y::new("scroll", Role::Group),
false,
Some(Id::from("scroll-host")),
None::<fn(_) -> f32>,
);
let msgs = drive_scroll(&mut scroller);
let _ = msgs;
}
fn walk_bounds(layout: iced::advanced::layout::Layout<'_>, out: &mut Vec<iced::Rectangle>) {
out.push(layout.bounds());
for child in layout.children() {
walk_bounds(child, out);
}
}
#[test]
fn list_view_rail_drag_updates_scroll_without_changing_range() {
use iced::advanced::clipboard;
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::mouse;
use iced::{Event, Font, Pixels, Point, Rectangle, Size};
let tok = named("dark").tokens;
let list = VecList {
items: (0..80)
.map(|i| crate::collection::ListRow::new(format!("r{i}")))
.collect(),
};
let row_h = 20.0;
let viewport = 200.0;
let window = crate::collection::visible_window(2.0, viewport, row_h, 80, 4, Some(0));
let start = window.start;
let end = window.end;
let mut el: Element<'_, VisibleWindow> = list_view(
&list,
&Sel::Single(0),
|_| window,
tok,
window,
row_h,
4,
|w| w,
"Empty",
|_| tok.muted,
Some(Id::from("list-rail")),
RowFace::FLUSH,
|_| window,
A11y::new("list", Role::List),
);
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(320.0, viewport));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let mut boxes = Vec::new();
walk_bounds(layout, &mut boxes);
let rail_w = crate::chrome::SCROLL_RAIL_WIDTH;
let rb = *boxes
.iter()
.find(|b| (b.width - rail_w).abs() < 0.6 && b.height > 20.0)
.expect("rail");
let content = 80.0 * row_h;
let (thumb_y, thumb_h) = crate::collection::scroller_span(
content,
viewport,
window.scroll,
rb.height,
crate::chrome::SCROLL_HANDLE_MIN,
);
let grab = Point::new(rb.x + rb.width / 2.0, rb.y + thumb_y + thumb_h / 2.0);
let moved = Point::new(grab.x, grab.y + 1.0);
let vp = Rectangle::new(Point::ORIGIN, Size::new(320.0, viewport));
let mut clipboard = clipboard::Null;
let mut messages = Vec::new();
{
let mut shell = iced::advanced::Shell::new(&mut messages);
el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
layout,
mouse::Cursor::Available(grab),
&renderer,
&mut clipboard,
&mut shell,
&vp,
);
el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::CursorMoved { position: moved }),
layout,
mouse::Cursor::Available(moved),
&renderer,
&mut clipboard,
&mut shell,
&vp,
);
}
assert!(!messages.is_empty());
let pixel = messages
.iter()
.copied()
.find(|w| w.start == start && w.end == end && (w.scroll - window.scroll).abs() > 0.5)
.expect("rail drag moves scroll without remounting");
let (y0, _) = crate::collection::scroller_span(
content,
viewport,
window.scroll,
rb.height,
crate::chrome::SCROLL_HANDLE_MIN,
);
let (y1, _) = crate::collection::scroller_span(
content,
viewport,
pixel.scroll,
rb.height,
crate::chrome::SCROLL_HANDLE_MIN,
);
assert!(y1 > y0);
}
#[test]
fn list_view_paints_each_row_at_index_times_height_minus_scroll() {
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::{Font, Pixels, Size};
let tok = named("dark").tokens;
let row_h = 20.0;
let viewport = 200.0;
let scroll = 200.0;
let overscan = 4;
let n = 80;
let list = VecList {
items: (0..n)
.map(|i| crate::collection::ListRow::new(format!("r{i}")))
.collect(),
};
let window = crate::collection::visible_window(scroll, viewport, row_h, n, overscan, None);
let mut el: Element<'_, VisibleWindow> = list_view(
&list,
&Sel::None,
|_| window,
tok,
window,
row_h,
overscan,
|w| w,
"Empty",
|_| tok.muted,
None,
RowFace::FLUSH,
|_| window,
A11y::new("list", Role::List),
);
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(320.0, viewport));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let origin = layout.bounds();
let mut boxes = Vec::new();
walk_bounds(layout, &mut boxes);
let rows: Vec<f32> = boxes
.iter()
.filter(|b| (b.height - row_h).abs() < 0.6 && b.width > 40.0)
.map(|b| b.y - origin.y)
.collect();
must(
rows.iter().any(|y| y.abs() < 1.0),
format!("row 10 must sit at the clip top, got {rows:?}"),
);
for y in &rows {
let along = y + scroll;
let i = (along / row_h).round();
must(
(along - i * row_h).abs() < 1.0,
format!("painted y {y} is not k*{row_h} - {scroll}"),
);
}
let sep_list = VecList {
items: vec![
crate::collection::ListRow::new("a"),
crate::collection::ListRow::separator(),
crate::collection::ListRow::new("b"),
],
};
let sep_h = 36.0;
let sep_win = VisibleWindow::new(140.0);
let mut sep_el: Element<'_, VisibleWindow> = list_view(
&sep_list,
&Sel::None,
|_| sep_win,
tok,
sep_win,
sep_h,
1,
|w| w,
"Empty",
|_| tok.muted,
None,
RowFace::FLUSH,
|_| sep_win,
A11y::new("sep-list", Role::List),
);
let mut sep_tree = Tree::new(sep_el.as_widget());
let sep_limits = Limits::new(Size::ZERO, Size::new(320.0, 140.0));
let sep_node = sep_el
.as_widget_mut()
.layout(&mut sep_tree, &renderer, &sep_limits);
let sep_layout = Layout::new(&sep_node);
let sep_origin = sep_layout.bounds();
let mut sep_boxes = Vec::new();
walk_bounds(sep_layout, &mut sep_boxes);
let sep_rows: Vec<f32> = sep_boxes
.iter()
.filter(|b| (b.height - sep_h).abs() < 0.6 && b.width > 40.0)
.map(|b| b.y - sep_origin.y)
.collect();
must(
sep_rows.iter().any(|y| (*y - sep_h).abs() < 1.0),
format!("separator index 1 must sit at y={sep_h}, got {sep_rows:?}"),
);
}
#[test]
fn list_view_variable_row_is_not_index_times_uniform() {
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::{Font, Pixels, Size};
let tok = named("dark").tokens;
let heights = [20.0_f32, 50.0, 20.0];
let list = VecList {
items: vec![
crate::collection::ListRow::new("a").with_meta("this morning"),
crate::collection::ListRow::new("b").with_meta("yesterday"),
crate::collection::ListRow::new("c"),
],
};
let window = crate::collection::visible_window_var(0.0, 200.0, &heights, 0, None);
let mut el: Element<'_, f32> = list_view(
&list,
&Sel::None,
|_| 0.0,
tok,
window,
crate::collection::RowHeights::PerRow(&heights),
0,
|w| w.scroll,
"Empty",
|_| tok.muted,
None,
RowFace::FLUSH,
|_| 0.0,
A11y::new("var-list", Role::List),
);
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(320.0, 200.0));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let origin = layout.bounds();
let mut boxes = Vec::new();
walk_bounds(layout, &mut boxes);
let tall: Vec<f32> = boxes
.iter()
.filter(|b| (b.height - 50.0).abs() < 1.0 && b.width > 40.0)
.map(|b| b.y - origin.y)
.collect();
must(
tall.iter().any(|y| (*y - 20.0).abs() < 2.0),
format!("second row starts at 20px, not 2*uniform, got {tall:?}"),
);
let _ = drive_scroll(&mut el);
let mut cards: Element<'_, ()> = list_view(
&list,
&Sel::Single(0),
|_| (),
tok,
window,
crate::collection::RowHeights::PerRow(&heights),
0,
|_| (),
"Empty",
|_| tok.muted,
None,
RowFace::Card {
meter: Some(|i| if i == 0 { 0.8 } else { 0.2 }),
},
|_| (),
A11y::new("card-list", Role::List),
);
let mut tree_c = Tree::new(cards.as_widget());
let node_c = cards.as_widget_mut().layout(
&mut tree_c,
&renderer,
&Limits::new(Size::ZERO, Size::new(320.0, 200.0)),
);
let layout_c = Layout::new(&node_c);
let origin_c = layout_c.bounds();
let mut boxes_c = Vec::new();
walk_bounds(layout_c, &mut boxes_c);
let cards_y: Vec<f32> = boxes_c
.iter()
.filter(|b| (b.height - 50.0).abs() < 1.0 && b.width > 40.0)
.map(|b| b.y - origin_c.y)
.collect();
must(
cards_y.iter().any(|y| (*y - 22.0).abs() < 3.0),
format!("card gap is 2px so the 50px row starts at 22, got {cards_y:?}"),
);
draw_once(&mut cards);
let bare = VecList {
items: vec![crate::collection::ListRow::new("only")],
};
let mut no_meter: Element<'_, ()> = list_view(
&bare,
&Sel::None,
|_| (),
tok,
VisibleWindow::new(80.0),
72.0,
0,
|_| (),
"Empty",
|_| tok.muted,
None,
RowFace::Card {
meter: None::<fn(usize) -> f32>,
},
|_| (),
A11y::new("card-bare", Role::List).with_disabled(true),
);
draw_once(&mut no_meter);
let mut tree0 = Tree::new(el.as_widget());
let renderer0 = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let _ = el.as_widget_mut().layout(
&mut tree0,
&renderer0,
&Limits::new(Size::ZERO, Size::new(320.0, 0.0)),
);
}
#[test]
fn data_table_frozen_column_stays_when_unfrozen_scrolls() {
use iced::advanced::clipboard;
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::mouse;
use iced::{Event, Font, Pixels, Point, Rectangle, Size};
let tok = named("dark").tokens;
let table = TableModel {
headers: vec!["A".into(), "B".into(), "C".into()],
rows: vec![vec!["1".into(), "2".into(), "3".into()]],
sort_col: None,
sort_asc: true,
checks: Vec::new(),
};
let mut cols = crate::collection::ColumnLayout::new(vec![80.0, 80.0, 80.0]).with_frozen(1);
cols.set_h_scroll(0.0);
let window = VisibleWindow::new(80.0);
let leading = |cols: &crate::collection::ColumnLayout| {
let mut el: Element<'_, ()> = data_table(
&table,
&Sel::None,
None,
cols,
false,
window,
24.0,
0,
|_, _| (),
|_| (),
|_| (),
|_| (),
|_| (),
tok,
A11y::new("pin", Role::Table),
);
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(200.0, 80.0));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let mut boxes = Vec::new();
walk_bounds(layout, &mut boxes);
let xs: Vec<f32> = boxes
.iter()
.filter(|b| (b.width - 80.0).abs() < 1.0)
.map(|b| b.x)
.collect();
xs
};
let x0 = leading(&cols);
must(
x0.iter().any(|x| x.abs() < 1.0),
format!("frozen column missing at x=0: {x0:?}"),
);
cols.set_h_scroll(90.0);
let x1 = leading(&cols);
must(
x1.iter().any(|x| x.abs() < 1.0),
format!("frozen column left the leading edge: {x1:?}"),
);
must(
x1.iter().any(|x| *x < -1.0),
format!("unfrozen columns did not scroll: {x1:?}"),
);
let mut el: Element<'_, f32> = data_table(
&table,
&Sel::None,
None,
&cols,
false,
window,
24.0,
0,
|_, _| 0.0,
|_| 0.0,
|_| 0.0,
|x| x,
|_| 0.0,
tok,
A11y::new("pin-h", Role::Table),
);
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(200.0, 80.0));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let vp = Rectangle::new(Point::ORIGIN, Size::new(200.0, 80.0));
let mut clipboard = clipboard::Null;
let mut messages = Vec::new();
{
let mut shell = iced::advanced::Shell::new(&mut messages);
el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::WheelScrolled {
delta: mouse::ScrollDelta::Pixels { x: 20.0, y: 0.0 },
}),
layout,
mouse::Cursor::Available(Point::new(120.0, 12.0)),
&renderer,
&mut clipboard,
&mut shell,
&vp,
);
el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::WheelScrolled {
delta: mouse::ScrollDelta::Lines { x: 1.0, y: 0.0 },
}),
layout,
mouse::Cursor::Available(Point::new(120.0, 12.0)),
&renderer,
&mut clipboard,
&mut shell,
&vp,
);
}
assert!(!messages.is_empty());
}
#[test]
fn data_table_disabled_zebra_and_cursor_still_layout() {
let tok = named("dark").tokens;
let table = TableModel {
headers: vec!["A".into(), "B".into()],
rows: vec![vec!["1".into(), "2".into()], vec!["3".into(), "4".into()]],
sort_col: None,
sort_asc: true,
checks: Vec::new(),
};
let cols = crate::collection::ColumnLayout::new(vec![80.0, 80.0]);
let window = VisibleWindow::new(80.0);
let mut el: Element<'_, ()> = data_table(
&table,
&Sel::Single(1),
Some((1, 1)),
&cols,
true,
window,
24.0,
0,
|_, _| (),
|_| (),
|_| (),
|_| (),
|_| (),
tok,
A11y::new("zebra", Role::Table).with_disabled(true),
);
draw_once(&mut el);
let empty = TableModel {
headers: vec!["A".into()],
rows: vec![],
sort_col: None,
sort_asc: true,
checks: Vec::new(),
};
let one = crate::collection::ColumnLayout::new(vec![80.0]);
let mut headers_only: Element<'_, ()> = data_table(
&empty,
&Sel::None,
None,
&one,
false,
window,
24.0,
0,
|_, _| (),
|_| (),
|_| (),
|_| (),
|_| (),
tok,
A11y::new("empty-table", Role::Table),
);
draw_once(&mut headers_only);
}
#[test]
fn log_view_wheel_forwards_scroll_offset() {
let tok = named("dark").tokens;
let lines: Vec<String> = (0..80).map(|i| format!("line {i}")).collect();
let mut el: Element<'_, f32> = log_view(
&lines,
VisibleWindow::new(80.0),
18.0,
2,
|w| w.scroll,
Some(Id::from("log-drive")),
tok,
A11y::new("log-drive", Role::List),
);
let _ = drive_scroll(&mut el);
}
#[test]
fn stick_scroll_snaps_to_row_boundary() {
let s = stick_scroll_snapped(200, 20.0, 250.0);
assert_eq!(s % 20.0, 0.0);
assert!(s <= crate::layout::end_offset(4000.0, 250.0));
assert_eq!(s, 3740.0);
assert_eq!(stick_scroll_snapped(5, 20.0, 200.0), 0.0);
assert_eq!(
stick_scroll_snapped(10, 0.0, 100.0),
crate::layout::end_offset(0.0, 100.0)
);
}
#[test]
fn stick_align_pad_makes_max_scroll_row_aligned() {
let n = 200usize;
let h = 20.0f32;
let viewport = 250.0f32;
let pad = stick_align_pad(n, h, viewport);
let content = n as f32 * h + pad;
let max_s = (content - viewport).max(0.0);
assert!((max_s % h).abs() < 1e-3);
assert_eq!(stick_align_pad(5, 20.0, 200.0), 0.0);
assert_eq!(stick_align_pad(10, 0.0, 100.0), 0.0);
assert_eq!(stick_align_pad(100, 20.0, 200.0), 0.0);
}
#[test]
fn ellipsize_line_keeps_short_and_trims_long() {
assert_eq!(ellipsize_line("short", 42), "short");
assert_eq!(ellipsize_line("any", 0), "any");
let long = "Quarterly notes for Lisbon and the Berlin office";
let e = ellipsize_line(long, 20);
assert!(e.ends_with('…'));
assert_eq!(e.chars().count(), 20);
assert!(!e.contains("office"));
}
fn drive_scroll(el: &mut Element<'_, f32>) -> Vec<f32> {
use iced::advanced::clipboard;
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::mouse;
use iced::{Event, Font, Pixels, Point, Rectangle, Size};
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(320.0, 120.0));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let viewport = Rectangle::new(Point::ORIGIN, Size::new(320.0, 120.0));
let mut clipboard = clipboard::Null;
let mut messages = Vec::new();
{
let mut shell = iced::advanced::Shell::new(&mut messages);
el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::WheelScrolled {
delta: mouse::ScrollDelta::Lines { x: 0.0, y: -4.0 },
}),
layout,
mouse::Cursor::Available(Point::new(16.0, 40.0)),
&renderer,
&mut clipboard,
&mut shell,
&viewport,
);
}
messages
}
#[test]
fn checkbox_indeterminate_and_range_slider_emit() {
use iced::advanced::clipboard;
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::mouse;
use iced::{Event, Font, Pixels, Point, Rectangle, Size};
let tok = named("dark").tokens;
let press = |el: &mut Element<'_, CheckState>, at: Point| {
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(240.0, 80.0));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let viewport = Rectangle::new(Point::ORIGIN, Size::new(240.0, 80.0));
let mut clipboard = clipboard::Null;
let mut messages = Vec::new();
{
let mut shell = iced::advanced::Shell::new(&mut messages);
el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
layout,
mouse::Cursor::Available(at),
&renderer,
&mut clipboard,
&mut shell,
&viewport,
);
}
messages
};
let mut box_el: Element<'_, CheckState> = checkbox_indeterminate(
"all",
CheckState::Unchecked,
|s| s,
tok,
A11y::new("all", Role::Checkbox),
);
assert!(!press(&mut box_el, Point::new(8.0, 8.0)).is_empty());
let mut range_el: Element<'_, (f32, f32)> = range_slider(
0.0..=100.0,
20.0,
80.0,
|pair| pair,
tok,
A11y::new("span", Role::Slider),
);
let mut tree = Tree::new(range_el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(240.0, 80.0));
let node = range_el
.as_widget_mut()
.layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let viewport = Rectangle::new(Point::ORIGIN, Size::new(240.0, 80.0));
let mut clipboard = clipboard::Null;
let mut messages = Vec::new();
{
let mut shell = iced::advanced::Shell::new(&mut messages);
range_el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
layout,
mouse::Cursor::Available(Point::new(40.0, 8.0)),
&renderer,
&mut clipboard,
&mut shell,
&viewport,
);
range_el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::CursorMoved {
position: Point::new(80.0, 8.0),
}),
layout,
mouse::Cursor::Available(Point::new(80.0, 8.0)),
&renderer,
&mut clipboard,
&mut shell,
&viewport,
);
range_el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
layout,
mouse::Cursor::Available(Point::new(200.0, 48.0)),
&renderer,
&mut clipboard,
&mut shell,
&viewport,
);
range_el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::CursorMoved {
position: Point::new(80.0, 48.0),
}),
layout,
mouse::Cursor::Available(Point::new(80.0, 48.0)),
&renderer,
&mut clipboard,
&mut shell,
&viewport,
);
}
let _ = messages;
}
#[test]
fn disabled_radio_does_not_emit_on_press() {
use iced::advanced::clipboard;
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::mouse;
use iced::{Event, Font, Pixels, Point, Rectangle, Size};
let tok = named("dark").tokens;
let mut el: Element<'_, u8> = themed_radio(
"Locked",
1u8,
Some(1u8),
|_| 9u8,
tok,
A11y::new("Locked", Role::Radio).with_disabled(true),
);
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(240.0, 40.0));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let viewport = Rectangle::new(Point::ORIGIN, Size::new(240.0, 40.0));
let mut clipboard = clipboard::Null;
let mut messages = Vec::new();
{
let mut shell = iced::advanced::Shell::new(&mut messages);
el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
layout,
mouse::Cursor::Available(Point::new(8.0, 8.0)),
&renderer,
&mut clipboard,
&mut shell,
&viewport,
);
}
assert!(messages.is_empty());
}
#[test]
fn disabled_slider_and_pick_do_not_emit() {
use iced::advanced::clipboard;
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::mouse;
use iced::{Event, Font, Pixels, Point, Rectangle, Size};
let tok = named("dark").tokens;
let mut slider_el: Element<'_, u8> = themed_slider(
0.0..=1.0,
0.5,
|_| 1u8,
SliderMarks {
ticks: 4,
min: "0",
max: "1",
..SliderMarks::NONE
},
tok,
A11y::new("vol", Role::Slider).with_disabled(true),
);
let opts = ["a".to_string(), "b".to_string()];
let mut pick_el: Element<'_, u8> = themed_pick_list(
opts,
Some("a".into()),
|_| 2u8,
tok,
A11y::new("pick", Role::ComboBox).with_disabled(true),
);
for el in [&mut slider_el, &mut pick_el] {
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(240.0, 40.0));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let viewport = Rectangle::new(Point::ORIGIN, Size::new(240.0, 40.0));
let mut clipboard = clipboard::Null;
let mut messages = Vec::new();
{
let mut shell = iced::advanced::Shell::new(&mut messages);
el.as_widget_mut().update(
&mut tree,
&Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)),
layout,
mouse::Cursor::Available(Point::new(12.0, 12.0)),
&renderer,
&mut clipboard,
&mut shell,
&viewport,
);
}
assert!(messages.is_empty());
}
}
#[test]
fn data_table_paints_each_row_at_index_times_height_minus_scroll() {
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::{Font, Pixels, Size};
let tok = named("dark").tokens;
let row_h = 20.0;
let viewport = 200.0;
let scroll = 200.0;
let overscan = 4;
let n = 80;
let table = TableModel {
headers: vec!["N".into()],
rows: (0..n).map(|i| vec![format!("r{i}")]).collect(),
sort_col: None,
sort_asc: true,
checks: Vec::new(),
};
let widths = crate::collection::ColumnLayout::new(vec![80.0]);
let window = crate::collection::visible_window(scroll, viewport, row_h, n, overscan, None);
let mut el: Element<'_, VisibleWindow> = data_table(
&table,
&Sel::None,
None,
&widths,
false,
window,
row_h,
overscan,
|_, _| window,
|_| window,
|w| w,
|_| window,
|_| window,
tok,
A11y::new("table", Role::Table),
);
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(320.0, viewport));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
let layout = Layout::new(&node);
let mut boxes = Vec::new();
walk_bounds(layout, &mut boxes);
let rail_w = crate::chrome::SCROLL_RAIL_WIDTH;
let clip_top = boxes
.iter()
.find(|b| (b.width - rail_w).abs() < 0.6 && b.height > 20.0)
.map(|b| b.y)
.expect("table rail");
let rows: Vec<f32> = boxes
.iter()
.filter(|b| (b.height - row_h).abs() < 0.6 && b.width > 40.0)
.map(|b| b.y - clip_top)
.collect();
must(
rows.iter().any(|y| y.abs() < 1.0),
format!("row 10 must sit at the clip top, got {rows:?}"),
);
for y in &rows {
let along = y + scroll;
let i = (along / row_h).round();
must(
(along - i * row_h).abs() < 1.0,
format!("painted y {y} is not k*{row_h} - {scroll}"),
);
}
}
#[test]
fn expander_closed_clips_to_collapsed_height() {
let tok = named("dark").tokens;
let a11y = |n: &str| A11y::new(n, Role::Status);
let tall = || {
Column::new()
.spacing(8)
.push(label("one", tok, a11y("one")))
.push(label("two", tok, a11y("two")))
.push(label("three", tok, a11y("three")))
.push(label("four", tok, a11y("four")))
.push(label("five", tok, a11y("five")))
.push(label("six", tok, a11y("six")))
.into()
};
let mut shut: Element<'_, bool> = expander(
"Notes",
tall(),
48.0,
false,
|open| open,
tok,
A11y::new("exp", Role::Group),
);
let mut open: Element<'_, bool> = expander(
"Notes",
tall(),
48.0,
true,
|open| open,
tok,
A11y::new("exp", Role::Group),
);
let max = iced::Size::new(320.0, 800.0);
let closed_h = layout_size(&mut shut, max).height;
let open_h = layout_size(&mut open, max).height;
assert!(open_h > closed_h + 8.0);
assert!(closed_h >= 48.0 + 12.0);
let src = include_str!("widget.rs");
let head = src
.split("fn disclosure_header")
.nth(1)
.unwrap()
.split("pub fn accordion_view")
.next()
.unwrap();
assert!(head.contains("Length::Fill"));
assert!(head.contains("▾"));
assert!(head.contains("▸"));
}
#[test]
fn expander_title_shares_the_card_inset() {
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::{Font, Pixels, Size};
let tok = named("dark").tokens;
let mut el: Element<'_, bool> = expander(
"Notes",
label("body-line", tok, A11y::new("body-line", Role::Status)),
Peek::Lines(2),
true,
|open| open,
tok,
A11y::new("exp", Role::Group),
);
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let node = el.as_widget_mut().layout(
&mut tree,
&renderer,
&Limits::new(Size::ZERO, Size::new(320.0, 400.0)),
);
let layout = Layout::new(&node);
let mut boxes = Vec::new();
walk_bounds(layout, &mut boxes);
let lefts: Vec<f32> = boxes
.iter()
.filter(|b| b.x > 1.0 && b.x < 40.0 && b.width > 24.0 && b.width < 280.0)
.map(|b| b.x)
.collect();
assert!(lefts.iter().any(|x| (*x - 12.0).abs() < 2.0));
assert!(!lefts.iter().any(|x| (*x - 24.0).abs() < 2.0));
}
#[test]
fn peek_lines_keeps_the_last_line_inside_the_clip() {
let one = Peek::Lines(1).height();
let two = Peek::Lines(2).height();
assert!(two > one);
let raw = 2.0 * Peek::body_line() + Peek::DESCENT;
assert!(two >= raw);
assert_eq!(Peek::Pixels(48.0).height(), 48.0);
assert_eq!(Peek::Pixels(45.0).height(), 48.0);
let mut lined: Element<'_, bool> = expander(
"Notes",
label(
"more",
named("dark").tokens,
A11y::new("more", Role::Status),
),
Peek::Lines(2),
false,
|open| open,
named("dark").tokens,
A11y::new("exp", Role::Group),
);
let h = layout_size(&mut lined, iced::Size::new(320.0, 800.0)).height;
assert!(h >= Peek::Lines(2).height());
}
fn layout_size<M: Clone>(el: &mut Element<'_, M>, max: iced::Size) -> iced::Size {
use iced::advanced::layout::Limits;
use iced::advanced::widget::Tree;
use iced::{Font, Pixels};
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(iced::Size::ZERO, max);
el.as_widget_mut()
.layout(&mut tree, &renderer, &limits)
.size()
}
#[test]
fn image_slot_ready_keeps_the_requested_box() {
let tok = named("dark").tokens;
let px = vec![
255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 255, 0, 255,
];
let handle = iced::widget::image::Handle::from_rgba(2, 2, px);
for fit in [iced::ContentFit::Contain, iced::ContentFit::Cover] {
let mut el: Element<'_, ()> = image_slot(
ImageSlot::Ready {
handle: handle.clone(),
fit,
},
120.0,
80.0,
tok,
A11y::new("img", Role::Image),
);
let size = layout_size(&mut el, iced::Size::new(400.0, 400.0));
assert_eq!(size, iced::Size::new(120.0, 80.0));
}
}
#[test]
fn item_grid_cells_share_the_row_width() {
use iced::advanced::layout::{Layout, Limits};
use iced::advanced::widget::Tree;
use iced::{Font, Pixels, Size};
let tok = named("dark").tokens;
let labels = vec!["Inbox".into(), "Calendar".into(), "Mail".into()];
let mut el: Element<'_, ()> =
item_grid(&labels, |_| (), Some(1), tok, A11y::new("grid", Role::List));
let mut tree = Tree::new(el.as_widget());
let renderer = iced::Renderer::Secondary(iced_tiny_skia::Renderer::new(
Font::DEFAULT,
Pixels::from(16u32),
));
let limits = Limits::new(Size::ZERO, Size::new(300.0, 240.0));
let node = el.as_widget_mut().layout(&mut tree, &renderer, &limits);
assert!((node.size().width - 300.0).abs() < 1.0);
let layout = Layout::new(&node);
let mut widths = Vec::new();
fn walk_row(layout: Layout<'_>, widths: &mut Vec<f32>) {
let kids: Vec<_> = layout.children().collect();
if kids.len() == 3 && kids.iter().all(|c| c.bounds().width > 40.0) {
*widths = kids.iter().map(|c| c.bounds().width).collect();
return;
}
for k in kids {
walk_row(k, widths);
}
}
walk_row(layout, &mut widths);
assert_eq!(widths.len(), 3);
assert!((widths[0] - widths[1]).abs() < 1.0);
assert!((widths[1] - widths[2]).abs() < 1.0);
must(
(widths[0] * 3.0 + 16.0 - 300.0).abs() < 4.0,
format!("cells {widths:?} should share the 300px row"),
);
}
#[test]
fn open_accordion_shows_a_body_under_its_header() {
let tok = named("dark").tokens;
let titles = ["Files".into()];
let body = || label("New, Open, Save", tok, A11y::new("body", Role::Status));
let mut closed: Element<'_, ()> = accordion_view(
&titles,
vec![body()],
&Accordion { open: None },
|_| (),
tok,
A11y::new("acc", Role::Group),
);
let mut open: Element<'_, ()> = accordion_view(
&titles,
vec![body()],
&Accordion { open: Some(0) },
|_| (),
tok,
A11y::new("acc", Role::Group),
);
let max = iced::Size::new(300.0, 400.0);
let closed_h = layout_size(&mut closed, max).height;
let open_size = layout_size(&mut open, max);
must(
open_size.height > closed_h + 8.0,
format!(
"open {} must include the body above closed {closed_h}",
open_size.height
),
);
assert!((open_size.width - 300.0).abs() < 1.0);
}
}