1use crate::Property;
2use std::{cell::RefCell, fmt, rc::Rc};
3
4#[derive(Clone, Default)]
6pub struct FocusHandle(Rc<FocusSignals>);
7
8#[derive(Default)]
9struct FocusSignals {
10 direct: Property<bool>,
11 within: Property<bool>,
12}
13
14impl fmt::Debug for FocusHandle {
15 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16 f.debug_struct("FocusHandle").finish_non_exhaustive()
17 }
18}
19
20impl PartialEq for FocusHandle {
21 fn eq(&self, other: &Self) -> bool {
22 Rc::ptr_eq(&self.0, &other.0)
23 }
24}
25
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27pub enum FocusAction {
28 Focus,
29 Clear,
30 Text(TextCommand),
31 EditTableCell { row: i64, column: i64 },
32 CancelTableEdit,
33}
34
35#[derive(Clone, Copy, Debug, PartialEq, Eq)]
37pub enum TextCommand {
38 SelectAll,
39 ClearSelection,
40 SetSelection { anchor: i64, focus: i64 },
41 Undo,
42 Redo,
43 Cut,
44 Copy,
45 Paste,
46}
47
48#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
49pub enum FocusReason {
50 #[default]
51 Programmatic,
52 TabNavigation,
53 PointerClick,
54 PopupActivation,
55 WindowActivation,
56 Accessibility,
57 LayoutChange,
58 KeyboardNavigation,
59}
60
61#[derive(Clone, Copy, Debug, PartialEq, Eq)]
62pub enum FocusEvent {
63 Gained(FocusReason),
64 Lost(FocusReason),
65 Within { focused: bool, reason: FocusReason },
66}
67
68impl FocusEvent {
69 #[doc(hidden)]
70 pub fn is_loss(self) -> bool {
71 matches!(self, Self::Lost(_) | Self::Within { focused: false, .. })
72 }
73}
74
75pub struct FocusRequest {
76 pub target: FocusHandle,
77 pub action: FocusAction,
78}
79
80#[derive(Clone, Default)]
82pub struct FocusRequests(Rc<RefCell<Vec<FocusRequest>>>);
83
84thread_local! {
85 static ACTIVE: RefCell<Option<FocusRequests>> = const { RefCell::new(None) };
86}
87
88pub struct FocusContext(Option<FocusRequests>);
89
90impl FocusRequests {
91 pub fn enter(&self) -> FocusContext {
92 FocusContext(ACTIVE.with(|active| active.replace(Some(self.clone()))))
93 }
94
95 pub fn drain(&self) -> Vec<FocusRequest> {
96 std::mem::take(&mut *self.0.borrow_mut())
97 }
98
99 pub fn is_empty(&self) -> bool {
100 self.0.borrow().is_empty()
101 }
102}
103
104impl Drop for FocusContext {
105 fn drop(&mut self) {
106 ACTIVE.with(|active| active.replace(self.0.take()));
107 }
108}
109
110impl FocusHandle {
111 pub fn has_focus(&self) -> bool {
112 self.0.direct.get(|| false)
113 }
114
115 pub fn focus_within(&self) -> bool {
116 self.0.within.get(|| false)
117 }
118
119 #[doc(hidden)]
120 pub fn publish(&self, direct: bool, within: bool) {
121 self.0.direct.set_if_changed(direct);
122 self.0.within.set_if_changed(within);
123 }
124
125 pub fn request(&self, action: FocusAction) {
126 ACTIVE.with(|active| {
127 if let Some(requests) = &*active.borrow() {
128 requests.0.borrow_mut().push(FocusRequest {
129 target: self.clone(),
130 action,
131 });
132 }
133 });
134 }
135}
136
137#[cfg(test)]
138mod tests {
139 use super::*;
140
141 #[test]
142 fn direct_and_within_focus_have_independent_dependencies() {
143 let handle = FocusHandle::default();
144 let direct = Property::default();
145 let within = Property::default();
146 assert!(!direct.get(|| handle.has_focus()));
147 assert!(!within.get(|| handle.focus_within()));
148 handle.publish(false, true);
149 assert!(!direct.is_dirty());
150 assert!(within.is_dirty());
151 assert!(within.get(|| handle.focus_within()));
152 handle.publish(true, true);
153 assert!(direct.is_dirty());
154 assert!(!within.is_dirty());
155 }
156}