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.
This commit is contained in:
bunnie 2023-11-30 23:05:41 +08:00
parent c5dff9d35d
commit 7222fb0de9
4 changed files with 103 additions and 59 deletions

View file

@ -2,9 +2,11 @@ pub mod attach;
pub mod author; pub mod author;
pub mod post; pub mod post;
use crate::now; use crate::ui::VisualProperties;
use crate::{now, default_textview};
use author::Author; use author::Author;
use gam::Gam;
use core::slice::Iter; use core::slice::Iter;
use post::Post; use post::Post;
use rkyv::{Archive, Deserialize, Serialize}; use rkyv::{Archive, Deserialize, Serialize};
@ -65,6 +67,7 @@ impl Dialogue {
/// * `timestamp` - the timestamp of the Post /// * `timestamp` - the timestamp of the Post
/// * `text` - the text content of the Post /// * `text` - the text content of the Post
/// * `attach_url` - a url of an attachment (image for example) /// * `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( pub fn post_add(
&mut self, &mut self,
@ -72,10 +75,11 @@ impl Dialogue {
timestamp: u64, timestamp: u64,
text: &str, text: &str,
_attach_url: Option<&str>, _attach_url: Option<&str>,
vp: Option<(&VisualProperties, &Gam)>,
) -> Result<(), Error> { ) -> Result<(), Error> {
match self.author_id(author) { match self.author_id(author) {
Some(author_id) => { Some(author_id) => {
let new = Post::new( let mut new = Post::new(
author_id, timestamp, text, None, // TODO implement author_id, timestamp, text, None, // TODO implement
); );
if self.posts.len() == 0 { if self.posts.len() == 0 {
@ -86,6 +90,15 @@ impl Dialogue {
let first_ts = self.posts.first().map_or(0, |p| p.timestamp()); let first_ts = self.posts.first().map_or(0, |p| p.timestamp());
let last_ts = self.posts.last().map_or(0, |p| p.timestamp()); let last_ts = self.posts.last().map_or(0, |p| p.timestamp());
log::trace!("{:?}", new); 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 { if new_ts > last_ts {
log::info!("insert new post at end"); log::info!("insert new post at end");
self.posts.push(new); self.posts.push(new);

View file

@ -2,6 +2,7 @@ use super::attach::Attach;
use crate::PostFlag; use crate::PostFlag;
use enumset::EnumSet; use enumset::EnumSet;
use graphics_server::Rectangle;
use rkyv::{Archive, Deserialize, Serialize}; use rkyv::{Archive, Deserialize, Serialize};
#[derive(Archive, Serialize, Deserialize, Debug)] #[derive(Archive, Serialize, Deserialize, Debug)]
@ -11,6 +12,7 @@ pub struct Post {
text: String, text: String,
attach: Option<Attach>, attach: Option<Attach>,
pub flags: u16, pub flags: u16,
pub bounding_box: Option<Rectangle>,
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -22,6 +24,7 @@ impl Post {
text: text.to_string(), text: text.to_string(),
attach: attach, attach: attach,
flags: 0, flags: 0,
bounding_box: None,
} }
} }

View file

@ -4,9 +4,12 @@ pub mod icontray;
pub mod ui; pub mod ui;
pub use api::*; pub use api::*;
use graphics_server::{TextView, Point, Rectangle, TextBounds};
use graphics_server::api::GlyphStyle;
pub use ui::BUSY_ANIMATION_RATE_MS; pub use ui::BUSY_ANIMATION_RATE_MS;
use gam::MenuItem; use gam::MenuItem;
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
use ui::VisualProperties;
use std::convert::TryInto; use std::convert::TryInto;
use std::thread; use std::thread;
use std::time::{SystemTime, UNIX_EPOCH}; 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::{Error, CID, SID, msg_scalar_unpack};
use xous_ipc::Buffer; 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 { pub struct Chat {
cid: CID, cid: CID,
} }

View file

@ -23,6 +23,18 @@ use xous_names::XousNames;
pub const BUSY_ANIMATION_RATE_MS: usize = 200; 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)] #[allow(dead_code)]
pub(crate) struct Ui { pub(crate) struct Ui {
// optional structures that indicate new input to the Chat loop per iteration // 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 // optional opcode ID to process UI-event msgs
opcode_event: Option<usize>, opcode_event: Option<usize>,
canvas: Gid,
gam: gam::Gam, gam: gam::Gam,
modals: Modals, modals: Modals,
tt: Ticktimer, tt: Ticktimer,
@ -58,21 +69,14 @@ pub(crate) struct Ui {
// layout post bubbles on the screen from top-down or bottom-up // layout post bubbles on the screen from top-down or bottom-up
post_topdown: bool, 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. /// TextView for the status bar. This encapsulates the state of the busy animation, and the text within.
status_tv: TextView, status_tv: TextView,
/// Track the last time we update the status bar; use this avoid double-updating busy animations /// Track the last time we update the status bar; use this avoid double-updating busy animations
status_last_update_ms: u64, status_last_update_ms: u64,
/// The default message to show when we exit a busy state /// The default message to show when we exit a busy state
status_idle_text: String, status_idle_text: String,
bubble_width: u16,
margin: Point, // margin to edge of canvas vp: VisualProperties,
bubble_margin: Point, // margin of text in bubbles
bubble_radius: u16,
bubble_space: i16, // spacing between text bubbles
// variables that define a menu // variables that define a menu
menu_mode: bool, menu_mode: bool,
@ -148,6 +152,16 @@ impl Ui {
write!(status_tv, "{}", t!("chat.status.initial", locales::LANG).to_string()).ok(); write!(status_tv, "{}", t!("chat.status.initial", locales::LANG).to_string()).ok();
let tt = ticktimer_server::Ticktimer::new().unwrap(); let tt = ticktimer_server::Ticktimer::new().unwrap();
let status_last_update_ms = tt.elapsed_ms(); 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 { Ui {
input: None, input: None,
msg: None, msg: None,
@ -158,22 +172,15 @@ impl Ui {
self_cid: xous::connect(sid).unwrap(), self_cid: xous::connect(sid).unwrap(),
app_cid, app_cid,
opcode_event, opcode_event,
canvas,
gam, gam,
modals, modals,
tt, tt,
screensize,
status_height,
status_tv, status_tv,
status_last_update_ms, status_last_update_ms,
post_selected: None, post_selected: None,
post_anchor: None, post_anchor: None,
post_topdown: false, post_topdown: false,
bubble_width: ((screensize.x / 5) * 4) as u16, // 80% width for the text bubbles vp: bubble_properties,
margin: Point::new(4, 4),
bubble_margin: Point::new(4, 4),
bubble_radius: 4,
bubble_space: 4,
menu_mode: true, menu_mode: true,
app_menu: app_menu.to_owned(), app_menu: app_menu.to_owned(),
menu_mgr: menu_mgr, menu_mgr: menu_mgr,
@ -393,7 +400,7 @@ impl Ui {
(Some(pddb_key), Some(ref mut dialogue)) => { (Some(pddb_key), Some(ref mut dialogue)) => {
if dialogue_id.len() == 0 || pddb_key.eq(&dialogue_id) { if dialogue_id.len() == 0 || pddb_key.eq(&dialogue_id) {
dialogue dialogue
.post_add(author, timestamp, text, attach_url) .post_add(author, timestamp, text, attach_url, Some((&self.vp, &self.gam)))
.unwrap(); .unwrap();
} else { } else {
log::warn!("dropping Post as dialogue_id does not match pddb_key: '{}' vs '{}'", pddb_key, dialogue_id); 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 /// * `anchor_y` - the vertical position on screen to draw TextView bubble
/// ///
fn bubble(&self, post: &Post, dialogue: &Dialogue, hilite: bool, anchor_y: i16) -> TextView { 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 // set alignment of bubble left/right
let mut align_right = false; 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 let Some(author) = dialogue.author(post.author_id()) {
if author.flag_is(AuthorFlag::Right) { if author.flag_is(AuthorFlag::Right) {
// align right // align right
align_right = true; 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 // set the text bounds of the bubble and the growth direction
let anchor = Point::new(anchor_x, anchor_y); 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) { let text_bounds = match (self.post_topdown, align_right) {
(true, true) => TextBounds::GrowableFromTr(anchor, width), (true, true) => TextBounds::GrowableFromTr(anchor, width),
(true, false) => TextBounds::GrowableFromTl(anchor, width), (true, false) => TextBounds::GrowableFromTl(anchor, width),
@ -549,28 +558,7 @@ impl Ui {
(false, false) => TextBounds::GrowableFromBl(anchor, width), (false, false) => TextBounds::GrowableFromBl(anchor, width),
}; };
// create the bubble with the anchor and a growable direction bubble_tv.bounds_hint = text_bounds;
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 bubble_tv
} }
@ -579,10 +567,10 @@ impl Ui {
fn clear_area(&self) { fn clear_area(&self) {
self.gam self.gam
.draw_rectangle( .draw_rectangle(
self.canvas, self.vp.canvas,
Rectangle::new_with_style( Rectangle::new_with_style(
Point::new(0, self.status_height as i16), Point::new(0, self.vp.status_height as i16),
self.screensize, self.vp.screensize,
DrawStyle { DrawStyle {
fill_color: Some(PixelColor::Light), fill_color: Some(PixelColor::Light),
stroke_color: None, stroke_color: None,
@ -716,12 +704,13 @@ impl Ui {
self.gam.post_textview(&mut self.status_tv) self.gam.post_textview(&mut self.status_tv)
.expect("couldn't render status bar"); .expect("couldn't render status bar");
let status_border = Line::new( let status_border = Line::new(
Point::new(0, self.status_height as i16), Point::new(0, self.vp.status_height as i16),
Point::new(self.screensize.x, self.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 status_border
).expect("couldn't draw status lower border"); ).expect("couldn't draw status lower border");
match (&self.dialogue, &self.post_anchor) { match (&self.dialogue, &self.post_anchor) {
(Some(dialogue), Some(post_anchor)) => { (Some(dialogue), Some(post_anchor)) => {
log::info!("redrawing dialogue: {}", dialogue.title); log::info!("redrawing dialogue: {}", dialogue.title);
@ -731,8 +720,8 @@ impl Ui {
// initialise the first post index AND the vertical position on the screen // initialise the first post index AND the vertical position on the screen
let mut post_index = *post_anchor; let mut post_index = *post_anchor;
let mut anchor_y = match self.post_topdown { let mut anchor_y = match self.post_topdown {
true => self.status_height as i16 + self.margin.y, true => self.vp.status_height as i16 + self.vp.margin.y,
false => self.screensize.y - self.margin.y, false => self.vp.screensize.y - self.vp.margin.y,
}; };
// fill the screen with post bubbles from top-down or bottom-up // fill the screen with post bubbles from top-down or bottom-up
@ -766,8 +755,8 @@ impl Ui {
} }
post_index += 1; post_index += 1;
anchor_y += (bounds.br.y - bounds.tl.y) anchor_y += (bounds.br.y - bounds.tl.y)
+ self.bubble_space + self.vp.bubble_space
+ self.bubble_margin.y; + self.vp.bubble_margin.y;
} }
false => { false => {
if post_index == 0 { if post_index == 0 {
@ -778,8 +767,8 @@ impl Ui {
} }
post_index -= 1; post_index -= 1;
anchor_y -= (bounds.br.y - bounds.tl.y) anchor_y -= (bounds.br.y - bounds.tl.y)
+ self.bubble_space + self.vp.bubble_space
+ self.bubble_margin.y; + self.vp.bubble_margin.y;
} }
}, },
None => { None => {