plannotator-tui 0.11.5

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
//! The header: the send button and, for file and folder reviews, the Review menu button; a
//! gated review puts its Approve button on the edge. They wrap onto another row only when
//! a pane is too narrow for them.

use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::text::Span;
use unicode_width::UnicodeWidthStr;

use super::App;
use crate::gate::Gate;
use crate::keys::Action;
use crate::theme::palette;
use crate::ui::chips::chips;

const REVIEW_LABEL: &str = "Review \u{25be}";

#[derive(Debug, Clone, Copy)]
enum Button {
    Approve,
    Send,
    Review,
}

impl App {
    /// Buttons with their rects relative to the header, right to left: send on the edge,
    /// the Review menu to its left. A gated review puts Approve on the edge, before send.
    fn header_buttons(&self, width: u16) -> Vec<(Button, String, Rect)> {
        if width == 0 {
            return Vec::new();
        }
        let mut labels = Vec::new();
        if self.gate == Gate::Approve {
            labels.push((Button::Approve, self.approve_label()));
        }
        labels.push((Button::Send, self.send_label()));
        if self.is_file_review() {
            labels.push((Button::Review, self.review_label()));
        }
        let mut right = width;
        let mut y = 0;
        labels
            .into_iter()
            .map(|(button, label)| {
                let label = format!(" {label} ");
                let button_width = label.width().min(usize::from(width)) as u16;
                if button_width > right {
                    y += 1;
                    right = width;
                }
                let rect = Rect { x: right - button_width, y, width: button_width, height: 1 };
                right = rect.x.saturating_sub(1);
                (button, label, rect)
            })
            .collect()
    }

    /// The Review button names the key that opens the menu, when it has one.
    fn review_label(&self) -> String {
        match self.keys.hint(Action::ReviewMenu) {
            key if key.is_empty() => REVIEW_LABEL.to_owned(),
            key => format!("{REVIEW_LABEL} ({key})"),
        }
    }

    pub(super) fn header_height(&self, width: u16) -> u16 {
        self.header_buttons(width).last().map_or(1, |(_, _, rect)| rect.y + 1)
    }

    pub(super) fn draw_header(&mut self, frame: &mut Frame, area: Rect) {
        for (button, label, mut rect) in self.header_buttons(area.width) {
            if rect.y >= area.height {
                continue;
            }
            rect.x += area.x;
            rect.y += area.y;
            let style = match button {
                Button::Approve => {
                    self.geometry.approve_button = Some(rect);
                    chips().approve
                }
                Button::Send => {
                    self.geometry.send_button = Some(rect);
                    self.button_style()
                }
                Button::Review => {
                    self.geometry.review_button = Some(rect);
                    Style::new().fg(chips().menu_fg).bg(palette().toolbar_bg)
                }
            };
            frame.buffer_mut().set_span(rect.x, rect.y, &Span::styled(label, style), rect.width);
        }
    }

    fn button_style(&self) -> Style {
        crate::ui::send_button::style(&self.send_state, self.send_count())
    }
}