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 => {