1mod widget_impl;
14
15use std::cell::RefCell;
16use std::rc::Rc;
17use std::sync::Arc;
18
19use crate::color::Color;
20use crate::draw_ctx::DrawCtx;
21use crate::event::{Event, EventResult};
22use crate::geometry::{Point, Rect, Size};
23use crate::layout_props::{HAnchor, Insets, VAnchor, WidgetBase};
24use crate::text::Font;
25use crate::widget::{InspectorNode, InspectorOverlay, Widget};
26use crate::widgets::tree_view::TreeView;
27
28struct InternalPresenceNode {
44 bounds: Rect,
45 children: Vec<Box<dyn Widget>>,
46 base: WidgetBase,
47 name: &'static str,
48}
49
50impl Widget for InternalPresenceNode {
51 fn type_name(&self) -> &'static str {
52 self.name
53 }
54 fn bounds(&self) -> Rect {
55 self.bounds
56 }
57 fn set_bounds(&mut self, b: Rect) {
58 self.bounds = b;
59 }
60 fn children(&self) -> &[Box<dyn Widget>] {
61 &self.children
62 }
63 fn children_mut(&mut self) -> &mut Vec<Box<dyn Widget>> {
64 &mut self.children
65 }
66 fn margin(&self) -> Insets {
67 self.base.margin
68 }
69 fn widget_base(&self) -> Option<&WidgetBase> {
70 Some(&self.base)
71 }
72 fn widget_base_mut(&mut self) -> Option<&mut WidgetBase> {
73 Some(&mut self.base)
74 }
75 fn h_anchor(&self) -> HAnchor {
76 self.base.h_anchor
77 }
78 fn v_anchor(&self) -> VAnchor {
79 self.base.v_anchor
80 }
81 fn min_size(&self) -> Size {
82 self.base.min_size
83 }
84 fn max_size(&self) -> Size {
85 self.base.max_size
86 }
87 fn layout(&mut self, _: Size) -> Size {
88 Size::new(self.bounds.width, self.bounds.height)
89 }
90 fn paint(&mut self, _: &mut dyn DrawCtx) {}
91 fn hit_test(&self, _: Point) -> bool {
92 false
93 }
94 fn on_event(&mut self, _: &Event) -> EventResult {
95 EventResult::Ignored
96 }
97 fn contributes_children_to_inspector(&self) -> bool {
98 false
99 }
100}
101
102const DEFAULT_PROPS_H: f64 = 180.0;
104pub(super) const FONT_SIZE: f64 = 12.0;
105const HEADER_H: f64 = 30.0;
106const SPLIT_HIT: f64 = 5.0;
107const MIN_PROPS_H: f64 = 60.0;
108const MIN_TREE_H: f64 = 60.0;
109
110fn c_panel_bg(v: &crate::theme::Visuals) -> Color {
114 v.panel_fill
115}
116fn c_header_bg(v: &crate::theme::Visuals) -> Color {
117 let f = if is_dark(v) { 0.80 } else { 0.94 };
119 Color::rgba(
120 v.panel_fill.r * f,
121 v.panel_fill.g * f,
122 v.panel_fill.b * f,
123 1.0,
124 )
125}
126fn c_props_bg(v: &crate::theme::Visuals) -> Color {
127 v.window_fill
128}
129fn c_split_bg(v: &crate::theme::Visuals) -> Color {
130 let t = if is_dark(v) { 1.0 } else { 0.0 };
131 Color::rgba(t, t, t, 0.10)
132}
133pub(super) fn c_border(v: &crate::theme::Visuals) -> Color {
134 v.separator
135}
136pub(super) fn c_text(v: &crate::theme::Visuals) -> Color {
137 v.text_color
138}
139pub(super) fn c_dim_text(v: &crate::theme::Visuals) -> Color {
140 v.text_dim
141}
142
143fn is_dark(v: &crate::theme::Visuals) -> bool {
144 let lum = 0.299 * v.panel_fill.r + 0.587 * v.panel_fill.g + 0.114 * v.panel_fill.b;
146 lum < 0.5
147}
148
149fn translate_event(event: &Event, offset_y: f64) -> Event {
154 match event {
155 Event::MouseDown {
156 pos,
157 button,
158 modifiers,
159 } => Event::MouseDown {
160 pos: Point::new(pos.x, pos.y - offset_y),
161 button: *button,
162 modifiers: *modifiers,
163 },
164 Event::MouseMove { pos } => Event::MouseMove {
165 pos: Point::new(pos.x, pos.y - offset_y),
166 },
167 Event::MouseUp {
168 pos,
169 button,
170 modifiers,
171 } => Event::MouseUp {
172 pos: Point::new(pos.x, pos.y - offset_y),
173 button: *button,
174 modifiers: *modifiers,
175 },
176 Event::MouseWheel {
177 pos,
178 delta_y,
179 delta_x,
180 modifiers,
181 } => Event::MouseWheel {
182 pos: Point::new(pos.x, pos.y - offset_y),
183 delta_y: *delta_y,
184 delta_x: *delta_x,
185 modifiers: *modifiers,
186 },
187 other => other.clone(),
188 }
189}
190
191pub struct InspectorPanel {
194 bounds: Rect,
195 _children: Vec<Box<dyn Widget>>,
199 base: WidgetBase,
200 font: Arc<Font>,
201 nodes: Rc<RefCell<Vec<InspectorNode>>>,
202 selected: Option<usize>,
204 props_h: f64,
205 split_dragging: bool,
206 pub hovered_bounds: Rc<RefCell<Option<InspectorOverlay>>>,
208 pub(crate) tree_view: TreeView,
210 pending_expanded: Option<Vec<bool>>,
214 pending_selected: Option<Option<usize>>,
215 snapshot_out: Option<Rc<RefCell<Option<InspectorSavedState>>>>,
219 #[cfg(feature = "reflect")]
225 pub edits: Option<Rc<RefCell<Vec<crate::widget::InspectorEdit>>>>,
226 pub base_edits: Option<Rc<RefCell<Vec<crate::widget::WidgetBaseEdit>>>>,
229 prop_hits: Vec<PropHit>,
233 last_inspector_nodes_fingerprint: Option<(usize, usize)>,
240}
241
242#[derive(Clone, Debug)]
243#[cfg_attr(not(feature = "reflect"), allow(dead_code))]
244pub(super) struct PropHit {
245 pub(super) rect: Rect,
246 pub(super) field: String,
247 pub(super) kind: PropHitKind,
248}
249
250#[derive(Clone, Debug)]
251pub(super) enum PropHitKind {
252 #[cfg_attr(not(feature = "reflect"), allow(dead_code))]
254 BoolToggle { current: bool },
255 #[cfg_attr(not(feature = "reflect"), allow(dead_code))]
257 NumericStep { current: f64, step: f64 },
258 InsetField {
260 target: InsetsTarget,
261 side: InsetsSide,
262 current: f64,
263 step: f64,
264 },
265 HAnchorCycle { current_bits: u8 },
267 VAnchorCycle { current_bits: u8 },
269}
270
271#[derive(Clone, Copy, Debug)]
273pub(super) enum InsetsTarget {
274 Margin,
275 }
277
278#[derive(Clone, Copy, Debug)]
280pub(super) enum InsetsSide {
281 Left,
282 Right,
283 Top,
284 Bottom,
285}
286
287#[derive(Clone, Debug, Default)]
289#[cfg_attr(feature = "reflect", derive(bevy_reflect::Reflect))]
290pub struct InspectorSavedState {
291 pub expanded: Vec<bool>,
292 pub selected: Option<usize>,
293 pub props_h: f64,
294}
295
296impl InspectorPanel {
297 pub fn new(
298 font: Arc<Font>,
299 nodes: Rc<RefCell<Vec<InspectorNode>>>,
300 hovered_bounds: Rc<RefCell<Option<InspectorOverlay>>>,
301 ) -> Self {
302 let tree_view = TreeView::new(Arc::clone(&font))
303 .with_row_height(20.0)
304 .with_font_size(12.0)
305 .with_indent_width(14.0)
306 .with_hover_repaint(false);
307 Self {
308 bounds: Rect::default(),
309 _children: vec![Box::new(InternalPresenceNode {
310 bounds: Rect::default(),
311 children: Vec::new(),
312 base: WidgetBase::new(),
313 name: "TreeView",
314 })],
315 base: WidgetBase::new(),
316 font,
317 nodes,
318 selected: None,
319 props_h: DEFAULT_PROPS_H,
320 split_dragging: false,
321 hovered_bounds,
322 tree_view,
323 #[cfg(feature = "reflect")]
324 edits: None,
325 base_edits: None,
326 prop_hits: Vec::new(),
327 pending_expanded: None,
328 pending_selected: None,
329 snapshot_out: None,
330 last_inspector_nodes_fingerprint: None,
331 }
332 }
333
334 pub fn with_snapshot_cell(mut self, cell: Rc<RefCell<Option<InspectorSavedState>>>) -> Self {
338 self.snapshot_out = Some(cell);
339 self
340 }
341
342 pub fn with_base_edit_queue(
347 mut self,
348 cell: Rc<RefCell<Vec<crate::widget::WidgetBaseEdit>>>,
349 ) -> Self {
350 self.base_edits = Some(cell);
351 self
352 }
353
354 #[cfg(feature = "reflect")]
360 pub fn with_edit_queue(
361 mut self,
362 cell: Rc<RefCell<Vec<crate::widget::InspectorEdit>>>,
363 ) -> Self {
364 self.edits = Some(cell);
365 self
366 }
367
368 pub fn saved_state(&self) -> InspectorSavedState {
379 InspectorSavedState {
380 expanded: self.tree_view.nodes.iter().map(|n| n.is_expanded).collect(),
381 selected: self.tree_view.nodes.iter().position(|n| n.is_selected),
382 props_h: self.props_h,
383 }
384 }
385
386 pub fn apply_saved_state(&mut self, s: InspectorSavedState) {
391 self.pending_expanded = Some(s.expanded);
392 self.pending_selected = Some(s.selected);
393 self.props_h = s.props_h.clamp(MIN_PROPS_H, 1024.0);
394 }
395
396 fn list_area_h(&self) -> f64 {
400 (self.bounds.height - HEADER_H).max(0.0)
401 }
402
403 fn split_y(&self) -> f64 {
405 self.props_h.clamp(
406 MIN_PROPS_H,
407 (self.list_area_h() - MIN_TREE_H).max(MIN_PROPS_H),
408 )
409 }
410
411 fn tree_origin_y(&self) -> f64 {
413 self.split_y() + 4.0
414 }
415
416 fn on_split_handle(&self, pos: Point) -> bool {
417 let sy = self.split_y();
418 pos.y >= sy - SPLIT_HIT && pos.y <= sy + SPLIT_HIT
419 }
420
421 fn pos_in_tree_area(&self, pos: Point) -> bool {
422 let tree_bot = self.tree_origin_y();
423 let tree_top = self.list_area_h();
424 pos.y >= tree_bot && pos.y <= tree_top
425 }
426
427 fn forward_to_tree(&mut self, event: &Event) -> EventResult {
429 let offset_y = self.tree_view.bounds().y;
433 let translated = translate_event(event, offset_y);
434 self.tree_view.on_event(&translated)
435 }
436
437 fn update_hovered_bounds_from_tree(&self) {
438 let nodes = self.nodes.borrow();
439 let next = self
440 .tree_view
441 .hovered_node_idx()
442 .and_then(|i| nodes.get(i))
443 .map(|n| InspectorOverlay {
444 bounds: n.screen_bounds,
445 margin: n.margin,
446 padding: n.padding,
447 });
448 let mut hovered = self.hovered_bounds.borrow_mut();
449 if *hovered != next {
450 *hovered = next;
451 crate::animation::request_draw_without_invalidation();
452 }
453 }
454}
455
456fn next_h_anchor(bits: u8) -> HAnchor {
459 if bits == HAnchor::FIT.bits() { HAnchor::STRETCH }
461 else if bits == HAnchor::STRETCH.bits() { HAnchor::LEFT }
462 else if bits == HAnchor::LEFT.bits() { HAnchor::CENTER }
463 else if bits == HAnchor::CENTER.bits() { HAnchor::RIGHT }
464 else { HAnchor::FIT }
465}
466
467fn next_v_anchor(bits: u8) -> VAnchor {
468 if bits == VAnchor::FIT.bits() { VAnchor::STRETCH }
470 else if bits == VAnchor::STRETCH.bits() { VAnchor::BOTTOM }
471 else if bits == VAnchor::BOTTOM.bits() { VAnchor::CENTER }
472 else if bits == VAnchor::CENTER.bits() { VAnchor::TOP }
473 else { VAnchor::FIT }
474}
475
476impl InspectorPanel {
479 pub fn with_margin(mut self, m: Insets) -> Self {
480 self.base.margin = m;
481 self
482 }
483 pub fn with_h_anchor(mut self, h: HAnchor) -> Self {
484 self.base.h_anchor = h;
485 self
486 }
487 pub fn with_v_anchor(mut self, v: VAnchor) -> Self {
488 self.base.v_anchor = v;
489 self
490 }
491 pub fn with_min_size(mut self, s: Size) -> Self {
492 self.base.min_size = s;
493 self
494 }
495 pub fn with_max_size(mut self, s: Size) -> Self {
496 self.base.max_size = s;
497 self
498 }
499}
500
501impl InspectorPanel {
506 fn paint_properties(&mut self, ctx: &mut dyn DrawCtx, available_h: f64) {
507 let panel_y_offset = 0.0; self.prop_hits.clear();
509 super::inspector_props::paint_properties(
510 ctx,
511 available_h,
512 panel_y_offset,
513 self.bounds.width,
514 &self.font,
515 self.selected,
516 &self.nodes.borrow(),
517 &mut self.prop_hits,
518 );
519 }
520
521 fn try_emit_base_edit_from_click(&self, pos: Point) -> bool {
525 let Some(queue) = &self.base_edits else {
526 return false;
527 };
528 let Some(sel_idx) = self.selected else {
529 return false;
530 };
531 let nodes = self.nodes.borrow();
532 let Some(node) = nodes.get(sel_idx) else {
533 return false;
534 };
535 let Some(hit) = self.prop_hits.iter().find(|h| {
536 pos.x >= h.rect.x
537 && pos.x <= h.rect.x + h.rect.width
538 && pos.y >= h.rect.y
539 && pos.y <= h.rect.y + h.rect.height
540 }) else {
541 return false;
542 };
543 let field = match &hit.kind {
544 PropHitKind::InsetField {
545 target: InsetsTarget::Margin,
546 side,
547 current,
548 step,
549 } => {
550 let mid = hit.rect.x + hit.rect.width * 0.5;
551 let new_v = (if pos.x < mid {
552 *current - *step
553 } else {
554 *current + *step
555 })
556 .max(0.0);
557 match side {
558 InsetsSide::Left => crate::widget::WidgetBaseField::MarginLeft(new_v),
559 InsetsSide::Right => crate::widget::WidgetBaseField::MarginRight(new_v),
560 InsetsSide::Top => crate::widget::WidgetBaseField::MarginTop(new_v),
561 InsetsSide::Bottom => crate::widget::WidgetBaseField::MarginBottom(new_v),
562 }
563 }
564 PropHitKind::HAnchorCycle { current_bits } => {
565 crate::widget::WidgetBaseField::HAnchor(next_h_anchor(*current_bits))
566 }
567 PropHitKind::VAnchorCycle { current_bits } => {
568 crate::widget::WidgetBaseField::VAnchor(next_v_anchor(*current_bits))
569 }
570 _ => return false,
571 };
572 queue.borrow_mut().push(crate::widget::WidgetBaseEdit {
573 path: node.path.clone(),
574 field,
575 });
576 crate::animation::request_draw();
577 true
578 }
579
580 #[cfg(feature = "reflect")]
583 fn try_emit_edit_from_click(&self, pos: Point) -> bool {
584 let Some(queue) = &self.edits else { return false };
585 let Some(sel_idx) = self.selected else {
586 return false;
587 };
588 let nodes = self.nodes.borrow();
589 let Some(node) = nodes.get(sel_idx) else {
590 return false;
591 };
592 let Some(hit) = self
593 .prop_hits
594 .iter()
595 .find(|h| pos.x >= h.rect.x
596 && pos.x <= h.rect.x + h.rect.width
597 && pos.y >= h.rect.y
598 && pos.y <= h.rect.y + h.rect.height)
599 else {
600 return false;
601 };
602
603 let edit = match &hit.kind {
604 PropHitKind::BoolToggle { current } => crate::widget::InspectorEdit {
605 path: node.path.clone(),
606 field_path: hit.field.clone(),
607 new_value: Box::new(!*current),
608 },
609 PropHitKind::NumericStep { current, step } => {
610 let mid = hit.rect.x + hit.rect.width * 0.5;
611 let new_v = if pos.x < mid {
612 *current - *step
613 } else {
614 *current + *step
615 };
616 crate::widget::InspectorEdit {
617 path: node.path.clone(),
618 field_path: hit.field.clone(),
619 new_value: Box::new(new_v),
620 }
621 }
622 _ => return false,
623 };
624 queue.borrow_mut().push(edit);
625 crate::animation::request_draw();
626 true
627 }
628}
629