implement chat app_menu

This commit is contained in:
nworbnhoj 2023-08-15 04:27:15 +10:00
parent b095462f29
commit 4f03a27e79
3 changed files with 73 additions and 34 deletions

View file

@ -16,6 +16,7 @@ pub enum ChatOp {
/// redraw our UI
GamRedraw,
ListenSet,
MenuAdd,
PostAdd,
PostDel,
PostFind,
@ -66,6 +67,7 @@ pub enum Event {
Bottom, // Bottom of post list reached
Key, // keystroke
Post, // new user Post committed
Menu, // menu item clicked
}
#[derive(

View file

@ -4,12 +4,13 @@ pub mod dialogue;
pub mod ui;
pub use api::*;
use gam::MenuItem;
use num_traits::FromPrimitive;
use std::convert::TryInto;
use std::thread;
use std::time::{SystemTime, UNIX_EPOCH};
use xous::{Error, CID, SID};
use xous::{Error, MessageEnvelope, CID, SID};
use xous_ipc::Buffer;
pub struct Chat {
@ -18,7 +19,8 @@ pub struct Chat {
impl Chat {
pub fn new(
app_name: &str,
app_name: &'static str,
app_menu: &'static str,
app_cid: Option<CID>,
opcode_post: Option<usize>,
opcode_event: Option<usize>,
@ -26,12 +28,19 @@ impl Chat {
) -> Self {
let chat_sid = xous::create_server().unwrap();
let chat_cid = xous::connect(chat_sid).unwrap();
let app_name = app_name.to_string();
log::info!("Starting chat UI server",);
thread::spawn({
move || {
server(chat_sid, app_cid, opcode_post, opcode_event, opcode_rawkeys);
server(
chat_sid,
app_name,
app_menu,
app_cid,
opcode_post,
opcode_event,
opcode_rawkeys,
);
}
});
@ -60,6 +69,13 @@ impl Chat {
}
}
pub fn menu_add(&self, item: MenuItem) -> Result<(), Error> {
match Buffer::into_buf(item) {
Ok(buf) => buf.send(self.cid, ChatOp::MenuAdd as u32).map(|_| ()),
Err(_) => Err(xous::Error::InternalError),
}
}
// add a new Post to the current Dialogue
pub fn post_add(
&self,
@ -145,7 +161,8 @@ impl Chat {
pub fn server(
sid: SID,
app_name: &str,
app_name: &'static str,
app_menu: &'static str,
app_cid: Option<CID>,
opcode_post: Option<usize>,
opcode_event: Option<usize>,
@ -160,7 +177,7 @@ pub fn server(
// let mut opcode_event = opcode_event;
// let mut opcode_rawkeys = opcode_rawkeys;
let mut ui = ui::Ui::new(sid, app_name, app_cid, opcode_event);
let mut ui = ui::Ui::new(sid, app_name, app_menu, app_cid, opcode_event);
let mut user_post = true;
let mut allow_redraw = false;
@ -236,14 +253,15 @@ pub fn server(
Some(ChatOp::PostFlag) => {
log::warn!("ChatOp::PostFlag not implemented");
}
Some(ChatOp::ListenSet) => {
// set listener sid and opcodes to receive UI-event msgs & user posts
// pub fn listen_set(&self, cid: xous::CID, opcode_post: Option<u32>, opcode_event: Option<u32>, opcode_rawkeys: Option<u32>) {
// self.listener = cid;
// self.opcode_post = opcode_post;
// self.opcode_event = opcode_event;
// self.opcode_rawkeys = opcode_rawkeys;
// }
Some(ChatOp::MenuAdd) => {
log::warn!("ChatOp::MenuAdd not implemented");
let buffer =
unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
if let Ok(menu_item) = buffer.to_original::<MenuItem, _>() {
ui.menu_add(menu_item);
} else {
log::warn!("failed to deserialize MenuItem");
}
}
Some(ChatOp::UiButton) => {
log::warn!("ChatOp::UiButton not implemented");

View file

@ -1,7 +1,7 @@
use super::*;
//use crate::{ChatOp, Dialogue, Event, Post, CHAT_SERVER_NAME};
use dialogue::{author::Author, post::Post, Dialogue};
use gam::UxRegistration;
use gam::{menu_matic, MenuMatic, MenuPayload, UxRegistration};
use graphics_server::api::GlyphStyle;
use graphics_server::{DrawStyle, Gid, PixelColor, Point, Rectangle, TextBounds, TextView};
use locales::t;
@ -49,6 +49,10 @@ pub(crate) struct Ui {
bubble_radius: u16,
bubble_space: i16, // spacing between text bubbles
// variables that define a menu
app_menu: String,
menu_mgr: MenuMatic,
// our security token for making changes to our record on the GAM
token: [u32; 4],
}
@ -57,7 +61,8 @@ pub(crate) struct Ui {
impl Ui {
pub(crate) fn new(
sid: xous::SID,
app_name: &str,
app_name: &'static str,
app_menu: &'static str,
app_cid: Option<xous::CID>,
opcode_event: Option<usize>,
) -> Self {
@ -89,6 +94,12 @@ impl Ui {
.expect("couldn't get dimensions of content canvas");
let pddb = pddb::Pddb::new();
pddb.try_mount();
let menu_mgr = menu_matic(
Vec::<MenuItem>::new(),
app_menu,
Some(xous::create_server().unwrap()),
)
.expect("couldn't create MenuMatic manager");
Ui {
input: None,
msg: None,
@ -107,7 +118,9 @@ impl Ui {
bubble_margin: Point::new(4, 4),
bubble_radius: 4,
bubble_space: 4,
token: token.unwrap(),
app_menu: app_menu.to_owned(),
menu_mgr: menu_mgr,
token: token,
}
}
@ -257,19 +270,9 @@ impl Ui {
}
}
// set listener sid and opcodes to receive UI-event msgs & user posts
// pub fn listen_set(
// &self,
// cid: xous::CID,
// opcode_post: Option<u32>,
// opcode_event: Option<u32>,
// opcode_rawkeys: Option<u32>,
// ) {
// self.listener = cid;
// self.opcode_post = opcode_post;
// self.opcode_event = opcode_event;
// self.opcode_rawkeys = opcode_rawkeys;
// }
pub fn menu_add(&self, item: MenuItem) {
self.menu_mgr.add_item(item);
}
// add a new Post to the current Dialogue
pub fn post_add(
@ -343,9 +346,18 @@ impl Ui {
}
// send a xous scalar message with an Event to the Chat App cid/opcode
fn event(&self, _event: Event) {
log::warn!("not implemented");
// Err(Error::new(ErrorKind::Other, "not implemented"))
pub fn event(&self, event: Event) {
log::info!("Event {:?}", event);
match (self.app_cid, self.opcode_event) {
(Some(cid), Some(opcode)) => match xous::send_message(
cid,
xous::Message::new_scalar(opcode as usize, event as usize, 0, 0, 0),
) {
Ok(_) => log::info!("sent event msg"),
Err(e) => log::warn!("failed to send event msg: {:?}", e),
},
_ => log::warn!("missing cid or event opcode"),
}
}
fn bubble(&self, post: &Post, author: &Author, baseline: i16) -> TextView {
@ -393,12 +405,19 @@ impl Ui {
.expect("can't clear canvas area");
}
pub(crate) fn raise_menu(&mut self) {
// self.title_dirty = true;
self.gam
.raise_menu(&self.app_menu)
.expect("couldn't raise our submenu");
log::info!("raised menu");
}
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()) {