#![windows_subsystem = "windows"]
use std::time::{Duration, Instant};
use taut::{Align, Flow, Key, Length, Mods, Ui, Window};
const ANSWERS: [&str; 5] = [
"Hello! Good to hear from you.",
"I was just about to write to you, in fact. How did the move go in the end, and is the new place as quiet as you hoped?",
"Ha, that sounds about right.",
"Shall we meet on Saturday?",
"Good. See you then!",
];
const THINKING: Duration = Duration::from_millis(1200);
pub struct Message {
mine: bool,
text: String,
}
#[derive(Default)]
pub struct Chat {
messages: Vec<Message>,
typed: String,
at_end: bool,
unread: usize,
to_end: bool,
now: Duration,
answer_at: Option<Duration>,
answered: usize,
}
impl Chat {
fn add(&mut self, mine: bool, text: String) {
self.messages.push(Message { mine, text });
self.unread += !self.at_end as usize;
}
}
pub fn describe(app: &mut Chat, ui: &mut Ui) {
let t = ui.theme;
match app.answer_at {
Some(at) if at <= app.now => {
app.add(false, ANSWERS[app.answered % ANSWERS.len()].into());
(app.answer_at, app.answered) = (None, app.answered + 1);
}
Some(at) => ui.wake_after(at - app.now),
None => {}
}
ui.column().grow().open();
ui.el().grow().flow(Flow::Stack).open();
let list = ui.column().key("list").grow().scroll().align(Align::End, Align::Spread).open();
ui.stick_to_bottom(&list);
if std::mem::take(&mut app.to_end) {
ui.scroll_to(&list, [f32::NAN, f32::INFINITY]);
}
let wide = ui.size().w;
for at in ui.variable_rows(app.messages.len(), 40.0) {
bubble(&app.messages[at], wide, ui);
}
ui.close();
ui.close();
app.at_end = ui.scroll_state(&list).at_end();
app.unread = if app.at_end { 0 } else { app.unread };
if app.messages.is_empty() {
ui.row().place(Align::Center).open();
ui.text("No messages yet. Say hello!", t.font, t.text_faint);
ui.close();
}
if app.answer_at.is_some() {
ui.row().place((Align::Start, Align::End)).pad((14.0, 4.0)).open();
ui.note("Sam is writing\u{2026}");
ui.close();
}
if !app.at_end {
ui.row().place((Align::Center, Align::End)).pad(6.0).gap(8.0).fill(t.raised).radius(t.radius).align(Align::Center, Align::Center).open();
if app.unread > 0 {
ui.note(&format!("{} new", app.unread));
}
app.to_end |= ui.button("Back to the newest").clicked;
ui.close();
}
ui.close();
ui.row().pad(10.0).gap(8.0).fill(t.panel).open();
ui.enter_submits();
let typing = ui.text_area(&mut app.typed, "Write a message", (1, 4));
let send = ui.primary_button("Send").clicked || typing.focused && ui.key(Key::Enter, Mods::NONE);
ui.close();
ui.close();
if send && !app.typed.trim().is_empty() {
let text = std::mem::take(&mut app.typed);
app.add(true, text.trim().into());
(app.to_end, app.answer_at) = (true, Some(app.now + THINKING));
}
}
fn bubble(message: &Message, wide: f32, ui: &mut Ui) {
let t = ui.theme;
let (side, fill, ink) = if message.mine { (Align::End, t.accent, t.on_accent) } else { (Align::Start, t.raised, t.text) };
ui.row().w(Length::Grow(1.0)).pad((12.0, 3.0)).align(side, Align::Start).open();
ui.column().max((wide * 0.75).max(120.0), f32::INFINITY).pad((10.0, 6.0)).fill(fill).radius(10.0).open();
ui.el().text(&message.text, t.font, ink).wrap().show();
ui.close();
ui.close();
}
fn main() {
let (mut app, started) = (Chat::default(), Instant::now());
let window = Window { title: "Chat", width: 420.0, height: 560.0, min_width: 280.0, min_height: 240.0, ..Default::default() };
taut::run(window, move |ui| {
app.now = started.elapsed();
describe(&mut app, ui);
});
}
#[cfg(test)]
mod tests {
use super::*;
use taut::Role;
use taut::testing::Trial;
fn send(trial: &mut Trial<Chat>, text: &str) {
trial.fill("Write a message", text);
trial.key("Enter");
}
#[test]
fn the_list_stays_at_its_end_while_the_person_is_there() {
let mut trial = Trial::new(Chat::default(), describe);
trial.set_time(|chat, now| chat.now = now);
trial.resize(360.0, 400.0, 1.0);
assert!(trial.has(Role::Text, "No messages yet. Say hello!"));
trial.fill("Write a message", "Hi Sam!");
trial.key("Shift+Enter");
trial.type_text("How are you?");
trial.key("Enter");
assert!(trial.has(Role::Text, "Hi Sam!\nHow are you?") && trial.has(Role::Text, "Sam is writing\u{2026}"));
assert_eq!(trial.find(Role::TextBox, "Write a message").value, "", "the box is empty at once");
trial.wait(Duration::from_secs(2));
assert!(trial.has(Role::Text, ANSWERS[0]) && !trial.has(Role::Text, "Sam is writing\u{2026}"));
trial.looks_like("tests/looks/chat.png");
for number in 1..=12 {
send(&mut trial, &format!("Message {number}"));
}
let (last, typing) = (trial.find(Role::Text, "Message 12").rect, trial.find(Role::TextBox, "Write a message").rect);
assert!(last.bottom() <= typing.y && last.bottom() > typing.y - 60.0, "the newest is just over the box to type in: {last:?}");
assert!(!trial.has(Role::Text, "Hi Sam!\nHow are you?"));
trial.wait(Duration::from_secs(2));
assert!(trial.has(Role::Text, ANSWERS[1]), "and the answer that came is in view, at the end:\n{}", trial.dump());
let middle = trial.center_of(Role::Text, "Message 12");
trial.wheel(middle, 0.0, -10_000.0);
assert!(trial.has(Role::Text, "Hi Sam!\nHow are you?") && trial.has(Role::Button, "Back to the newest"));
trial.app.answer_at = Some(trial.app.now + THINKING); trial.run();
trial.wait(Duration::from_secs(2));
assert!(trial.has(Role::Text, "Hi Sam!\nHow are you?") && trial.has(Role::Text, "1 new"));
trial.press(Role::Button, "Back to the newest");
assert!(trial.has(Role::Text, ANSWERS[2]) && !trial.has(Role::Text, "Hi Sam!\nHow are you?"));
assert!(!trial.has(Role::Button, "Back to the newest"));
let middle = trial.center_of(Role::Text, ANSWERS[2]);
trial.wheel(middle, 0.0, -10_000.0);
send(&mut trial, "Bye!");
assert!(trial.has(Role::Text, "Bye!") && trial.app.at_end);
}
}