use crate::accessibility::Role;
use crate::color::Color;
use crate::event::{Key, Mods};
use crate::geom::{Point, Rect, Size, pt};
use crate::id::{Id, hash};
use crate::layout::{Align, Flow, Length};
use crate::path::Path;
use crate::text::Font;
use crate::ui::{Anchor, Icon, Response, Ui};
use crate::widgets::{Before, CHECK, DOWN, RIGHT};
use std::time::Duration;
const TIP_AFTER: Duration = Duration::from_millis(500);
const MENU_AFTER: Duration = Duration::from_millis(150);
const TIP: Id = 0x7171_7171_7171_7171;
const DIALOG: Id = 0xd1a1_09d1_a109_d1a1;
const CONTEXT: Id = 0xc0c0_7e87_c0c0_7e87;
const INNER: Id = 0x5b5b_3e3e_5b5b_3e3e;
const PINNED: Id = 0x0710_71ce_0710_71ce;
const NOTIFIED: Id = 0x5a1d_0471_5a1d_0471;
const POPOVER: Id = 0x9090_0e7e_9090_0e7e;
const ARROW: Id = 0xa770_3a77_a770_3a77;
const POINT: [f32; 2] = [8.0, 16.0];
const EDGE: f32 = 4.0;
const ALT: Mods = Mods { alt: true, ..Mods::NONE };
const DOT: Icon = Icon::solid("M8 5a3 3 0 1 1 0 6 3 3 0 0 1 0-6z");
#[derive(Default)]
pub(crate) struct Framing {
pub held: Mods,
pub alone: bool,
tapped: bool,
back: Id,
on_bar: bool,
on_bar_now: bool,
keyed: bool,
go_row: Option<u32>,
rows: [Vec<(Id, u32)>; 2],
leads: Vec<Id>,
marked: Vec<Id>,
arrows: Vec<(Id, Point, bool)>,
pub bars: Vec<crate::toolbar::Bar>,
pub left: Vec<crate::toolbar::Left>,
pub left_count: usize,
pub picked: Id,
pub panels: [Vec<(Id, Rect)>; 2],
pub window: Size,
pub into: Option<String>,
pub into_tab: Id,
pub into_box: Option<(u32, Id)>,
pub docks: u32,
pub closable: [Vec<Id>; 2],
pub title_twice: bool,
pub later: [Option<fn(&mut Ui)>; 3],
pub notices: u64,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FrameWords {
pub more: &'static str,
pub close: &'static str,
pub fill: &'static str,
pub back: &'static str,
pub splitter: &'static str,
}
impl Default for FrameWords {
fn default() -> Self {
FrameWords { more: "More", close: "Close", fill: "Fill the dock", back: "Back to its place", splitter: "Width of the pane" }
}
}
#[derive(Clone, Copy, PartialEq)]
pub(crate) enum Lead {
None,
Tick(bool),
Dot(bool),
Icon(Icon, bool),
}
#[derive(Clone, Copy)]
pub(crate) struct Row {
pub lead: Lead,
pub inner: bool,
pub lit: bool,
pub role: Role,
pub on: Option<bool>,
pub letters: bool,
}
impl Row {
pub const PLAIN: Row = Row { lead: Lead::None, inner: false, lit: false, role: Role::MenuItem, on: None, letters: false };
const ITEM: Row = Row { letters: true, ..Row::PLAIN };
}
pub(crate) fn lettered(name: &str, words: &mut String) -> Option<(usize, char)> {
words.clear();
let (mut letter, mut chars) = (None, name.chars().peekable());
while let Some(c) = chars.next() {
let next = chars.peek().copied();
if c == '&' && next == Some('&') {
chars.next();
}
match next.filter(|next| c == '&' && next.is_alphanumeric() && letter.is_none()) {
Some(marked) => letter = Some((words.len(), marked)),
None => words.push(c),
}
}
letter.map(|(at, c)| (at, c.to_lowercase().next().unwrap_or(c)))
}
impl Ui {
pub(crate) fn hold(&mut self, held: Mods) -> bool {
let was = std::mem::replace(&mut self.frame.held, held);
let (down, up) = (held.alt && !was.alt, was.alt && !held.alt);
self.frame.tapped |= up && self.frame.alone && held == Mods::NONE;
self.frame.alone = held == ALT && (down || self.frame.alone);
let shown = !self.bar.is_empty() || !self.popups.is_empty();
(down || up) && shown || self.frame.tapped
}
#[cfg(windows)]
pub(crate) fn takes_alt(&self) -> bool {
!self.bar.is_empty()
}
fn letters_show(&self) -> bool {
let held = self.frame.held;
held.alt && !held.ctrl && self.window_focused || self.frame.on_bar || self.frame.keyed
}
fn menu_words(&mut self, words: &str, letter: Option<usize>, font: Font, color: Color) {
let Some(at) = letter.filter(|_| self.letters_show()) else {
self.text(words, font, color);
return;
};
let end = at + words[at..].chars().next().map_or(0, char::len_utf8);
self.row().open();
for (piece, font) in [(&words[..at], font), (&words[at..end], font.underlined()), (&words[end..], font)] {
if !piece.is_empty() {
self.text(piece, font, color);
}
}
self.close();
}
pub(crate) fn has_rested(&mut self, wait: Duration) -> bool {
let Some(since) = self.rested else { return false };
let so_far = self.now.saturating_sub(since);
if so_far < wait {
self.wake_after(wait - so_far);
}
so_far >= wait
}
pub fn tooltip(&mut self, of: &Response, text: &str) {
let keyboard = of.focused && self.focus_visible();
if text.is_empty() || !keyboard && !(of.hovered && self.has_rested(TIP_AFTER)) {
return;
}
let t = self.theme;
let (words, keys) = text.split_once('\t').unwrap_or((text, ""));
self.layer(of.id ^ TIP, Anchor::Under(of.rect, true), false, Color::NONE);
let tip = self.el().pad((8.0, 5.0)).max(280.0, f32::INFINITY).fill(t.raised).border(t.line_strong, 1.0).radius(5.0).shadow(8.0);
if keys.is_empty() {
tip.open();
self.el().text(words, t.small_font, t.text).wrap().show();
} else {
tip.flow(Flow::Row).gap(12.0).align(Align::Start, Align::Center).open();
self.text(words, t.small_font, t.text);
self.text(keys, t.small_font, t.text_dim);
}
self.close();
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn menu(&mut self, id: Id) -> bool {
let Some(depth) = self.popups.iter().position(|popup| popup.id == id) else { return false };
if depth > 0 && depth + 1 == self.popups.len() && self.key(Key::Left, Mods::NONE) {
self.close_popups(depth);
return false;
}
let wide = if self.popups[depth].beside { 0.0 } else { self.popups[depth].anchor.w };
if !self.popup(id) {
return false;
}
let step = self.key(Key::Down, Mods::NONE) as i8 - self.key(Key::Up, Mods::NONE) as i8;
if step != 0 {
self.move_focus(step > 0);
}
let front = depth + 1 == self.popups.len();
if front && self.key(Key::Home, Mods::NONE) {
self.frame.go_row = Some(0);
}
if front && self.key(Key::End, Mods::NONE) {
self.frame.go_row = Some(u32::MAX);
}
self.menu_box(wide);
true
}
pub(crate) fn popup_most(&self) -> [f32; 2] {
[self.size().w - 2.0 * EDGE, self.size().h - 2.0 * EDGE].map(|most| most.max(self.theme.control_height))
}
#[cfg_attr(debug_assertions, track_caller)]
pub(crate) fn menu_box(&mut self, wide: f32) {
let (t, high) = (self.theme, self.size().h - 2.0 * EDGE);
let rows = self.column().pad(4.0).min(wide.max(170.0), 0.0).max(f32::INFINITY, high.max(t.control_height)).scroll();
let rows = rows.fill(t.raised).border(t.line_strong, 1.0).radius(8.0).shadow(16.0).sense().open();
self.tell(&rows, Role::Menu, None, "");
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn context_menu(&mut self, of: &Response) -> bool {
if let (true, Some(at)) = (of.right_clicked, self.pointer()) {
self.open_popup(of.id ^ CONTEXT, Rect { x: at.x, y: at.y, w: 0.0, h: 0.0 });
}
if of.focused && (self.key(Key::Menu, Mods::NONE) || self.key(Key::F(10), Mods::SHIFT)) {
self.open_popup(of.id ^ CONTEXT, of.rect);
(self.frame.keyed, self.frame.go_row) = (true, Some(0));
}
self.menu(of.id ^ CONTEXT)
}
pub(crate) fn menu_row(&mut self, label: &str, row: Row) -> Response {
let t = self.theme;
let mut state = self.press(self.next_id());
let (name, keys) = label.split_once('\t').unwrap_or((label, ""));
let mut words = std::mem::take(&mut self.scratch);
words.clear();
let letter = if row.letters { lettered(name, &mut words) } else { (words.push_str(name), None).1 };
let depth = self.depth();
let front = depth > 0 && depth == self.popups.len();
let picked = letter.filter(|_| front).is_some_and(|(_, c)| self.key(Key::Char(c), Mods::NONE) || self.key(Key::Char(c), ALT));
let fill = if state.hovered || state.focused || row.lit { t.hover } else { Color::NONE };
self.row().h(Length::Px(t.control_height)).pad((10.0, 0.0)).gap(8.0).fill(fill).radius(4.0).sense().focusable().open();
self.tell(&state, row.role, row.on, &words);
self.tell_shortcut(keys);
let (nth, count) = self.menu_place(depth);
if count > 0 {
self.tell_position(nth as usize + 1, count as usize);
}
self.lead(row.lead, depth);
self.menu_words(&words, letter.map(|(at, _)| at), t.font, t.text);
self.el().w(Length::Grow(1.0)).min(12.0, 0.0).show();
self.note(keys);
if row.inner {
self.el().icon(RIGHT, 12.0, t.text_dim).show();
}
self.close();
self.scratch = words;
if state.hovered && !row.lit && self.popups.len() > depth && self.has_rested(MENU_AFTER) {
self.close_popups(depth);
}
let sent = self.frame.go_row.map(|row| if row == u32::MAX { count.saturating_sub(1) } else { row });
if front && sent == Some(nth) {
(self.frame.go_row, self.focus_shown) = (None, true);
self.set_focus(state.id);
}
(state.clicked, self.again) = (state.clicked || picked, self.again || picked);
state
}
fn menu_place(&mut self, depth: usize) -> (u32, u32) {
let Some(menu) = depth.checked_sub(1).and_then(|at| self.popups.get(at)).map(|popup| popup.id) else { return (0, 0) };
let [last, now] = &mut self.frame.rows;
let nth = match now.iter_mut().find(|(id, _)| *id == menu) {
Some((_, count)) => std::mem::replace(count, *count + 1),
None => (now.push((menu, 1)), 0).1,
};
let known = last.iter().find(|(id, _)| *id == menu).map_or(0, |(_, count)| *count);
(nth, known.max(nth + 1))
}
fn lead(&mut self, lead: Lead, depth: usize) {
let t = self.theme;
let menu = depth.checked_sub(1).and_then(|at| self.popups.get(at)).map_or(0, |popup| popup.id);
if lead != Lead::None && menu != 0 && !self.frame.marked.contains(&menu) {
self.frame.marked.push(menu);
}
if menu == 0 || !self.frame.leads.contains(&menu) {
return;
}
let room = self.el().size(16.0, 16.0);
let _ = match lead {
Lead::None | Lead::Tick(false) | Lead::Dot(false) => room.show(),
Lead::Tick(true) => room.icon(CHECK, 16.0, t.text).pad(2.0).show(),
Lead::Dot(true) => room.icon(DOT, 16.0, t.text).show(),
Lead::Icon(icon, down) => room.icon(icon, 16.0, t.text).fill(if down { t.selected } else { Color::NONE }).radius(3.0).show(),
};
}
pub fn menu_item(&mut self, label: &str) -> Response {
self.menu_pick(label, Lead::None)
}
pub fn icon_menu_item(&mut self, icon: Icon, label: &str) -> Response {
self.menu_pick(label, Lead::Icon(icon, false))
}
pub(crate) fn menu_pick(&mut self, label: &str, lead: Lead) -> Response {
let state = self.menu_row(label, Row { lead, ..Row::ITEM });
if state.clicked {
self.close_popup();
}
state
}
pub fn menu_checkbox(&mut self, on: &mut bool, label: &str) -> Response {
let mut state = self.menu_row(label, Row { lead: Lead::Tick(*on), on: Some(*on), ..Row::ITEM });
if state.clicked {
(*on, state.changed) = (!*on, true);
self.close_popup();
}
state
}
pub fn menu_radio<T: PartialEq>(&mut self, choice: &mut T, value: T, label: &str) -> Response {
let picked = *choice == value;
let mut state = self.menu_row(label, Row { lead: Lead::Dot(picked), role: Role::MenuItemRadio, on: Some(picked), ..Row::ITEM });
if state.clicked {
(state.changed, *choice) = (!picked, value);
self.close_popup();
}
state
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn submenu(&mut self, label: &str) -> bool {
let (id, depth) = (self.next_id() ^ INNER, self.depth());
let open = self.popups.get(depth).is_some_and(|popup| popup.id == id);
let state = self.menu_row(label, Row { inner: true, lit: open, role: Role::Submenu, on: Some(open), ..Row::ITEM });
let keyed = state.focused && self.key(Key::Right, Mods::NONE) || state.clicked && !self.input.released[0];
let asked = state.clicked || keyed || state.hovered && self.has_rested(MENU_AFTER);
if asked && !open {
self.pop_up(id, state.rect, true);
self.frame.go_row = if keyed { Some(0) } else { self.frame.go_row };
}
self.menu(id)
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn menu_button(&mut self, label: &str) -> bool {
let t = self.theme;
let state = self.press(self.next_id());
self.bar.push((state.id, state.rect));
self.frame.on_bar_now |= state.focused;
let open = self.popups.first().is_some_and(|popup| popup.id == state.id);
let mut words = std::mem::take(&mut self.scratch);
let letter = lettered(label, &mut words);
let alone = self.frame.on_bar && self.popups.is_empty();
let lettered = letter.is_some_and(|(_, c)| self.key(Key::Char(c), ALT) || alone && self.key(Key::Char(c), Mods::NONE));
let keyed = lettered || state.focused && self.key(Key::Down, Mods::NONE) || state.clicked && !self.input.released[0];
let over = self.input.moved && self.pointer().is_some_and(|at| state.rect.contains(at));
if state.clicked || keyed || !open && over && self.popups.first().is_some_and(|popup| popup.bar) {
self.open_popup(state.id, state.rect);
self.popups.iter_mut().for_each(|popup| popup.bar = true);
(self.frame.keyed, self.frame.go_row) = (self.frame.keyed || keyed, keyed.then_some(0));
}
if state.focused && self.popups.is_empty() && self.key(Key::Escape, Mods::NONE) {
self.set_focus(self.frame.back);
}
let lit = open || state.hovered || state.focused;
self.row().h(Length::Px(t.control_height - 4.0)).pad((9.0, 0.0)).fill(if lit { t.hover } else { Color::NONE }).radius(4.0).sense().focusable().open();
self.tell(&state, Role::Fold, Some(open), &words);
self.menu_words(&words, letter.map(|(at, _)| at), t.font, if lit { t.text } else { t.text_dim });
self.close();
self.scratch = words;
self.menu(state.id)
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn menu_bar(&mut self) -> Response {
let t = self.theme;
let bar = self.row().pad((4.0, 2.0)).gap(2.0).fill(t.panel).open();
self.tell(&bar, Role::MenuBar, None, "");
let first = self.next_id();
if std::mem::take(&mut self.frame.tapped) {
self.tap_bar(first);
}
if self.key(Key::F(10), Mods::NONE) {
self.open_popup(first, self.visible_rect(first));
self.popups.iter_mut().for_each(|popup| popup.bar = true);
(self.frame.keyed, self.frame.go_row) = (true, Some(0));
}
bar
}
fn tap_bar(&mut self, first: Id) {
if self.frame.on_bar || !self.popups.is_empty() {
let back = self.frame.back;
self.close_popup();
self.set_focus(back);
return;
}
self.frame.back = self.focus();
self.set_focus(first);
}
#[doc(alias("dropdown", "combobox"))]
pub fn select(&mut self, choice: &mut usize, options: &[&str]) -> Response {
self.select_named(choice, options.len(), &|at| options[at])
}
pub fn select_value<T: PartialEq + Clone>(&mut self, value: &mut T, options: &[(T, &str)]) -> Response {
let mut choice = options.iter().position(|(option, _)| option == value).unwrap_or(usize::MAX);
let state = self.select_named(&mut choice, options.len(), &|at| options[at].1);
if let Some((option, _)) = options.get(choice).filter(|_| state.changed) {
*value = option.clone();
}
state
}
fn select_named<'a>(&mut self, choice: &mut usize, count: usize, name: &dyn Fn(usize) -> &'a str) -> Response {
let shown = if *choice < count { name(*choice) } else { "-" };
let mut state = self.button_like(Before::None, shown, Some(DOWN), false);
if state.clicked || state.focused && self.key(Key::Down, Mods::NONE) {
self.open_popup(state.id, state.rect);
self.frame.go_row = Some(if *choice < count { *choice as u32 } else { 0 });
}
let open = self.popups.iter().any(|popup| popup.id == state.id);
self.tell_choice(&state, open, if *choice < count { name(*choice) } else { "" });
if !self.menu(state.id) {
return state;
}
for number in 0..count {
if self.menu_row(name(number), Row { on: Some(number == *choice), ..Row::PLAIN }).clicked {
(*choice, state.changed) = (number, true);
self.close_popup();
}
}
self.close();
state
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn popover(&mut self, id: Id) -> bool {
let Some(depth) = self.popups.iter().position(|popup| popup.id == id) else { return false };
let by = self.popups[depth].anchor;
if depth + 1 == self.popups.len() && self.key(Key::Escape, Mods::NONE) {
self.close_popups(depth);
return false;
}
let t = self.theme;
let around = self.layer(id ^ POPOVER, Anchor::At(pt(0.0, 0.0)), depth == 0, Color::NONE);
let layer = self.layers.len() - 1;
(self.layers[layer].keys, self.layers[layer].depth) = (true, depth as u32 + 1);
if around.hovered && (self.input.pressed[0] || self.input.pressed[1]) {
self.close_popup();
}
let whole = self.id("popover");
let (was, room) = (self.visible_rect(whole), self.size());
let under = by.bottom() + POINT[0] + was.h + EDGE <= room.h || by.y - POINT[0] - was.h < EDGE;
let x = (by.x + by.w / 2.0 - was.w / 2.0).min(room.w - was.w - EDGE).max(EDGE).round();
let y = if under { by.bottom() + POINT[0] } else { by.y - POINT[0] - was.h }.round();
self.layers[layer].anchor = Anchor::At(pt(x, y));
self.again |= (was.x, was.y) != (x, y) || was.is_empty();
let tip = (by.x + by.w / 2.0).min(x + was.w - POINT[1]).max(x + POINT[1]).round();
self.frame.arrows.push((id, pt(tip, if under { y } else { y + was.h }), under));
self.frame.later[1] = Some(Ui::show_arrows);
let whole = self.column().key("popover").pad(12.0).gap(t.gap).fill(t.raised).border(t.line_strong, 1.0).radius(8.0).shadow(16.0).sense().open();
self.tell(&whole, Role::Group, None, "");
true
}
fn show_arrows(&mut self) {
let (t, arrows) = (self.theme, std::mem::take(&mut self.frame.arrows));
let [deep, wide] = POINT;
for (id, at, under) in arrows.iter().copied() {
let y = |y: f32| if under { y } else { deep + 2.0 - y };
let mut fill = Path::new();
fill.move_to(wide / 2.0, y(0.5)).line_to(0.0, y(deep + 0.5)).line_to(0.0, y(deep + 2.0)).line_to(wide, y(deep + 2.0)).line_to(wide, y(deep + 0.5)).close();
let mut sides = Path::new();
sides.move_to(0.0, y(deep + 0.5)).line_to(wide / 2.0, y(0.5)).line_to(wide, y(deep + 0.5));
let top = if under { at.y - deep } else { at.y - 2.0 };
self.layer(id ^ ARROW, Anchor::At(pt(at.x - wide / 2.0, top)), false, Color::NONE);
self.el().flow(Flow::Stack).size(wide, deep + 2.0).open();
self.el().grow().shape(&fill, t.raised).show();
self.el().grow().outline(&sides, 1.0, t.line_strong).show();
self.close();
}
self.frame.arrows = arrows;
self.frame.arrows.clear();
}
#[cfg_attr(debug_assertions, track_caller)]
#[doc(alias("modal"))]
pub fn dialog(&mut self) -> Response {
self.dialog_max_width(520.0)
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn pin(&mut self, across: Align, down: Align) {
let id = hash((self.next_id(), PINNED, across as u8, down as u8));
self.layer(id, Anchor::Corner(across, down), false, Color::NONE);
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn notices(&mut self, across: Align, down: Align) {
let gap = self.theme.gap;
self.pin(across, down);
self.column().pad(12.0).gap(gap).align(Align::Start, across).open();
}
#[doc(alias("toast"))]
pub fn notice(&mut self, text: &str) {
self.notice_for(text, Duration::from_secs(4));
}
pub fn notice_for(&mut self, text: &str, stays: Duration) {
self.frame.notices += 1;
self.notified.push((text.into(), self.now.saturating_add(stays), self.frame.notices));
self.announce(text);
self.again = true;
}
pub(crate) fn show_after(&mut self) {
for later in self.frame.later.into_iter().flatten() {
later(self);
}
let (size, quiet, frame) = (self.size(), self.popups.is_empty(), &mut self.frame);
let news = frame.marked != frame.leads || frame.closable[0] != frame.closable[1] || frame.rows[0] != frame.rows[1];
std::mem::swap(&mut frame.marked, &mut frame.leads);
frame.closable.swap(0, 1);
frame.rows.swap(0, 1);
frame.rows[1].clear();
frame.panels.swap(0, 1);
(frame.tapped, frame.on_bar, frame.on_bar_now, frame.window, frame.docks) = (false, frame.on_bar_now, false, size, 0);
(frame.keyed, frame.go_row) = (frame.keyed && !quiet, frame.go_row.filter(|_| !quiet));
[&mut frame.marked, &mut frame.closable[1]].into_iter().for_each(Vec::clear);
frame.panels[1].clear();
self.again |= news;
self.show_notified();
}
fn show_notified(&mut self) {
let (t, now) = (self.theme, self.now);
self.notified.retain(|(_, until, _)| *until > now);
if self.notified.is_empty() {
return;
}
let notified = std::mem::take(&mut self.notified);
self.layer(NOTIFIED, Anchor::Corner(Align::End, Align::End), false, Color::NONE);
self.column().pad(12.0).gap(t.gap).align(Align::Start, Align::End).open();
let mut pressed = None;
for (text, until, number) in ¬ified {
let state = self.press(self.id((NOTIFIED, number)));
self.el().key((NOTIFIED, number)).pad((12.0, 8.0)).max(320.0, f32::INFINITY).fill(t.raised).border(t.line_strong, 1.0).radius(8.0).shadow(12.0).sense().open();
self.el().text(text, t.font, t.text).wrap().show();
self.close();
pressed = pressed.or(state.clicked.then_some(*number));
self.wake_after(*until - now);
}
self.close();
self.notified = notified;
if let Some(number) = pressed {
self.notified.retain(|(.., kept)| *kept != number);
self.again = true;
}
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn dialog_max_width(&mut self, widest: f32) -> Response {
let t = self.theme;
let mut beside = self.layer(self.next_id() ^ DIALOG, Anchor::Middle, true, Color::BLACK.with_alpha(0.5));
let away = std::mem::take(&mut self.escaped) == Some(beside.id);
(beside.clicked, self.again, self.dialog_escape) = (beside.clicked || away, self.again || away, Some(beside.id));
let whole = self.column().pad(18.0).gap(12.0).min(320.0f32.min(widest), 0.0).max(widest, f32::INFINITY).fill(t.panel).border(t.line_strong, 1.0).radius(10.0).shadow(24.0).sense().open();
self.tell(&whole, Role::Dialog, None, "");
beside
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_letter_of_a_name_is_found_and_the_marks_are_left_out() {
let mut words = String::new();
let mut read = |name: &str| (lettered(name, &mut words), words.clone());
assert_eq!(read("&File"), (Some((0, 'f')), "File".to_string()));
assert_eq!(read("E&xit"), (Some((1, 'x')), "Exit".to_string()));
assert_eq!(read("Save &As…"), (Some((5, 'a')), "Save As…".to_string()));
assert_eq!(read("R&&D"), (None, "R&D".to_string()));
assert_eq!(read("Rock & Roll"), (None, "Rock & Roll".to_string()));
assert_eq!(read("&One &Two"), (Some((0, 'o')), "One &Two".to_string()));
assert_eq!(read("Ö&ffnen && &Speichern"), (Some((2, 'f')), "Öffnen & &Speichern".to_string()));
assert_eq!(read("Plain"), (None, "Plain".to_string()));
assert_eq!(read("Ends in &"), (None, "Ends in &".to_string()));
}
}