Skip to main content

dear_imnodes/context/
tokens.rs

1use crate::sys;
2
3use super::{Context, ImNodesScope};
4
5/// RAII token for a node block
6pub struct NodeToken<'a> {
7    pub(super) scope: ImNodesScope,
8    pub(crate) _phantom: std::marker::PhantomData<&'a Context>,
9}
10
11impl NodeToken<'_> {
12    pub fn title_bar<F: FnOnce()>(&self, f: F) {
13        struct TitleBarToken {
14            scope: ImNodesScope,
15        }
16
17        impl Drop for TitleBarToken {
18            fn drop(&mut self) {
19                unsafe {
20                    self.scope.bind();
21                    sys::imnodes_EndNodeTitleBar();
22                }
23            }
24        }
25
26        unsafe {
27            self.scope.bind();
28            sys::imnodes_BeginNodeTitleBar();
29        }
30        let _title_bar = TitleBarToken {
31            scope: self.scope.clone(),
32        };
33        f();
34    }
35
36    pub fn end(self) {}
37}
38
39impl Drop for NodeToken<'_> {
40    fn drop(&mut self) {
41        unsafe {
42            self.scope.bind();
43            sys::imnodes_EndNode();
44        }
45    }
46}
47
48pub(crate) enum AttrKind {
49    Input,
50    Output,
51    Static,
52}
53
54pub struct AttributeToken<'a> {
55    pub(crate) kind: AttrKind,
56    pub(super) scope: ImNodesScope,
57    pub(crate) _phantom: std::marker::PhantomData<&'a Context>,
58}
59
60impl AttributeToken<'_> {
61    pub fn end(self) {}
62}
63
64impl Drop for AttributeToken<'_> {
65    fn drop(&mut self) {
66        unsafe {
67            self.scope.bind();
68            match self.kind {
69                AttrKind::Input => sys::imnodes_EndInputAttribute(),
70                AttrKind::Output => sys::imnodes_EndOutputAttribute(),
71                AttrKind::Static => sys::imnodes_EndStaticAttribute(),
72            }
73        }
74    }
75}