Skip to main content

cranpose_ui/modifier/
focus.rs

1//! Focus modifier nodes for Cranpose.
2//!
3//! This module implements focus management that mirrors Jetpack Compose's
4//! focus system. Focus nodes participate in focus traversal, track focus state,
5//! and integrate with the modifier chain lifecycle.
6
7use std::cell::Cell;
8use std::hash::{Hash, Hasher};
9use std::rc::Rc;
10
11use cranpose_foundation::{
12    impl_focus_node, DelegatableNode, FocusNode, FocusState, ModifierNode, ModifierNodeContext,
13    ModifierNodeElement, NodeCapabilities, NodeState,
14};
15
16/// Focus direction for navigation.
17#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
18pub enum FocusDirection {
19    /// Enter focus from outside.
20    Enter,
21    /// Exit focus to outside.
22    Exit,
23    /// Move to next focusable.
24    Next,
25    /// Move to previous focusable.
26    Previous,
27    /// Move up (2D navigation).
28    Up,
29    /// Move down (2D navigation).
30    Down,
31    /// Move left (2D navigation).
32    Left,
33    /// Move right (2D navigation).
34    Right,
35}
36
37/// A focus target node that can receive focus.
38///
39/// This is the core building block for focusable components. Each focus target
40/// tracks its own focus state and participates in the focus traversal system.
41pub struct FocusTargetNode {
42    state: NodeState,
43    focus_state: Cell<FocusState>,
44    on_focus_changed: Option<Rc<dyn Fn(FocusState)>>,
45}
46
47impl FocusTargetNode {
48    pub fn new() -> Self {
49        Self {
50            state: NodeState::new(),
51            focus_state: Cell::new(FocusState::Inactive),
52            on_focus_changed: None,
53        }
54    }
55
56    pub fn with_callback<F>(callback: F) -> Self
57    where
58        F: Fn(FocusState) + 'static,
59    {
60        Self {
61            state: NodeState::new(),
62            focus_state: Cell::new(FocusState::Inactive),
63            on_focus_changed: Some(Rc::new(callback)),
64        }
65    }
66
67    /// Sets the focus state for this node.
68    pub fn set_focus_state(&self, state: FocusState) {
69        let old_state = self.focus_state.get();
70        if old_state != state {
71            self.focus_state.set(state);
72            if let Some(callback) = &self.on_focus_changed {
73                callback(state);
74            }
75        }
76    }
77
78    /// Clears focus from this node.
79    pub fn clear_focus(&self) {
80        self.set_focus_state(FocusState::Inactive);
81    }
82}
83
84impl Default for FocusTargetNode {
85    fn default() -> Self {
86        Self::new()
87    }
88}
89
90impl DelegatableNode for FocusTargetNode {
91    fn node_state(&self) -> &NodeState {
92        &self.state
93    }
94}
95
96impl ModifierNode for FocusTargetNode {
97    fn on_attach(&mut self, _context: &mut dyn ModifierNodeContext) {
98        self.state.set_attached(true);
99    }
100
101    fn on_detach(&mut self) {
102        self.state.set_attached(false);
103        self.clear_focus();
104    }
105
106    // Capability-driven implementation using helper macro
107    impl_focus_node!();
108}
109
110impl FocusNode for FocusTargetNode {
111    fn focus_state(&self) -> FocusState {
112        self.focus_state.get()
113    }
114
115    fn on_focus_changed(&mut self, _context: &mut dyn ModifierNodeContext, state: FocusState) {
116        self.set_focus_state(state);
117    }
118}
119
120/// Modifier element for focus targets.
121///
122/// Creates a focusable modifier that can receive and track focus.
123#[derive(Clone)]
124pub struct FocusTargetElement {
125    on_focus_changed: Option<Rc<dyn Fn(FocusState)>>,
126}
127
128impl FocusTargetElement {
129    pub fn new() -> Self {
130        Self {
131            on_focus_changed: None,
132        }
133    }
134
135    pub fn with_callback<F>(callback: F) -> Self
136    where
137        F: Fn(FocusState) + 'static,
138    {
139        Self {
140            on_focus_changed: Some(Rc::new(callback)),
141        }
142    }
143}
144
145impl Default for FocusTargetElement {
146    fn default() -> Self {
147        Self::new()
148    }
149}
150
151impl std::fmt::Debug for FocusTargetElement {
152    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
153        f.debug_struct("FocusTargetElement")
154            .field("has_callback", &self.on_focus_changed.is_some())
155            .finish()
156    }
157}
158
159impl PartialEq for FocusTargetElement {
160    fn eq(&self, other: &Self) -> bool {
161        // Type-based matching: compare only presence of callback, not pointer
162        // Nodes are updated via update() method, preserving behavior
163        self.on_focus_changed.is_some() == other.on_focus_changed.is_some()
164    }
165}
166
167impl Hash for FocusTargetElement {
168    fn hash<H: Hasher>(&self, state: &mut H) {
169        // Consistent hash based on callback presence only
170        "focus_target".hash(state);
171        self.on_focus_changed.is_some().hash(state);
172    }
173}
174
175impl ModifierNodeElement for FocusTargetElement {
176    type Node = FocusTargetNode;
177
178    fn create(&self) -> Self::Node {
179        if let Some(callback) = &self.on_focus_changed {
180            FocusTargetNode::with_callback({
181                let callback = callback.clone();
182                move |state| callback(state)
183            })
184        } else {
185            FocusTargetNode::new()
186        }
187    }
188
189    fn update(&self, node: &mut Self::Node) {
190        node.on_focus_changed = self.on_focus_changed.clone();
191    }
192
193    fn inspector_name(&self) -> &'static str {
194        "focusTarget"
195    }
196
197    fn capabilities(&self) -> NodeCapabilities {
198        NodeCapabilities::FOCUS
199    }
200
201    fn always_update(&self) -> bool {
202        // Always update to capture new closure if it changed
203        true
204    }
205}
206
207/// A focus requester node that can request focus for associated targets.
208///
209/// This node allows programmatic focus requests and is typically used to
210/// trigger focus on specific components in response to user actions or
211/// app logic.
212pub struct FocusRequesterNode {
213    state: NodeState,
214    requester_id: usize,
215}
216
217impl FocusRequesterNode {
218    pub fn new(requester_id: usize) -> Self {
219        Self {
220            state: NodeState::new(),
221            requester_id,
222        }
223    }
224}
225
226impl DelegatableNode for FocusRequesterNode {
227    fn node_state(&self) -> &NodeState {
228        &self.state
229    }
230}
231
232impl ModifierNode for FocusRequesterNode {
233    fn on_attach(&mut self, _context: &mut dyn ModifierNodeContext) {
234        self.state.set_attached(true);
235    }
236
237    fn on_detach(&mut self) {
238        self.state.set_attached(false);
239    }
240}
241
242/// Modifier element for focus requesters.
243///
244/// Creates a modifier that can be used to programmatically request focus.
245#[derive(Debug, Clone, PartialEq, Eq, Hash)]
246pub struct FocusRequesterElement {
247    requester_id: usize,
248}
249
250impl FocusRequesterElement {
251    pub fn new(requester_id: usize) -> Self {
252        Self { requester_id }
253    }
254}
255
256impl ModifierNodeElement for FocusRequesterElement {
257    type Node = FocusRequesterNode;
258
259    fn create(&self) -> Self::Node {
260        FocusRequesterNode::new(self.requester_id)
261    }
262
263    fn update(&self, node: &mut Self::Node) {
264        node.requester_id = self.requester_id;
265    }
266
267    fn inspector_name(&self) -> &'static str {
268        "focusRequester"
269    }
270
271    fn capabilities(&self) -> NodeCapabilities {
272        NodeCapabilities::FOCUS
273    }
274}
275
276/// A handle for requesting focus programmatically.
277///
278/// This mirrors Jetpack Compose's FocusRequester class and provides
279/// an API for triggering focus changes from application code.
280#[derive(Clone, Debug, Default)]
281pub struct FocusRequester {
282    id: usize,
283}
284
285impl FocusRequester {
286    pub fn new() -> Self {
287        use std::sync::atomic::{AtomicUsize, Ordering};
288        static NEXT_ID: AtomicUsize = AtomicUsize::new(1);
289        Self {
290            id: NEXT_ID.fetch_add(1, Ordering::Relaxed),
291        }
292    }
293
294    pub fn id(&self) -> usize {
295        self.id
296    }
297
298    /// Requests focus for components associated with this requester.
299    pub fn request_focus(&self) -> bool {
300        // This will be wired up to the focus manager in the next phase
301        true
302    }
303
304    /// Captures focus, preventing other components from taking focus.
305    pub fn capture_focus(&self) -> bool {
306        // This will be wired up to the focus manager in the next phase
307        true
308    }
309
310    /// Releases captured focus.
311    pub fn free_focus(&self) -> bool {
312        // This will be wired up to the focus manager in the next phase
313        true
314    }
315}
316
317#[cfg(test)]
318mod tests {
319    use super::*;
320    use cranpose_foundation::{BasicModifierNodeContext, ModifierNodeChain};
321
322    #[test]
323    fn focus_target_node_lifecycle() {
324        let mut node = FocusTargetNode::new();
325        let mut context = BasicModifierNodeContext::new();
326
327        assert_eq!(node.focus_state(), FocusState::Inactive);
328        assert!(!node.node_state().is_attached());
329
330        node.on_attach(&mut context);
331        assert!(node.node_state().is_attached());
332
333        node.set_focus_state(FocusState::Active);
334        assert_eq!(node.focus_state(), FocusState::Active);
335        assert!(node.focus_state().is_focused());
336
337        node.on_detach();
338        assert!(!node.node_state().is_attached());
339        assert_eq!(node.focus_state(), FocusState::Inactive);
340    }
341
342    #[test]
343    fn focus_target_callback_invoked() {
344        use std::cell::RefCell;
345        let states = Rc::new(RefCell::new(Vec::new()));
346        let states_clone = states.clone();
347
348        let node = FocusTargetNode::with_callback(move |state| {
349            states_clone.borrow_mut().push(state);
350        });
351
352        node.set_focus_state(FocusState::Active);
353        node.set_focus_state(FocusState::ActiveParent);
354        node.set_focus_state(FocusState::Inactive);
355
356        let recorded = states.borrow();
357        assert_eq!(recorded.len(), 3);
358        assert_eq!(recorded[0], FocusState::Active);
359        assert_eq!(recorded[1], FocusState::ActiveParent);
360        assert_eq!(recorded[2], FocusState::Inactive);
361    }
362
363    #[test]
364    fn focus_element_creates_node() {
365        let element = FocusTargetElement::new();
366        let node = element.create();
367        assert_eq!(node.focus_state(), FocusState::Inactive);
368    }
369
370    #[test]
371    fn focus_chain_integration() {
372        let element = FocusTargetElement::new();
373        let dyn_element = cranpose_foundation::modifier_element(element);
374
375        let mut chain = ModifierNodeChain::new();
376        let mut context = BasicModifierNodeContext::new();
377
378        chain.update(vec![dyn_element], &mut context);
379
380        assert_eq!(chain.len(), 1);
381        assert!(chain.has_capability(NodeCapabilities::FOCUS));
382    }
383
384    #[test]
385    fn focus_requester_unique_ids() {
386        let req1 = FocusRequester::new();
387        let req2 = FocusRequester::new();
388        assert_ne!(req1.id(), req2.id());
389    }
390
391    #[test]
392    fn focus_state_predicates() {
393        assert!(FocusState::Active.is_focused());
394        assert!(FocusState::Captured.is_focused());
395        assert!(!FocusState::Inactive.is_focused());
396        assert!(!FocusState::ActiveParent.is_focused());
397
398        assert!(FocusState::Active.has_focus());
399        assert!(FocusState::ActiveParent.has_focus());
400        assert!(FocusState::Captured.has_focus());
401        assert!(!FocusState::Inactive.has_focus());
402
403        assert!(FocusState::Captured.is_captured());
404        assert!(!FocusState::Active.is_captured());
405    }
406}