Skip to main content

Context

Struct Context 

Source
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
// context

Never 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(); // PANIC

Implementations§

Source§

impl Context

Source

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.

Source

pub fn try_create_with_shared_font_atlas( shared_font_atlas: SharedFontAtlas, ) -> Result<Context, ImGuiError>

Tries to create a new active Dear ImGui context with a shared font atlas.

Source

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().

Source

pub fn create_with_shared_font_atlas( shared_font_atlas: SharedFontAtlas, ) -> Context

Creates a new active Dear ImGui context with a shared font atlas (panics on error).

Source

pub fn as_raw(&self) -> *mut ImGuiContext

Returns the raw ImGuiContext* for FFI integrations.

Source

pub fn io_mut(&mut self) -> &mut Io

Returns a mutable reference to the active context’s IO object

Source

pub fn io(&self) -> &Io

Get access to the IO structure

Source

pub fn style(&self) -> &Style

Get access to the Style structure

Source

pub fn style_mut(&mut self) -> &mut Style

Get mutable access to the Style structure

Source

pub fn frame(&mut self) -> &mut Ui

Creates a new frame and returns a Ui object for building the interface

Source

pub fn frame_with<F, R>(&mut self, f: F) -> R
where F: FnOnce(&Ui) -> R,

Create a new frame with a callback

Source

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.

Source

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().

Source

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.

Source

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.

Source

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.

Source

pub fn set_ini_filename<P>( &mut self, filename: Option<P>, ) -> Result<(), ImGuiError>
where P: Into<PathBuf>,

Sets the INI filename for settings persistence

§Errors

Returns an error if the filename contains null bytes

Source

pub fn set_log_filename<P>( &mut self, filename: Option<P>, ) -> Result<(), ImGuiError>
where P: Into<PathBuf>,

Sets the log filename

§Errors

Returns an error if the filename contains null bytes

Source

pub fn set_platform_name<S>( &mut self, name: Option<S>, ) -> Result<(), ImGuiError>
where S: Into<String>,

Sets the platform name

§Errors

Returns an error if the name contains null bytes

Source

pub fn set_renderer_name<S>( &mut self, name: Option<S>, ) -> Result<(), ImGuiError>
where S: Into<String>,

Sets the renderer name

§Errors

Returns an error if the name contains null bytes

Source

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[].

Source

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.

Source

pub fn suspend(self) -> SuspendedContext

Suspends this context so another context can be the active context

Source

pub fn push_font(&mut self, font: &Font)

Push a font onto the font stack

Source

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().

Source

pub fn current_font(&self) -> &Font

Get the current font

Source

pub fn current_font_size(&self) -> f32

Get the current font size

Source

pub fn font_atlas(&self) -> FontAtlas

Get the font atlas from the IO structure

Source

pub fn font_atlas_mut(&mut self) -> FontAtlas

Get a mutable reference to the font atlas from the IO structure

Source

pub fn fonts(&mut self) -> FontAtlas

Returns the font atlas (alias for font_atlas_mut)

This provides compatibility with imgui-rs naming convention

Source

pub fn clone_shared_font_atlas(&mut self) -> Option<SharedFontAtlas>

Attempts to clone the interior shared font atlas if it exists.

Source

pub fn load_ini_settings(&mut self, data: &str)

Loads settings from a string slice containing settings in .Ini file format

Source

pub fn save_ini_settings(&mut self, buf: &mut String)

Saves settings to a mutable string buffer in .Ini file format

Source

pub fn load_ini_settings_from_disk<P>( &mut self, filename: P, ) -> Result<(), ImGuiError>
where P: Into<PathBuf>,

Loads settings from a .ini file on disk.

This is a convenience wrapper over ImGui::LoadIniSettingsFromDisk.

Note: this is not available on wasm32 targets.

Source

pub fn save_ini_settings_to_disk<P>( &mut self, filename: P, ) -> Result<(), ImGuiError>
where P: Into<PathBuf>,

Saves settings to a .ini file on disk.

This is a convenience wrapper over ImGui::SaveIniSettingsToDisk.

Note: this is not available on wasm32 targets.

Source

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.

Source

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.

Source

pub fn set_clipboard_backend<T>(&mut self, backend: T)

Sets the clipboard backend used for clipboard operations

Trait Implementations§

Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Drop for Context

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more