Skip to main content

FocusManager

Struct FocusManager 

Source
pub struct FocusManager {
    pub focused_node: Option<DomNodeId>,
    pub pending_focus_request: Option<FocusTarget>,
    pub cursor_needs_initialization: bool,
    pub pending_contenteditable_focus: Option<PendingContentEditableFocus>,
}
Expand description

Manager for keyboard focus and tab navigation

Note: Text cursor management is now handled by the separate CursorManager.

The FocusManager only tracks which node has focus, while CursorManager tracks the cursor position within that node (if it’s contenteditable).

§W3C Focus/Selection Model

The W3C model maintains a strict separation between keyboard focus and selection:

  1. Focus lands on the contenteditable container (document.activeElement)
  2. Selection/Cursor is placed in a descendant text node (Selection.focusNode)

This separation requires a “flag and defer” pattern:

  • During focus event: Set cursor_needs_initialization = true
  • After layout pass: Call finalize_pending_focus_changes() to actually initialize the cursor

This is necessary because cursor positioning requires text layout information, which isn’t available during the focus event handling phase.

Fields§

§focused_node: Option<DomNodeId>

Currently focused node (if any)

§pending_focus_request: Option<FocusTarget>

Pending focus request from callback

§cursor_needs_initialization: bool

Flag indicating that cursor initialization is pending (set during focus, consumed after layout)

§pending_contenteditable_focus: Option<PendingContentEditableFocus>

Information about the pending contenteditable focus

Implementations§

Source§

impl FocusManager

Source

pub fn new() -> Self

Create a new focus manager

Source

pub fn get_focused_node(&self) -> Option<&DomNodeId>

Get the currently focused node

Source

pub fn set_focused_node(&mut self, node: Option<DomNodeId>)

Set the focused node directly (used by event system)

Note: Cursor initialization/clearing is now handled by CursorManager. The event system should check if the newly focused node is contenteditable and call CursorManager::initialize_cursor_at_end() if needed.

Source

pub fn request_focus_change(&mut self, target: FocusTarget)

Request a focus change (to be processed by event system)

Source

pub fn take_focus_request(&mut self) -> Option<FocusTarget>

Take the pending focus request (one-shot)

Source

pub fn clear_focus(&mut self)

Clear focus

Source

pub fn has_focus(&self, node: &DomNodeId) -> bool

Check if a specific node has focus

Source

pub fn set_pending_contenteditable_focus( &mut self, dom_id: DomId, container_node_id: NodeId, text_node_id: NodeId, )

Mark that cursor initialization is needed for a contenteditable element.

This is called during focus event handling. The actual cursor initialization happens later in finalize_pending_focus_changes() after layout is complete.

§W3C Conformance

In the W3C model, when focus lands on a contenteditable element:

  1. The focus event fires on the container element
  2. The browser’s editing engine modifies the Selection to place a caret
  3. The Selection’s anchorNode/focusNode point to the child text node

Since we need layout information to position the cursor, we defer step 2+3.

Source

pub fn clear_pending_contenteditable_focus(&mut self)

Clear the pending contenteditable focus (when focus moves away or is cleared).

Source

pub fn take_pending_contenteditable_focus( &mut self, ) -> Option<PendingContentEditableFocus>

Take the pending contenteditable focus (consumes the flag).

Returns Some(info) if cursor initialization is pending, None otherwise. After calling this, cursor_needs_initialization is set to false.

Source

pub fn needs_cursor_initialization(&self) -> bool

Check if cursor initialization is pending.

Trait Implementations§

Source§

impl Clone for FocusManager

Source§

fn clone(&self) -> FocusManager

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FocusManager

Source§

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

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

impl Default for FocusManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl FocusManagerQuery for FocusManager

Source§

fn get_focused_node_id(&self) -> Option<DomNodeId>

Get the currently focused node ID
Source§

impl PartialEq for FocusManager

Source§

fn eq(&self, other: &FocusManager) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for FocusManager

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

Source§

fn is_within(&self, b: &G2) -> bool