mod tokens;
use crate::render::{RenderContext, TextAlign, TextBaseline};
use crate::types::Rect;
use crate::ui::widgets::atomic::session_pill::path_round_rect;
#[derive(Clone, Debug)]
pub struct BubbleView<'a> {
pub body: &'a str,
pub from: Option<&'a str>,
pub mine: bool,
}
#[derive(Clone, Debug)]
pub struct BubbleStyle {
pub mine_bg: &'static str,
pub mine_fg: &'static str,
pub other_bg: &'static str,
pub other_fg: &'static str,
pub from_fg: &'static str,
pub font: &'static str,
pub line_h: f64,
pub max_share: f64,
}
impl Default for BubbleStyle {
fn default() -> Self {
Self {
mine_bg: tokens::MINE_BG,
mine_fg: tokens::MINE_FG,
other_bg: tokens::OTHER_BG,
other_fg: tokens::OTHER_FG,
from_fg: tokens::FROM_FG,
font: "14.5px sans-serif",
line_h: 21.0,
max_share: 0.72,
}
}
}
pub fn wrap_lines(ctx: &mut dyn RenderContext, text: &str, max_w: f64) -> Vec<String> {
let mut out = Vec::new();
for para in text.split('\n') {
let mut line = String::new();
for (wi, word) in para.split(' ').enumerate() {
for (si, seg) in hyphen_segments(word).into_iter().enumerate() {
let sep = if si == 0 && wi > 0 && !line.is_empty() { " " } else { "" };
let cand = format!("{line}{sep}{seg}");
if ctx.measure_text(&cand) <= max_w || line.is_empty() {
line = cand;
} else {
out.push(std::mem::take(&mut line));
line = seg.to_string();
}
}
}
out.push(line);
}
out
}
pub(crate) fn hyphen_segments(word: &str) -> Vec<&str> {
let mut out = Vec::new();
let mut start = 0;
let cs: Vec<(usize, char)> = word.char_indices().collect();
for i in 1..cs.len().saturating_sub(1) {
let (bi, c) = cs[i];
if c == '-' && cs[i - 1].1.is_alphanumeric() && cs[i + 1].1.is_alphanumeric() {
out.push(&word[start..bi + 1]);
start = bi + 1;
}
}
out.push(&word[start..]);
out
}
pub fn bubble_height(ctx: &mut dyn RenderContext, view: &BubbleView<'_>, col_w: f64, style: &BubbleStyle) -> f64 {
ctx.set_font(style.font);
let n = wrap_lines(ctx, view.body, col_w * style.max_share - 28.0).len() as f64;
let from = if view.from.is_some() && !view.mine { 18.0 } else { 0.0 };
from + n * style.line_h + 18.0
}
pub fn draw_bubble(ctx: &mut dyn RenderContext, col: Rect, view: &BubbleView<'_>, style: &BubbleStyle) -> f64 {
ctx.set_font(style.font);
let lines = wrap_lines(ctx, view.body, col.width * style.max_share - 28.0);
let tw = lines.iter().map(|l| ctx.measure_text(l)).fold(0.0, f64::max);
let w = tw + 28.0;
let mut y = col.y;
ctx.set_text_align(TextAlign::Left);
ctx.set_text_baseline(TextBaseline::Middle);
if let (Some(f), false) = (view.from, view.mine) {
ctx.set_font("12.5px sans-serif");
ctx.set_fill_color(style.from_fg);
ctx.fill_text(f, col.x + 4.0, y + 8.0);
ctx.set_font(style.font);
y += 18.0;
}
let h = lines.len() as f64 * style.line_h + 18.0;
let x = if view.mine { col.x + col.width - w } else { col.x };
path_round_rect(ctx, Rect::new(x, y, w, h), 18.0);
ctx.set_fill_color(if view.mine { style.mine_bg } else { style.other_bg });
ctx.fill();
ctx.set_fill_color(if view.mine { style.mine_fg } else { style.other_fg });
for (i, l) in lines.iter().enumerate() {
ctx.fill_text(l, x + 14.0, y + 9.0 + style.line_h * (i as f64 + 0.5));
}
(y - col.y) + h
}
#[cfg(test)]
mod wrap_tests {
use super::*;
use crate::testing::RecordingRenderContext;
#[test]
fn wraps_at_spaces_and_after_hyphens_like_css() {
let mut ctx = RecordingRenderContext::with_char_width(1.0);
assert_eq!(wrap_lines(&mut ctx, "canary one-task-model", 16.0), vec!["canary one-task-", "model"]);
assert_eq!(wrap_lines(&mut ctx, "a b", 10.0), vec!["a b"]);
assert_eq!(wrap_lines(&mut ctx, "x prompt/tools/sub", 8.0), vec!["x", "prompt/tools/sub"]);
assert_eq!(hyphen_segments("-a-b-"), vec!["-a-", "b-"]);
assert_eq!(wrap_lines(&mut ctx, "l1\nl2", 80.0), vec!["l1", "l2"]);
}
}