agg_gui/widgets/chevron.rs
1//! `ChevronWidget` — a small clickable arrow that toggles a
2//! collapsed/expanded state. Composes into title bars, accordion
3//! headers, tree rows, anywhere a "fold" affordance is needed.
4//!
5//! The chevron is a real `Widget`: it has its own bounds, paints
6//! itself, and consumes mouse-down events that land inside it. The
7//! parent uses standard `children_mut()` + `layout()` to place it,
8//! and supplies an `on_click` closure to act on the toggle. Parents
9//! that need to share collapse state across multiple widgets pass an
10//! `Rc<Cell<bool>>` for the chevron to read each paint — keeping a
11//! single source of truth without copy-on-every-frame boilerplate.
12
13use std::cell::Cell;
14use std::rc::Rc;
15
16use crate::color::Color;
17use crate::draw_ctx::DrawCtx;
18use crate::event::{Event, EventResult, MouseButton};
19use crate::geometry::{Point, Rect, Size};
20use crate::layout_props::WidgetBase;
21use crate::widget::Widget;
22use crate::widgets::window::chrome::paint_chevron;
23
24/// Logical size of the chevron's hit / paint region. The arrow itself
25/// is ~8 px wide; the surrounding padding gives the user a comfortable
26/// click target.
27pub const CHEVRON_SIZE: f64 = 16.0;
28
29/// A clickable collapse / expand chevron.
30pub struct ChevronWidget {
31 bounds: Rect,
32 base: WidgetBase,
33 children: Vec<Box<dyn Widget>>,
34 /// Shared collapse flag — the chevron reads this each paint to pick
35 /// its glyph orientation. The parent writes it when the user (or
36 /// any other code path) toggles the fold.
37 collapsed: Rc<Cell<bool>>,
38 /// Shared glyph colour cell — the parent writes the theme colour
39 /// each paint pass; the chevron reads it without needing a typed
40 /// downcast through the children Vec. Defaults to white so a
41 /// caller that never wires the cell still gets a visible glyph.
42 color: Rc<Cell<Color>>,
43 /// Invoked on left-click. Parents put their toggle logic in here.
44 on_click: Option<Box<dyn FnMut()>>,
45}
46
47impl ChevronWidget {
48 /// Build a chevron sharing `collapsed` with its parent. The parent
49 /// is the source of truth — the chevron only renders + emits clicks.
50 pub fn new(collapsed: Rc<Cell<bool>>) -> Self {
51 Self {
52 bounds: Rect::new(0.0, 0.0, CHEVRON_SIZE, CHEVRON_SIZE),
53 base: WidgetBase::new(),
54 children: Vec::new(),
55 collapsed,
56 color: Rc::new(Cell::new(Color::white())),
57 on_click: None,
58 }
59 }
60
61 /// Wire a left-click handler. Typical implementations flip the
62 /// shared `collapsed` cell and request a redraw / notify their
63 /// owner. Builder form — chain at construction.
64 pub fn on_click(mut self, f: impl FnMut() + 'static) -> Self {
65 self.on_click = Some(Box::new(f));
66 self
67 }
68
69 /// Hand a shared colour cell to the chevron. The parent keeps a
70 /// clone of the returned cell and writes the active theme colour
71 /// into it each paint pass; the chevron picks it up automatically.
72 pub fn with_color_cell(mut self, c: Rc<Cell<Color>>) -> Self {
73 self.color = c;
74 self
75 }
76}
77
78impl Widget for ChevronWidget {
79 fn type_name(&self) -> &'static str {
80 "ChevronWidget"
81 }
82 fn bounds(&self) -> Rect {
83 self.bounds
84 }
85 fn set_bounds(&mut self, b: Rect) {
86 self.bounds = b;
87 }
88 fn children(&self) -> &[Box<dyn Widget>] {
89 &self.children
90 }
91 fn children_mut(&mut self) -> &mut Vec<Box<dyn Widget>> {
92 &mut self.children
93 }
94 fn widget_base(&self) -> Option<&WidgetBase> {
95 Some(&self.base)
96 }
97
98 fn layout(&mut self, _available: Size) -> Size {
99 Size::new(self.bounds.width, self.bounds.height)
100 }
101
102 fn paint(&mut self, ctx: &mut dyn DrawCtx) {
103 let cx = self.bounds.width * 0.5;
104 let cy = self.bounds.height * 0.5;
105 paint_chevron(ctx, cx, cy, self.collapsed.get(), self.color.get());
106 }
107
108 fn hit_test(&self, local: Point) -> bool {
109 local.x >= 0.0
110 && local.x <= self.bounds.width
111 && local.y >= 0.0
112 && local.y <= self.bounds.height
113 }
114
115 fn on_event(&mut self, event: &Event) -> EventResult {
116 if let Event::MouseDown {
117 button: MouseButton::Left,
118 ..
119 } = event
120 {
121 if let Some(cb) = self.on_click.as_mut() {
122 cb();
123 }
124 crate::animation::request_draw();
125 return EventResult::Consumed;
126 }
127 EventResult::Ignored
128 }
129}