Skip to main content

ferroui_base/
visual_tree_attachment_event_args.rs

1use crate::rendering::IPresentationSource;
2use crate::{Ref, Visual};
3use std::rc::Rc;
4
5/// Holds the event arguments for the `attached_to_visual_tree` and
6/// `detached_from_visual_tree` events.
7#[derive(Clone)]
8pub struct VisualTreeAttachmentEventArgs {
9    attachment_point: Option<Ref<Visual>>,
10    presentation_source: Rc<dyn IPresentationSource>,
11    root_visual: Ref<Visual>,
12}
13
14impl VisualTreeAttachmentEventArgs {
15    /// Panics if the presentation source has no root visual.
16    pub fn new(attachment_point: Option<Ref<Visual>>, presentation_source: Rc<dyn IPresentationSource>) -> Self {
17        let root_visual =
18            presentation_source.root_visual().expect("PresentationSource must have a non-null RootVisual.");
19        Self { attachment_point, presentation_source, root_visual }
20    }
21
22    /// The parent that the visual's tree is being attached to or detached
23    /// from. `None` when the presentation source's root visual itself is
24    /// being attached or detached.
25    pub fn attachment_point(&self) -> Option<&Ref<Visual>> {
26        self.attachment_point.as_ref()
27    }
28
29    /// The parent that the visual's tree is being attached to or detached
30    /// from.
31    #[deprecated(note = "Use attachment_point")]
32    pub fn parent(&self) -> Option<&Ref<Visual>> {
33        self.attachment_point()
34    }
35
36    /// The presentation source that the visual is being attached to or
37    /// detached from.
38    pub fn presentation_source(&self) -> &Rc<dyn IPresentationSource> {
39        &self.presentation_source
40    }
41
42    /// The root visual of the tree.
43    #[deprecated(
44        note = "This was previously always returning TopLevel. This is no longer guaranteed. Use TopLevel::get_top_level(visual) if you need a TopLevel or root_visual if you are interested in the root of the visual tree."
45    )]
46    pub fn root(&self) -> &Ref<Visual> {
47        &self.root_visual
48    }
49
50    /// The root visual of the tree this visual is being attached to or
51    /// detached from: the root visual of the presentation source.
52    pub fn root_visual(&self) -> &Ref<Visual> {
53        &self.root_visual
54    }
55
56    pub fn set_root_visual(&mut self, value: Ref<Visual>) {
57        self.root_visual = value;
58    }
59}
60
61/// Copies compare equal when they describe the same attachment (the same
62/// visuals and presentation source).
63impl PartialEq for VisualTreeAttachmentEventArgs {
64    fn eq(&self, other: &Self) -> bool {
65        self.attachment_point == other.attachment_point
66            && self.root_visual == other.root_visual
67            && std::ptr::addr_eq(Rc::as_ptr(&self.presentation_source), Rc::as_ptr(&other.presentation_source))
68    }
69}