use crate::accessibility::Role;
use crate::color::Color;
use crate::event::{Cursor, Key, Mods};
use crate::geom::{Insets, Rect};
use crate::id::{Id, hash};
use crate::layout::{Align, Flow, Length, NONE};
use crate::number::Number;
use crate::path::Path;
use crate::text::Font;
use crate::ui::{FRAME, Icon, Picture, Response, Rover, Stroke, Ui};
use std::ops::RangeInclusive;
use std::time::Duration;
pub(crate) const CHECK: Icon = Icon::line("M3 8.5 6.5 12 13 4.5");
pub(crate) const DOWN: Icon = Icon::line("M4 6.5 8 10.5 12 6.5");
pub(crate) const RIGHT: Icon = Icon::line("M6.5 4 10.5 8 6.5 12");
pub(crate) const CROSS: Icon = Icon::line("M4.5 4.5 11.5 11.5M11.5 4.5 4.5 11.5");
const SLIDE: Duration = Duration::from_millis(120);
const BRANCH: &str = "branch";
const LEAD: f32 = 16.0;
#[derive(Clone, Copy)]
pub(crate) enum Before {
None,
Icon(Icon),
Picture(Picture, fn(&mut Ui, Picture)),
}
impl Ui {
pub fn label(&mut self, text: &str) -> Response {
self.text(text, self.theme.font, self.theme.text)
}
pub fn note(&mut self, text: &str) -> Response {
self.text(text, self.theme.small_font, self.theme.text_dim)
}
pub fn text(&mut self, text: &str, font: Font, color: Color) -> Response {
self.el().text(text, font, color).show()
}
pub fn paragraph(&mut self, text: &str) -> Response {
let (font, color) = (self.theme.font, self.theme.text);
self.el().w(Length::Grow(1.0)).text(text, font, color).wrap().show()
}
pub fn link(&mut self, label: &str) -> Response {
let t = self.theme;
let state = self.press(self.next_id());
if state.hovered {
self.set_cursor(Cursor::Hand);
}
let font = if state.hovered || state.focused { t.font.underlined() } else { t.font };
self.el().text(label, font, t.accent).radius(2.0).sense().focusable().show();
self.tell(&state, Role::Link, None, "");
state
}
pub fn words(&mut self, text: &str, font: Font, color: Color) {
let before = self.notes.len();
for line in text.split_inclusive('\n') {
for word in line.trim_end_matches('\n').split_inclusive(' ') {
self.el().text(word, font, color).show();
self.tell_words(text, before);
}
if line.ends_with('\n') {
self.el().w(Length::Grow(1.0)).show();
}
}
}
pub fn icon(&mut self, icon: Icon) -> Response {
let color = self.theme.text_dim;
self.el().icon(icon, 16.0, color).show()
}
pub fn picture(&mut self, picture: Picture) -> Response {
self.el().picture(picture).show()
}
pub fn separator(&mut self) {
let line = self.theme.line;
self.el().w(Length::Grow(1.0)).h(Length::Px(1.0)).fill(line).show();
}
pub fn button(&mut self, label: &str) -> Response {
self.button_like(Before::None, label, None, false)
}
pub fn primary_button(&mut self, label: &str) -> Response {
self.button_like(Before::None, label, None, true)
}
pub fn button_with_icon(&mut self, icon: Icon, label: &str) -> Response {
self.button_like(Before::Icon(icon), label, None, false)
}
pub fn button_with_picture(&mut self, picture: Picture, label: &str) -> Response {
self.button_like(Before::Picture(picture, Ui::lead_picture), label, None, false)
}
pub fn icon_button(&mut self, icon: Icon, hint: &str) -> Response {
let state = self.button_like(Before::Icon(icon), "", None, false);
self.tell(&state, Role::Button, None, hint);
self.tooltip(&state, hint);
state
}
pub fn toggle(&mut self, on: &mut bool, label: &str) -> Response {
self.toggle_like(on, None, label)
}
pub fn icon_toggle(&mut self, on: &mut bool, icon: Icon, hint: &str) -> Response {
let state = self.toggle_like(on, Some(icon), hint);
self.tooltip(&state, hint);
state
}
fn toggle_like(&mut self, on: &mut bool, icon: Option<Icon>, name: &str) -> Response {
let t = self.theme;
let mut state = self.press(self.next_id());
(*on, state.changed) = (*on != state.clicked, state.clicked);
if state.hovered {
self.set_cursor(Cursor::Hand);
}
let (fill, ink, edge) = match (*on, state.hovered) {
(true, _) => (t.selected, t.text, t.accent),
(false, true) => (t.hover, t.text, t.line_strong),
(false, false) => (t.raised, t.text_dim, t.line),
};
let frame = self.el().h(Length::Px(t.control_height)).min(t.control_height, 0.0).fill(fill).border(edge, 1.0).radius(t.radius).sense().focusable();
let Some(icon) = icon else {
frame.text(name, t.font, ink).text_align(Align::Center).pad((11.0, 0.0)).show();
self.tell(&state, Role::Button, Some(*on), "");
return state;
};
frame.flow(Flow::Row).align(Align::Center, Align::Center).open();
self.el().icon(icon, 16.0, ink).show();
self.close();
self.tell(&state, Role::Button, Some(*on), name);
state
}
pub(crate) fn button_like(&mut self, lead: Before, label: &str, trail: Option<Icon>, primary: bool) -> Response {
let (t, icon_only) = (self.theme, label.is_empty());
let state = self.press(self.next_id());
if state.hovered {
self.set_cursor(Cursor::Hand);
}
let (fill, ink, edge) = match (primary, state.pressed, state.hovered) {
(true, true, _) => (t.accent.with_alpha(0.8), t.on_accent, Color::NONE),
(true, false, true) => (t.accent.with_alpha(0.9), t.on_accent, Color::NONE),
(true, false, false) => (t.accent, t.on_accent, Color::NONE),
(false, true, _) => (t.selected, t.text, t.line_strong),
(false, false, true) => (t.hover, t.text, t.line_strong),
(false, false, false) if icon_only => (Color::NONE, t.text_dim, Color::NONE),
(false, false, false) => (t.raised, t.text, t.line),
};
let pad = if icon_only { Insets::ZERO } else { Insets::xy(11.0, 0.0) };
if let (Before::None, None, false) = (lead, trail, icon_only) {
let font = if primary { t.font.bold() } else { t.font };
self.el()
.text(label, font, ink)
.text_align(Align::Center)
.h(Length::Px(t.control_height))
.min(t.control_height, 0.0)
.pad(pad)
.fill(fill)
.border(edge, 1.0)
.radius(t.radius)
.sense()
.focusable()
.show();
self.tell(&state, Role::Button, None, "");
return state;
}
self.el()
.flow(Flow::Row)
.align(Align::Center, Align::Center)
.h(Length::Px(t.control_height))
.min(t.control_height, 0.0)
.pad(pad)
.gap(6.0)
.fill(fill)
.border(edge, 1.0)
.radius(t.radius)
.sense()
.focusable()
.open();
match lead {
Before::Icon(icon) => _ = self.el().icon(icon, LEAD, ink).show(),
Before::Picture(picture, show) => show(self, picture),
Before::None => {}
}
if !icon_only {
self.text(label, if primary { t.font.bold() } else { t.font }, ink);
}
if let Some(icon) = trail {
self.el().icon(icon, 12.0, t.text_dim).show();
}
self.close();
self.tell(&state, Role::Button, None, "");
state
}
fn lead_picture(&mut self, picture: Picture) {
let wide = self.picture_size(picture).map_or(LEAD, |(wide, high)| (LEAD * wide as f32 / high.max(1) as f32).min(4.0 * LEAD));
self.el().picture(picture).size(wide.round(), LEAD).show();
}
fn choice(&mut self) -> Response {
let state = self.press(self.next_id());
if state.hovered {
self.set_cursor(Cursor::Hand);
}
self.row().gap(8.0).radius(4.0).align(Align::Start, Align::Start).shrink_width(1).sense().focusable().open();
state
}
fn choice_label(&mut self, label: &str) {
let (font, color) = (self.theme.font, self.theme.text);
self.el().text(label, font, color).wrap().shrink(1).show();
}
pub fn checkbox(&mut self, on: &mut bool, label: &str) -> Response {
let t = self.theme;
let mut state = self.choice();
(*on, state.changed) = (*on != state.clicked, state.clicked);
self.tell(&state, Role::CheckBox, Some(*on), "");
let (fill, edge) = if *on { (t.accent, t.accent) } else { (t.panel, if state.hovered { t.text_faint } else { t.line_strong }) };
self.row().size(16.0, 16.0).fill(fill).border(edge, 1.0).radius(4.0).align(Align::Center, Align::Center).open();
if *on {
self.el().icon(CHECK, 12.0, t.on_accent).show();
}
self.close();
self.choice_label(label);
self.close();
state
}
pub fn switch(&mut self, on: &mut bool, label: &str) -> Response {
let t = self.theme;
let mut state = self.choice();
(*on, state.changed) = (*on != state.clicked, state.clicked);
self.tell(&state, Role::Switch, Some(*on), "");
let over = self.animate(state.id, if *on { 1.0 } else { 0.0 }, SLIDE);
self.row().size(30.0, 16.0).pad(2.0).radius(8.0).fill(if *on { t.accent } else { t.line_strong }).open();
self.el().w(Length::Px(14.0 * over)).show();
self.el().size(12.0, 12.0).radius(6.0).fill(if *on { t.on_accent } else { t.text }).show();
self.close();
self.choice_label(label);
self.close();
state
}
pub fn radio<T: PartialEq>(&mut self, choice: &mut T, value: T, label: &str) -> Response {
let t = self.theme;
let mut state = self.choice();
let arrived = state.focused && state.id != 0 && self.arrowed == state.id;
state.changed = (state.clicked || arrived) && *choice != value;
self.again |= state.changed;
let selected = state.changed || *choice == value;
if self.enabled {
let group = hash(("radio", choice as *const T as usize));
self.rovers.push(Rover { group, id: state.id, stop: selected, selects: true });
}
if state.changed {
*choice = value;
}
self.tell(&state, Role::RadioButton, Some(selected), "");
let edge = if selected {
t.accent
} else if state.hovered {
t.text_faint
} else {
t.line_strong
};
self.row().size(16.0, 16.0).fill(t.panel).border(edge, 1.0).radius(8.0).align(Align::Center, Align::Center).open();
if selected {
self.el().size(8.0, 8.0).radius(4.0).fill(t.accent).show();
}
self.close();
self.choice_label(label);
self.close();
state
}
pub fn slider(&mut self, value: &mut f32, range: RangeInclusive<f32>) -> Response {
let (t, knob, was) = (self.theme, 14.0, *value);
let (low, span) = (*range.start(), range.end() - range.start());
let mut state = self.interact(self.next_id());
if let (true, Some(p)) = (state.pressed, self.pointer()) {
*value = low + span * ((p.x - state.rect.x - knob / 2.0) / (state.rect.w - knob).max(1.0)).clamp(0.0, 1.0);
}
if let Some((_, to)) = self.reader_sets.take_if(|(of, _)| *of == state.id) {
*value = to.clamp(low.min(low + span), low.max(low + span));
}
if state.focused {
let steps = self.key_steps(true);
*value = (*value + steps * span / 20.0).clamp(low.min(low + span), low.max(low + span));
}
state.changed = *value != was;
self.again |= state.changed;
let along = ((*value - low) / span).clamp(0.0, 1.0);
self.row().w(Length::Grow(1.0)).h(Length::Px(t.control_height)).radius(4.0).sense().focusable().open();
self.tell_value(&state, Role::Slider, [*value, low, low + span]);
self.tell_step(span / 20.0);
self.el().w(Length::Grow(along)).h(Length::Px(4.0)).radius(2.0).fill(t.accent).show();
self.el().size(knob, knob).radius(knob / 2.0).fill(if state.hovered || state.pressed { t.text } else { t.text_dim }).show();
self.el().w(Length::Grow(1.0 - along)).h(Length::Px(4.0)).radius(2.0).fill(t.line_strong).show();
self.close();
state
}
pub fn range(&mut self, low: &mut f32, high: &mut f32, range: RangeInclusive<f32>) -> Response {
let (t, knob, was) = (self.theme, 14.0, (*low, *high));
let (least, span) = (*range.start(), (range.end() - range.start()).max(f32::MIN_POSITIVE));
let mut state = self.interact(self.next_id());
let at = self.pointer().map(|p| least + span * ((p.x - state.rect.x - knob) / (state.rect.w - 2.0 * knob).max(1.0)).clamp(0.0, 1.0));
let nearer = at.is_some_and(|at| (at - *high).abs() < (at - *low).abs() || *low == *high && at > *high);
let taken = self.drag_delta(&state, [nearer as u8 as f32, 0.0, 0.0, 0.0]).map(|(_, caught)| caught[0] > 0.0);
match (taken, at) {
(Some(true), Some(at)) => *high = at.max(*low),
(Some(false), Some(at)) => *low = at.min(*high),
_ => {}
}
self.row().w(Length::Grow(1.0)).h(Length::Px(t.control_height)).radius(4.0).sense().open();
let line = self.open.last().map_or(0, |open| open.at);
let names: [String; 2] = match self.listening {
true => [self.widget_words.low, self.widget_words.high].map(|end| [crate::accessibility::label_before(&self.tree, &self.notes, line), end].join(", ").trim_start_matches(", ").to_string()),
false => Default::default(),
};
let [first, second] = ["low", "high"].map(|name| self.interact(self.id(name)));
let held = taken.map(|high| [first.id, second.id][high as usize]);
if let Some(knob) = held.filter(|knob| self.focus() != *knob) {
self.set_focus(knob);
}
let step = span / 20.0;
*low = (*low + self.arrows(&first) * step).clamp(least, *high);
*high = (*high + self.arrows(&second) * step).clamp(*low, least + span);
let along = |value: f32| ((value - least) / span).clamp(0.0, 1.0);
let lit = |of: &Response, held: bool| if of.focused || state.pressed && taken == Some(held) { t.text } else { t.text_dim };
self.el().w(Length::Grow(along(*low))).h(Length::Px(4.0)).radius(2.0).fill(t.line_strong).show();
self.el().key("low").size(knob, knob).radius(knob / 2.0).fill(lit(&first, false)).focusable().show();
self.tell_value_named(&first, Role::Slider, [*low, least, *high], &names[0]);
self.tell_step(step);
self.el().w(Length::Grow(along(*high) - along(*low))).h(Length::Px(4.0)).fill(t.accent).show();
self.el().key("high").size(knob, knob).radius(knob / 2.0).fill(lit(&second, true)).focusable().show();
self.tell_value_named(&second, Role::Slider, [*high, *low, least + span], &names[1]);
self.tell_step(step);
self.el().w(Length::Grow(1.0 - along(*high))).h(Length::Px(4.0)).radius(2.0).fill(t.line_strong).show();
self.close();
state.changed = (*low, *high) != was;
self.again |= state.changed;
state
}
fn arrows(&mut self, of: &Response) -> f32 {
if of.focused { self.key_steps(true) } else { 0.0 }
}
fn key_steps(&mut self, ends: bool) -> f32 {
let keys = [(Key::Right, 1.0), (Key::Up, 1.0), (Key::Left, -1.0), (Key::Down, -1.0), (Key::PageUp, 4.0), (Key::PageDown, -4.0), (Key::Home, -1e9), (Key::End, 1e9)];
keys.into_iter().take(if ends { 8 } else { 6 }).filter(|(key, _)| self.key(*key, Mods::NONE)).map(|(_, steps)| steps).sum()
}
pub fn progress(&mut self, part: f32) -> Response {
let (t, part) = (self.theme, part.clamp(0.0, 1.0));
let bar = self.row().w(Length::Grow(1.0)).h(Length::Px(6.0)).radius(3.0).fill(t.line_strong).clip().open();
self.tell_value(&bar, Role::Progress, [part, 0.0, 1.0]);
self.el().w(Length::Grow(part)).h(Length::Grow(1.0)).radius(3.0).fill(t.accent).show();
self.el().w(Length::Grow(1.0 - part)).show();
self.close();
bar
}
pub fn progress_with_percent(&mut self, part: f32) -> Response {
let part = if part.is_nan() { 0.0 } else { part.clamp(0.0, 1.0) };
self.row().w(Length::Grow(1.0)).gap(10.0).align(Align::Start, Align::Center).open();
let bar = self.progress(part);
self.slide_value((part * 100.0).round(), [0.0, 100.0], 1.0, "%");
self.close();
bar
}
#[doc(alias("loading"))]
pub fn spinner(&mut self) -> Response {
let (t, size, millis) = (self.theme, 16.0f32, self.now.as_millis());
let still = self.reduced_motion();
let turned = if still { 0.0 } else { (millis % 1000) as f32 / 1000.0 * std::f32::consts::TAU };
let solid = if still { 0.55 + 0.45 * ((millis % 2000) as f32 / 2000.0 * std::f32::consts::TAU).cos() } else { 1.0 };
let mut ring = Path::new();
ring.arc(size / 2.0, size / 2.0, size / 2.0 - 1.5, turned.to_degrees(), 270.0);
self.wake_after(if still { FRAME * 3 } else { FRAME });
let shown = self.el().size(size, size).outline(&ring, 2.0, t.accent.with_alpha(solid)).open();
self.close();
self.tell(&shown, Role::Progress, None, "");
shown
}
pub fn segmented(&mut self, choice: &mut usize, names: &[&str]) -> Response {
let (t, was) = (self.theme, *choice);
let mut all = self.row().place((Align::Start, Align::Center)).pad(2.0).gap(2.0).fill(t.panel).border(t.line, 1.0).radius(t.radius).open();
self.tell(&all, Role::Tabs, None, "");
for (number, name) in names.iter().enumerate() {
let state = self.press(self.next_id());
*choice = if state.clicked { number } else { *choice };
let (fill, ink) = match (*choice == number, state.hovered) {
(true, _) => (t.selected, t.text),
(false, true) => (t.hover, t.text),
(false, false) => (Color::NONE, t.text_dim),
};
self.el().text(name, t.font, ink).text_align(Align::Center).h(Length::Px(t.control_height - 6.0)).pad((10.0, 0.0)).fill(fill).radius((t.radius - 2.0).max(0.0)).sense().focusable().show();
self.tell(&state, Role::Tab, Some(*choice == number), "");
self.tell_position(number + 1, names.len());
self.tab_stop(all.id, state.id, *choice == number);
}
self.close();
all.changed = *choice != was;
all
}
pub fn tabs(&mut self, choice: &mut usize, names: &[&str]) -> Response {
let (t, was) = (self.theme, *choice);
let mut all = self.wrapping_row().gap(2.0).open();
self.tell(&all, Role::Tabs, None, "");
for (number, name) in names.iter().enumerate() {
let state = self.press(self.next_id());
if state.clicked {
*choice = number;
}
let selected = *choice == number;
self.column().pad((10.0, 6.0, 10.0, 0.0)).gap(5.0).sense().focusable().open();
self.tell(&state, Role::Tab, Some(selected), "");
self.tell_position(number + 1, names.len());
self.tab_stop(all.id, state.id, selected);
self.text(name, t.font, if selected || state.hovered { t.text } else { t.text_dim });
self.el().h(Length::Px(2.0)).radius(1.0).fill(if selected { t.accent } else { Color::NONE }).show();
self.close();
}
self.close();
all.changed = *choice != was;
all
}
pub fn tabs_of<T>(&mut self, choice: &mut usize, items: &mut Vec<T>, name: impl Fn(&T) -> &str) -> Response {
let was = (*choice, items.len());
let mut all = self.wrapping_row().gap(2.0).open();
self.tell(&all, Role::Tabs, None, "");
let (mut closed, mut moved, mut after) = (None, None, 0);
for number in 0..items.len() {
let beside = [number.checked_sub(1), Some(number + 1).filter(|after| *after < items.len())].map(|at| at.map(|at| name(&items[at])));
let [selected, shut, back, on, keyed] = self.closing_tab(name(&items[number]), *choice == number, beside, all.id);
self.tell_position(number + 1, items.len());
*choice = if selected { number } else { *choice };
closed = closed.or(shut.then_some(number));
moved = moved.or(back.then(|| (number, number - 1))).or(on.then_some((number, number + 1)));
if keyed && shut {
after = beside[1].or(beside[0]).map_or(0, |name| self.id(name));
}
}
self.close();
if after != 0 {
self.set_focus(after);
self.focus_shown = true;
}
if let Some((from, to)) = moved {
items.swap(from, to);
*choice = [to, from, *choice][if *choice == from { 0 } else { 1 + (*choice != to) as usize }];
}
if let Some(at) = closed {
items.remove(at);
*choice = (*choice - (*choice > at) as usize).min(items.len().saturating_sub(1));
}
all.changed = was != (*choice, items.len()) || moved.is_some();
self.again |= all.changed;
all
}
fn closing_tab(&mut self, name: &str, selected: bool, beside: [Option<&str>; 2], row: Id) -> [bool; 5] {
let t = self.theme;
let tab = self.press(self.id(name));
let dragged = self.drag_delta(&tab, [0.0; 4]).is_some_and(|(gone, _)| gone.x.abs() > 4.0);
let at = self.pointer().filter(|_| dragged).map_or(f32::NAN, |at| at.x);
let middle = |of: Rect| if of.is_empty() { f32::NAN } else { of.x + of.w / 2.0 };
let [before, after] = beside.map(|name| name.map_or(f32::NAN, |name| middle(self.seen(self.id(name)))));
let moves = |ui: &mut Ui, key: Key, to: Option<&str>| tab.focused && to.is_some() && ui.key(key, Mods { ctrl: true, shift: true, alt: false });
let (back, on) = (moves(self, Key::Left, beside[0]), moves(self, Key::Right, beside[1]));
let deleted = tab.focused && self.key(Key::Delete, Mods::NONE);
self.column().key(name).pad((10.0, 4.0, 6.0, 0.0)).gap(3.0).sense().focusable().open();
self.row().gap(6.0).open();
self.text(name, t.font, if selected || tab.hovered { t.text } else { t.text_dim });
let cross = self.press(self.next_id());
self.el().icon(CROSS, 12.0, if cross.hovered { t.text } else { t.text_faint }).sense().show();
if self.listening {
self.tell(&cross, Role::Button, None, &format!("{} {name}", self.frame_words.close));
self.tell_separately(&cross);
}
self.close();
self.el().h(Length::Px(2.0)).radius(1.0).fill(if selected { t.accent } else { Color::NONE }).show();
self.close();
self.tell(&tab, Role::Tab, Some(selected), "");
self.tab_stop(row, tab.id, selected);
let keyed = deleted || back || on;
[tab.clicked && self.gone().x.abs() <= 4.0, cross.clicked || deleted, at < before || back, at >= after || on, keyed]
}
fn tab_stop(&mut self, row: Id, tab: Id, selected: bool) {
if self.enabled {
self.rovers.push(Rover { group: row, id: tab, stop: selected, selects: false });
}
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn fold(&mut self, open: &mut bool, title: &str) -> bool {
let state = self.press(self.next_id());
*open ^= state.clicked;
self.folding(&state, *open, title, Color::NONE);
self.tell(&state, Role::Fold, Some(*open), "");
*open
}
#[cfg_attr(debug_assertions, track_caller)]
fn folding(&mut self, state: &Response, open: bool, title: &str, fill: Color) {
let t = self.theme;
if state.hovered {
self.set_cursor(Cursor::Hand);
}
self.row().gap(4.0).h(Length::Px(t.control_height)).radius(4.0).fill(fill).sense().focusable().open();
self.el().icon(if open { DOWN } else { RIGHT }, 12.0, t.text_dim).show();
self.text(title, t.font.bold(), t.text);
self.close();
}
pub fn selectable(&mut self, picked: bool, label: &str) -> Response {
let t = self.theme;
let state = self.press_to_select(self.next_id());
let fill = if picked {
t.selected
} else if state.hovered {
t.hover
} else {
Color::NONE
};
self.el().text(label, t.font, t.text).h(Length::Px(t.control_height)).pad((10.0, 0.0)).fill(fill).radius(4.0).sense().focusable().show();
self.tell(&state, Role::Row, Some(picked), "");
let place = self.open.last().filter(|around| around.row > 0.0).map(|around| (around.children as usize - 1, around.count as usize));
place.into_iter().for_each(|(nth, count)| self.tell_position(nth, count));
state
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn tree(&mut self, open: &mut bool, title: &str) -> Response {
self.tree_heading(open, title, None)
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn selectable_tree(&mut self, open: &mut bool, title: &str, picked: bool) -> Response {
self.tree_heading(open, title, Some(picked))
}
#[cfg_attr(debug_assertions, track_caller)]
fn tree_heading(&mut self, open: &mut bool, title: &str, selected: Option<bool>) -> Response {
let (mut state, was) = (self.press(self.next_id()), *open);
let (level, above) = if state.focused || self.listening { self.tree_around() } else { (1, 0) };
if state.focused {
self.walk_tree(open, above);
}
*open ^= state.clicked;
state.changed = *open != was;
let fill = if selected == Some(true) { self.theme.selected } else { Color::NONE };
self.folding(&state, *open, title, fill);
self.tell(&state, Role::TreeItem, selected, "");
self.tell_tree_item(level, Some(*open));
if *open {
self.column().key(BRANCH).pad((16.0, 0.0, 0.0, 0.0)).open();
}
state
}
fn tree_around(&self) -> (usize, Id) {
let specs = &self.tree.specs;
let under = |at: u32| specs[at as usize].frame.parent != NONE && specs[at as usize].id == hash((specs[specs[at as usize].frame.parent as usize].id, hash(BRANCH)));
let (count, last) = self.open.iter().map(|open| open.at).filter(|at| under(*at)).fold((0, None), |(count, _), at| (count + 1, Some(at)));
(count + 1, last.map_or(0, |at| specs[specs[at as usize - 1].frame.parent as usize].id))
}
fn walk_tree(&mut self, open: &mut bool, above: Id) {
match (self.key(Key::Right, Mods::NONE), *open) {
(true, false) => (*open, self.again) = (true, true),
(true, true) => self.input.strokes.push(Stroke::Key(Key::Down, Mods::NONE)),
_ => {}
}
match (self.key(Key::Left, Mods::NONE), *open, above) {
(true, true, _) => (*open, self.again) = (false, true),
(true, false, above) if above != 0 => self.set_focus(above),
_ => {}
}
}
pub fn drag_value<T: Number>(&mut self, value: &mut T, per_unit: f64) -> Response {
self.dragged(value, None, per_unit)
}
pub fn drag_value_in<T: Number>(&mut self, value: &mut T, range: RangeInclusive<T>, per_unit: f64) -> Response {
let (low, high) = (range.start().to_f64(), range.end().to_f64());
self.dragged(value, Some([low.min(high), high.max(low)]), per_unit)
}
fn dragged<T: Number>(&mut self, value: &mut T, range: Option<[f64; 2]>, per_unit: f64) -> Response {
let (t, was) = (self.theme, *value);
let [low, high] = range.unwrap_or([f64::MIN, f64::MAX]);
let within = |value: f64| T::from_f64(value.clamp(low, high));
let mut state = self.interact(self.next_id());
if state.hovered || state.pressed {
self.set_cursor(Cursor::ResizeX);
}
let whole = was.to_f64() as f32;
if let Some((gone, caught)) = self.drag_delta(&state, [whole, (was.to_f64() - whole as f64) as f32, 0.0, 0.0]) {
*value = within(caught[0] as f64 + caught[1] as f64 + gone.x as f64 * per_unit);
}
if state.focused {
let steps = self.key_steps(false) as f64;
*value = if steps == 0.0 { *value } else { within(value.to_f64() + steps * per_unit * 10.0) };
}
if let Some((_, to)) = self.reader_sets.take_if(|(of, _)| *of == state.id) {
*value = within(to as f64);
}
state.changed = *value != was;
self.again |= state.changed;
let decimals = (-per_unit.abs().max(1e-6).log10()).ceil().clamp(0.0, 6.0) as usize;
let edge = if state.hovered || state.pressed { t.text_faint } else { t.line_strong };
self.row().h(Length::Px(t.control_height)).min(64.0, 0.0).pad((8.0, 0.0)).align(Align::Center, Align::Center).fill(t.panel).border(edge, 1.0).radius(t.radius).sense().focusable().open();
self.label(&format!("{:.*}", decimals, value.to_f64()));
self.close();
self.tell_value(&state, Role::Slider, [value.to_f64() as f32, low.max(f32::MIN as f64) as f32, high.min(f32::MAX as f64) as f32]);
self.tell_step((per_unit * 10.0) as f32);
state
}
pub fn plot_lines(&mut self, values: &[f32], width: f32, height: f32) -> Response {
self.plot(values, width, height, false)
}
pub fn plot_bars(&mut self, values: &[f32], width: f32, height: f32) -> Response {
self.plot(values, width, height, true)
}
fn plot(&mut self, values: &[f32], width: f32, height: f32, bars: bool) -> Response {
let t = self.theme;
let (low, high) = values.iter().fold((if bars { 0.0 } else { f32::MAX }, f32::MIN), |(low, high), v| (low.min(*v), high.max(*v)));
let (span, last) = ((high - low).max(1e-6), values.len().saturating_sub(1).max(1) as f32);
let step = width / values.len().max(1) as f32;
let y = |v: f32| 3.0 + (height - 6.0) * (1.0 - (v - low) / span);
let mut shape = Path::new();
for (i, v) in values.iter().enumerate() {
let (left, right) = (i as f32 * step + 1.0, (i + 1) as f32 * step - 1.0);
match (bars, i) {
(true, _) => shape.move_to(left, height).line_to(left, y(*v)).line_to(right, y(*v)).line_to(right, height).close(),
(false, 0) => shape.move_to(0.0, y(*v)),
(false, _) => shape.line_to(i as f32 * width / last, y(*v)),
};
}
let seat = self.next_id();
let frame = self.el().key(("plot", seat)).size(width, height).fill(t.panel).radius(4.0);
let plot = if bars { frame.shape(&shape, t.accent).show() } else { frame.outline(&shape, 1.5, t.accent).show() };
if self.listening {
self.tell(&plot, Role::Text, None, &crate::chart::values_written(values));
}
plot
}
pub fn splitter(&mut self, width: &mut f32, least: f32, most: f32) -> Response {
let (t, was, step) = (self.theme, *width, 10.0);
let mut state = self.interact(self.next_id());
if state.hovered || state.pressed {
self.set_cursor(Cursor::ResizeX);
}
if let Some((gone, caught)) = self.drag_delta(&state, [was, 0.0, 0.0, 0.0]) {
*width = (caught[0] + gone.x).clamp(least, most);
}
if state.focused {
let keys = [(Key::Right, step), (Key::Left, -step), (Key::Home, f32::MIN), (Key::End, f32::MAX)];
let by: f32 = keys.into_iter().filter(|(key, _)| self.key(*key, Mods::NONE)).map(|(_, by)| by).sum();
*width = (*width + by).clamp(least, most.max(least));
}
if let Some((_, to)) = self.reader_sets.take_if(|(of, _)| *of == state.id) {
*width = to.clamp(least, most.max(least));
}
state.changed = *width != was;
self.again |= state.changed;
let line = if state.hovered || state.pressed || state.focused { t.accent } else { t.line };
self.row().w(Length::Px(5.0)).h(Length::Grow(1.0)).align(Align::Center, Align::Spread).sense().focusable().open();
if self.listening {
let at = self.open.last().map_or(0, |open| open.at);
let name = crate::accessibility::words_before(&self.tree, at).unwrap_or(self.frame_words.splitter).to_string();
self.tell_value_named(&state, Role::Slider, [*width, least, most], &name);
self.tell_step(step);
}
self.el().w(Length::Px(1.0)).fill(line).show();
self.close();
state
}
}
#[cfg(test)]
mod tests {
use crate::accessibility::{AccessNode, Role};
use crate::layout::Length;
use crate::testing::Trial;
use crate::ui::Ui;
fn on<A>(trial: &Trial<A>) -> Option<(Role, String)> {
trial.visible_access_nodes().iter().find(|said| said.focused).map(|said| (said.role, said.name.clone()))
}
fn tab_stops<A>(trial: &mut Trial<A>, times: usize) -> Vec<String> {
trial.ui.set_focus(0);
trial.run();
(0..times).map(|_| (trial.key("Tab"), on(trial).map_or(String::new(), |(_, name)| name)).1).collect()
}
#[derive(Default)]
struct Choosing {
size: u8,
tone: u8,
}
fn choosing(app: &mut Choosing, ui: &mut Ui) {
ui.column().pad(8.0).gap(4.0).open();
ui.button("Before");
ui.label("Size");
for (value, name) in ["Small", "Medium", "Large"].into_iter().enumerate() {
ui.radio(&mut app.size, value as u8, name);
}
ui.label("Tone");
for (value, name) in ["Warm", "Cold"].into_iter().enumerate() {
ui.radio(&mut app.tone, value as u8, name);
}
ui.button("After");
ui.close();
}
#[test]
fn the_radio_buttons_of_a_choice_are_one_stop_and_the_arrows_go_round_them_picking() {
let mut trial = Trial::new(Choosing { size: 1, tone: 0 }, choosing);
assert_eq!(tab_stops(&mut trial, 5), ["Before", "Medium", "Warm", "After", "Before"]);
trial.key("Shift+Tab");
trial.key("Shift+Tab");
assert_eq!(on(&trial), Some((Role::RadioButton, "Warm".into())), "Shift and Tab come back to the choice before");
trial.key("Shift+Tab");
assert_eq!(on(&trial), Some((Role::RadioButton, "Medium".into())));
let places: Vec<Option<[u32; 2]>> = trial.visible_access_nodes().iter().filter(|said| said.role == Role::RadioButton).map(|said| said.position).collect();
assert_eq!(places, [Some([1, 3]), Some([2, 3]), Some([3, 3]), Some([1, 2]), Some([2, 2])]);
trial.key("Down");
assert_eq!((trial.app.size, on(&trial)), (2, Some((Role::RadioButton, "Large".into()))));
trial.key("Right");
assert_eq!((trial.app.size, on(&trial)), (0, Some((Role::RadioButton, "Small".into()))));
trial.key("Up");
assert_eq!((trial.app.size, trial.app.tone), (2, 0), "Up from the first goes round to the last, and the other choice is left as it was");
assert!(trial.find(Role::RadioButton, "Large").on == Some(true));
}
struct Paging {
page: usize,
span: usize,
paper: usize,
papers: Vec<&'static str>,
}
fn paging(app: &mut Paging, ui: &mut Ui) {
ui.column().pad(8.0).gap(6.0).open();
ui.tabs(&mut app.page, &["One", "Two", "Three"]);
ui.segmented(&mut app.span, &["Day", "Week"]);
ui.tabs_of(&mut app.paper, &mut app.papers, |paper| paper);
ui.button("After");
ui.close();
}
#[test]
fn a_row_of_tabs_is_one_stop_gone_along_with_the_arrows_home_and_end() {
let mut trial = Trial::new(Paging { page: 1, span: 0, paper: 0, papers: vec!["a.txt", "b.txt", "c.txt"] }, paging);
assert_eq!(tab_stops(&mut trial, 4), ["Two", "Day", "a.txt", "After"]);
trial.ui.set_focus(0);
trial.run();
trial.key("Tab");
for (key, to) in [("Right", "Three"), ("Right", "One"), ("Left", "Three"), ("Home", "One"), ("End", "Three")] {
trial.key(key);
assert_eq!(on(&trial), Some((Role::Tab, to.into())), "{key}");
}
assert_eq!(trial.app.page, 1);
trial.key("Enter");
assert_eq!(trial.app.page, 2);
trial.key("Down");
assert!(on(&trial).is_some_and(|(_, name)| name == "Day" || name == "Week"), "{:?}", on(&trial));
}
#[test]
fn tabs_that_close_are_closed_and_moved_with_keys_and_their_cross_is_told() {
let mut trial = Trial::new(Paging { page: 0, span: 0, paper: 1, papers: vec!["a.txt", "b.txt", "c.txt"] }, paging);
let cross = trial.find(Role::Button, "Close b.txt").clone();
assert_eq!(trial.visible_access_nodes()[cross.parent.unwrap()].role, Role::Tabs, "the cross is told in the row of tabs");
let stops = tab_stops(&mut trial, 3);
assert_eq!(stops[2], "b.txt", "the row stops at the tab in front");
trial.key("Ctrl+Shift+Right");
assert_eq!((trial.app.papers.as_slice(), trial.app.paper), (["a.txt", "c.txt", "b.txt"].as_slice(), 2));
assert_eq!(on(&trial), Some((Role::Tab, "b.txt".into())));
trial.key("Ctrl+Shift+Right");
assert_eq!(trial.app.papers, ["a.txt", "c.txt", "b.txt"], "the last goes no further");
trial.key("Ctrl+Shift+Left");
trial.key("Ctrl+Shift+Left");
assert_eq!((trial.app.papers.as_slice(), trial.app.paper), (["b.txt", "a.txt", "c.txt"].as_slice(), 0));
trial.key("Delete");
assert_eq!(trial.app.papers, ["a.txt", "c.txt"]);
assert_eq!(on(&trial), Some((Role::Tab, "a.txt".into())));
trial.key("End");
trial.key("Delete");
assert_eq!((trial.app.papers.as_slice(), on(&trial)), (["a.txt"].as_slice(), Some((Role::Tab, "a.txt".into()))), "the last tab hands the keyboard back to the one before");
}
#[test]
fn with_larger_text_tabs_that_do_not_fit_go_onto_another_line() {
let mut trial = Trial::new(Paging { page: 0, span: 0, paper: 0, papers: vec!["a.txt", "b.txt", "c.txt"] }, paging);
trial.ui.theme = trial.ui.theme.text_scaled(2.5);
trial.resize(300.0, 400.0, 1.0);
let tabs: Vec<&AccessNode> = trial.visible_access_nodes().iter().filter(|said| said.role == Role::Tab).collect();
assert!(tabs.iter().all(|said| said.rect.right() <= 300.0), "{:?}", tabs.iter().map(|said| said.rect).collect::<Vec<_>>());
assert!(tabs.windows(2).any(|pair| pair[1].rect.y > pair[0].rect.y), "on more than one line");
}
struct Splitting {
side: f32,
named: bool,
}
fn splitting(app: &mut Splitting, ui: &mut Ui) {
ui.row().grow().open();
ui.column().w(Length::Px(app.side)).open();
if app.named {
ui.label("Folders");
}
ui.close();
ui.splitter(&mut app.side, 60.0, 200.0);
ui.button("Beside");
ui.close();
}
#[test]
fn a_splitter_is_reached_and_moved_with_keys_and_told_as_a_slider() {
let mut trial = Trial::new(Splitting { side: 100.0, named: true }, splitting);
trial.key("Tab");
let told = trial.find(Role::Slider, "Folders").clone();
assert!(told.focused && told.value_range == Some([100.0, 60.0, 200.0]) && told.step == 10.0, "{told:?}");
for (key, to) in [("Right", 110.0), ("Left", 100.0), ("End", 200.0), ("Right", 200.0), ("Home", 60.0)] {
trial.key(key);
assert_eq!(trial.app.side, to, "{key}");
}
trial.app.named = false;
trial.run();
assert!(trial.has(Role::Slider, trial.ui.frame_words.splitter));
}
#[derive(Default)]
struct Form {
volume: f32,
size: usize,
copies: i32,
town: String,
low: f32,
high: f32,
loose: f32,
}
fn form(app: &mut Form, ui: &mut Ui) {
ui.column().pad(8.0).gap(4.0).open();
ui.label("Volume");
ui.slider(&mut app.volume, 0.0..=10.0);
ui.label("Size");
ui.select(&mut app.size, &["Small", "Large"]);
ui.label("Copies");
ui.number(&mut app.copies, 1);
ui.label("Town");
ui.text_box(&mut app.town, "");
ui.label("Price");
ui.row().open();
ui.range(&mut app.low, &mut app.high, 0.0..=10.0);
ui.close();
ui.button("Words, not a label");
ui.slider(&mut app.loose, 0.0..=1.0);
ui.close();
}
#[test]
fn a_part_with_no_words_of_its_own_is_called_by_the_label_in_front_of_it() {
let trial = Trial::new(Form { copies: 3, ..Form::default() }, form);
let names: Vec<(Role, &str)> = trial.visible_access_nodes().iter().filter(|said| said.role != Role::Text).map(|said| (said.role, said.name.as_str())).collect();
let wanted = [
(Role::Slider, "Volume"),
(Role::ComboBox, "Size"),
(Role::SpinButton, "Copies"),
(Role::TextBox, "Town"),
(Role::Slider, "Price, low end"),
(Role::Slider, "Price, high end"),
(Role::Button, "Words, not a label"),
(Role::Slider, ""),
];
assert_eq!(names, wanted, "(a button's words are no label: the slider after it has no name)");
assert_eq!(trial.find(Role::ComboBox, "Size").value, "Small");
assert_eq!(trial.find(Role::SpinButton, "Copies").value, "3");
}
struct Long {
on: bool,
}
fn long(app: &mut Long, ui: &mut Ui) {
ui.row().gap(4.0).open();
ui.column().w(Length::Px(40.0)).open();
ui.label("Words much wider than their column");
ui.close();
ui.column().open();
ui.toggle(&mut app.on, "On");
ui.close();
ui.close();
}
#[test]
fn words_wider_than_their_box_are_drawn_again_with_what_they_spill_over() {
let mut trial = Trial::new(Long { on: false }, long);
trial.press(Role::Button, "On");
trial.press(Role::Button, "On");
assert!(!trial.app.on);
}
struct Rows;
fn rows(_: &mut Rows, ui: &mut Ui) {
ui.column().grow().open();
for row in ui.list(100, 20.0) {
ui.selectable(false, &format!("Row {row}"));
}
ui.close();
ui.close();
}
#[test]
fn rows_are_never_lower_than_a_line_of_the_themes_text() {
let mut trial = Trial::new(Rows, rows);
assert_eq!(trial.find(Role::Row, "Row 1").rect.h, 20.0);
trial.ui.theme = trial.ui.theme.text_scaled(2.5);
trial.run();
assert_eq!(trial.find(Role::Row, "Row 1").rect.h, 40.0);
}
}