From c2becc089b98419a3cb38b580c77b9d496854c83 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 28 Nov 2023 17:19:25 +0800 Subject: [PATCH 01/12] implement default behavior when no dialog is specified. - the `post_add()` API on the 'lib' side of things says Add a new Post to the current `Dialogue`, and it passes an empty `dialogue_id` string. - the `post_add()` handler on the server dutifully unpacks the `dialog_id` field with `post.dialogue_id.as_str().unwrap()`, and then passes on to the `post_add()` call inside the UI object - the `post_add()` implementation in the UI object then checks the `dialog_id` with if `pddb_key.eq(&dialogue_id)`, expecting that the key should match the dialog id. But through this path, the dialogue ID is always the empty string, so the function can't work. This patch causes the code to "fall through" and use the default `pddb_key` if the length of the `dialog_id` is zero. Not sure if this is the intended behavior, but in the test case at least I get posts appearing now once this patch is in. --- libs/chat/src/ui.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/chat/src/ui.rs b/libs/chat/src/ui.rs index f564e425e..522053465 100644 --- a/libs/chat/src/ui.rs +++ b/libs/chat/src/ui.rs @@ -391,12 +391,12 @@ impl Ui { ) -> Result<(), Error> { match (&self.pddb_key, &mut self.dialogue) { (Some(pddb_key), Some(ref mut dialogue)) => { - if pddb_key.eq(&dialogue_id) { + if dialogue_id.len() == 0 || pddb_key.eq(&dialogue_id) { dialogue .post_add(author, timestamp, text, attach_url) .unwrap(); } else { - log::warn!("dropping Post as dialogue_id does not match pddb_key"); + log::warn!("dropping Post as dialogue_id does not match pddb_key: '{}' vs '{}'", pddb_key, dialogue_id); } } (None, _) => log::warn!("no pddb_key set to match dialogue_id"), From 60db6053bc2645cb2f49f345119bbb35bc437cc9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 28 Nov 2023 17:25:27 +0800 Subject: [PATCH 02/12] add a feature for testing the UI on mtxchat when `test-chat-ui` is selected as a feature and mtxchat is selected as an app to build, this will override the mainloop of the `mtxchat` app and create a test dialog & present it, laying a framework for controlled debugging of the layout engine. --- apps/mtxchat/Cargo.toml | 1 + apps/mtxchat/src/main.rs | 5 +++++ apps/mtxchat/src/tests.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 apps/mtxchat/src/tests.rs diff --git a/apps/mtxchat/Cargo.toml b/apps/mtxchat/Cargo.toml index f4fcc7dd4..837e3bdd3 100644 --- a/apps/mtxchat/Cargo.toml +++ b/apps/mtxchat/Cargo.toml @@ -41,3 +41,4 @@ url = "2.2.2" [features] default = [] +test-chat-ui = [] diff --git a/apps/mtxchat/src/main.rs b/apps/mtxchat/src/main.rs index 0b7ea6ef3..04f1a29bd 100644 --- a/apps/mtxchat/src/main.rs +++ b/apps/mtxchat/src/main.rs @@ -2,6 +2,9 @@ #![cfg_attr(target_os = "none", no_main)] mod api; +#[cfg(feature="test-chat-ui")] +mod tests; + use api::*; use chat::{Chat, Event, POST_TEXT_MAX}; use gam::{MenuItem, MenuPayload}; @@ -97,6 +100,8 @@ fn wrapped_main() -> ! { let mut mtxchat = MtxChat::new(&chat); let mut first_focus = true; let mut user_post: Option = None; + #[cfg(feature="test-chat-ui")] + tests::test_ui(&chat); loop { let msg = xous::receive_message(sid).unwrap(); log::debug!("got message {:?}", msg); diff --git a/apps/mtxchat/src/tests.rs b/apps/mtxchat/src/tests.rs new file mode 100644 index 000000000..01f150bed --- /dev/null +++ b/apps/mtxchat/src/tests.rs @@ -0,0 +1,34 @@ +use chat::Chat; + +pub fn test_ui(chat: &Chat) { + let tt = ticktimer_server::Ticktimer::new().unwrap(); + let pddb = pddb::Pddb::new(); + // nuke any existing test dictionary, if it exists + pddb.delete_dict("tests.ui", None).ok(); + pddb.sync().ok(); + + // re-create the test room from scratch every time + chat.dialogue_set("tests.ui", Some("test_room")).expect("couldn't set dialog"); + + // generate a list of test posts that are repeatable each time + chat.post_add("alice", 1_700_000_000, "hello world!", None).ok(); + chat.post_add("bob", 1_700_000_002, "hi alice!", None).ok(); + + tt.sleep_ms(2000).ok(); + log::info!("triggering save"); + xous::send_message( + chat.cid(), + xous::Message::new_scalar(chat::ChatOp::DialogueSave as usize, 0, 0, 0, 0), + ) + .expect("failed to send new inbound msgs"); + + log::info!("sleep"); + tt.sleep_ms(5000).ok(); + log::info!("redraw"); + chat.redraw(); + + loop { + tt.sleep_ms(5000).ok(); + log::info!("test done, displaying result"); + } +} From 5ae064fb3855a52f1abd957309d6fadc0ec2a697 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 28 Nov 2023 17:52:52 +0800 Subject: [PATCH 03/12] an initial test for look at post layouts --- apps/mtxchat/src/tests.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/apps/mtxchat/src/tests.rs b/apps/mtxchat/src/tests.rs index 01f150bed..55aa9d19a 100644 --- a/apps/mtxchat/src/tests.rs +++ b/apps/mtxchat/src/tests.rs @@ -13,8 +13,13 @@ pub fn test_ui(chat: &Chat) { // generate a list of test posts that are repeatable each time chat.post_add("alice", 1_700_000_000, "hello world!", None).ok(); chat.post_add("bob", 1_700_000_002, "hi alice!", None).ok(); + for i in 0..5 { + chat.post_add("alice", 1_700_000_005 + i*4, &format!("alice sez {}", i), None).ok(); + chat.post_add("bob", 1_700_000_006 + i*4, &format!("bob sez {}", i), None).ok(); + chat.post_add("trent", 1_700_000_007 + i*4, &format!("trent sez {}", i), None).ok(); + } + chat.post_add("alice", 1_700_001_000, "eom", None).ok(); - tt.sleep_ms(2000).ok(); log::info!("triggering save"); xous::send_message( chat.cid(), @@ -22,13 +27,9 @@ pub fn test_ui(chat: &Chat) { ) .expect("failed to send new inbound msgs"); - log::info!("sleep"); - tt.sleep_ms(5000).ok(); - log::info!("redraw"); - chat.redraw(); - + log::info!("test done, displaying result"); loop { + // just idle so that the main loop of mtxchat doesn't trigger and interfere with the test tt.sleep_ms(5000).ok(); - log::info!("test done, displaying result"); } } From dcd80763063b0e3775f875283540c4a40fcafb3d Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 28 Nov 2023 17:53:12 +0800 Subject: [PATCH 04/12] pull in the comments and notes from #445 so it's in the code --- libs/chat/src/ui.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libs/chat/src/ui.rs b/libs/chat/src/ui.rs index 522053465..7474aa270 100644 --- a/libs/chat/src/ui.rs +++ b/libs/chat/src/ui.rs @@ -687,12 +687,23 @@ impl Ui { /// Layout the post bubbles on the screen. /// + /// The challenge is to layout a sub-set of the posts on screen, ensuring that + /// the selected-post is fully displayed, and to do something non-jarring as the + /// user moves the selection up or down. + /// + /// That is, when the user clicks up then the currently selected post should go + /// un-bold, and the post above should go bold, without movement - unless the newly + /// selected post is partially or fully off-screen, in which case, the posts need + /// to move down. There are three edge cases, when the first or last post is reached, + /// or when the post is too big for the screen. And an additional challenge, + /// that the only way to calculate the vertical height of a post is to lay it out. + /// /// The layout proceeds from top-down or bottom-up (starting with the /// `post_anchor`), drawing a bubble for each Post, until the available space /// is exhausted. /// * If the `post_selected` is fully displayed, then `Ok(true)` is Returned. /// * If the `post_selected` is NOT fully displayed, then the `post_anchor` - /// is set as the `post_anchor`, and `Ok(false)` is Returned - signalling that + /// is set as the `post_selected`, and `Ok(false)` is Returned - signalling that /// a re-layout is in order. /// * If the first/last Post is fully displayed, then the `post_topdown` is /// toggled, the `post_anchor` is set to the first/last Post, and `Ok(false)` From 7222fb0de97de774248e9fb46a26de816a3a6233 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 30 Nov 2023 23:05:41 +0800 Subject: [PATCH 05/12] add an optional bounding rectangle field to posts this is computed when the post is added to the list of posts. Also split out the visual properties of a UI into a separate struct so that we can pass it as a single argument to routines that could handle layout-related tasks. --- libs/chat/src/dialogue.rs | 17 +++++- libs/chat/src/dialogue/post.rs | 3 + libs/chat/src/lib.rs | 39 +++++++++++++ libs/chat/src/ui.rs | 103 +++++++++++++++------------------ 4 files changed, 103 insertions(+), 59 deletions(-) diff --git a/libs/chat/src/dialogue.rs b/libs/chat/src/dialogue.rs index 996fb243e..b2b90d1af 100644 --- a/libs/chat/src/dialogue.rs +++ b/libs/chat/src/dialogue.rs @@ -2,9 +2,11 @@ pub mod attach; pub mod author; pub mod post; -use crate::now; +use crate::ui::VisualProperties; +use crate::{now, default_textview}; use author::Author; +use gam::Gam; use core::slice::Iter; use post::Post; use rkyv::{Archive, Deserialize, Serialize}; @@ -65,6 +67,7 @@ impl Dialogue { /// * `timestamp` - the timestamp of the Post /// * `text` - the text content of the Post /// * `attach_url` - a url of an attachment (image for example) + /// * `vp` - the visual properties of the system - so that we can pre-compute the size extents of the post /// pub fn post_add( &mut self, @@ -72,10 +75,11 @@ impl Dialogue { timestamp: u64, text: &str, _attach_url: Option<&str>, + vp: Option<(&VisualProperties, &Gam)>, ) -> Result<(), Error> { match self.author_id(author) { Some(author_id) => { - let new = Post::new( + let mut new = Post::new( author_id, timestamp, text, None, // TODO implement ); if self.posts.len() == 0 { @@ -86,6 +90,15 @@ impl Dialogue { let first_ts = self.posts.first().map_or(0, |p| p.timestamp()); let last_ts = self.posts.last().map_or(0, |p| p.timestamp()); log::trace!("{:?}", new); + + // compute the bounds for the post if visual properties are specified + if let Some((vp, gam)) = vp { + let mut layout_bubble = default_textview(&new, false, vp); + if gam.bounds_compute_textview(&mut layout_bubble).is_ok() { + new.bounding_box = layout_bubble.bounds_computed; + } + } + if new_ts > last_ts { log::info!("insert new post at end"); self.posts.push(new); diff --git a/libs/chat/src/dialogue/post.rs b/libs/chat/src/dialogue/post.rs index 0240ea68f..c7be6ee5d 100644 --- a/libs/chat/src/dialogue/post.rs +++ b/libs/chat/src/dialogue/post.rs @@ -2,6 +2,7 @@ use super::attach::Attach; use crate::PostFlag; use enumset::EnumSet; +use graphics_server::Rectangle; use rkyv::{Archive, Deserialize, Serialize}; #[derive(Archive, Serialize, Deserialize, Debug)] @@ -11,6 +12,7 @@ pub struct Post { text: String, attach: Option, pub flags: u16, + pub bounding_box: Option, } #[allow(dead_code)] @@ -22,6 +24,7 @@ impl Post { text: text.to_string(), attach: attach, flags: 0, + bounding_box: None, } } diff --git a/libs/chat/src/lib.rs b/libs/chat/src/lib.rs index d5f6e5834..a59700510 100644 --- a/libs/chat/src/lib.rs +++ b/libs/chat/src/lib.rs @@ -4,9 +4,12 @@ pub mod icontray; pub mod ui; pub use api::*; +use graphics_server::{TextView, Point, Rectangle, TextBounds}; +use graphics_server::api::GlyphStyle; pub use ui::BUSY_ANIMATION_RATE_MS; use gam::MenuItem; use num_traits::FromPrimitive; +use ui::VisualProperties; use std::convert::TryInto; use std::thread; use std::time::{SystemTime, UNIX_EPOCH}; @@ -15,6 +18,42 @@ use std::sync::{Arc, atomic::AtomicBool, atomic::Ordering}; use xous::{Error, CID, SID, msg_scalar_unpack}; use xous_ipc::Buffer; +/// Create a TextView with the default properties common to all text bubbles. +pub(crate) fn default_textview( + post: &crate::dialogue::post::Post, + hilite: bool, + vp: &VisualProperties, +) -> TextView { + use std::fmt::Write; + let mut bubble_tv = TextView::new( + vp.canvas, + TextBounds::GrowableFromBl( + Point::new(vp.margin.x, vp.screensize.y), + vp.bubble_width + ) + ); + if hilite { + bubble_tv.border_width = 3; + } else { + bubble_tv.border_width = 1; + } + bubble_tv.clip_rect = Some( + Rectangle::new( + Point::new(0, vp.status_height as i16 + vp.margin.y), + vp.screensize + ) + ); + bubble_tv.draw_border = true; + bubble_tv.clear_area = true; + bubble_tv.rounded_border = Some(vp.bubble_radius); + bubble_tv.style = GlyphStyle::Regular; + bubble_tv.margin = vp.bubble_margin; + bubble_tv.ellipsis = false; + bubble_tv.insertion = None; + write!(bubble_tv.text, "{}", post.text()).expect("couldn't write history text to TextView"); + bubble_tv +} + pub struct Chat { cid: CID, } diff --git a/libs/chat/src/ui.rs b/libs/chat/src/ui.rs index 7474aa270..af21fad32 100644 --- a/libs/chat/src/ui.rs +++ b/libs/chat/src/ui.rs @@ -23,6 +23,18 @@ use xous_names::XousNames; pub const BUSY_ANIMATION_RATE_MS: usize = 200; +pub struct VisualProperties { + pub canvas: Gid, + // variables that define our graphical attributes + pub screensize: Point, + /// height of the status bar. This is subtracted from screensize. + pub status_height: u16, + pub bubble_width: u16, + pub margin: Point, // margin to edge of canvas + pub bubble_margin: Point, // margin of text in bubbles + pub bubble_radius: u16, + pub bubble_space: i16, // spacing between text bubbles +} #[allow(dead_code)] pub(crate) struct Ui { // optional structures that indicate new input to the Chat loop per iteration @@ -45,7 +57,6 @@ pub(crate) struct Ui { // optional opcode ID to process UI-event msgs opcode_event: Option, - canvas: Gid, gam: gam::Gam, modals: Modals, tt: Ticktimer, @@ -58,21 +69,14 @@ pub(crate) struct Ui { // layout post bubbles on the screen from top-down or bottom-up post_topdown: bool, - // variables that define our graphical attributes - screensize: Point, - /// height of the status bar. This is subtracted from screensize. - status_height: u16, /// TextView for the status bar. This encapsulates the state of the busy animation, and the text within. status_tv: TextView, /// Track the last time we update the status bar; use this avoid double-updating busy animations status_last_update_ms: u64, /// The default message to show when we exit a busy state status_idle_text: String, - bubble_width: u16, - margin: Point, // margin to edge of canvas - bubble_margin: Point, // margin of text in bubbles - bubble_radius: u16, - bubble_space: i16, // spacing between text bubbles + + vp: VisualProperties, // variables that define a menu menu_mode: bool, @@ -148,6 +152,16 @@ impl Ui { write!(status_tv, "{}", t!("chat.status.initial", locales::LANG).to_string()).ok(); let tt = ticktimer_server::Ticktimer::new().unwrap(); let status_last_update_ms = tt.elapsed_ms(); + let bubble_properties = VisualProperties { + canvas, + screensize, + status_height, + bubble_width: ((screensize.x / 5) * 4) as u16, // 80% width for the text bubbles + margin: Point::new(4, 4), + bubble_margin: Point::new(4, 4), + bubble_radius: 4, + bubble_space: 4, + }; Ui { input: None, msg: None, @@ -158,22 +172,15 @@ impl Ui { self_cid: xous::connect(sid).unwrap(), app_cid, opcode_event, - canvas, gam, modals, tt, - screensize, - status_height, status_tv, status_last_update_ms, post_selected: None, post_anchor: None, post_topdown: false, - bubble_width: ((screensize.x / 5) * 4) as u16, // 80% width for the text bubbles - margin: Point::new(4, 4), - bubble_margin: Point::new(4, 4), - bubble_radius: 4, - bubble_space: 4, + vp: bubble_properties, menu_mode: true, app_menu: app_menu.to_owned(), menu_mgr: menu_mgr, @@ -393,7 +400,7 @@ impl Ui { (Some(pddb_key), Some(ref mut dialogue)) => { if dialogue_id.len() == 0 || pddb_key.eq(&dialogue_id) { dialogue - .post_add(author, timestamp, text, attach_url) + .post_add(author, timestamp, text, attach_url, Some((&self.vp, &self.gam))) .unwrap(); } else { log::warn!("dropping Post as dialogue_id does not match pddb_key: '{}' vs '{}'", pddb_key, dialogue_id); @@ -528,20 +535,22 @@ impl Ui { /// * `anchor_y` - the vertical position on screen to draw TextView bubble /// fn bubble(&self, post: &Post, dialogue: &Dialogue, hilite: bool, anchor_y: i16) -> TextView { + // create a textview with all of our default properties + let mut bubble_tv = default_textview(post, hilite, &self.vp); + // set alignment of bubble left/right let mut align_right = false; - let mut anchor_x = self.margin.x; // default to align left + let mut anchor_x = self.vp.margin.x; // default to align left if let Some(author) = dialogue.author(post.author_id()) { if author.flag_is(AuthorFlag::Right) { // align right align_right = true; - anchor_x = self.screensize.x - self.margin.x; + anchor_x = self.vp.screensize.x - self.vp.margin.x; } } - // set the text bounds of the bubble and the growth direction let anchor = Point::new(anchor_x, anchor_y); - let width = self.bubble_width; + let width = self.vp.bubble_width; let text_bounds = match (self.post_topdown, align_right) { (true, true) => TextBounds::GrowableFromTr(anchor, width), (true, false) => TextBounds::GrowableFromTl(anchor, width), @@ -549,28 +558,7 @@ impl Ui { (false, false) => TextBounds::GrowableFromBl(anchor, width), }; - // create the bubble with the anchor and a growable direction - use std::fmt::Write; - let mut bubble_tv = TextView::new(self.canvas, text_bounds); - if hilite { - bubble_tv.border_width = 3; - } else { - bubble_tv.border_width = 1; - } - bubble_tv.clip_rect = Some( - Rectangle::new( - Point::new(0, self.status_height as i16 + self.margin.y), - self.screensize - ) - ); - bubble_tv.draw_border = true; - bubble_tv.clear_area = true; - bubble_tv.rounded_border = Some(self.bubble_radius); - bubble_tv.style = GlyphStyle::Regular; - bubble_tv.margin = self.bubble_margin; - bubble_tv.ellipsis = false; - bubble_tv.insertion = None; - write!(bubble_tv.text, "{}", post.text()).expect("couldn't write history text to TextView"); + bubble_tv.bounds_hint = text_bounds; bubble_tv } @@ -579,10 +567,10 @@ impl Ui { fn clear_area(&self) { self.gam .draw_rectangle( - self.canvas, + self.vp.canvas, Rectangle::new_with_style( - Point::new(0, self.status_height as i16), - self.screensize, + Point::new(0, self.vp.status_height as i16), + self.vp.screensize, DrawStyle { fill_color: Some(PixelColor::Light), stroke_color: None, @@ -716,12 +704,13 @@ impl Ui { self.gam.post_textview(&mut self.status_tv) .expect("couldn't render status bar"); let status_border = Line::new( - Point::new(0, self.status_height as i16), - Point::new(self.screensize.x, self.status_height as i16) + Point::new(0, self.vp.status_height as i16), + Point::new(self.vp.screensize.x, self.vp.status_height as i16) ); - self.gam.draw_line(self.canvas, + self.gam.draw_line(self.vp.canvas, status_border ).expect("couldn't draw status lower border"); + match (&self.dialogue, &self.post_anchor) { (Some(dialogue), Some(post_anchor)) => { log::info!("redrawing dialogue: {}", dialogue.title); @@ -731,8 +720,8 @@ impl Ui { // initialise the first post index AND the vertical position on the screen let mut post_index = *post_anchor; let mut anchor_y = match self.post_topdown { - true => self.status_height as i16 + self.margin.y, - false => self.screensize.y - self.margin.y, + true => self.vp.status_height as i16 + self.vp.margin.y, + false => self.vp.screensize.y - self.vp.margin.y, }; // fill the screen with post bubbles from top-down or bottom-up @@ -766,8 +755,8 @@ impl Ui { } post_index += 1; anchor_y += (bounds.br.y - bounds.tl.y) - + self.bubble_space - + self.bubble_margin.y; + + self.vp.bubble_space + + self.vp.bubble_margin.y; } false => { if post_index == 0 { @@ -778,8 +767,8 @@ impl Ui { } post_index -= 1; anchor_y -= (bounds.br.y - bounds.tl.y) - + self.bubble_space - + self.bubble_margin.y; + + self.vp.bubble_space + + self.vp.bubble_margin.y; } }, None => { From 157f0031e636df0537561007058e0579284623d5 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 1 Dec 2023 02:23:47 +0800 Subject: [PATCH 06/12] refactor layout to take advantage of pre-computed height bubbles mostly works, so checkpointing it here. need to debug why going down after going up (in terms of selection) is not working. --- libs/chat/src/dialogue.rs | 17 +- libs/chat/src/lib.rs | 50 +++++- libs/chat/src/ui.rs | 333 ++++++++++++++++++++------------------ 3 files changed, 243 insertions(+), 157 deletions(-) diff --git a/libs/chat/src/dialogue.rs b/libs/chat/src/dialogue.rs index b2b90d1af..166f1e868 100644 --- a/libs/chat/src/dialogue.rs +++ b/libs/chat/src/dialogue.rs @@ -7,7 +7,7 @@ use crate::{now, default_textview}; use author::Author; use gam::Gam; -use core::slice::Iter; +use core::slice::{Iter, IterMut}; use post::Post; use rkyv::{Archive, Deserialize, Serialize}; use std::collections::HashMap; @@ -178,12 +178,27 @@ impl Dialogue { } } + /// Return a slice of posts + pub fn posts_as_slice(&self) -> &[Post] { + &self.posts + } + + pub fn posts_as_slice_mut(&mut self) -> &mut [Post] { + &mut self.posts + } + /// Return an iterator over the Dialogue Posts (oldest first) /// pub fn posts(&self) -> Iter { return self.posts.iter(); } + /// Return a mut iterator over the Dialogue Posts (oldest first) + /// + pub fn posts_mut(&mut self) -> IterMut { + return self.posts.iter_mut(); + } + /// Return Some by id, or None. /// /// # Arguments diff --git a/libs/chat/src/lib.rs b/libs/chat/src/lib.rs index a59700510..836ce40ca 100644 --- a/libs/chat/src/lib.rs +++ b/libs/chat/src/lib.rs @@ -28,7 +28,7 @@ pub(crate) fn default_textview( let mut bubble_tv = TextView::new( vp.canvas, TextBounds::GrowableFromBl( - Point::new(vp.margin.x, vp.screensize.y), + Point::new(vp.margin.x, vp.layout_screensize.y), vp.bubble_width ) ); @@ -40,7 +40,7 @@ pub(crate) fn default_textview( bubble_tv.clip_rect = Some( Rectangle::new( Point::new(0, vp.status_height as i16 + vp.margin.y), - vp.screensize + vp.layout_screensize ) ); bubble_tv.draw_border = true; @@ -54,6 +54,52 @@ pub(crate) fn default_textview( bubble_tv } +/// Return a TextView bubble representing a Dialogue Post +/// +/// # Arguments +/// +/// * `vp` - the visual properties to be applied to the textview +/// * `topdown` - direction of the layout +/// * `post` - the post to represent in a TextView bubble +/// * `dialogue` - containing the Post for context info +/// * `hilite` - hilite this Post on the screen (thicker border) +/// * `anchor_y` - the vertical position on screen to draw TextView bubble +/// +fn bubble( + vp: &VisualProperties, + topdown: bool, + post: &crate::dialogue::post::Post, + dialogue: &crate::dialogue::Dialogue, + hilite: bool, + anchor_y: i16) -> TextView { + // create a textview with all of our default properties + let mut bubble_tv = default_textview(post, hilite, vp); + + // set alignment of bubble left/right + let mut align_right = false; + let mut anchor_x = vp.margin.x; // default to align left + if let Some(author) = dialogue.author(post.author_id()) { + if author.flag_is(AuthorFlag::Right) { + // align right + align_right = true; + anchor_x = vp.layout_screensize.x - vp.margin.x; + } + } + // set the text bounds of the bubble and the growth direction + let anchor = Point::new(anchor_x, anchor_y); + let width = vp.bubble_width; + let text_bounds = match (topdown, align_right) { + (true, true) => TextBounds::GrowableFromTr(anchor, width), + (true, false) => TextBounds::GrowableFromTl(anchor, width), + (false, true) => TextBounds::GrowableFromBr(anchor, width), + (false, false) => TextBounds::GrowableFromBl(anchor, width), + }; + + bubble_tv.bounds_hint = text_bounds; + bubble_tv +} + + pub struct Chat { cid: CID, } diff --git a/libs/chat/src/ui.rs b/libs/chat/src/ui.rs index af21fad32..fb5f9efb1 100644 --- a/libs/chat/src/ui.rs +++ b/libs/chat/src/ui.rs @@ -23,10 +23,11 @@ use xous_names::XousNames; pub const BUSY_ANIMATION_RATE_MS: usize = 200; +/// Variables that define the visual properties of the layout pub struct VisualProperties { pub canvas: Gid, - // variables that define our graphical attributes - pub screensize: Point, + pub total_screensize: Point, + pub layout_screensize: Point, /// height of the status bar. This is subtracted from screensize. pub status_height: u16, pub bubble_width: u16, @@ -61,13 +62,16 @@ pub(crate) struct Ui { modals: Modals, tt: Ticktimer, - // variables regarding the posts currently onscreen - // the selected post is hilighted onscreen and the focus of the msg menu F4 - post_selected: Option, - // the anchor post is drawn first, at the top or bottom of the screen - post_anchor: Option, - // layout post bubbles on the screen from top-down or bottom-up - post_topdown: bool, + /// These variables are managed exclusively by the layout routine. + /// the selected post is highlighted onscreen and the focus of the msg menu + layout_selected: Option, + /// the range of posts that are currently drawable. This was originally implemented as + /// an Option, but we need to be able to do RangeInclusive and Reversed ranges. + /// The RangeBounds trait isn't object-safe, so we can't Box/dyn it either... + /// So, instead, we turn the ranges into a Vec and operate from there... + layout_range: Vec, + /// layout post bubbles on the screen from top-down or bottom-up + layout_topdown: bool, /// TextView for the status bar. This encapsulates the state of the busy animation, and the text within. status_tv: TextView, @@ -154,7 +158,8 @@ impl Ui { let status_last_update_ms = tt.elapsed_ms(); let bubble_properties = VisualProperties { canvas, - screensize, + total_screensize: screensize, + layout_screensize: Point::new(screensize.x, screensize.y - status_height as i16), status_height, bubble_width: ((screensize.x / 5) * 4) as u16, // 80% width for the text bubbles margin: Point::new(4, 4), @@ -177,9 +182,9 @@ impl Ui { tt, status_tv, status_last_update_ms, - post_selected: None, - post_anchor: None, - post_topdown: false, + layout_selected: None, + layout_range: Vec::new(), + layout_topdown: false, vp: bubble_properties, menu_mode: true, app_menu: app_menu.to_owned(), @@ -212,9 +217,9 @@ impl Ui { { Ok(dialogue) => { // show most recent posts onscreen - self.post_selected = dialogue.post_last(); - self.post_anchor = self.post_selected; - self.post_topdown = false; + self.layout_selected = dialogue.post_last(); + self.layout_range.clear(); + self.layout_topdown = false; Some(dialogue) } Err(e) => { @@ -464,11 +469,11 @@ impl Ui { /// /// * `index` - POST_SELECT_NEXT or POST_SELECT_PREV or an arbitraty index pub fn post_select(&mut self, index: usize) { - self.post_selected = match &self.dialogue { + self.layout_selected = match &self.dialogue { Some(dialogue) => { match dialogue.post_last() { Some(last_post) => { - match (index, self.post_selected) { + match (index, self.layout_selected) { (POST_SELECTED_NEXT, Some(selected)) => { if selected >= last_post { self.event(Event::Bottom); @@ -525,52 +530,15 @@ impl Ui { } } - /// Return a TextView bubble representing a Dialogue Post + /// Clear the screen area, not including the status bar /// - /// # Arguments - /// - /// * `post` - the post to represent in a TextView bubble - /// * `dialogue` - containing the Post for context info - /// * `hilite` - hilite this Post on the screen (thicker border) - /// * `anchor_y` - the vertical position on screen to draw TextView bubble - /// - fn bubble(&self, post: &Post, dialogue: &Dialogue, hilite: bool, anchor_y: i16) -> TextView { - // create a textview with all of our default properties - let mut bubble_tv = default_textview(post, hilite, &self.vp); - - // set alignment of bubble left/right - let mut align_right = false; - let mut anchor_x = self.vp.margin.x; // default to align left - if let Some(author) = dialogue.author(post.author_id()) { - if author.flag_is(AuthorFlag::Right) { - // align right - align_right = true; - anchor_x = self.vp.screensize.x - self.vp.margin.x; - } - } - // set the text bounds of the bubble and the growth direction - let anchor = Point::new(anchor_x, anchor_y); - let width = self.vp.bubble_width; - let text_bounds = match (self.post_topdown, align_right) { - (true, true) => TextBounds::GrowableFromTr(anchor, width), - (true, false) => TextBounds::GrowableFromTl(anchor, width), - (false, true) => TextBounds::GrowableFromBr(anchor, width), - (false, false) => TextBounds::GrowableFromBl(anchor, width), - }; - - bubble_tv.bounds_hint = text_bounds; - bubble_tv - } - - // Clear the screen area - // fn clear_area(&self) { self.gam .draw_rectangle( self.vp.canvas, Rectangle::new_with_style( Point::new(0, self.vp.status_height as i16), - self.vp.screensize, + self.vp.total_screensize, DrawStyle { fill_color: Some(PixelColor::Light), stroke_color: None, @@ -604,10 +572,7 @@ impl Ui { /// pub(crate) fn redraw(&mut self) -> Result<(), xous::Error> { if self.dialogue.is_some() { - let mut attempt = 0; - while !self.layout().unwrap_or(true) && attempt < 3 { - attempt += 1; - } + self.layout().expect("layout failed to execute"); self.status_last_update_ms = self.tt.elapsed_ms(); } else { self.clear_area(); // no dialogue so clear screen @@ -699,110 +664,170 @@ impl Ui { /// /// Error if there if Dialogue is None /// - fn layout(&mut self) -> Result { - self.clear_area(); - self.gam.post_textview(&mut self.status_tv) - .expect("couldn't render status bar"); - let status_border = Line::new( - Point::new(0, self.vp.status_height as i16), - Point::new(self.vp.screensize.x, self.vp.status_height as i16) - ); - self.gam.draw_line(self.vp.canvas, - status_border - ).expect("couldn't draw status lower border"); + fn layout(&mut self) -> Result<(), Error> { + if let Some(dialogue) = self.dialogue.as_mut() { + log::info!("redrawing dialogue: {}", dialogue.title); - match (&self.dialogue, &self.post_anchor) { - (Some(dialogue), Some(post_anchor)) => { - log::info!("redrawing dialogue: {}", dialogue.title); - let mut post_selected_visible = false; - let mut bubble_count = 0; + // 1. Consistency check the layout range versus selected post. + let search_required = if let Some(selected) = self.layout_selected { + !self.layout_range.contains(&selected) + } else { + true + }; - // initialise the first post index AND the vertical position on the screen - let mut post_index = *post_anchor; - let mut anchor_y = match self.post_topdown { - true => self.vp.status_height as i16 + self.vp.margin.y, - false => self.vp.screensize.y - self.vp.margin.y, + // 2. Adjust the displayable range. + if search_required { + let starting_at = if let Some(selected) = self.layout_selected { + if self.layout_range.len() > 0 { + self.layout_topdown = selected <= *self.layout_range.iter().min().unwrap_or(&0); + selected + } else { + // if no range is available, go from the bottom up, starting with the selected post + self.layout_topdown = false; + selected + } + } else { + // no post selected, always layout from bottom up, starting at the most recent post + self.layout_topdown = false; + dialogue.post_last().unwrap_or(0) }; - - // fill the screen with post bubbles from top-down or bottom-up - let mut post_is_fully_visible = true; - let mut is_selected = false; - while post_is_fully_visible { - log::trace!("redrawing post: {post_index}"); - match (dialogue.post_get(post_index), &self.post_selected) { - (Some(post), Some(post_selected)) => { - is_selected = post_index == *post_selected; - - // create a bubble and place on canvas - let mut bubble_tv = self.bubble(post, dialogue, is_selected, anchor_y); - self.gam - .post_textview(&mut bubble_tv) - .expect("couldn't render bubble textview"); - bubble_count += 1; - post_is_fully_visible = !bubble_tv.overflow.unwrap_or(true); - if post_is_fully_visible { - // step to the next post AND the next vertical position on the screen - match bubble_tv.bounds_computed { - Some(bounds) => match self.post_topdown { - true => { - if post_index - >= dialogue.post_last().unwrap_or(usize::MAX) - { - log::info!("trigger a re-layout from bottom-up"); - self.post_topdown = false; - self.post_anchor = dialogue.post_last(); - return Ok(false); - } - post_index += 1; - anchor_y += (bounds.br.y - bounds.tl.y) - + self.vp.bubble_space - + self.vp.bubble_margin.y; - } - false => { - if post_index == 0 { - log::info!("trigger a re-layout from top-down"); - self.post_topdown = true; - self.post_anchor = Some(0); - return Ok(false); - } - post_index -= 1; - anchor_y -= (bounds.br.y - bounds.tl.y) - + self.vp.bubble_space - + self.vp.bubble_margin.y; - } - }, - None => { - log::info!("bubble is offscreen so noop"); - post_is_fully_visible = false; - } - }; - - // check if the selected post is fully visible - post_selected_visible = post_selected_visible || is_selected; + let mut fwd_iter; + let mut rev_iter; + let search_window: &mut dyn Iterator = if self.layout_topdown { + // search from the selected post to all newer posts, top-to-down + fwd_iter = dialogue.posts_as_slice_mut()[starting_at..].iter_mut(); + &mut fwd_iter + } else { + // search from oldest post to selected post, bottom-to-top + rev_iter = dialogue.posts_as_slice_mut()[..=starting_at].iter_mut().rev(); + &mut rev_iter + }; + let mut total_height = 0; + self.layout_range.clear(); + for (i, post) in search_window.enumerate() { + let next_height = if let Some(bb) = post.bounding_box { + bb.height() + self.vp.bubble_space as u32 + self.vp.bubble_margin.y as u32 + } else { + // if the "natural height" has not been computed, do so now. + let mut layout_bubble = default_textview(post, false, &self.vp); + if self.gam.bounds_compute_textview(&mut layout_bubble).is_ok() { + post.bounding_box = layout_bubble.bounds_computed; + match layout_bubble.bounds_computed { + Some(r) => r.height() + self.vp.bubble_space as u32 + self.vp.bubble_margin.y as u32, + None => { + log::warn!("Unexpected null bounds in computing textview heights, layout will be incorrect."); + 0 + } } + } else { + log::warn!("Unexpected error in computing textview heights, layout will be incorrect."); + 0 } - (None, _) => { - log::trace!("not enough post bubbles to fill the screen"); - return Ok(true); + }; + if total_height + next_height > self.vp.layout_screensize.y as u32 { + if self.layout_topdown { + self.layout_range = (starting_at..starting_at + i).collect(); + } else { + self.layout_range = (starting_at - i..=starting_at).rev().collect(); } - (_, _) => return Ok(true), // get me outa-here + break + } + total_height += next_height; + } + if self.layout_range.len() == 0 { + // not enough elements to fill the entire screen. Just select everything from selected + // to the last possible message. + if self.layout_topdown { + self.layout_range = (starting_at..).collect(); + } else { + self.layout_range = (0..=starting_at).rev().collect(); } } - if post_selected_visible || (bubble_count == 1 && is_selected) { - Ok(true) + } + assert!(self.layout_range.len() > 0, "Layout range should be set at this point."); + + // 3. clear the entire area, and re-draw the status bar + self.gam + .draw_rectangle( + self.vp.canvas, + Rectangle::new_with_style( + Point::new(0, 0), + self.vp.total_screensize, + DrawStyle { + fill_color: Some(PixelColor::Light), + stroke_color: None, + stroke_width: 0, + }, + ), + ) + .expect("can't clear canvas area"); + self.gam.post_textview(&mut self.status_tv) + .expect("couldn't render status bar"); + let status_border = Line::new( + Point::new(0, self.vp.status_height as i16), + Point::new(self.vp.total_screensize.x, self.vp.status_height as i16) + ); + self.gam.draw_line(self.vp.canvas, + status_border + ).expect("couldn't draw status lower border"); + + // 4. draw the text bubbles, in the order computed in step 2. + let mut y = if self.layout_topdown { + self.vp.status_height as i16 + self.vp.bubble_margin.y + } else { + self.vp.status_height as i16 + self.vp.layout_screensize.y - self.vp.bubble_margin.y + }; + for &post_index in &self.layout_range { + let post = dialogue.post_get(post_index).expect("should be a valid post at this index"); + let highlight = if let Some(selected) = self.layout_selected { + selected == post_index } else { - log::info!("trigger a re-layout with selected post visible"); - self.post_topdown = self.post_selected >= self.post_anchor; - self.post_anchor = self.post_selected; - Ok(false) + false + }; + let mut bubble_tv = bubble( + &self.vp, self.layout_topdown, post, dialogue, highlight, y); + self.gam + .post_textview(&mut bubble_tv) + .expect("couldn't render bubble textview"); + // double check the actual bounds against expected bounds + match bubble_tv.bounds_computed { + Some(actual_r) => { + let expected_r = post.bounding_box.expect("bb should be computed by now"); + if expected_r.height() != actual_r.height() { + log::warn!( + "Height mismatch of drawn versus pre-computed text (expected {}, got {}) for {}", + expected_r.height(), + actual_r.height(), + bubble_tv.to_str() + ); + } + if self.layout_topdown { + y += actual_r.height() as i16; + } else { + y -= actual_r.height() as i16; + } + // sanity check the computations + if y > self.vp.layout_screensize.y + self.vp.status_height as i16 + || y < self.vp.status_height as i16 { + log::error!("Computed range of elements sent to layout overflows at index {}", post_index); + // stop laying out to avoid text artifacts + break; + } + // add y-margin before the next iteration + if self.layout_topdown { + y += self.vp.bubble_space + self.vp.bubble_margin.y; + } else { + y -= self.vp.bubble_space + self.vp.bubble_margin.y; + } + } + _ => { + log::error!("No bounds computed for {}, this is a GAM or typesetter bug!", bubble_tv.to_str()); + } } } - (Some(_dialogue), None) => { - log::info!("no posts to display"); - // TODO show dialogue info as a default? - Ok(true) - } - (None, _) => Err(Error::new(ErrorKind::InvalidData, "missing dialogue")), + Ok(()) + } else { + Err(Error::new(ErrorKind::InvalidData, "missing dialogue")) } } } From e4fba76ed23c109eecf6d28edffa8234a37cbabc Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 1 Dec 2023 03:17:00 +0800 Subject: [PATCH 07/12] add some debugging infos the scroll-down freeze is actually because of the nature of the test program hijacking the main loop. the test program probably needs to be redesigned so it takes in events so we don't eventually end up in deadlock --- libs/chat/src/ui.rs | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/libs/chat/src/ui.rs b/libs/chat/src/ui.rs index fb5f9efb1..debe628b2 100644 --- a/libs/chat/src/ui.rs +++ b/libs/chat/src/ui.rs @@ -650,20 +650,6 @@ impl Ui { /// to move down. There are three edge cases, when the first or last post is reached, /// or when the post is too big for the screen. And an additional challenge, /// that the only way to calculate the vertical height of a post is to lay it out. - /// - /// The layout proceeds from top-down or bottom-up (starting with the - /// `post_anchor`), drawing a bubble for each Post, until the available space - /// is exhausted. - /// * If the `post_selected` is fully displayed, then `Ok(true)` is Returned. - /// * If the `post_selected` is NOT fully displayed, then the `post_anchor` - /// is set as the `post_selected`, and `Ok(false)` is Returned - signalling that - /// a re-layout is in order. - /// * If the first/last Post is fully displayed, then the `post_topdown` is - /// toggled, the `post_anchor` is set to the first/last Post, and `Ok(false)` - /// is Returned - signalling that a re-layout is in order. - /// - /// Error if there if Dialogue is None - /// fn layout(&mut self) -> Result<(), Error> { if let Some(dialogue) = self.dialogue.as_mut() { log::info!("redrawing dialogue: {}", dialogue.title); @@ -737,6 +723,7 @@ impl Ui { if self.layout_range.len() == 0 { // not enough elements to fill the entire screen. Just select everything from selected // to the last possible message. + log::debug!("Not enough elements to fill the screen"); if self.layout_topdown { self.layout_range = (starting_at..).collect(); } else { @@ -777,6 +764,7 @@ impl Ui { } else { self.vp.status_height as i16 + self.vp.layout_screensize.y - self.vp.bubble_margin.y }; + log::debug!("Laying out with selected {:?} in range {:?}; topdown: {:?}", self.layout_selected, self.layout_range, self.layout_topdown); for &post_index in &self.layout_range { let post = dialogue.post_get(post_index).expect("should be a valid post at this index"); let highlight = if let Some(selected) = self.layout_selected { From 0da86640b1a790d2d55ca379d69ace9e1da2b0b9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 1 Dec 2023 17:55:26 +0800 Subject: [PATCH 08/12] cleanup debug statement --- libs/chat/src/dialogue.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/libs/chat/src/dialogue.rs b/libs/chat/src/dialogue.rs index 166f1e868..7924e9b2d 100644 --- a/libs/chat/src/dialogue.rs +++ b/libs/chat/src/dialogue.rs @@ -94,6 +94,7 @@ impl Dialogue { // compute the bounds for the post if visual properties are specified if let Some((vp, gam)) = vp { let mut layout_bubble = default_textview(&new, false, vp); + log::debug!("Computing bounds on {:?}", layout_bubble); if gam.bounds_compute_textview(&mut layout_bubble).is_ok() { new.bounding_box = layout_bubble.bounds_computed; } From f88afd1a1acc9c6c0edd88defac61329547d0579 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 1 Dec 2023 17:55:39 +0800 Subject: [PATCH 09/12] fix various edge cases; move status bar redraw to end of routine putting the status bar redraw at the end means that even if the fitting of the boxes isn't 100% correct, the status bar still ends up "on top". --- libs/chat/src/ui.rs | 49 +++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/libs/chat/src/ui.rs b/libs/chat/src/ui.rs index debe628b2..a464d8ab7 100644 --- a/libs/chat/src/ui.rs +++ b/libs/chat/src/ui.rs @@ -685,7 +685,13 @@ impl Ui { &mut fwd_iter } else { // search from oldest post to selected post, bottom-to-top - rev_iter = dialogue.posts_as_slice_mut()[..=starting_at].iter_mut().rev(); + if dialogue.posts_as_slice().len() > 0 { + rev_iter = dialogue.posts_as_slice_mut()[..=starting_at].iter_mut().rev(); + } else { + // zero-length case we still have to return an empty iterator, but + // we can't have the range be inclusive and the code still work + rev_iter = dialogue.posts_as_slice_mut().iter_mut().rev(); + } &mut rev_iter }; let mut total_height = 0; @@ -696,6 +702,7 @@ impl Ui { } else { // if the "natural height" has not been computed, do so now. let mut layout_bubble = default_textview(post, false, &self.vp); + log::debug!("compute bounds on {}", layout_bubble); if self.gam.bounds_compute_textview(&mut layout_bubble).is_ok() { post.bounding_box = layout_bubble.bounds_computed; match layout_bubble.bounds_computed { @@ -727,11 +734,16 @@ impl Ui { if self.layout_topdown { self.layout_range = (starting_at..).collect(); } else { - self.layout_range = (0..=starting_at).rev().collect(); + if dialogue.posts_as_slice().len() > 0 { + self.layout_range = (0..=starting_at).rev().collect(); + } else { + // "empty range" in case of no posts + self.layout_range = (0..0).rev().collect(); + } } } } - assert!(self.layout_range.len() > 0, "Layout range should be set at this point."); + assert!(dialogue.posts_as_slice().len() == 0 || self.layout_range.len() > 0, "Layout range should be set at this point."); // 3. clear the entire area, and re-draw the status bar self.gam @@ -748,15 +760,6 @@ impl Ui { ), ) .expect("can't clear canvas area"); - self.gam.post_textview(&mut self.status_tv) - .expect("couldn't render status bar"); - let status_border = Line::new( - Point::new(0, self.vp.status_height as i16), - Point::new(self.vp.total_screensize.x, self.vp.status_height as i16) - ); - self.gam.draw_line(self.vp.canvas, - status_border - ).expect("couldn't draw status lower border"); // 4. draw the text bubbles, in the order computed in step 2. let mut y = if self.layout_topdown { @@ -766,7 +769,15 @@ impl Ui { }; log::debug!("Laying out with selected {:?} in range {:?}; topdown: {:?}", self.layout_selected, self.layout_range, self.layout_topdown); for &post_index in &self.layout_range { - let post = dialogue.post_get(post_index).expect("should be a valid post at this index"); + let post = match dialogue.post_get(post_index) { + Some(p) => p, + None => { + log::warn!("Expected post at index {}, returned nothing. Range {:?}, posts {:?}", + post_index, self.layout_range, dialogue.posts_as_slice() + ); + continue; + } + }; let highlight = if let Some(selected) = self.layout_selected { selected == post_index } else { @@ -813,6 +824,18 @@ impl Ui { } } } + + // 5. draw status bar on top of any post that happens to flow over the top... + self.gam.post_textview(&mut self.status_tv) + .expect("couldn't render status bar"); + let status_border = Line::new( + Point::new(0, self.vp.status_height as i16), + Point::new(self.vp.total_screensize.x, self.vp.status_height as i16) + ); + self.gam.draw_line(self.vp.canvas, + status_border + ).expect("couldn't draw status lower border"); + Ok(()) } else { Err(Error::new(ErrorKind::InvalidData, "missing dialogue")) From 0632e510be273ec413ee54b6bcd1736cb12aa84d Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 2 Dec 2023 16:09:37 +0800 Subject: [PATCH 10/12] remove tests from mtxchat app --- apps/mtxchat/Cargo.toml | 1 - apps/mtxchat/src/api.rs | 2 +- apps/mtxchat/src/main.rs | 4 ---- apps/mtxchat/src/tests.rs | 35 ----------------------------------- 4 files changed, 1 insertion(+), 41 deletions(-) delete mode 100644 apps/mtxchat/src/tests.rs diff --git a/apps/mtxchat/Cargo.toml b/apps/mtxchat/Cargo.toml index 675743321..c02f1e02d 100644 --- a/apps/mtxchat/Cargo.toml +++ b/apps/mtxchat/Cargo.toml @@ -40,4 +40,3 @@ url = "2.2.2" [features] default = [] -test-chat-ui = [] diff --git a/apps/mtxchat/src/api.rs b/apps/mtxchat/src/api.rs index 928450532..08609cca1 100644 --- a/apps/mtxchat/src/api.rs +++ b/apps/mtxchat/src/api.rs @@ -1,5 +1,5 @@ #[allow(dead_code)] -pub(crate) const SERVER_NAME_MTXCHAT: &str = "_Matrix chat_"; +pub(crate) const SERVER_NAME_MTXCHAT: &str = "_Chat UI test_"; #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] pub enum MtxchatOp { diff --git a/apps/mtxchat/src/main.rs b/apps/mtxchat/src/main.rs index 04f1a29bd..e2b0dc668 100644 --- a/apps/mtxchat/src/main.rs +++ b/apps/mtxchat/src/main.rs @@ -2,8 +2,6 @@ #![cfg_attr(target_os = "none", no_main)] mod api; -#[cfg(feature="test-chat-ui")] -mod tests; use api::*; use chat::{Chat, Event, POST_TEXT_MAX}; @@ -100,8 +98,6 @@ fn wrapped_main() -> ! { let mut mtxchat = MtxChat::new(&chat); let mut first_focus = true; let mut user_post: Option = None; - #[cfg(feature="test-chat-ui")] - tests::test_ui(&chat); loop { let msg = xous::receive_message(sid).unwrap(); log::debug!("got message {:?}", msg); diff --git a/apps/mtxchat/src/tests.rs b/apps/mtxchat/src/tests.rs deleted file mode 100644 index 55aa9d19a..000000000 --- a/apps/mtxchat/src/tests.rs +++ /dev/null @@ -1,35 +0,0 @@ -use chat::Chat; - -pub fn test_ui(chat: &Chat) { - let tt = ticktimer_server::Ticktimer::new().unwrap(); - let pddb = pddb::Pddb::new(); - // nuke any existing test dictionary, if it exists - pddb.delete_dict("tests.ui", None).ok(); - pddb.sync().ok(); - - // re-create the test room from scratch every time - chat.dialogue_set("tests.ui", Some("test_room")).expect("couldn't set dialog"); - - // generate a list of test posts that are repeatable each time - chat.post_add("alice", 1_700_000_000, "hello world!", None).ok(); - chat.post_add("bob", 1_700_000_002, "hi alice!", None).ok(); - for i in 0..5 { - chat.post_add("alice", 1_700_000_005 + i*4, &format!("alice sez {}", i), None).ok(); - chat.post_add("bob", 1_700_000_006 + i*4, &format!("bob sez {}", i), None).ok(); - chat.post_add("trent", 1_700_000_007 + i*4, &format!("trent sez {}", i), None).ok(); - } - chat.post_add("alice", 1_700_001_000, "eom", None).ok(); - - log::info!("triggering save"); - xous::send_message( - chat.cid(), - xous::Message::new_scalar(chat::ChatOp::DialogueSave as usize, 0, 0, 0, 0), - ) - .expect("failed to send new inbound msgs"); - - log::info!("test done, displaying result"); - loop { - // just idle so that the main loop of mtxchat doesn't trigger and interfere with the test - tt.sleep_ms(5000).ok(); - } -} From 7dc4b9213fb1799cb9d34dde206abb8535ba264e Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 2 Dec 2023 16:09:59 +0800 Subject: [PATCH 11/12] add chat-test mini-app this now contains the UI tests as a stand-alone app --- Cargo.toml | 1 + apps/chat-test/Cargo.toml | 42 ++++++++++++++ apps/chat-test/README.md | 5 ++ apps/chat-test/src/api.rs | 32 +++++++++++ apps/chat-test/src/main.rs | 112 ++++++++++++++++++++++++++++++++++++ apps/chat-test/src/tests.rs | 30 ++++++++++ apps/manifest.json | 25 +++++--- 7 files changed, 239 insertions(+), 8 deletions(-) create mode 100644 apps/chat-test/Cargo.toml create mode 100644 apps/chat-test/README.md create mode 100644 apps/chat-test/src/api.rs create mode 100644 apps/chat-test/src/main.rs create mode 100644 apps/chat-test/src/tests.rs diff --git a/Cargo.toml b/Cargo.toml index a11afd5c7..6bc1c7802 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,6 +83,7 @@ members = [ "apps/vault", "apps/vault/tools/vaultbackup-rs", "apps/transientdisk", + "apps/chat-test", "services/libstd-test", "services/ffi-test", "services/tts", diff --git a/apps/chat-test/Cargo.toml b/apps/chat-test/Cargo.toml new file mode 100644 index 000000000..831a7399c --- /dev/null +++ b/apps/chat-test/Cargo.toml @@ -0,0 +1,42 @@ +[package] +name = "chat-test" +version = "0.1.0" +authors = ["bunnie "] +edition = "2021" +description = "Chat UI test routines" + +# Dependency versions enforced by Cargo.lock. +[dependencies] +log = "0.4.14" +num-derive = { version = "0.3.3", default-features = false} +num-traits = { version = "0.2.14", default-features = false} +xous = "0.9.54" +xous-ipc = "0.9.54" +log-server = { package = "xous-api-log", version = "0.1.50" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.50" } +xous-names = { package = "xous-api-names", version = "0.9.52" } +gam = { path = "../../services/gam" } +graphics-server = { path = "../../services/graphics-server" } +trng = { path = "../../services/trng"} +locales = { path = "../../locales"} +ime-plugin-api = { path = "../../services/ime-plugin-api"} +ime-plugin-shell = { path = "../../services/ime-plugin-shell"} +content-plugin-api = { path = "../../services/content-plugin-api"} # all content canvas providers must provide this API +codec = { path = "../../services/codec"} +com = { path = "../../services/com"} +com_rs = { git = "https://github.com/betrusted-io/com_rs", rev = "891bdd3ca8e41f81510d112483e178aea3e3a921" } +llio = { path = "../../services/llio"} +net = { path = "../../services/net" } +pddb = { path = "../../services/pddb" } +# new dependencies for mtxcli +chat = { path = "../../libs/chat" } +modals = { path = "../../services/modals" } +percent-encoding = "2.2" +rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} +serde = { version = "1.0", features = [ "derive" ] } +tls = { path = "../../libs/tls" } +ureq = { version = "2.7.1", features = ["json"] } +url = "2.2.2" + +[features] +default = [] diff --git a/apps/chat-test/README.md b/apps/chat-test/README.md new file mode 100644 index 000000000..6802197e1 --- /dev/null +++ b/apps/chat-test/README.md @@ -0,0 +1,5 @@ +# chat-test + +Routines for testing aspects of the chat application APIs. + +Not suitable for human consumption, side effects include confusion, frustration, and panic attacks. \ No newline at end of file diff --git a/apps/chat-test/src/api.rs b/apps/chat-test/src/api.rs new file mode 100644 index 000000000..84995111f --- /dev/null +++ b/apps/chat-test/src/api.rs @@ -0,0 +1,32 @@ +#[allow(dead_code)] +pub(crate) const SERVER_NAME_MTXCHAT: &str = "_Matrix chat_"; + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub enum ChatTestOp { + /// chat ui event + Event = 0, + /// menu item click + Menu, + /// chat ui user post + Post, + /// chat ui keystroke + Rawkeys, + /// exit the application + Quit, +} + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub enum MenuOp { + Login, + Logout, + Noop, + Room, +} + +#[allow(dead_code)] +pub struct Msg { + pub type_: String, + pub body: Option, + pub sender: Option, + pub ts: Option, +} diff --git a/apps/chat-test/src/main.rs b/apps/chat-test/src/main.rs new file mode 100644 index 000000000..797bbc83e --- /dev/null +++ b/apps/chat-test/src/main.rs @@ -0,0 +1,112 @@ +#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(target_os = "none", no_main)] + +mod api; +mod tests; + +use api::*; +use chat::{Chat, Event, POST_TEXT_MAX}; +use num_traits::*; +use xous_ipc::Buffer; + +fn main() -> ! { + let stack_size = 1024 * 1024; + std::thread::Builder::new() + .stack_size(stack_size) + .spawn(wrapped_main) + .unwrap() + .join() + .unwrap() +} + +fn wrapped_main() -> ! { + log_server::init_wait().unwrap(); + log::set_max_level(log::LevelFilter::Info); + log::info!("my PID is {}", xous::process::id()); + + const HEAP_LARGER_LIMIT: usize = 2048 * 1024; + let new_limit = HEAP_LARGER_LIMIT; + let result = xous::rsyscall(xous::SysCall::AdjustProcessLimit( + xous::Limits::HeapMaximum as usize, + 0, + new_limit, + )); + + if let Ok(xous::Result::Scalar2(1, current_limit)) = result { + xous::rsyscall(xous::SysCall::AdjustProcessLimit( + xous::Limits::HeapMaximum as usize, + current_limit, + new_limit, + )) + .unwrap(); + log::info!("Heap limit increased to: {}", new_limit); + } else { + panic!("Unsupported syscall!"); + } + + let xns = xous_names::XousNames::new().unwrap(); + let sid = xns + .register_name(SERVER_NAME_MTXCHAT, None) + .expect("can't register server"); + log::trace!("registered with NS -- {:?}", sid); + + let chat = Chat::new( + gam::APP_NAME_CHAT_TEST, + gam::APP_MENU_0_CHAT_TEST, + Some(xous::connect(sid).unwrap()), + Some(ChatTestOp::Post as usize), + Some(ChatTestOp::Event as usize), + Some(ChatTestOp::Rawkeys as usize), + ); + // not used yet, but probably will use in the future + let _cid = xous::connect(sid).unwrap(); + + tests::test_ui(&chat); + loop { + let msg = xous::receive_message(sid).unwrap(); + log::debug!("got message {:?}", msg); + match FromPrimitive::from_usize(msg.body.id()) { + Some(ChatTestOp::Event) => { + log::info!("got Chat UI Event"); + xous::msg_scalar_unpack!(msg, event_code, _, _, _, { + match FromPrimitive::from_usize(event_code) { + Some(Event::Focus) => { + chat.redraw(); + } + _ => (), + } + }); + } + Some(ChatTestOp::Menu) => { + log::info!("got Chat Menu Click"); + xous::msg_scalar_unpack!(msg, menu_code, _, _, _, { + let code: Option = FromPrimitive::from_usize(menu_code); + log::info!("Got menu code {:?}", code); + }); + } + Some(ChatTestOp::Post) => { + let buffer = + unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let s = buffer + .to_original::, _>() + .unwrap(); + if s.len() > 0 { + // capture input instead of calling here, so message can drop and calling server is released + log::info!("got Post {:?}", s.to_string()); + } + } + Some(ChatTestOp::Rawkeys) => log::info!("got rawkeys"), + Some(ChatTestOp::Quit) => { + log::error!("got Quit"); + break; + } + _ => (), + } + } + // clean up our program + log::error!("main loop exit, destroying servers"); + xns.unregister_server(sid).unwrap(); + xous::destroy_server(sid).unwrap(); + log::trace!("quitting"); + xous::terminate_process(0) +} diff --git a/apps/chat-test/src/tests.rs b/apps/chat-test/src/tests.rs new file mode 100644 index 000000000..283688f07 --- /dev/null +++ b/apps/chat-test/src/tests.rs @@ -0,0 +1,30 @@ +use chat::Chat; + +pub fn test_ui(chat: &Chat) { + let pddb = pddb::Pddb::new(); + // nuke any existing test dictionary, if it exists + pddb.delete_dict("tests.ui", None).ok(); + pddb.sync().ok(); + + // re-create the test room from scratch every time + chat.dialogue_set("tests.ui", Some("test_room")).expect("couldn't set dialog"); + + // generate a list of test posts that are repeatable each time + chat.post_add("alice", 1_700_000_000, "hello world!", None).ok(); + chat.post_add("bob", 1_700_000_002, "hi alice!", None).ok(); + for i in 0..5 { + chat.post_add("alice", 1_700_000_005 + i*4, &format!("alice sez {}", i), None).ok(); + chat.post_add("bob", 1_700_000_006 + i*4, &format!("bob sez {}", i), None).ok(); + chat.post_add("trent", 1_700_000_007 + i*4, &format!("trent sez {}", i), None).ok(); + } + chat.post_add("alice", 1_700_001_000, "eom", None).ok(); + + log::info!("triggering save"); + xous::send_message( + chat.cid(), + xous::Message::new_scalar(chat::ChatOp::DialogueSave as usize, 0, 0, 0, 0), + ) + .expect("failed to send new inbound msgs"); + + log::info!("test setup done"); +} diff --git a/apps/manifest.json b/apps/manifest.json index ec4552148..9a160de59 100644 --- a/apps/manifest.json +++ b/apps/manifest.json @@ -1,13 +1,13 @@ { "app-loader": { - "context_name": "App Loader", - "menu_name": { - "appmenu.app-loader": { - "en": "App Loader", - "en-tts": "App Loader" - } - }, - "submenu": 2 + "context_name": "App Loader", + "menu_name": { + "appmenu.app-loader": { + "en": "App Loader", + "en-tts": "App Loader" + } + }, + "submenu": 2 }, "ball": { "context_name": "ball demo app", @@ -54,6 +54,15 @@ }, "submenu": 1 }, + "chat-test": { + "context_name": "Chat UI test", + "menu_name": { + "appmenu.mtxchat": { + "en": "Chat UI test" + } + }, + "submenu": 1 + }, "repl": { "context_name": "repl demo app", "menu_name": { From 2596ebed868107117976863abacebc19112da012 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 2 Dec 2023 16:10:23 +0800 Subject: [PATCH 12/12] add chat-test to the lockfile --- Cargo.lock | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index f8d9f16e0..43f3c48a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -614,6 +614,41 @@ dependencies = [ "xous-ipc 0.9.54 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "chat-test" +version = "0.1.0" +dependencies = [ + "chat", + "codec", + "com", + "com_rs 0.1.0 (git+https://github.com/betrusted-io/com_rs?rev=891bdd3ca8e41f81510d112483e178aea3e3a921)", + "content-plugin-api", + "gam", + "graphics-server", + "ime-plugin-api", + "ime-plugin-shell", + "llio", + "locales", + "log", + "modals", + "net", + "num-derive", + "num-traits", + "pddb", + "percent-encoding", + "rkyv", + "serde", + "tls", + "trng", + "ureq", + "url", + "xous 0.9.54 (registry+https://github.com/rust-lang/crates.io-index)", + "xous-api-log", + "xous-api-names", + "xous-api-ticktimer", + "xous-ipc 0.9.54 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "checked_int_cast" version = "1.0.0"