use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::Span;
use unicode_width::UnicodeWidthStr;
use super::App;
use super::send::SendState;
use crate::gate::Gate;
use crate::keys::Action;
use crate::theme::palette;
const SEND_BG: Color = Color::Indexed(30);
const SENT_BG: Color = Color::Indexed(22);
const BLOCKED_BG: Color = Color::Indexed(58);
const APPROVE_BG: Color = Color::Indexed(28);
const REVIEW_LABEL: &str = "Review \u{25be}";
#[derive(Debug, Clone, Copy)]
enum Button {
Approve,
Send,
Review,
}
impl App {
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()
}
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);
Style::new().fg(Color::Black).bg(APPROVE_BG).bold()
}
Button::Send => {
self.geometry.send_button = Some(rect);
self.button_style()
}
Button::Review => {
self.geometry.review_button = Some(rect);
Style::new().fg(palette().accent).bg(palette().toolbar_bg)
}
};
frame.buffer_mut().set_span(rect.x, rect.y, &Span::styled(label, style), rect.width);
}
}
fn button_style(&self) -> Style {
match &self.send_state {
SendState::Ready if self.send_count() == 0 => {
Style::new().fg(palette().idle_fg).bg(palette().toolbar_bg).add_modifier(Modifier::DIM)
}
SendState::Ready => Style::new().fg(Color::Black).bg(SEND_BG).bold(),
SendState::Sent => Style::new().fg(Color::Black).bg(SENT_BG).bold(),
SendState::Blocked(_) => Style::new().fg(Color::Black).bg(BLOCKED_BG).bold(),
}
}
}