mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
implement chat Dialogue navigation with up/down keys
This commit is contained in:
parent
6362ff9e4d
commit
7b7a7003f1
4 changed files with 224 additions and 55 deletions
|
|
@ -3,6 +3,11 @@ use rkyv::{Archive, Deserialize, Serialize};
|
|||
|
||||
|
||||
|
||||
// these are used to increment and decrement the selected post
|
||||
pub const POST_SELECTED_NEXT: usize = usize::MAX - 0;
|
||||
pub const POST_SELECTED_PREV: usize = usize::MAX - 1;
|
||||
|
||||
|
||||
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
|
||||
pub enum ChatOp {
|
||||
///
|
||||
|
|
|
|||
|
|
@ -115,6 +115,20 @@ impl Dialogue {
|
|||
None
|
||||
}
|
||||
|
||||
// return an arbitrary post in the dialogue by index
|
||||
pub fn post_get(&self, index: usize) -> Option<&Post> {
|
||||
self.posts.get(index)
|
||||
}
|
||||
|
||||
// return the index of the most recent post in the dialogue
|
||||
pub fn post_last(&self) -> Option<usize> {
|
||||
if self.posts.len() == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(self.posts.len() - 1)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn posts(&self) -> Iter<Post> {
|
||||
return self.posts.iter();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -232,12 +232,14 @@ pub fn server(
|
|||
ui.event(Event::F4);
|
||||
}
|
||||
"↑" => {
|
||||
log::info!("click ↑ : previous msg : pull request welcome!");
|
||||
ui.event(Event::Up);
|
||||
ui.post_select(POST_SELECTED_PREV);
|
||||
ui.redraw().expect("failed to redraw chat");
|
||||
}
|
||||
"↓" => {
|
||||
log::info!("click ↓ : next msg : pull request welcome!");
|
||||
ui.event(Event::Down);
|
||||
ui.post_select(POST_SELECTED_NEXT);
|
||||
ui.redraw().expect("failed to redraw chat");
|
||||
}
|
||||
"←" => {
|
||||
log::info!("click ← : noop : suggestions welcome!");
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use super::*;
|
||||
//use crate::{ChatOp, Dialogue, Event, Post, CHAT_SERVER_NAME};
|
||||
use crate::icontray::Icontray;
|
||||
use dialogue::{author::Author, post::Post, Dialogue};
|
||||
use dialogue::{post::Post, Dialogue};
|
||||
use gam::{menu_matic, MenuMatic, UxRegistration};
|
||||
use graphics_server::api::GlyphStyle;
|
||||
use graphics_server::{DrawStyle, Gid, PixelColor, Point, Rectangle, TextBounds, TextView};
|
||||
|
|
@ -11,6 +11,7 @@ use rkyv::de::deserializers::AllocDeserializer;
|
|||
use rkyv::ser::serializers::WriteSerializer;
|
||||
use rkyv::ser::Serializer;
|
||||
use rkyv::Deserialize;
|
||||
use std::cmp::min;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use std::io::{Error, ErrorKind, Read, Write};
|
||||
|
|
@ -42,6 +43,14 @@ pub(crate) struct Ui {
|
|||
gam: gam::Gam,
|
||||
modals: Modals,
|
||||
|
||||
// variables regarding the posts currently onscreen
|
||||
// the selected post is hilighted onscreen and the focus of the msg menu F4
|
||||
post_selected: Option<usize>,
|
||||
// the anchor post is drawn first, at the top or bottom of the screen
|
||||
post_anchor: Option<usize>,
|
||||
// layout post bubbles on the screen from top-down or bottom-up
|
||||
post_topdown: bool,
|
||||
|
||||
// variables that define our graphical attributes
|
||||
screensize: Point,
|
||||
bubble_width: u16,
|
||||
|
|
@ -122,6 +131,9 @@ impl Ui {
|
|||
gam,
|
||||
modals,
|
||||
screensize,
|
||||
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),
|
||||
|
|
@ -152,7 +164,13 @@ impl Ui {
|
|||
unsafe { rkyv::archived_value::<Dialogue>(&bytes, pos) };
|
||||
self.dialogue = match archive.deserialize(&mut AllocDeserializer {})
|
||||
{
|
||||
Ok(dialogue) => Some(dialogue),
|
||||
Ok(dialogue) => {
|
||||
// show most recent posts onscreen
|
||||
self.post_selected = dialogue.post_last();
|
||||
self.post_anchor = self.post_selected;
|
||||
self.post_topdown = false;
|
||||
Some(dialogue)
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!(
|
||||
"failed to deserialize Dialogue {}:{} {}",
|
||||
|
|
@ -322,9 +340,11 @@ impl Ui {
|
|||
}
|
||||
|
||||
// get a Post from the current Dialogue
|
||||
pub fn post_get(&self, _key: u32) -> Result<Post, Error> {
|
||||
log::warn!("not implemented");
|
||||
Err(Error::new(ErrorKind::Other, "not implemented"))
|
||||
pub fn post_get(&self, key: usize) -> Option<&Post> {
|
||||
match &self.dialogue {
|
||||
Some(dialogue) => dialogue.post_get(key),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
// set various status flags on a Post in the current Dialogue
|
||||
|
|
@ -333,6 +353,40 @@ impl Ui {
|
|||
Err(Error::new(ErrorKind::Other, "not implemented"))
|
||||
}
|
||||
|
||||
// set the index of the selected post to an arbitrary index
|
||||
// or select adjacent post with POST_SELECT_NEXT or POST_SELECT_PREV
|
||||
pub fn post_select(&mut self, index: usize) {
|
||||
self.post_selected = match &self.dialogue {
|
||||
Some(dialogue) => {
|
||||
match dialogue.post_last() {
|
||||
Some(last_post) => {
|
||||
match (index, self.post_selected) {
|
||||
(POST_SELECTED_NEXT, Some(selected)) => {
|
||||
if selected >= last_post {
|
||||
self.event(Event::Bottom);
|
||||
Some(last_post)
|
||||
} else {
|
||||
Some(selected + 1)
|
||||
}
|
||||
},
|
||||
(POST_SELECTED_PREV, Some(selected)) => {
|
||||
if selected == 0 {
|
||||
self.event(Event::Top);
|
||||
Some(selected)
|
||||
} else {
|
||||
Some(selected - 1)
|
||||
}
|
||||
},
|
||||
(index, _) => Some(min(index, last_post)), // arbitrary post
|
||||
}
|
||||
},
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
// set the text displayed on each of the Precursor Fn buttons
|
||||
pub fn ui_button(
|
||||
&self,
|
||||
|
|
@ -370,23 +424,42 @@ impl Ui {
|
|||
}
|
||||
}
|
||||
|
||||
fn bubble(&self, post: &Post, author: &Author, baseline: i16) -> TextView {
|
||||
use std::fmt::Write;
|
||||
let mut bubble_tv = if author.flag_is(AuthorFlag::Right) {
|
||||
TextView::new(
|
||||
self.canvas,
|
||||
TextBounds::GrowableFromBr(
|
||||
Point::new(self.screensize.x - self.margin.x, baseline),
|
||||
self.bubble_width,
|
||||
),
|
||||
)
|
||||
} else {
|
||||
TextView::new(
|
||||
self.canvas,
|
||||
TextBounds::GrowableFromBl(Point::new(self.margin.x, baseline), self.bubble_width),
|
||||
)
|
||||
// Create a bubble representing a Dialogue Post
|
||||
// return: a TextView bubble representing a Post
|
||||
// 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 {
|
||||
// set alignment of bubble left/right
|
||||
let mut align_right = false;
|
||||
let mut anchor_x = self.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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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 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.border_width = 1;
|
||||
|
||||
// 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.draw_border = true;
|
||||
bubble_tv.clear_area = true;
|
||||
bubble_tv.rounded_border = Some(self.bubble_radius);
|
||||
|
|
@ -423,43 +496,118 @@ impl Ui {
|
|||
log::info!("raised menu");
|
||||
}
|
||||
|
||||
// redraw posts on the screen ensuring the selected post is fully visible
|
||||
pub(crate) fn redraw(&mut self) -> Result<(), xous::Error> {
|
||||
self.clear_area();
|
||||
|
||||
// this defines the bottom border of the text bubbles as they stack up wards
|
||||
let mut bubble_baseline = self.screensize.y - self.margin.y;
|
||||
if let Some(dialogue) = &self.dialogue {
|
||||
for post in dialogue.posts().rev() {
|
||||
if let Some(author) = dialogue.author(post.author_id()) {
|
||||
let mut bubble_tv = self.bubble(post, author, bubble_baseline);
|
||||
self.gam
|
||||
.post_textview(&mut bubble_tv)
|
||||
.expect("couldn't render bubble textview");
|
||||
|
||||
if let Some(bounds) = bubble_tv.bounds_computed {
|
||||
// we only subtract 1x of the margin because the bounds were computed from a "bottom right" that already counted
|
||||
// the margin once.
|
||||
bubble_baseline -=
|
||||
(bounds.br.y - bounds.tl.y) + self.bubble_space + self.bubble_margin.y;
|
||||
if bubble_baseline <= 0 {
|
||||
// don't draw history that overflows the top of the screen
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break; // we get None on the bounds computed if the text view fell off the top of the screen
|
||||
}
|
||||
} else {
|
||||
log::warn!(
|
||||
"Post missing Author: {:?}:{:?} {:?}",
|
||||
self.pddb_dict,
|
||||
self.pddb_key,
|
||||
post
|
||||
);
|
||||
}
|
||||
if self.dialogue.is_some() {
|
||||
let mut attempt = 0;
|
||||
while !self.layout().unwrap_or(true) && attempt < 3
|
||||
{
|
||||
attempt += 1;
|
||||
}
|
||||
} else {
|
||||
self.clear_area(); // no dialogue so clear screen
|
||||
}
|
||||
log::trace!("chat app redraw##");
|
||||
self.gam.redraw().expect("couldn't redraw screen");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Layout the post bubbles on the screen from top-down or bottom-up
|
||||
// return: true if the selected post is fully visible
|
||||
// error: if Dialogue == None
|
||||
fn layout(&mut self) -> Result<bool, Error> {
|
||||
self.clear_area();
|
||||
match (&self.dialogue, &self.post_anchor) {
|
||||
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;
|
||||
|
||||
// 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 => 0,
|
||||
false => self.screensize.y - self.margin.y,
|
||||
};
|
||||
|
||||
// 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.bubble_space
|
||||
+ self.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.bubble_space
|
||||
+ self.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;
|
||||
}
|
||||
},
|
||||
(None, _) => {
|
||||
log::trace!("not enough post bubbles to fill the screen");
|
||||
return Ok(true)
|
||||
},
|
||||
(_,_) => return Ok(true), // get me outa-here
|
||||
}
|
||||
}
|
||||
if post_selected_visible || (bubble_count == 1 && is_selected){
|
||||
Ok(true)
|
||||
} 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)
|
||||
}
|
||||
},
|
||||
(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")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue