#![allow(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_precision_loss,
clippy::cast_sign_loss
)]
use ratatui::Frame;
use ratatui::layout::{Alignment, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, BorderType, Borders, Clear, Paragraph};
use super::dim;
use super::history::rule_line;
use super::style::glyph;
use crate::app_view::AppView;
const WELCOME_MAX: u16 = 110;
pub(super) fn render_welcome(f: &mut Frame, app: &mut AppView, area: Rect) {
let mut lines: Vec<Line> = Vec::new();
let banner_lines: Vec<&str> = app.banner.lines().collect();
let n = banner_lines.len().max(1);
for (i, l) in banner_lines.into_iter().enumerate() {
let t = if n > 1 {
i as f32 / (n - 1) as f32
} else {
0.0
};
let color = ramp(app.theme.accent, app.theme.accent2, t);
lines.push(Line::from(Span::styled(
l.to_string(),
Style::default().fg(color).add_modifier(Modifier::BOLD),
)));
}
lines.push(Line::from(""));
lines.push(Line::from(Span::styled(
app.greeting.to_string(),
Style::default().add_modifier(Modifier::BOLD),
)));
lines.push(Line::from(dim(
chrono::Local::now()
.format("%A, %B %-d %Y · %H:%M:%S")
.to_string(),
&app.theme,
)));
let model = app.current_model.as_deref().map_or_else(
|| "pick a model with /model".to_string(),
super::short_model_label,
);
lines.push(Line::from(vec![
Span::styled(
format!("{} ", glyph::MODEL),
Style::default().fg(app.theme.accent),
),
Span::styled(model, Style::default().fg(app.theme.accent)),
super::style::sep(&app.theme),
dim(
format!("{} {}", glyph::SPACE, app.active_space.name),
&app.theme,
),
]));
if !app.settings.hide_hints {
lines.push(Line::from(""));
lines.push(chip_row(
&["/research", "/swarm", "/model", "/help"],
&app.theme,
));
}
let recent_rows = push_recent(&mut lines, app, area);
let panel_w = area.width.min(WELCOME_MAX);
let panel_h = (lines.len() + 2).min(area.height as usize) as u16;
let panel = Rect {
x: area.x + area.width.saturating_sub(panel_w) / 2,
y: area.y + area.height.saturating_sub(panel_h) / 2,
width: panel_w,
height: panel_h,
};
f.render_widget(Clear, panel);
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(app.theme.border_dim));
let inner = block.inner(panel);
f.render_widget(block, panel);
f.render_widget(Paragraph::new(lines).alignment(Alignment::Center), inner);
app.welcome_targets = recent_rows
.into_iter()
.filter_map(|(row, id)| {
let y = inner.y + u16::try_from(row).ok()?;
(y < inner.bottom()).then(|| (Rect::new(inner.x, y, inner.width, 1), id))
})
.collect();
}
fn ramp(a: Color, b: Color, t: f32) -> Color {
let mix = |x: u8, y: u8| {
let (xf, yf) = (f32::from(x), f32::from(y));
(xf + (yf - xf) * t).round() as u8
};
match (a, b) {
(Color::Rgb(r1, g1, b1), Color::Rgb(r2, g2, b2)) => {
Color::Rgb(mix(r1, r2), mix(g1, g2), mix(b1, b2))
}
_ => a,
}
}
fn chip_row(cmds: &[&str], theme: &crate::theme::Theme) -> Line<'static> {
let mut spans = Vec::new();
for (i, cmd) in cmds.iter().enumerate() {
if i > 0 {
spans.push(Span::raw(" "));
}
spans.push(Span::styled("[ ", Style::default().fg(theme.border_dim)));
spans.push(Span::styled(
cmd.to_string(),
Style::default().fg(theme.accent),
));
spans.push(Span::styled(" ]", Style::default().fg(theme.border_dim)));
}
Line::from(spans)
}
fn push_recent(lines: &mut Vec<Line<'static>>, app: &AppView, area: Rect) -> Vec<(usize, String)> {
let mut recent_rows: Vec<(usize, String)> = Vec::new();
if let Ok(sessions) = app.db.list_sessions(&app.active_space.id) {
let recent: Vec<_> = sessions.into_iter().take(4).collect();
if !recent.is_empty() {
lines.push(Line::from(""));
let inner_w = area.width.min(WELCOME_MAX).saturating_sub(4) as usize;
let row_w = inner_w.min(60);
rule_line(lines, "recent", row_w, &app.theme);
for (i, s) in recent.iter().enumerate() {
let when = super::fmt_created(&s.created_at);
let title_w = row_w.saturating_sub(4 + crate::ui::style::width(&when));
let title = crate::ui::popups::chrome::truncate(&s.title, title_w);
let pad = title_w.saturating_sub(crate::ui::style::width(&title));
recent_rows.push((lines.len(), s.id.clone()));
lines.push(Line::from(vec![
Span::styled(
format!("{} ", i + 1),
Style::default()
.fg(app.theme.accent)
.add_modifier(Modifier::BOLD),
),
Span::styled(title, Style::default().fg(app.theme.fg)),
Span::raw(" ".repeat(pad + 2)),
Span::styled(when, Style::default().fg(app.theme.fg_dim)),
]));
}
if !app.settings.hide_hints {
lines.push(Line::from(dim("Alt+1–4 or click to reopen", &app.theme)));
}
}
}
recent_rows
}