pub struct Context { /* private fields */ }Expand description
An imgui context.
A context needs to be created to access most library functions. Due to current Dear ImGui design choices, at most one active Context can exist at any time. This limitation will likely be removed in a future Dear ImGui version.
If you need more than one context, you can use suspended contexts. As long as only one context is active at a time, it’s possible to have multiple independent contexts.
§Examples
Creating a new active context:
let ctx = dear_imgui_rs::Context::create();
// ctx is dropped naturally when it goes out of scope, which deactivates and destroys the
// contextNever try to create an active context when another one is active:
let ctx1 = dear_imgui_rs::Context::create();
let ctx2 = dear_imgui_rs::Context::create(); // PANICImplementations§
Source§impl Context
impl Context
Sourcepub fn try_create() -> Result<Context, ImGuiError>
pub fn try_create() -> Result<Context, ImGuiError>
Tries to create a new active Dear ImGui context.
Returns an error if another context is already active or creation fails.
Tries to create a new active Dear ImGui context with a shared font atlas.
Sourcepub fn create() -> Context
pub fn create() -> Context
Creates a new active Dear ImGui context (panics on error).
This aligns with imgui-rs behavior. For fallible creation use try_create().
Creates a new active Dear ImGui context with a shared font atlas (panics on error).
Sourcepub fn as_raw(&self) -> *mut ImGuiContext
pub fn as_raw(&self) -> *mut ImGuiContext
Returns the raw ImGuiContext* for FFI integrations.
Sourcepub fn alive_token(&self) -> ContextAliveToken
pub fn alive_token(&self) -> ContextAliveToken
Returns a token that can be used to check whether this context is still alive.
Useful for extension crates that store raw pointers and need to avoid calling into FFI
after the owning Context has been dropped.
Sourcepub fn io_mut(&mut self) -> &mut Io
pub fn io_mut(&mut self) -> &mut Io
Returns a mutable reference to the active context’s IO object
Sourcepub fn frame(&mut self) -> &mut Ui
pub fn frame(&mut self) -> &mut Ui
Creates a new frame and returns a Ui object for building the interface
Sourcepub fn frame_with<F, R>(&mut self, f: F) -> R
pub fn frame_with<F, R>(&mut self, f: F) -> R
Create a new frame with a callback
Sourcepub fn render(&mut self) -> &DrawData
pub fn render(&mut self) -> &DrawData
Renders the frame and returns a reference to the resulting draw data
This finalizes the Dear ImGui frame and prepares all draw data for rendering. The returned draw data contains all the information needed to render the frame.
Sourcepub fn draw_data(&self) -> Option<&DrawData>
pub fn draw_data(&self) -> Option<&DrawData>
Gets the draw data for the current frame
This returns the draw data without calling render. Only valid after
render() has been called and before the next new_frame().
Sourcepub fn register_user_texture(&mut self, texture: &mut TextureData)
pub fn register_user_texture(&mut self, texture: &mut TextureData)
Register a user-created texture in ImGui’s global texture list (ImGui 1.92+).
Dear ImGui builds DrawData::textures() from its internal PlatformIO.Textures[] list.
If you create a TextureData yourself (e.g. OwnedTextureData::new()), you must register
it for renderer backends (with BackendFlags::RENDERER_HAS_TEXTURES) to receive
Create/Update/Destroy requests automatically.
Note: RegisterUserTexture() is currently an experimental ImGui API.
§Safety & Lifetime
The underlying ImTextureData must remain alive and registered until you call
unregister_user_texture(). Unregister before dropping the texture to avoid leaving a
dangling pointer inside ImGui.
Sourcepub fn register_user_texture_token(
&mut self,
texture: &mut TextureData,
) -> RegisteredUserTexture
pub fn register_user_texture_token( &mut self, texture: &mut TextureData, ) -> RegisteredUserTexture
Register a user-created texture and return an RAII token which unregisters on drop.
This is a convenience wrapper around register_user_texture().
§Safety & Drop Ordering
The returned token must be dropped before the underlying ImGuiContext is destroyed.
If you store it in a struct alongside Context, ensure the token is dropped first.
Sourcepub fn unregister_user_texture(&mut self, texture: &mut TextureData)
pub fn unregister_user_texture(&mut self, texture: &mut TextureData)
Unregister a user texture previously registered with register_user_texture().
This removes the ImTextureData* from ImGui’s internal texture list.
Sourcepub fn set_ini_filename<P>(
&mut self,
filename: Option<P>,
) -> Result<(), ImGuiError>
pub fn set_ini_filename<P>( &mut self, filename: Option<P>, ) -> Result<(), ImGuiError>
Sets the INI filename for settings persistence
§Errors
Returns an error if the filename contains null bytes
Sourcepub fn set_log_filename<P>(
&mut self,
filename: Option<P>,
) -> Result<(), ImGuiError>
pub fn set_log_filename<P>( &mut self, filename: Option<P>, ) -> Result<(), ImGuiError>
Sourcepub fn set_platform_name<S>(
&mut self,
name: Option<S>,
) -> Result<(), ImGuiError>
pub fn set_platform_name<S>( &mut self, name: Option<S>, ) -> Result<(), ImGuiError>
Sourcepub fn set_renderer_name<S>(
&mut self,
name: Option<S>,
) -> Result<(), ImGuiError>
pub fn set_renderer_name<S>( &mut self, name: Option<S>, ) -> Result<(), ImGuiError>
Sourcepub fn platform_io_mut(&mut self) -> &mut PlatformIo
pub fn platform_io_mut(&mut self) -> &mut PlatformIo
Get mutable access to the platform IO.
Note: ImGuiPlatformIO exists even when multi-viewport is disabled. We expose it
unconditionally so callers can use ImGui 1.92+ texture management via PlatformIO.Textures[].
Sourcepub fn main_viewport(&mut self) -> &Viewport
pub fn main_viewport(&mut self) -> &Viewport
Returns a reference to the main Dear ImGui viewport.
The returned reference is owned by the currently active ImGui context and must not be used after the context is destroyed.
Sourcepub fn suspend(self) -> SuspendedContext
pub fn suspend(self) -> SuspendedContext
Suspends this context so another context can be the active context
Sourcepub fn pop_font(&mut self)
pub fn pop_font(&mut self)
Pop a font from the font stack
This restores the previous font. Must be paired with a call to push_font().
Sourcepub fn current_font(&self) -> &Font
pub fn current_font(&self) -> &Font
Get the current font
Sourcepub fn current_font_size(&self) -> f32
pub fn current_font_size(&self) -> f32
Get the current font size
Sourcepub fn font_atlas(&self) -> FontAtlas
pub fn font_atlas(&self) -> FontAtlas
Get the font atlas from the IO structure
Sourcepub fn font_atlas_mut(&mut self) -> FontAtlas
pub fn font_atlas_mut(&mut self) -> FontAtlas
Get a mutable reference to the font atlas from the IO structure
Sourcepub fn fonts(&mut self) -> FontAtlas
pub fn fonts(&mut self) -> FontAtlas
Returns the font atlas (alias for font_atlas_mut)
This provides compatibility with imgui-rs naming convention
Attempts to clone the interior shared font atlas if it exists.
Sourcepub fn load_ini_settings(&mut self, data: &str)
pub fn load_ini_settings(&mut self, data: &str)
Loads settings from a string slice containing settings in .Ini file format
Sourcepub fn save_ini_settings(&mut self, buf: &mut String)
pub fn save_ini_settings(&mut self, buf: &mut String)
Saves settings to a mutable string buffer in .Ini file format
Sourcepub fn load_ini_settings_from_disk<P>(
&mut self,
filename: P,
) -> Result<(), ImGuiError>
pub fn load_ini_settings_from_disk<P>( &mut self, filename: P, ) -> Result<(), ImGuiError>
Loads settings from a .ini file on disk.
This is a convenience wrapper over ImGui::LoadIniSettingsFromDisk.
Note: this is not available on wasm32 targets.
Sourcepub fn save_ini_settings_to_disk<P>(
&mut self,
filename: P,
) -> Result<(), ImGuiError>
pub fn save_ini_settings_to_disk<P>( &mut self, filename: P, ) -> Result<(), ImGuiError>
Saves settings to a .ini file on disk.
This is a convenience wrapper over ImGui::SaveIniSettingsToDisk.
Note: this is not available on wasm32 targets.
Sourcepub fn clipboard_text(&self) -> Option<String>
pub fn clipboard_text(&self) -> Option<String>
Returns the current clipboard text, if available.
This calls Dear ImGui’s clipboard callbacks (configured via
Context::set_clipboard_backend). When no backend is installed, this returns None.
Note: returned data is copied into a new String.
Sourcepub fn set_clipboard_text(&self, text: impl AsRef<str>)
pub fn set_clipboard_text(&self, text: impl AsRef<str>)
Sets the clipboard text.
This calls Dear ImGui’s clipboard callbacks (configured via
Context::set_clipboard_backend). If no backend is installed, this is a no-op.
Interior NUL bytes are sanitized to ? to match other scratch-string helpers.
Sourcepub fn set_clipboard_backend<T>(&mut self, backend: T)where
T: ClipboardBackend,
pub fn set_clipboard_backend<T>(&mut self, backend: T)where
T: ClipboardBackend,
Sets the clipboard backend used for clipboard operations