Skip to main content

dear_imnodes/context/
ui.rs

1use super::{Context, EditorContext, ImNodesScope, NodeEditor, NodesUi};
2use dear_imgui_rs::Ui;
3use dear_imgui_rs::sys as imgui_sys;
4
5impl<'ui> NodesUi<'ui> {
6    pub(crate) fn new(ui: &'ui Ui, ctx: &'ui Context) -> Self {
7        // Keep ImNodes bound to the ImGui context this ImNodes context was created with.
8        // This avoids accidental cross-context use when users manage multiple ImGui contexts.
9        assert_eq!(
10            unsafe { imgui_sys::igGetCurrentContext() },
11            ctx.imgui_ctx_raw,
12            "dear-imnodes: NodesUi must be used with the currently-active ImGui context"
13        );
14        let scope = ImNodesScope {
15            imgui_ctx_raw: ctx.imgui_ctx_raw,
16            imgui_alive: ctx.imgui_alive.clone(),
17            ctx_raw: ctx.raw,
18            editor_raw: None,
19        };
20        scope.bind();
21        Self { _ui: ui, _ctx: ctx }
22    }
23
24    /// Begin a node editor with an optional EditorContext
25    pub fn editor(&self, editor: Option<&'ui EditorContext>) -> NodeEditor<'ui> {
26        if let Some(editor) = editor {
27            if let Some(bound) = editor.bound_ctx_raw {
28                assert_eq!(
29                    bound, self._ctx.raw,
30                    "dear-imnodes: EditorContext is bound to a different ImNodes context"
31                );
32            }
33            if let Some(bound) = editor.bound_imgui_ctx_raw {
34                assert_eq!(
35                    bound, self._ctx.imgui_ctx_raw,
36                    "dear-imnodes: EditorContext is bound to a different ImGui context"
37                );
38            }
39        }
40        NodeEditor::begin(self._ui, self._ctx, editor)
41    }
42}