use crate::input::core::coordinator::LayerId;
use crate::input::{InputCoordinator, Sense, WidgetKind};
use crate::render::{RenderContext, TextAlign};
use crate::ui::css_text::{fill_text_css, INTER};
use crate::types::{Rect, WidgetId};
use crate::ui::widgets::atomic::session_pill::path_round_rect;
#[derive(Clone, Debug)]
pub struct ComposerView<'a> {
pub text: &'a str,
pub placeholder: &'a str,
pub hovered: Option<ComposerPart>,
pub cursor: Option<usize>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ComposerPart {
Attach,
Mic,
Send,
Text,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ComposerHits {
pub frame: Rect,
pub text: Rect,
pub attach: Rect,
pub mic: Rect,
pub send: Rect,
}
#[derive(Clone, Debug)]
pub struct ComposerStyle {
pub height: f64,
pub max_width: f64,
pub side: f64,
pub bottom: f64,
pub radius: f64,
pub pad: f64,
pub gap: f64,
pub btn: f64,
pub bg: &'static str,
pub border: &'static str,
pub border_focus: &'static str,
pub font: &'static str,
pub font_px: f64,
pub text: &'static str,
pub placeholder: &'static str,
pub caret: &'static str,
pub btn_bg: &'static str,
pub btn_hover: &'static str,
pub btn_fg: &'static str,
pub send_bg: &'static str,
pub send_hover: &'static str,
}
impl Default for ComposerStyle {
fn default() -> Self {
Self {
height: 44.0,
max_width: 780.0,
side: 16.0,
bottom: 16.0,
radius: 22.0,
pad: 7.0,
gap: 8.0,
btn: 28.0,
bg: "#1f1f1f",
border: "#353535",
border_focus: "#404040",
font: "14.5px Inter, sans-serif",
font_px: 14.5,
text: "#f5f5f5",
placeholder: "#5e5e66",
caret: "#f5f5f5",
btn_bg: "#282828",
btn_hover: "#323232",
btn_fg: "#f5f5f5",
send_bg: "#116dd2",
send_hover: "#1884fc",
}
}
}
pub const COMPOSER_HEIGHT: f64 = 44.0;
pub fn composer_layout_styled(area: Rect, st: &ComposerStyle) -> ComposerHits {
let side = st.side.max((area.width - st.max_width) / 2.0);
let w = (area.width - 2.0 * side).max(120.0);
let frame = Rect::new(area.x + side, area.y + area.height - st.bottom - st.height, w, st.height);
let inner = 1.0 + st.pad;
let by = frame.y + inner;
let attach = Rect::new(frame.x + inner, by, st.btn, st.btn);
let send = Rect::new(frame.x + frame.width - inner - st.btn, by, st.btn, st.btn);
let mic = Rect::new(send.x - st.gap - st.btn, by, st.btn, st.btn);
let tx = attach.x + st.btn + st.gap;
ComposerHits { frame, text: Rect::new(tx, frame.y + 1.0, (mic.x - st.gap - tx).max(10.0), st.height - 2.0), attach, mic, send }
}
pub fn composer_layout(area: Rect) -> ComposerHits {
composer_layout_styled(area, &ComposerStyle::default())
}
fn round_btn(ctx: &mut dyn RenderContext, r: Rect, fill: &str) {
ctx.begin_path();
ctx.arc(r.x + r.width / 2.0, r.y + r.height / 2.0, r.width / 2.0, 0.0, std::f64::consts::TAU);
ctx.set_fill_color(fill);
ctx.fill();
}
struct Svg {
x: f64,
y: f64,
k: f64,
}
impl Svg {
fn new(r: Rect, size: f64) -> Self {
Self { x: (r.x + (r.width - size) / 2.0).round(), y: (r.y + (r.height - size) / 2.0).round(), k: size / 24.0 }
}
fn m(&self, ctx: &mut dyn RenderContext, x: f64, y: f64) {
ctx.move_to(self.x + x * self.k, self.y + y * self.k);
}
fn l(&self, ctx: &mut dyn RenderContext, x: f64, y: f64) {
ctx.line_to(self.x + x * self.k, self.y + y * self.k);
}
}
pub fn draw_composer_styled(ctx: &mut dyn RenderContext, hits: &ComposerHits, view: &ComposerView<'_>, st: &ComposerStyle) -> Vec<f64> {
let f = hits.frame;
path_round_rect(ctx, f, st.radius);
ctx.set_fill_color(if view.cursor.is_some() { st.border_focus } else { st.border });
ctx.fill();
path_round_rect(ctx, Rect::new(f.x + 1.0, f.y + 1.0, f.width - 2.0, f.height - 2.0), st.radius - 1.0);
ctx.set_fill_color(st.bg);
ctx.fill();
ctx.set_font(st.font);
let cy = f.y + f.height / 2.0;
let (t, c) = if view.text.is_empty() { (view.placeholder, st.placeholder) } else { (view.text, st.text) };
ctx.set_fill_color(c);
let lh = st.font_px * 1.45;
fill_text_css(ctx, t, hits.text.x, cy - lh / 2.0, st.font_px, lh, INTER, TextAlign::Left);
let mut positions = vec![hits.text.x];
let mut acc = String::new();
for ch in view.text.chars() {
acc.push(ch);
positions.push(hits.text.x + ctx.measure_text(&acc));
}
if let Some(c) = view.cursor {
let x = positions.get(c).copied().unwrap_or(*positions.last().unwrap_or(&hits.text.x));
ctx.set_fill_color(st.caret);
ctx.fill_rect(x.round(), (cy - 9.0).round(), 1.0, 18.0);
}
let hov = |p| view.hovered == Some(p);
round_btn(ctx, hits.attach, if hov(ComposerPart::Attach) { st.btn_hover } else { st.btn_bg });
round_btn(ctx, hits.mic, if hov(ComposerPart::Mic) { st.btn_hover } else { st.btn_bg });
round_btn(ctx, hits.send, if hov(ComposerPart::Send) { st.send_hover } else { st.send_bg });
ctx.set_line_cap("round");
ctx.set_line_join("round");
let g = Svg::new(hits.attach, 18.0);
ctx.set_stroke_color(st.btn_fg);
ctx.set_stroke_width(2.0 * g.k);
ctx.begin_path();
g.m(ctx, 12.0, 5.0); g.l(ctx, 12.0, 19.0);
g.m(ctx, 5.0, 12.0); g.l(ctx, 19.0, 12.0);
ctx.stroke();
let g = Svg::new(hits.mic, 17.0);
ctx.set_stroke_width(2.0 * g.k);
path_round_rect(ctx, Rect::new(g.x + 9.0 * g.k, g.y + 3.0 * g.k, 6.0 * g.k, 11.0 * g.k), 3.0 * g.k);
ctx.stroke();
ctx.begin_path();
ctx.arc(g.x + 12.0 * g.k, g.y + 11.0 * g.k, 7.0 * g.k, 0.0, std::f64::consts::PI);
ctx.stroke();
ctx.begin_path();
g.m(ctx, 12.0, 18.0); g.l(ctx, 12.0, 21.0);
ctx.stroke();
let g = Svg::new(hits.send, 17.0);
ctx.set_stroke_color("#ffffff");
ctx.set_stroke_width(2.4 * g.k);
ctx.begin_path();
g.m(ctx, 12.0, 19.0); g.l(ctx, 12.0, 5.0);
g.m(ctx, 6.0, 11.0); g.l(ctx, 12.0, 5.0); g.l(ctx, 18.0, 11.0);
ctx.stroke();
ctx.set_line_cap("butt");
ctx.set_line_join("miter");
positions
}
pub fn draw_composer(ctx: &mut dyn RenderContext, hits: &ComposerHits, view: &ComposerView<'_>) -> Vec<f64> {
draw_composer_styled(ctx, hits, view, &ComposerStyle::default())
}
pub fn register_composer(coord: &mut InputCoordinator, id_prefix: &str, hits: &ComposerHits, layer: &LayerId) {
for (s, r) in [("attach", hits.attach), ("mic", hits.mic), ("send", hits.send), ("text", hits.text)] {
coord.register_atomic(WidgetId::from(format!("{id_prefix}-{s}")), WidgetKind::Button, r, Sense::CLICK, layer);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn layout_bottom_centred() {
let h = composer_layout(Rect::new(259.0, 0.0, 841.0, 680.0));
assert_eq!((h.frame.x, h.frame.y, h.frame.width, h.frame.height), (289.5, 620.0, 780.0, 44.0));
assert_eq!((h.attach.x, h.attach.y), (297.5, 628.0));
assert_eq!(h.mic.x, 997.5);
assert_eq!(h.send.x, 1033.5);
assert_eq!(h.text.x, 333.5);
}
}