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.

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.

Warning: Popping a non-top modal with a focus group will NOT restore focus correctly. The focus trap stack is LIFO, so only the top modal’s trap can be safely popped. Prefer using pop() for correct focus handling.

§Behavior
  • If the modal is the top modal and has a focus group, pop_trap() is called
  • If the modal is NOT the top modal, the focus trap is NOT popped (would corrupt state)
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) -> 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.

Source

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

Render all modals.

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 stack_mut(&mut self) -> &mut ModalStack

Get a mutable reference to the underlying modal stack.

Warning: Direct manipulation may desync focus state.

Source

pub fn focus_manager(&self) -> &FocusManager

Get a reference to the focus manager.

Source

pub fn focus_manager_mut(&mut self) -> &mut FocusManager

Get a mutable 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