Skip to main content

UiSystem

Struct UiSystem 

Source
pub struct UiSystem { /* private fields */ }
Expand description

Main UI system managing tree, layout, rendering, and events.

This wraps UiCore and adds rendering capabilities.

Implementations§

Source§

impl UiSystem

Source

pub fn new(context: Arc<GraphicsContext>) -> Self

Create a new UI system with default configuration (no depth testing).

Warning: This creates a renderer without depth testing. If your render pass has a depth attachment, use from_window instead to ensure pipeline-renderpass compatibility.

§Example
// For simple use without depth testing
let graphics = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
let ui = UiSystem::new(graphics);
Source

pub fn from_window(context: Arc<GraphicsContext>, window: &RenderWindow) -> Self

Create a new UI system configured for a specific window.

This is the recommended constructor as it ensures the renderer’s pipelines are compatible with the window’s render pass configuration (surface format and depth format).

§Example
let graphics = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");

// Create window with depth buffer
let window = RenderWindowBuilder::new()
    .with_depth_default()
    .build(winit_window, graphics.clone())
    .expect("Failed to create window");

// UI automatically uses matching formats
let ui = UiSystem::from_window(graphics, &window);
Source

pub fn with_descriptor( context: Arc<GraphicsContext>, descriptor: UiRendererDescriptor, ) -> Self

Create a new UI system with explicit configuration.

Use this when you need full control over the renderer configuration, or when the target is not a RenderWindow.

§Example
let graphics = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");

let desc = UiRendererDescriptor {
    name: "Game HUD".to_string(),
    surface_format: wgpu::TextureFormat::Bgra8UnormSrgb,
    depth_format: Some(wgpu::TextureFormat::Depth32Float),
};

let ui = UiSystem::with_descriptor(graphics, desc);
Source

pub fn build<F>(&mut self, build_fn: F)
where F: FnOnce(&mut UiBuilder<'_>),

Build the UI tree using a declarative builder API.

Note: This does a full rebuild. For incremental updates, use update methods.

Source

pub fn update(&mut self, delta_time: f32)

Update UI state (animations, hover, etc.).

Note: This no longer marks the entire tree dirty - only changed widgets are marked.

Source

pub fn set_viewport(&mut self, viewport: Viewport)

Set the viewport size for layout calculations.

Source

pub fn handle_events(&mut self, events: &mut EventBatch)

Handle events from the event batch.

Source

pub fn compute_layout(&mut self)

Compute layout for all widgets.

Source

pub fn get_node_id(&self, widget_id: WidgetId) -> Option<NodeId>

Get the node ID for a widget ID.

Source

pub fn register_widget(&mut self, widget_id: WidgetId, node_id: NodeId)

Register a widget ID to node ID mapping.

Source

pub fn update_text( &mut self, widget_id: WidgetId, new_content: impl Into<String>, ) -> bool

Update text content of a Text widget by ID with automatic dirty marking.

This is much faster than rebuilding the entire UI tree. Returns true if the content changed.

§Example
let counter_id = WidgetId::new("counter");
ui.update_text(counter_id, "Count: 42");
Source

pub fn text_cache_stats(&self) -> String

Get text cache statistics from the renderer.

Source

pub fn text_cache_hit_rate(&self) -> f32

Get text cache hit rate.

Source

pub fn log_text_cache_stats(&self)

Log text cache statistics.

Source

pub fn update_button_label( &mut self, widget_id: WidgetId, new_label: impl Into<String>, ) -> bool

Update button label by ID with automatic dirty marking.

Returns true if the label changed.

Source

pub fn update_text_input( &mut self, widget_id: WidgetId, new_value: impl Into<String>, ) -> bool

Update text input value by ID with automatic dirty marking.

Returns true if the value changed.

Source

pub fn update_color(&mut self, widget_id: WidgetId, color: Color) -> bool

Update widget color by ID with automatic dirty marking.

Returns true if the color changed.

Source

pub fn update_opacity(&mut self, widget_id: WidgetId, opacity: f32) -> bool

Update widget opacity by ID with automatic dirty marking.

Source

pub fn update_translate(&mut self, widget_id: WidgetId, translate: Vec2) -> bool

Update widget visual translation by ID.

Source

pub fn update_translate_x(&mut self, widget_id: WidgetId, x: f32) -> bool

Update widget visual X translation by ID.

Source

pub fn update_translate_y(&mut self, widget_id: WidgetId, y: f32) -> bool

Update widget visual Y translation by ID.

Source

pub fn update_scale(&mut self, widget_id: WidgetId, scale: Vec2) -> bool

Update widget visual scale by ID.

Source

pub fn update_scale_x(&mut self, widget_id: WidgetId, x: f32) -> bool

Update widget visual X scale by ID.

Source

pub fn update_scale_y(&mut self, widget_id: WidgetId, y: f32) -> bool

Update widget visual Y scale by ID.

Source

pub fn set_visible(&mut self, widget_id: WidgetId, visible: bool) -> bool

Set widget visibility by ID.

Source

pub fn toggle_visible(&mut self, widget_id: WidgetId) -> bool

Toggle widget visibility by ID.

Source

pub fn render(&mut self, render_pass: &mut RenderPass<'_>)

Render the UI using retained mode instanced rendering.

This is the high-performance path that only updates dirty nodes and uses GPU instancing for efficient rendering.

Note: This automatically computes layout. If you need to control layout computation separately (e.g., for middleware freeze functionality), use compute_layout() + render_without_layout() instead.

Source

pub fn render_without_layout( &mut self, render_pass: &mut RenderPass<'_>, clear_dirty_flags: bool, )

Render the UI without computing layout.

Use this when you want to manually control layout computation, for example when implementing layout freeze functionality with middleware.

§Parameters
  • render_pass: The WGPU render pass to render into
  • clear_dirty_flags: Whether to clear dirty flags after rendering. Set to false when layout is frozen to preserve dirty state for inspection.

Typical usage:

// Check if middleware wants to freeze layout
let skip_layout = middlewares.pre_layout(&ctx);
if !skip_layout {
    ui.compute_layout();
}
// Don't clear flags when frozen so inspector can keep showing them
ui.render_without_layout(render_pass, !skip_layout);
Source

pub fn core_mut(&mut self) -> &mut UiCore

Get mutable access to the core for advanced usage.

Source

pub fn core(&self) -> &UiCore

Get reference to the core.

Source

pub fn tree_mut(&mut self) -> &mut UiTree

Get mutable access to the tree for advanced usage.

Source

pub fn tree(&self) -> &UiTree

Get reference to the tree.

Source

pub fn event_system_mut(&mut self) -> &mut UiEventSystem

Get mutable access to the event system.

Source

pub fn font_renderer(&self) -> &FontRenderer

Get reference to the font renderer.

Source

pub fn set_theme(&mut self, theme: Theme)

Set the theme, marking all widget colors dirty.

Source

pub fn theme(&self) -> &Theme

Get a reference to the current theme.

Source

pub fn docking_style(&self) -> &DockingStyle

Get a reference to the docking style.

Source

pub fn set_docking_style(&mut self, style: DockingStyle)

Replace the docking style.

Source

pub fn add_plugin<P: UiPlugin>(&mut self, plugin: P) -> PluginHandle<P>

Add a plugin to the UI system and return a handle for typed access.

The plugin’s widget types are registered immediately.

§Panics

Panics if a plugin of the same concrete type is already registered.

Source

pub fn plugin_handle<P: UiPlugin>(&self) -> Option<PluginHandle<P>>

Get a handle for an already-registered plugin.

Returns Some(PluginHandle) if the plugin is registered, None otherwise. Useful for obtaining handles to auto-registered plugins.

Source

pub fn plugin<P: UiPlugin>(&self, handle: &PluginHandle<P>) -> &P

Get a reference to a registered plugin by type, using a handle as proof.

Source

pub fn plugin_mut<P: UiPlugin>(&mut self, handle: &PluginHandle<P>) -> &mut P

Get a mutable reference to a registered plugin by type, using a handle as proof.

Source

pub fn plugin_manager(&self) -> &PluginManager

Get a reference to the plugin manager.

Source

pub fn renderer_descriptor(&self) -> &UiRendererDescriptor

Get the current renderer configuration.

Returns the descriptor used to create the renderer, including surface format and depth format settings.

§Example
let desc = ui.renderer_descriptor();
println!("Surface format: {:?}", desc.surface_format);
Source

pub fn reconfigure(&mut self, descriptor: UiRendererDescriptor)

Reconfigure the renderer with new format settings.

Call this when the target surface format changes (e.g., window moved to a different monitor).

§Example
// Window moved to different monitor - surface format may have changed
ui.reconfigure(UiRendererDescriptor::from_window(window));
Source

pub fn reconfigure_from_window(&mut self, window: &RenderWindow)

Reconfigure from a window, inheriting its format configuration.

Convenience method for updating the renderer when a window’s surface format changes (e.g., when moved to a different monitor).

§Example
// Handle surface format change after window moved
ui.reconfigure_from_window(window);

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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

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