ratatui_toolkit/primitives/button/methods/
render_at_offset.rs

1use ratatui::layout::Rect;
2use ratatui::text::Span;
3
4use crate::primitives::button::Button;
5
6impl Button {
7    /// Renders the button at a specified offset from the right edge.
8    ///
9    /// # Arguments
10    ///
11    /// * `panel_area` - The area where the button will be rendered
12    /// * `offset_from_right` - Distance from the right edge of the panel
13    ///
14    /// # Returns
15    ///
16    /// A tuple containing the styled span and the rendered area
17    pub fn render_at_offset(
18        &self,
19        panel_area: Rect,
20        offset_from_right: u16,
21    ) -> (Span<'static>, Rect) {
22        let button_text = format!(" [{}] ", self.text);
23        let button_width = button_text.len() as u16;
24        let button_x = panel_area.x
25            + panel_area
26                .width
27                .saturating_sub(offset_from_right + button_width + 2);
28        let button_y = panel_area.y;
29
30        let area = Rect {
31            x: button_x,
32            y: button_y,
33            width: button_width,
34            height: 1,
35        };
36
37        let style = if self.hovered {
38            self.hover_style
39        } else {
40            self.normal_style
41        };
42
43        (Span::styled(button_text, style), area)
44    }
45}