Skip to main content

FocusAwareModalStack

Struct FocusAwareModalStack 

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

Modal stack with integrated focus management.

This wrapper provides automatic focus trapping when modals open and focus restoration when they close. It manages both the modal stack and focus manager in a coordinated way.

§Invariants

  • Focus trap stack depth equals the number of modals with focus groups.
  • Each modal’s focus group ID is unique and not reused.
  • Pop operations always call pop_trap for modals with focus groups.

Implementations§

Source§

impl FocusAwareModalStack

Source

pub fn new() -> Self

Create a new focus-aware modal stack.

Source

pub fn with_focus_manager(focus_manager: FocusManager) -> Self

Create from existing stack and focus manager.

Use this when you already have a FocusManager in your application and want to integrate modal focus trapping.

The provided manager must not already have active modal traps. This wrapper only tracks traps for modals it owns, so starting from an already-trapped manager would make later rebuild/pop operations silently corrupt unrelated trap state.

Source

pub fn push(&mut self, modal: Box<dyn StackModal>) -> ModalId

Push a modal without focus trapping.

The modal will be rendered and receive events, but focus is not managed.

Source

pub fn push_with_trap( &mut self, modal: Box<dyn StackModal>, focusable_ids: Vec<FocusId>, ) -> ModalId

Push a modal with automatic focus trapping.

§Parameters
  • modal: The modal content
  • focusable_ids: The focus IDs of elements inside the modal
§Behavior
  1. Creates a focus group with the provided IDs
  2. Pushes a focus trap (saving current focus)
  3. Moves focus to the first element in the group
Source

pub fn pop(&mut self) -> Option<ModalResult>

Pop the top modal.

If the modal had a focus group, the focus trap is popped and focus is restored to where it was before the modal opened.

Source

pub fn pop_id(&mut self, id: ModalId) -> Option<ModalResult>

Pop a specific modal by ID.

Source

pub fn pop_all(&mut self) -> Vec<ModalResult>

Pop all modals, restoring focus to the original state.

Source

pub fn handle_event( &mut self, event: &Event, hit: Option<HitTestResult>, ) -> Option<ModalResult>

Handle an event, routing to the top modal.

If the modal closes (via Escape, backdrop click, etc.), the focus trap is automatically popped and focus is restored. For mouse events, pass the provenance-aware result from Frame::hit_test_detailed.

Source

pub fn render(&self, frame: &mut Frame<'_>, screen: Rect)

Render all modals.

Source

pub fn with_focus_graph_mut<R>( &mut self, f: impl FnOnce(&mut FocusGraph) -> R, ) -> R

Perform a direct focus-graph mutation and automatically resynchronize modal focus state.

Source

pub fn focus(&mut self, id: FocusId) -> Option<FocusId>

Focus a specific target through the wrapped focus manager.

Source

pub fn is_empty(&self) -> bool

Check if the modal stack is empty.

Source

pub fn depth(&self) -> usize

Get the number of open modals.

Source

pub fn is_focus_trapped(&self) -> bool

Check if focus is currently trapped in a modal.

Source

pub fn stack(&self) -> &ModalStack

Get a reference to the underlying modal stack.

Source

pub fn focus_manager(&self) -> &FocusManager

Get a reference to the focus manager.

Trait Implementations§

Source§

impl Default for FocusAwareModalStack

Source§

fn default() -> Self

Returns the “default value” for a 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