use gtk4::cairo::Context;
use gtk4::pango;
use super::cairo_rgb;
use crate::primitives::message_list::MessageList;
#[allow(clippy::too_many_arguments)]
pub fn draw_message_list(
cr: &Context,
layout: &pango::Layout,
list: &MessageList,
x: f64,
y: f64,
w: f64,
max_y: f64,
line_height: f64,
) {
if w <= 0.0 || line_height <= 0.0 {
return;
}
let to_u16 = |c: u8| -> u16 { ((c as u16) << 8) | c as u16 };
layout.set_attributes(None);
for (i, row) in list.rows.iter().skip(list.scroll_top).enumerate() {
let ry = y + i as f64 * line_height;
if ry + line_height > max_y {
break;
}
if !row.spans.is_empty() {
let attrs = pango::AttrList::new();
if (row.scale - 1.0).abs() > 0.01 {
let mut a = pango::AttrFloat::new_scale(row.scale as f64);
a.set_start_index(0);
a.set_end_index(row.text.len() as u32);
attrs.insert(a);
}
let mut byte_pos = 0usize;
for span in &row.spans {
let start = byte_pos as u32;
let end = (byte_pos + span.text.len()) as u32;
if let Some(c) = span.fg {
let mut a =
pango::AttrColor::new_foreground(to_u16(c.r), to_u16(c.g), to_u16(c.b));
a.set_start_index(start);
a.set_end_index(end);
attrs.insert(a);
}
if span.bold {
let mut a = pango::AttrInt::new_weight(pango::Weight::Bold);
a.set_start_index(start);
a.set_end_index(end);
attrs.insert(a);
}
if span.italic {
let mut a = pango::AttrInt::new_style(pango::Style::Italic);
a.set_start_index(start);
a.set_end_index(end);
attrs.insert(a);
}
if span.underline {
let mut a = pango::AttrInt::new_underline(pango::Underline::Single);
a.set_start_index(start);
a.set_end_index(end);
attrs.insert(a);
}
byte_pos += span.text.len();
}
let (r, g, b) = cairo_rgb(row.fg);
cr.set_source_rgb(r, g, b);
layout.set_text(&row.text);
layout.set_attributes(Some(&attrs));
let (_, lh) = layout.pixel_size();
cr.move_to(x + row.indent as f64, ry + (line_height - lh as f64) / 2.0);
super::painted_text::show_layout(cr, layout);
layout.set_attributes(None);
} else {
let (r, g, b) = cairo_rgb(row.fg);
cr.set_source_rgb(r, g, b);
layout.set_text(&row.text);
let (_, lh) = layout.pixel_size();
cr.move_to(x + row.indent as f64, ry + (line_height - lh as f64) / 2.0);
super::painted_text::show_layout(cr, layout);
}
}
}