Skip to main content

bhoo/systems/material/
chat.rs

1//! Material chat message bubbles and docked message composer bar.
2use crate::*;
3
4/// One chat message for `Frame::chat_bubble`.
5#[derive(Clone, Copy, Debug)]
6pub struct ChatMessage<'a> {
7    pub body: &'a str,
8    pub timestamp: &'a str,
9    pub outgoing: bool,
10}
11
12impl Frame<'_> {
13    /// Render one incoming or outgoing Material chat message bubble with wrapped body text
14    /// and optional timestamp.
15    pub fn chat_bubble(&mut self, message: ChatMessage<'_>) {
16        let t = self.theme();
17        let avail = self.available();
18        let max_w = (avail.width * 0.78).clamp(120.0, 460.0);
19        let style = TextStyle { size: 14.0, ..TextStyle::default() };
20        let inner_w = (max_w - 28.0).max(40.0);
21        let lines = self.ui.text_metrics.lines(message.body, inner_w, style, |_| {}).max(1);
22        let has_ts = !message.timestamp.is_empty();
23        let bubble_h = lines as f32 * 22.0 + 20.0 + if has_ts { 16.0 } else { 0.0 };
24        let row = self.reserve(bubble_h);
25        let content_w = if lines == 1 {
26            (self.text_width(message.body, style) + 32.0).clamp(72.0, max_w)
27        } else {
28            max_w
29        };
30        let bubble = Rect {
31            x: if message.outgoing { row.x + row.width - content_w } else { row.x },
32            y: row.y,
33            width: content_w,
34            height: row.height,
35        };
36        let (bg, fg) = if message.outgoing {
37            (t.primary_container, t.on_primary_container)
38        } else {
39            (t.surface_high, t.text)
40        };
41        self.paint_rect(bubble, 16.0, bg);
42        let text_area = Rect {
43            x: bubble.x + 14.0,
44            y: bubble.y + 10.0,
45            width: (bubble.width - 28.0).max(0.0),
46            height: (bubble.height - 20.0).max(0.0),
47        };
48        self.within(text_area, |f| {
49            f.paragraph(text_area, message.body, fg, style, 22.0);
50        });
51        if has_ts {
52            self.styled_text(
53                Rect {
54                    x: bubble.x + 14.0,
55                    y: bubble.y + bubble.height - 18.0,
56                    width: (bubble.width - 28.0).max(0.0),
57                    height: 14.0,
58                },
59                message.timestamp,
60                fg.opacity(0.65),
61                TextStyle { size: 11.0, align: if message.outgoing { 2 } else { 0 }, ..TextStyle::default() },
62            );
63        }
64    }
65
66    /// Docked message composer bar combining a text input field and send icon button.
67    /// Returns `(field_response, send_clicked)`.
68    pub fn message_composer(
69        &mut self,
70        id: u64,
71        bounds: Rect,
72        placeholder: &str,
73        draft: &str,
74        enabled: bool,
75    ) -> (Response, bool) {
76        let t = self.theme();
77        self.paint_rect(bounds, 0.0, t.surface_low);
78        self.paint_rect(Rect { height: 1.0, ..bounds }, 0.0, t.outline_variant);
79        let pad = 12.0;
80        let send_sz = 44.0;
81        let field_rect = Rect {
82            x: bounds.x + pad,
83            y: bounds.y + (bounds.height - 56.0).max(0.0) * 0.5,
84            width: (bounds.width - pad * 3.0 - send_sz).max(0.0),
85            height: 56.0_f32.min(bounds.height),
86        };
87        let field_resp = self.text_field_at(id, field_rect, placeholder, draft, false, true);
88        let send_rect = Rect {
89            x: bounds.x + bounds.width - pad - send_sz,
90            y: bounds.y + (bounds.height - send_sz) * 0.5,
91            width: send_sz,
92            height: send_sz,
93        };
94        let can_send = enabled && !draft.trim().is_empty();
95        let send_resp = self.material_icon_button_at(
96            id.wrapping_add(1),
97            send_rect,
98            MaterialIcon::Send,
99            "Send message",
100            Options {
101                enabled: can_send,
102                variant: ButtonVariant::Filled,
103                ..Options::default()
104            },
105        );
106        (field_resp, send_resp.clicked)
107    }
108}
109
110#[cfg(test)]
111#[path = "chat.spec.rs"]
112mod tests;