use egui::{Color32, Pos2, Rect, Response, Sense, Stroke, vec2};
use super::super::{
BRAND, INK_FAINT, INK_WEAK, RADIUS, RAISED, ROW, RULE, State, accent, space, tint,
};
pub fn row(
ui: &mut egui::Ui,
words: &str,
ink: Color32,
state: Option<Color32>,
chosen: bool,
indent: f32,
help: &str,
) -> Response {
Row {
mark: (String::new(), ink),
words: words.to_owned(),
ink,
state,
chosen,
indent,
aside: None,
beneath: Vec::new(),
tail: None,
door: None,
help: help.to_owned(),
}
.paint(ui)
}
pub fn middle(ui: &egui::Ui, blocked: bool) -> f32 {
let (top, band) = head(ui, blocked);
top + band / 2.0
}
fn head(ui: &egui::Ui, blocked: bool) -> (f32, f32) {
if blocked {
(space::XXS, tall(ui, &egui::TextStyle::Body))
} else {
(0.0, ROW)
}
}
pub fn row_width(ui: &egui::Ui, words: &str) -> f32 {
let font = egui::TextStyle::Body.resolve(ui.style());
let said = ui.fonts(|fonts| {
fonts
.layout_no_wrap(words.to_owned(), font, Color32::PLACEHOLDER)
.size()
.x
});
RULE + space::M + said + space::S
}
pub struct Row {
pub mark: (String, Color32),
pub words: String,
pub ink: Color32,
pub state: Option<Color32>,
pub chosen: bool,
pub indent: f32,
pub aside: Option<(String, Option<State>)>,
pub beneath: Vec<(String, Color32, egui::TextStyle)>,
pub tail: Option<String>,
pub door: Option<(&'static str, &'static str)>,
pub help: String,
}
impl Row {
pub fn paint(&self, ui: &mut egui::Ui) -> Response {
self.paint_doored(ui).0
}
pub fn paint_doored(&self, ui: &mut egui::Ui) -> (Response, Option<Response>) {
let said = [self.mark.0.as_str(), self.words.as_str()]
.into_iter()
.filter(|part| !part.is_empty())
.collect::<Vec<_>>()
.join(" ");
let blocked = !self.beneath.is_empty();
let (pad, band) = head(ui, blocked);
let height = pad
+ band
+ self
.beneath
.iter()
.map(|(_, _, s)| tall(ui, s))
.sum::<f32>()
+ pad;
let (rect, seat) =
ui.allocate_exact_size(vec2(ui.available_width(), height), Sense::click());
seat.widget_info(|| {
egui::WidgetInfo::selected(
egui::WidgetType::SelectableLabel,
ui.is_enabled(),
self.chosen,
&said,
)
});
if !ui.is_rect_visible(rect) {
return (super::told(seat, &self.help), None);
}
let fill = if self.chosen {
RAISED
} else if seat.contains_pointer() {
super::super::hover()
} else {
Color32::TRANSPARENT
};
ui.painter().rect_filled(rect, RADIUS, fill);
if let Some(accent) = self.chosen.then_some(BRAND).or(self.state) {
super::rule_at(ui, rect.left_top(), rect.height(), accent);
}
if seat.has_focus() {
ui.painter()
.rect_stroke(rect, RADIUS, Stroke::new(1.0, BRAND));
}
let left = rect.min.x + RULE + self.indent + space::M;
let mut right = rect.max.x - space::S;
let middle = rect.min.y + pad + band / 2.0;
let door = self
.door
.filter(|_| seat.contains_pointer())
.map(|(glyph, help)| {
let strip = Rect::from_min_max(
Pos2::new(left, rect.min.y + pad),
Pos2::new(right, rect.min.y + pad + band),
);
let mut child = ui.new_child(
egui::UiBuilder::new()
.id_salt(seat.id)
.max_rect(strip)
.layout(egui::Layout::right_to_left(egui::Align::Center)),
);
let door = super::tab(&mut child, glyph, INK_FAINT, help);
right = door.rect.min.x - space::S;
door
});
if let Some((aside, state)) = &self.aside {
let ink = state.map_or(INK_WEAK, accent);
let small = egui::TextStyle::Small;
let galley = elided(ui, aside, 0, (ink, ink), &small, f32::INFINITY);
right -= galley.size().x + state.map_or(0.0, |_| space::XS);
let at = Pos2::new(right, middle - galley.size().y / 2.0);
if let Some(state) = state {
let pill = Rect::from_min_size(at, galley.size()).expand2(vec2(space::XS, 1.0));
ui.painter().rect_filled(pill, RADIUS, tint(*state));
}
ui.painter().galley(at, galley, ink);
right -= space::S;
}
let lead = said.len().saturating_sub(self.words.len());
let body = egui::TextStyle::Body;
let inks = (self.mark.1, self.ink);
let galley = elided(ui, &said, lead, inks, &body, right - left);
let at = Pos2::new(left, middle - galley.size().y / 2.0);
ui.painter().galley(at, galley, self.ink);
self.hang(ui, rect, left, rect.min.y + pad + band);
let seat = if door.as_ref().is_some_and(Response::hovered) {
crate::ui::act::bridge(&seat);
seat
} else {
super::told(seat, &self.help)
};
(seat, door)
}
fn hang(&self, ui: &egui::Ui, rect: Rect, left: f32, top: f32) {
let mut top = top;
let last = self.beneath.len().saturating_sub(1);
for (i, (line, ink, style)) in self.beneath.iter().enumerate() {
let mut end = rect.max.x - space::S;
if let Some(tail) = self.tail.as_ref().filter(|_| i == last) {
let small = egui::TextStyle::Small;
let galley = elided(ui, tail, 0, (INK_WEAK, INK_WEAK), &small, f32::INFINITY);
end -= galley.size().x;
let at = Pos2::new(end, top + (tall(ui, style) - galley.size().y) / 2.0);
ui.painter().galley(at, galley, INK_WEAK);
end -= space::S;
}
let galley = elided(ui, line, 0, (*ink, *ink), style, end - left);
ui.painter().galley(Pos2::new(left, top), galley, *ink);
top += tall(ui, style);
}
}
}
fn tall(ui: &egui::Ui, style: &egui::TextStyle) -> f32 {
let font = style.resolve(ui.style());
ui.fonts(|fonts| fonts.row_height(&font))
}
fn elided(
ui: &egui::Ui,
said: &str,
lead: usize,
(marked, ink): (Color32, Color32),
style: &egui::TextStyle,
width: f32,
) -> std::sync::Arc<egui::Galley> {
let font = style.resolve(ui.style());
let mut job = egui::text::LayoutJob::simple_singleline(said.to_owned(), font, ink);
if let Some(first) = job.sections.first_mut() {
first.byte_range = lead..said.len();
let mut mark = first.clone();
mark.byte_range = 0..lead;
mark.format.color = marked;
job.sections.insert(0, mark);
}
job.wrap = egui::text::TextWrapping::truncate_at_width(width.max(0.0));
ui.fonts(|fonts| fonts.layout_job(job))
}