use super::{LEAD, Run, SEP};
use crate::ui::theme;
pub fn width(ui: &egui::Ui, text: &str) -> f32 {
let font = egui::TextStyle::Small.resolve(ui.style());
ui.fonts(|fonts| {
fonts
.layout_no_wrap(text.to_owned(), font, theme::INK)
.size()
.x
})
}
pub fn elide(ui: &egui::Ui, text: &str, room: f32) -> String {
let mut chars: Vec<char> = text.chars().collect();
let mut said = text.to_owned();
while width(ui, &said) > room && !chars.is_empty() {
chars.pop();
said = format!(
"{}{}",
chars.iter().collect::<String>(),
theme::glyph::ELIDED
);
}
said
}
fn cost(ui: &egui::Ui, run: &Run) -> f32 {
let gap = ui.spacing().item_spacing.x;
let mark = run
.mark
.map_or(0.0, |(glyph, _)| width(ui, &format!(" {glyph}")));
width(ui, SEP) + 2.0 * gap + width(ui, &run.text) + mark
}
pub fn fitting(ui: &egui::Ui, runs: &[Run], kept: usize, name: f32, room: f32) -> (usize, f32) {
let gap = ui.spacing().item_spacing.x;
let mut used = width(ui, LEAD) * 2.0 + name + 4.0 * gap;
let core: f32 = runs.iter().take(kept).map(|run| cost(ui, run)).sum();
if used + core > room {
let words: f32 = runs.iter().take(kept).map(|r| width(ui, &r.text)).sum();
let left = room - used - (core - words);
let n = runs.iter().take(kept).map(|_| 1.0).sum::<f32>().max(1.0);
return (kept, (left / n).max(0.0));
}
used += core;
let mut shown = kept;
for run in runs.iter().skip(kept) {
used += cost(ui, run);
if used > room {
break;
}
shown += 1;
}
(shown, f32::INFINITY)
}
pub fn fits(ui: &egui::Ui, runs: &[Run], kept: usize, name: f32, room: f32) -> bool {
width(ui, LEAD) * 2.0
+ name
+ 4.0 * ui.spacing().item_spacing.x
+ runs.iter().take(kept).map(|run| cost(ui, run)).sum::<f32>()
<= room
}