use crate::icon_text::IconText;
use eframe::egui;
pub const INDENT: f32 = 16.0;
const BOX: f32 = 13.0;
const GAP: f32 = 4.0;
const RIGHT_PAD: f32 = 10.0;
pub struct TreeRow<'a> {
pub guides: &'a [bool],
pub is_last: bool,
pub expandable: bool,
pub expanded: bool,
pub root: bool,
pub glyph: Option<&'a str>,
pub label: &'a str,
pub selected: bool,
pub highlighted: bool,
pub draggable: bool,
pub tint: Option<egui::Color32>,
}
impl<'a> TreeRow<'a> {
pub fn branch(guides: &'a [bool], is_last: bool, expanded: bool, label: &'a str) -> Self {
Self {
guides,
is_last,
expandable: true,
expanded,
root: false,
glyph: None,
label,
selected: false,
highlighted: false,
draggable: false,
tint: None,
}
}
pub fn leaf(guides: &'a [bool], is_last: bool, label: &'a str) -> Self {
Self {
guides,
is_last,
expandable: false,
expanded: false,
root: false,
glyph: None,
label,
selected: false,
highlighted: false,
draggable: false,
tint: None,
}
}
pub fn glyph(mut self, glyph: Option<&'a str>) -> Self {
self.glyph = glyph;
self
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self.highlighted = selected;
self
}
pub fn draggable(mut self, draggable: bool) -> Self {
self.draggable = draggable;
self
}
pub fn tint(mut self, tint: Option<egui::Color32>) -> Self {
self.tint = tint;
self
}
}
pub struct NodeResponse {
pub row_rect: egui::Rect,
pub box_rect: egui::Rect,
pub toggled: bool,
pub label: egui::Response,
pub band: egui::Response,
pub band_drag_started: bool,
pub lift_started: bool,
pub lifted: bool,
}
impl NodeResponse {
pub fn clicked(&self) -> bool {
!self.lifted && (self.label.clicked() || self.band.clicked())
}
pub fn secondary_clicked(&self) -> bool {
!self.lifted && (self.band.secondary_clicked() || self.label.secondary_clicked())
}
pub fn double_clicked(&self) -> bool {
!self.lifted && (is_double_click(&self.label) || is_double_click(&self.band))
}
}
pub fn is_double_click(response: &egui::Response) -> bool {
response.double_clicked() || response.triple_clicked()
}
impl NodeResponse {
pub fn drag_started(&self) -> bool {
self.label.drag_started() || self.band_drag_started || self.lift_started
}
}
pub fn child_guides(guides: &[bool], is_last: bool) -> Vec<bool> {
let mut next = guides.to_vec();
next.push(!is_last);
next
}
pub(crate) fn hover_band_fill(theme: &crate::theme::Theme) -> egui::Color32 {
theme.colors.hover
}
pub fn node(
ui: &mut egui::Ui,
row: TreeRow,
add_right: impl FnOnce(&mut egui::Ui),
) -> NodeResponse {
let connector_col = if row.root { 0 } else { row.guides.len() };
let indent = if row.root {
0.0
} else {
(connector_col as f32 + 1.0) * INDENT
};
let mut box_rect = egui::Rect::NOTHING;
let mut toggled = false;
let mut label_resp: Option<egui::Response> = None;
let highlight = ui.painter().add(egui::Shape::Noop);
let mut controls = egui::Rect::NOTHING;
let hold_to_drag = row.draggable && crate::viewport::touch::enabled(ui.ctx());
let band_sense = if hold_to_drag {
egui::Sense::click()
} else {
egui::Sense::click_and_drag()
};
let outer = ui.scope_builder(
egui::UiBuilder::new().sense(band_sense),
|ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
if indent > 0.0 {
ui.add_space(indent);
}
let (brect, bresp) = ui.allocate_exact_size(
crate::viewport::touch::target_size(ui.ctx(), egui::vec2(BOX, BOX)),
if row.expandable {
egui::Sense::click()
} else {
egui::Sense::hover()
},
);
box_rect = brect;
if row.expandable {
let painted = egui::Rect::from_center_size(brect.center(), egui::Vec2::splat(BOX));
paint_box(ui, painted, row.expanded);
if bresp.clicked() {
toggled = true;
}
}
ui.add_space(GAP);
if let Some(glyph) = row.glyph {
match crate::icons::artwork(glyph) {
Some(icon) => {
egui_extras::install_image_loaders(ui.ctx());
let height = ui.text_style_height(&egui::TextStyle::Body);
let mut art = crate::icon_text::image(icon, height);
if icon.mono {
art = art.tint(ui.visuals().text_color());
}
ui.add(art);
}
None => {
ui.label(egui::RichText::new(glyph).color(ui.visuals().text_color()));
}
}
ui.add_space(GAP);
}
let theme = crate::theme::Theme::of(ui);
let mut text = egui::RichText::new(row.label).font(theme.font(crate::theme::Text::Data));
if let Some(tint) = row.tint {
text = text.color(tint);
}
let sense = if row.draggable && !hold_to_drag {
egui::Sense::CLICK | egui::Sense::DRAG
} else {
egui::Sense::CLICK
};
let has_icon = row.label.chars().any(crate::icons::has);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.add_space(RIGHT_PAD);
add_right(ui);
controls = ui.min_rect();
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
label_resp = Some(if has_icon && !row.draggable {
IconText::new(text)
.sense(sense)
.selectable(false)
.truncate()
.show(ui)
} else {
ui.add(
egui::Label::new(text)
.sense(sense)
.selectable(!row.draggable)
.truncate(),
)
});
});
});
})
},
);
let band = outer.response;
let theme = crate::theme::Theme::of(ui);
let press = ui.input(|i| i.pointer.press_origin());
let from_a_control = press
.is_some_and(|at| box_rect.contains(at) || controls.contains(at));
let (lift_started, lifted, held) = if hold_to_drag {
touch_lift(ui, band.id, band.interact_rect, from_a_control)
} else {
(false, false, false)
};
if held {
let shadow = band.rect.translate(egui::vec2(0.0, 3.0));
let ring = egui::Stroke::new(crate::theme::RING, theme.colors.accent_ring);
ui.painter().set(
highlight,
egui::Shape::Vec(vec![
egui::Shape::rect_filled(shadow, 2.0, egui::Color32::from_black_alpha(70)),
egui::Shape::rect_filled(band.rect, 2.0, theme.colors.accent_muted),
egui::Shape::rect_stroke(band.rect, 2.0, ring, egui::StrokeKind::Inside),
]),
);
} else if !row.root && (row.selected || row.highlighted) {
let mut shapes = vec![egui::Shape::Rect(egui::epaint::RectShape::filled(band.rect, 0.0, theme.colors.accent_muted))];
if row.selected {
let edge = egui::Rect::from_min_max(band.rect.left_top(), egui::pos2(band.rect.left() + crate::theme::RING, band.rect.bottom()));
shapes.push(egui::Shape::Rect(egui::epaint::RectShape::filled(edge, 0.0, theme.colors.accent_ring)));
}
ui.painter().set(highlight, egui::Shape::Vec(shapes));
} else if !row.root && band.contains_pointer() && ui.ctx().dragged_id().is_none() {
ui.painter().set(highlight, egui::epaint::RectShape::filled(band.rect, 0.0, hover_band_fill(&theme)));
}
let row_rect = outer.inner.response.rect;
if !row.root {
paint_connectors(ui, row_rect, row.guides, connector_col, row.is_last);
}
if band.has_focus() {
crate::ui_kit::paint::focus_ring(ui, row_rect, crate::theme::radius::INPUT, &theme);
}
let band_drag_started = band.drag_started() && !from_a_control;
NodeResponse {
row_rect,
box_rect,
toggled,
label: label_resp.expect("label always drawn"),
band,
band_drag_started,
lift_started,
lifted,
}
}
pub const LIFT_HOLD: f64 = 0.45;
pub const LIFT_SLOP: f32 = 8.0;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum LiftState {
#[default]
Idle,
Pending { row: egui::Id, origin: egui::Pos2, since: f64, touch: bool },
Lifted { row: egui::Id },
}
#[derive(Clone, Copy, Debug)]
pub struct LiftInput {
pub pressed_here: bool,
pub origin: egui::Pos2,
pub down: bool,
pub pos: Option<egui::Pos2>,
pub time: f64,
pub touch: bool,
pub multi: bool,
}
impl LiftState {
pub fn step(self, row: egui::Id, input: LiftInput) -> (LiftState, bool) {
if input.pressed_here && input.down && !input.multi {
let pending = LiftState::Pending { row, origin: input.origin, since: input.time, touch: input.touch };
return (pending, false);
}
match self {
LiftState::Pending { row: r, origin, since, touch } if r == row => {
if !input.down || input.multi {
return (LiftState::Idle, false);
}
let moved = input.pos.is_some_and(|p| p.distance(origin) > LIFT_SLOP);
if moved {
return if touch { (LiftState::Idle, false) } else { (LiftState::Lifted { row }, true) };
}
if touch && input.time - since >= LIFT_HOLD {
return (LiftState::Lifted { row }, true);
}
(self, false)
}
LiftState::Lifted { row: r } if r == row => {
if input.down { (self, false) } else { (LiftState::Idle, false) }
}
other => (other, false),
}
}
pub fn holds(self, row: egui::Id) -> bool {
matches!(self, LiftState::Lifted { row: r } if r == row)
}
}
fn touch_lift(ui: &egui::Ui, row: egui::Id, rect: egui::Rect, from_a_control: bool) -> (bool, bool, bool) {
let key = egui::Id::new("brep-tree-touch-lift");
let ctx = ui.ctx();
let before: LiftState = ctx.data(|d| d.get_temp(key)).unwrap_or_default();
let input = ctx.input(|i| {
let origin = i.pointer.press_origin().unwrap_or(egui::Pos2::ZERO);
LiftInput {
pressed_here: i.pointer.primary_pressed() && !from_a_control && rect.contains(origin),
origin,
down: i.pointer.primary_down(),
pos: i.pointer.latest_pos().or(i.pointer.interact_pos()),
time: i.time,
touch: i.any_touches(),
multi: i.multi_touch().is_some(),
}
});
let (after, started) = before.step(row, input);
if after != before {
ctx.data_mut(|d| d.insert_temp(key, after));
}
if let LiftState::Pending { row: r, since, touch: true, .. } = after {
if r == row {
let left = (since + LIFT_HOLD - input.time).max(0.0);
ctx.request_repaint_after(std::time::Duration::from_secs_f64(left + 0.01));
}
}
let held = after.holds(row);
if held && ctx.dragged_id() != Some(row) {
ctx.set_dragged_id(row);
}
(started, held || before.holds(row), held)
}
pub fn message_leaf(
ui: &mut egui::Ui,
guides: &[bool],
is_last: bool,
text: &str,
color: egui::Color32,
) -> egui::Rect {
let connector_col = guides.len();
let indent = (connector_col as f32 + 1.0) * INDENT + BOX + GAP;
let line_h = ui.text_style_height(&egui::TextStyle::Body);
let inner = ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.add_space(indent);
ui.vertical(|ui| {
ui.set_max_width((ui.available_width() - RIGHT_PAD).max(40.0));
ui.add(
egui::Label::new(egui::RichText::new(text).color(color))
.wrap_mode(egui::TextWrapMode::Wrap),
);
});
});
let row_rect = inner.response.rect;
let tick_y = row_rect.top() + line_h * 0.5;
paint_connectors_at(ui, row_rect, guides, connector_col, is_last, tick_y);
row_rect
}
fn paint_box(ui: &egui::Ui, rect: egui::Rect, expanded: bool) {
let painter = ui.painter();
let stroke = egui::Stroke::new(1.0, line_color(ui));
painter.rect(
rect,
egui::CornerRadius::same(2),
egui::Color32::TRANSPARENT,
stroke,
egui::StrokeKind::Inside,
);
let c = rect.center();
let arm = rect.width() * 0.28;
let sign = egui::Stroke::new(1.4, crate::theme::Theme::of(ui).colors.text_secondary);
painter.line_segment(
[egui::pos2(c.x - arm, c.y), egui::pos2(c.x + arm, c.y)],
sign,
);
if !expanded {
painter.line_segment(
[egui::pos2(c.x, c.y - arm), egui::pos2(c.x, c.y + arm)],
sign,
);
}
}
fn paint_connectors(
ui: &egui::Ui,
row: egui::Rect,
guides: &[bool],
connector_col: usize,
is_last: bool,
) {
paint_connectors_at(ui, row, guides, connector_col, is_last, row.center().y);
}
fn paint_connectors_at(
ui: &egui::Ui,
row: egui::Rect,
guides: &[bool],
connector_col: usize,
is_last: bool,
mid: f32,
) {
let painter = ui.painter();
let stroke = egui::Stroke::new(1.0, line_color(ui));
let sp = ui.spacing().item_spacing.y + 1.0;
let left = row.left();
let top = row.top() - sp;
let bottom = row.bottom() + sp;
let col_x = |c: usize| left + c as f32 * INDENT + INDENT * 0.5;
for (i, on) in guides.iter().enumerate() {
if *on {
let x = col_x(i);
painter.line_segment([egui::pos2(x, top), egui::pos2(x, bottom)], stroke);
}
}
let x = col_x(connector_col);
let v_bottom = if is_last { mid } else { bottom };
painter.line_segment([egui::pos2(x, top), egui::pos2(x, v_bottom)], stroke);
painter.line_segment(
[egui::pos2(x, mid), egui::pos2(x + INDENT * 0.5, mid)],
stroke,
);
}
fn line_color(ui: &egui::Ui) -> egui::Color32 {
crate::theme::Theme::of(ui).colors.border_strong
}