Skip to main content

rat_focus/
lib.rs

1#![doc = include_str!("../readme.md")]
2
3pub mod doc {
4    #![doc = include_str!("../doc.md")]
5}
6mod builder;
7mod core;
8mod flag;
9mod focus;
10
11/// Reexport of types used by a macro.
12pub mod ratatui {
13    pub mod layout {
14        pub use ratatui_core::layout::Rect;
15    }
16}
17
18pub use crate::builder::FocusBuilder;
19pub use crate::flag::FocusFlag;
20pub use crate::focus::{Focus, handle_focus};
21
22/// Focus navigation for widgets.
23///
24/// The effects that hinder focus-change (`Reach*`, `Lock`) only work
25/// when navigation changes via next()/prev()/focus_at().
26///
27/// Programmatic focus changes are always possible.
28#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
29pub enum Navigation {
30    /// Widget is not reachable with normal keyboard or mouse navigation.
31    None,
32    /// Focus is locked to stay with this widget. No mouse or keyboard navigation
33    /// can change that.
34    Lock,
35    /// Widget is not reachable with keyboard navigation, but can be focused with the mouse.
36    Mouse,
37    /// Widget cannot be reached with normal keyboard navigation, but can be left.
38    /// (e.g. Tabs, Split-Divider)
39    Leave,
40    /// Widget can be reached with normal keyboard navigation, but not left.
41    /// (e.g. TextArea)
42    Reach,
43    /// Widget can be reached with normal keyboard navigation, but only be left with
44    /// backward navigation.
45    /// (e.g. some widget with internal structure)
46    ReachLeaveFront,
47    /// Widget can be reached with normal keyboard navigation, but only be left with
48    /// forward navigation.
49    /// (e.g. some widget with internal structure)
50    ReachLeaveBack,
51    /// Widget can be reached and left with normal keyboard and mouse navigation.
52    #[default]
53    Regular,
54}
55
56/// Trait for a widget that takes part of focus handling.
57///
58/// When used for a simple widget implement
59/// - build()
60/// - focus()
61/// - area()
62///
63/// and optionally
64///
65/// - area_z() and navigable()
66///
67/// ```rust no_run
68/// use rat_focus::ratatui::layout::Rect;
69/// use rat_focus::{FocusBuilder, FocusFlag, HasFocus};
70///
71/// struct MyWidgetState { pub focus: FocusFlag, pub area: Rect }
72///
73/// impl HasFocus for MyWidgetState {
74///     fn build(&self, builder: &mut FocusBuilder) {
75///         builder.leaf_widget(self);
76///     }
77///
78///     fn focus(&self) -> FocusFlag {
79///         self.focus.clone()
80///     }
81///
82///     fn area(&self) -> Rect {
83///         self.area
84///     }
85/// }
86/// ```
87///
88///
89/// When used for a container widget implement
90/// - build()
91/// ```rust no_run
92/// use rat_focus::ratatui::layout::Rect;
93/// use rat_focus::{FocusBuilder, FocusFlag, HasFocus};
94///
95/// struct MyWidgetState { pub focus: FocusFlag, pub area: Rect }
96/// # impl HasFocus for MyWidgetState {
97/// #     fn build(&self, builder: &mut FocusBuilder) {
98/// #         builder.leaf_widget(self);
99/// #     }
100/// #
101/// #     fn focus(&self) -> FocusFlag {
102/// #         self.focus.clone()
103/// #     }
104/// #
105/// #     fn area(&self) -> Rect {
106/// #         self.area
107/// #     }
108/// # }
109/// struct SomeWidgetState { pub focus: FocusFlag, pub area: Rect, pub component_a: MyWidgetState, pub component_b: MyWidgetState }
110///
111/// impl HasFocus for SomeWidgetState {
112///     fn build(&self, builder: &mut FocusBuilder) {
113///         let tag = builder.start(self);
114///         builder.widget(&self.component_a);
115///         builder.widget(&self.component_b);
116///         builder.end(tag);
117///     }
118///
119///     fn focus(&self) -> FocusFlag {
120///         self.focus.clone()
121///     }
122///
123///     fn area(&self) -> Rect {
124///         self.area
125///     }
126/// }
127/// ```
128/// Creates a container with an identity.
129///
130/// Or
131/// ```rust no_run
132/// use rat_focus::ratatui::layout::Rect;
133/// use rat_focus::{FocusBuilder, FocusFlag, HasFocus};
134///
135/// struct MyWidgetState { pub focus: FocusFlag, pub area: Rect }
136/// # impl HasFocus for MyWidgetState {
137/// #     fn build(&self, builder: &mut FocusBuilder) {
138/// #         builder.leaf_widget(self);
139/// #     }
140/// #
141/// #     fn focus(&self) -> FocusFlag {
142/// #         self.focus.clone()
143/// #     }
144/// #
145/// #     fn area(&self) -> Rect {
146/// #         self.area
147/// #     }
148/// # }
149/// struct SomeWidgetState { pub focus: FocusFlag, pub area: Rect, pub component_a: MyWidgetState, pub component_b: MyWidgetState }
150///
151/// impl HasFocus for SomeWidgetState {
152///     fn build(&self, builder: &mut FocusBuilder) {
153///         builder.widget(&self.component_a);
154///         builder.widget(&self.component_b);
155///     }
156///
157///     fn focus(&self) -> FocusFlag {
158///         unimplemented!("not in use")
159///     }
160///
161///     fn area(&self) -> Rect {
162///         unimplemented!("not in use")
163///     }
164/// }
165/// ```
166/// for an anonymous container.
167///
168/// focus(), area() and area_z() are only used for the first case.
169/// navigable() is ignored for containers, leave it at the default.
170///
171pub trait HasFocus {
172    /// Build the focus-structure for the container/widget.
173    fn build(&self, builder: &mut FocusBuilder);
174
175    /// Build the focus-structure for the container/widget.
176    ///
177    /// This function is called when the default navigation is
178    /// overridden by calling [FocusBuilder::widget_navigate].
179    /// You only need to implement this function, if you have
180    /// a container-widget, that wants to react to an
181    /// alternate navigation.
182    ///
183    /// For regular widgets this will be called too, but
184    /// the overridden flag will be used by Focus, regardless
185    /// of what you do. It's only useful to get a notification
186    /// of an alternate navigation.
187    ///
188    /// It defaults to calling build. If you don't have very
189    /// specific requirements, you need not concern with this;
190    /// just implement [HasFocus::build].
191    #[allow(unused_variables)]
192    fn build_nav(&self, navigable: Navigation, builder: &mut FocusBuilder) {
193        self.build(builder);
194    }
195
196    /// Access to the focus flag.
197    fn focus(&self) -> FocusFlag;
198
199    /// Provide a unique id for the widget.
200    fn id(&self) -> usize {
201        self.focus().widget_id()
202    }
203
204    /// Area for mouse focus.
205    ///
206    /// Generally, this area shouldn't overlap with other areas.
207    /// If it does, you can use `area_z()` to give an extra z-value
208    /// for mouse interactions. Default is 0, higher values mean
209    /// `above`.
210    /// If two areas with the same z overlap, the last one will
211    /// be used.
212    fn area(&self) -> ratatui::layout::Rect;
213
214    /// Z-value for the area.
215    ///
216    /// When testing for mouse interactions the z-value is taken into account.
217    fn area_z(&self) -> u16 {
218        0
219    }
220
221    /// Declares how the widget interacts with focus.
222    ///
223    /// Default is [Navigation::Regular].
224    fn navigable(&self) -> Navigation {
225        Navigation::Regular
226    }
227
228    /// Does this widget have the focus.
229    /// Or, if the flag is used for a container, does any of
230    /// widget inside the container have the focus.
231    ///
232    /// This flag is set by [Focus::handle].
233    fn is_focused(&self) -> bool {
234        self.focus().get()
235    }
236
237    /// This widget just lost the focus. This flag is set by [Focus::handle]
238    /// if there is a focus transfer, and will be reset by the next
239    /// call to [Focus::handle].
240    ///
241    /// See [on_lost!](crate::on_lost!)
242    fn lost_focus(&self) -> bool {
243        self.focus().lost()
244    }
245
246    /// This widget just gained the focus. This flag is set by [Focus::handle]
247    /// if there is a focus transfer, and will be reset by the next
248    /// call to [Focus::handle].
249    ///
250    /// See [on_gained!](crate::on_gained!)
251    fn gained_focus(&self) -> bool {
252        self.focus().gained()
253    }
254
255    /// Is the widget area hit by the current mouse-position
256    /// and is there no other widget/container with a
257    /// higher z-index. Then this widget is *not* obscured.
258    ///
259    /// This flag is set when handling a mouse-event.
260    fn is_obscured(&self) -> bool {
261        self.focus().obscured()
262    }
263}
264
265/// Does a match on the state struct of a widget. If `widget_state.lost_focus()` is true
266/// the block is executed. This requires that `widget_state` implements [HasFocus],
267/// but that's the basic requirement for this whole crate.
268///
269/// ```rust ignore
270/// use rat_focus::on_lost;
271///
272/// on_lost!(
273///     state.field1 => {
274///         // do checks
275///     },
276///     state.field2 => {
277///         // do checks
278///     }
279/// );
280/// ```
281#[macro_export]
282macro_rules! on_lost {
283    ($($field:expr => $validate:expr),*) => {{
284        use $crate::HasFocus;
285        $(if $field.lost_focus() { _ = $validate })*
286    }};
287}
288
289/// Does a match on the state struct of a widget. If `widget_state.gained_focus()` is true
290/// the block is executed. This requires that `widget_state` implements [HasFocus],
291/// but that's the basic requirement for this whole crate.
292///
293/// ```rust ignore
294/// use rat_focus::on_gained;
295///
296/// on_gained!(
297///     state.field1 => {
298///         // do prep
299///     },
300///     state.field2 => {
301///         // do prep
302///     }
303/// );
304/// ```
305#[macro_export]
306macro_rules! on_gained {
307    ($($field:expr => $validate:expr),*) => {{
308        use $crate::HasFocus;
309        $(if $field.gained_focus() { _ = $validate })*
310    }};
311}
312
313/// Does a match on several fields and can return a result.
314/// Does a `widget_state.is_focused()` for each field and returns
315/// the first that is true. There is an `else` branch too.
316///
317/// This requires that `widget_state` implements [HasFocus],
318/// but that's the basic requirement for this whole crate.
319///
320/// ```rust ignore
321/// use rat_focus::match_focus;
322///
323/// let res = match_focus!(
324///     state.field1 => {
325///         // do this
326///         true
327///     },
328///     state.field2 => {
329///         // do that
330///         true
331///     },
332///     else => {
333///         false
334///     }
335/// );
336///
337/// if res {
338///     // react
339/// }
340/// ```
341///
342#[macro_export]
343macro_rules! match_focus {
344    ($($field:expr => $block:expr),* $(, else => $final:expr)?) => {{
345        use $crate::HasFocus;
346        if false {
347            unreachable!();
348        }
349        $(else if $field.is_focused() { $block })*
350        $(else { $final })?
351    }};
352}
353
354/// Create the implementation of HasFocus for the
355/// given list of struct members.
356///
357/// Create a container with no identity.
358/// ```
359/// # use rat_focus::{impl_has_focus, FocusFlag};
360/// # struct MyState { field1: FocusFlag, field2: FocusFlag, field3: FocusFlag }
361/// impl_has_focus!(field1, field2, field3 for MyState);
362/// ```
363///
364/// Create a container with an identity.
365/// ```
366/// # use rat_focus::{impl_has_focus, FocusFlag};
367/// # struct MyState { container: FocusFlag, field1: FocusFlag, field2: FocusFlag, field3: FocusFlag }
368/// impl_has_focus!(container: field1, field2, field3 for MyState);
369/// ```
370///
371/// Create a container with an identity and an area that will react to mouse clicks.
372/// ```
373/// # use rat_focus::ratatui::layout::Rect;
374/// # use rat_focus::{impl_has_focus, FocusFlag};
375/// # struct MyState { container: FocusFlag, area: Rect, field1: FocusFlag, field2: FocusFlag, field3: FocusFlag }
376/// impl_has_focus!(container:area: field1, field2, field3 for MyState);
377/// ```
378#[macro_export]
379macro_rules! impl_has_focus {
380    ($cc:ident:$area:ident: $($n:ident),* for $ty:ty) => {
381        impl $crate::HasFocus for $ty {
382            fn build(&self, builder: &mut $crate::FocusBuilder) {
383                let tag = builder.start(self);
384                $(builder.widget(&self.$n);)*
385                builder.end(tag);
386            }
387
388            fn focus(&self) -> $crate::FocusFlag {
389                self.$cc.clone()
390            }
391
392            fn area(&self) -> $crate::ratatui::layout::Rect {
393                self.$area
394            }
395        }
396    };
397    ($cc:ident: $($n:ident),* for $ty:ty) => {
398        impl $crate::HasFocus for $ty {
399            fn build(&self, builder: &mut $crate::FocusBuilder) {
400                let tag = builder.start(self);
401                $(builder.widget(&self.$n);)*
402                builder.end(tag);
403            }
404
405            fn focus(&self) -> $crate::FocusFlag {
406                self.$cc.clone()
407            }
408
409            fn area(&self) -> $crate::ratatui::layout::Rect {
410                $crate::ratatui::layout::Rect::default()
411            }
412        }
413    };
414    ($($n:ident),* for $ty:ty) => {
415        impl $crate::HasFocus for $ty {
416            fn build(&self, builder: &mut $crate::FocusBuilder) {
417                $(builder.widget(&self.$n);)*
418            }
419
420            fn focus(&self) -> $crate::FocusFlag {
421                unimplemented!("not defined")
422            }
423
424            fn area(&self) -> $crate::ratatui::layout::Rect {
425                unimplemented!("not defined")
426            }
427        }
428    };
429}