ratatui_toolkit/widgets/markdown_widget/extensions/toc/methods/
entry_at_position.rs

1//! Position-based entry lookup for hover and click detection.
2
3use ratatui::layout::Rect;
4
5use super::super::Toc;
6use super::find_entry_at_position_compact::find_entry_at_position_compact;
7use super::find_entry_at_position_expanded::find_entry_at_position_expanded;
8use super::get_expanded_content_area::get_expanded_content_area;
9
10impl<'a> Toc<'a> {
11    /// Find the entry index at a given screen position.
12    ///
13    /// # Arguments
14    ///
15    /// * `x` - Screen X coordinate.
16    /// * `y` - Screen Y coordinate.
17    /// * `area` - The area the TOC is rendered in.
18    ///
19    /// # Returns
20    ///
21    /// The entry index at that position, or None if no entry is there.
22    pub fn entry_at_position(&self, x: u16, y: u16, area: Rect) -> Option<usize> {
23        // Check horizontal bounds - must be within the TOC width
24        if x < area.x || x >= area.x + area.width {
25            return None;
26        }
27
28        // Check if above the TOC area
29        if y < area.y {
30            return None;
31        }
32
33        let entries = self.toc_state.entries();
34        if entries.is_empty() {
35            return None;
36        }
37
38        if self.expanded {
39            // Calculate content area based on actual entries rather than passed area height
40            let content_area = get_expanded_content_area(area, &self.config, entries.len());
41            find_entry_at_position_expanded(
42                x,
43                y,
44                content_area,
45                entries,
46                self.toc_state.scroll_offset,
47            )
48        } else {
49            find_entry_at_position_compact(y, area, &self.config, entries)
50        }
51    }
52}