From a9a7ac435f725074bc1bc0e6349b5697c4a842b3 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 10 Mar 2021 00:25:14 +0800 Subject: [PATCH] add support for rounded rectangle text bubbles and dynamic sizing this makes more more natural chat-style text bubble interactions --- Cargo.lock | 16 ++- Cargo.toml | 2 + imports/blitstr-ref/Cargo.toml | 2 +- services/gam/Cargo.toml | 1 + services/gam/src/api.rs | 19 ++- services/gam/src/lib.rs | 28 ++++ services/gam/src/main.rs | 30 +++- services/graphics-server/src/api/shapes.rs | 2 +- services/graphics-server/src/api/text.rs | 2 +- services/graphics-server/src/lib.rs | 2 +- services/graphics-server/src/main.rs | 120 ++++++++++++++-- services/graphics-server/src/op.rs | 7 +- services/ime-frontend/src/main.rs | 2 +- services/ime-plugin-shell/src/main.rs | 1 + services/shellchat/Cargo.toml | 2 + services/shellchat/src/main.rs | 160 +++++++++++++++++++-- 16 files changed, 359 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 74baa1aa4..e572e67be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -121,7 +121,7 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "blitstr" version = "0.5.0" -source = "git+https://github.com/betrusted-io/blitstr.git?rev=216422d5b57af7464970552cc384a24cf5e1d782#216422d5b57af7464970552cc384a24cf5e1d782" +source = "git+https://github.com/betrusted-io/blitstr.git?rev=d4516b0a6fa3ae6d29e5e323513158c4cd6b8b16#d4516b0a6fa3ae6d29e5e323513158c4cd6b8b16" dependencies = [ "log", "rkyv", @@ -261,6 +261,17 @@ dependencies = [ "com_rs", ] +[[package]] +name = "content-plugin-api" +version = "0.1.0" +dependencies = [ + "graphics-server", + "log", + "rkyv", + "xous", + "xous-names", +] + [[package]] name = "crc" version = "1.8.1" @@ -388,6 +399,7 @@ dependencies = [ "bitflags", "blitstr-ref", "com", + "content-plugin-api", "graphics-server", "heapless", "ime-plugin-api", @@ -1078,7 +1090,9 @@ dependencies = [ name = "shellchat" version = "0.1.0" dependencies = [ + "content-plugin-api", "gam", + "graphics-server", "heapless", "ime-plugin-api", "log", diff --git a/Cargo.toml b/Cargo.toml index 40b9ef23b..9831205f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ members = [ "imports/blitstr-ref", "imports/com_rs-ref", "services/ime-plugin-api", + "services/content-plugin-api", "services/llio", "services/trng-tester", ] @@ -46,6 +47,7 @@ default-members = [ "services/benchmark-target", "services/ime-frontend", "services/ime-plugin-shell", + "services/content-plugin-api", "services/shellchat", "services/llio", "services/trng-tester", diff --git a/imports/blitstr-ref/Cargo.toml b/imports/blitstr-ref/Cargo.toml index 236090f5c..3639464d7 100644 --- a/imports/blitstr-ref/Cargo.toml +++ b/imports/blitstr-ref/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" description = "blitstr import package" [dependencies] -blitstr = { git = "https://github.com/betrusted-io/blitstr.git", rev = "216422d5b57af7464970552cc384a24cf5e1d782" } +blitstr = { git = "https://github.com/betrusted-io/blitstr.git", rev = "d4516b0a6fa3ae6d29e5e323513158c4cd6b8b16" } #blitstr = { path = "../../../blitstr" } diff --git a/services/gam/Cargo.toml b/services/gam/Cargo.toml index 86a45601f..47b9b2300 100644 --- a/services/gam/Cargo.toml +++ b/services/gam/Cargo.toml @@ -20,6 +20,7 @@ log = "0.4" com = { path = "../com"} blitstr-ref = { path = "../../imports/blitstr-ref" } ime-plugin-api = {path="../ime-plugin-api"} +content-plugin-api = {path="../content-plugin-api"} [target.'cfg(not(any(windows,unix)))'.dependencies] diff --git a/services/gam/src/api.rs b/services/gam/src/api.rs index 584374d52..12494fe7f 100644 --- a/services/gam/src/api.rs +++ b/services/gam/src/api.rs @@ -22,6 +22,14 @@ pub struct SetCanvasBoundsRequest { pub granted: Option, } +#[derive(Debug, rkyv::Archive, rkyv::Unarchive, Copy, Clone)] +pub struct ContentCanvasRequest { + // return value of the canvas Gid + pub canvas: Gid, + // name of the server requesting the content canvas + pub servername: xous::String<256>, +} + #[derive(Debug, rkyv::Archive, rkyv::Unarchive, Copy, Clone)] // #[archive(derive(Copy, Clone))] pub enum Opcode { @@ -44,20 +52,17 @@ pub enum Opcode { // forces a redraw (which also does defacement, etc.) Redraw, - /////// planned + // returns a GID to the "content" Canvas; currently, anyone can request it and draw to it, but maybe that policy should be stricter. + // the Gid argument is the rkyv return value. + RequestContentCanvas(ContentCanvasRequest), - // returns a GID to the "content" Canvas; requires an authentication token - RequestContentCanvas(Gid), + /////// planned // hides a canvas with a given GID HideCanvas(Gid), - // requests the GID to the "input" Canvas; call only works once (for the IME server), then maps out - RequestInputCanvas, - // indicates if the current UI layout requires an input field HasInput(bool), - } impl core::convert::TryFrom<&Message> for Opcode { diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index 549ba2f27..9ab9a3ee0 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -124,3 +124,31 @@ pub fn set_canvas_bounds_request(gam_cid: xous::CID, req: &mut SetCanvasBoundsRe } Ok(()) } + +pub fn request_content_canvas(gam_cid: xous::CID, requestor_name: &str) -> Result { + let mut server = xous::String::<256>::new(); + use core::fmt::Write; + write!(server, "{}", requestor_name).expect("GAM_API: couldn't write request_content_canvas server name"); + let req = ContentCanvasRequest { + canvas: Gid::new([0,0,0,0]), + servername: server, + }; + let rkyv = api::Opcode::RequestContentCanvas(req); + + let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096)); + let pos = writer.archive(&rkyv).expect("GAM_API: couldn't archive RequestContentCanvas"); + let mut xous_buffer = writer.into_inner(); + xous_buffer.lend_mut(gam_cid, pos as u32).expect("GAM_API: RequestContentCanvas operation failure"); + + // recover the mutable values and mirror the ones we care about back into our local structure + let returned = unsafe { rkyv::archived_value::(xous_buffer.as_ref(), pos)}; + if let rkyv::Archived::::RequestContentCanvas(result) = returned { + let ret: ContentCanvasRequest = result.unarchive(); + Ok(ret.canvas) + } else { + let ret = returned.unarchive(); + info!("got {:?}", ret); + log::error!("GAM_API: request_content_canvas got a return value from the server that isn't expected or handled"); + Err(xous::Error::InternalError) + } +} diff --git a/services/gam/src/main.rs b/services/gam/src/main.rs index f26eb697a..f04a3eb11 100644 --- a/services/gam/src/main.rs +++ b/services/gam/src/main.rs @@ -13,6 +13,8 @@ use blitstr::GlyphStyle; use graphics_server::*; use ime_plugin_api::ImeFrontEndApi; +use content_plugin_api::{ContentCanvasConnection, ContentCanvasApi}; + use log::info; use core::convert::TryFrom; @@ -106,7 +108,6 @@ impl ChatLayout { } pub fn resize_input(&mut self, new_height: i16, canvases: &mut FnvIndexMap) -> Result { let input_canvas = canvases.get(&self.input).expect("GAM: couldn't find input canvas"); - let content_canvas = canvases.get(&self.content).expect("GAM: couldn't find content canvas"); let predictive_canvas = canvases.get(&self.predictive).expect("GAM: couldn't find predictive canvas"); let status_canvas = canvases.get(&self.status).expect("GAM: couldn't find status canvas"); @@ -217,6 +218,9 @@ fn xmain() -> ! { imef.set_predictor(xous::names::SERVER_NAME_IME_PLUGIN_SHELL).expect("GAM: couldn't set IMEF prediction to shell"); // NOTE: all three API calls (set_input_canvas, set_prediction_canvas, set_predictor) are mandatory for IMEF initialization + // no content canvas initially, but keep a placeholder for one + let mut ccc: ContentCanvasConnection = ContentCanvasConnection{connection: None}; + let mut last_time: u64 = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap(); info!("GAM: entering main loop"); loop { @@ -328,6 +332,10 @@ fn xmain() -> ! { let newheight = chatlayout.resize_input(cb.requested.y, &mut canvases).expect("GAM: SetCanvasBoundsRequest couldn't recompute input canvas height"); cb.granted = Some(newheight); canvases = recompute_canvases(canvases, Rectangle::new(Point::new(0, 0), screensize)); + + if ccc.connection.is_some() { + ccc.redraw_canvas().expect("GAM: couldn't issue redraw to content canvas"); + } } else { cb.granted = None; } @@ -336,6 +344,26 @@ fn xmain() -> ! { let mut writer = rkyv::ArchiveBuffer::new(buf); writer.archive(&api::Opcode::SetCanvasBounds(cb)).expect("GAM: SetCanvasBoundsRequest couldn't re-archive return value"); }, + rkyv::Archived::::RequestContentCanvas(rcc) => { + let mut req: ContentCanvasRequest = rcc.unarchive(); + if debug1{info!("GAM: RequestContentCanvas {:?}", req);} + // for now, we do nothing with the incoming gid value; but, in the future, we can use it + // as an authentication token perhaps to control access + + //// here make a connection back to the requesting server, so that we can tell it to redraw if the layout has changed, etc. + if let Ok(cc) = xous_names::request_connection_blocking(req.servername.as_str().expect("GAM: malformed server name in content canvas request")) { + ccc.connection = Some(cc); + } else { + log::error!("GAM: content requestor gave us a bogus canvas result, aborting"); + continue; + }; + + req.canvas = chatlayout.content; + // pack our data back into the buffer to return + use rkyv::Write; + let mut writer = rkyv::ArchiveBuffer::new(buf); + writer.archive(&api::Opcode::RequestContentCanvas(req)).expect("GAM: RequestContentCanvas couldn't re-archive return value"); + }, _ => panic!("GAM: invalid mutable borrow message"), }; } else if let xous::Message::Borrow(m) = &envelope.body { diff --git a/services/graphics-server/src/api/shapes.rs b/services/graphics-server/src/api/shapes.rs index 5c5a37ec7..6651c8657 100644 --- a/services/graphics-server/src/api/shapes.rs +++ b/services/graphics-server/src/api/shapes.rs @@ -199,7 +199,7 @@ impl Rectangle { impl Into for Rectangle { fn into(self) -> ClipRect { - ClipRect::new(self.x0() as u32, self.y0() as u32, self.x1() as u32, self.y1() as u32) + ClipRect::new(self.x0() as _, self.y0() as _, self.x1() as _, self.y1() as _) } } diff --git a/services/graphics-server/src/api/text.rs b/services/graphics-server/src/api/text.rs index b22b43f35..3002e606f 100644 --- a/services/graphics-server/src/api/text.rs +++ b/services/graphics-server/src/api/text.rs @@ -67,7 +67,7 @@ pub struct TextView { pub style: GlyphStyle, pub text: xous::String::<3072>, pub cursor: Cursor, - pub insertion: Option, // this is the insertion point offset, if it's to be drawn, on the string + pub insertion: Option, // this is the insertion point offset, if it's to be drawn, on the string pub ellipsis: bool, pub draw_border: bool, diff --git a/services/graphics-server/src/lib.rs b/services/graphics-server/src/lib.rs index e41f3f731..ffd5dd06b 100644 --- a/services/graphics-server/src/lib.rs +++ b/services/graphics-server/src/lib.rs @@ -57,7 +57,7 @@ pub fn get_cursor(cid: CID) -> Result { let response = send_message(cid, api::Opcode::GetCursor.into())?; if let xous::Result::Scalar2(pt_as_usize, h) = response { let p: Point = pt_as_usize.into(); - Ok(Cursor::new(p.x as u32, p.y as u32, h as _)) + Ok(Cursor::new(p.x as _, p.y as _, h as _)) } else { panic!("unexpected return value: {:#?}", response); } diff --git a/services/graphics-server/src/main.rs b/services/graphics-server/src/main.rs index db53adce6..9f0abfa48 100644 --- a/services/graphics-server/src/main.rs +++ b/services/graphics-server/src/main.rs @@ -166,6 +166,7 @@ fn xmain() -> ! { let mut tv = rtv.unarchive(); if tv.clip_rect.is_none() { continue } // if no clipping rectangle is specified, nothing to draw + let clip_rect = tv.clip_rect.unwrap(); // this is the clipping rectangle of the canvas let screen_offset: Point = tv.clip_rect.unwrap().tl; // this is the translation vector to and from screen space let paintfn = if tv.dry_run { @@ -182,18 +183,121 @@ fn xmain() -> ! { TextBounds::BoundingBox(r) => { tv.bounds_computed = Some(r); }, - TextBounds::GrowableFromBr(_br, _width) => { - unimplemented!() + TextBounds::GrowableFromBr(br, width) => { + if !clip_rect.intersects_point(br) { + continue; + } + // assume: clip_rect is the total canvas area we could draw + // assume: br is the point we want to extend the drawable text bubble on + let checkedwidth: i16 = if width as i16 <= (br.x - clip_rect.tl.x) { + width as _ + } else { + (br.x - clip_rect.tl.x) as _ + }; + // first, create a clip that's the width of the growable, but as big as the height of the screen + let clip = blitstr::ClipRect::new(0, 0, checkedwidth as _, display.screen_size().y as _); + let mut c = blitstr::Cursor::new(0, 0, 0); + // now simulate the string painting + blitstr::paint_str( + display.native_buffer(), + clip, + &mut c, + tv.style.into(), + tv.text.as_str().unwrap(), + false, + None, + false, + blitstr::simulate_char + ); + // the resulting cursor position + line_height + margin is the height of the bounds + let checkedheight: i16 = if (c.pt.y as i16 + c.line_height as i16 + (tv.margin.y as i16) * 2) <= (br.y - clip_rect.tl.y as i16) { + c.pt.y as i16 + c.line_height as i16 + 2 * tv.margin.y + } else { + br.y as i16 - clip_rect.tl.y as i16 + }; + // if less than one line of text, shrink the box + let finalwidth = if c.pt.y <= c.line_height { + if c.pt.x as i16 + tv.margin.x * 3 < checkedwidth { + c.pt.x as i16 + tv.margin.x * 3 + } else { + checkedwidth + } + } else { + checkedwidth + }; + let tl = Point::new(br.x - finalwidth, br.y - checkedheight); + if clip_rect.intersects_point(tl) { + tv.bounds_computed = Some(Rectangle::new( + tl, + br + )); + } else { + tv.bounds_computed = None; + } }, TextBounds::GrowableFromTl(_tl, _width) => { unimplemented!() }, - TextBounds::GrowableFromBl(_bl, _width) => { - unimplemented!() - } + TextBounds::GrowableFromBl(bl, width) => { + if !clip_rect.intersects_point(bl) { + continue; + } + // assume: clip_rect is the total canvas area we could draw + // assume: bl is the point we want to extend the drawable text bubble on + let checkedwidth: i16 = if width as i16 <= (clip_rect.br.x - bl.x) { + width as _ + } else { + (clip_rect.br.x - bl.x) as _ + }; + // first, create a clip that's the width of the growable, but as big as the height of the screen + let clip = blitstr::ClipRect::new(0, 0, checkedwidth as _, display.screen_size().y as _); + let mut c = blitstr::Cursor::new(0, 0, 0); + // now simulate the string painting + blitstr::paint_str( + display.native_buffer(), + clip, + &mut c, + tv.style.into(), + tv.text.as_str().unwrap(), + false, + None, + false, + blitstr::simulate_char + ); + // the resulting cursor position + line_height is the height of the bounds + let checkedheight: i16 = if (c.pt.y as i16 + c.line_height as i16 + 2 * tv.margin.y as i16) <= (bl.y as i16 - clip_rect.tl.y as i16) { + c.pt.y as i16 + c.line_height as i16 + 2 * tv.margin.y + } else { + bl.y as i16 - clip_rect.tl.y as i16 + }; + + // if less than one line of text, shrink the box + let finalwidth = if c.pt.y <= c.line_height { + if c.pt.x as i16 + tv.margin.x * 3 < checkedwidth { + c.pt.x as i16 + tv.margin.x * 3 + } else { + checkedwidth + } + } else { + checkedwidth + }; + let tl = Point::new(bl.x, bl.y - checkedheight); + if clip_rect.intersects_point(tl) { + tv.bounds_computed = Some(Rectangle::new( + tl, + Point::new(bl.x + finalwidth, bl.y) + )); + } else { + tv.bounds_computed = None + } + }, } } if debugtv { info!("GFX(TV): computed bounds {:?}", tv.bounds_computed); } + if tv.bounds_computed.is_none() { + // the bounds weren't valid, so don't draw + continue; + } // clear the bounding box if requested let mut clear_rect = tv.bounds_computed.unwrap(); @@ -224,7 +328,7 @@ fn xmain() -> ! { }; if tv.rounded_border.is_some() { op::rounded_rectangle(display.native_buffer(), - RoundedRectangle::new(clear_rect, tv.rounded_border.unwrap() as _), tv.clip_rect); + RoundedRectangle::new(clear_rect, tv.rounded_border.unwrap() as _), Some(clear_rect)); } else { if debugtv { info!("GFX(TV): clearing rectangle {:?}", clear_rect); } op::rectangle(display.native_buffer(), clear_rect, tv.clip_rect); @@ -254,8 +358,8 @@ fn xmain() -> ! { // translate the cursor return value back to canvas coordinates tv.cursor = blitstr::Cursor { pt: blitstr::Pt::new( - ref_cursor.pt.x - screen_offset.x as u32, - ref_cursor.pt.y - screen_offset.y as u32, + ref_cursor.pt.x - screen_offset.x as i32, + ref_cursor.pt.y - screen_offset.y as i32, ), line_height: ref_cursor.line_height, }; diff --git a/services/graphics-server/src/op.rs b/services/graphics-server/src/op.rs index 7f9c539ae..088f5d4e0 100644 --- a/services/graphics-server/src/op.rs +++ b/services/graphics-server/src/op.rs @@ -270,7 +270,7 @@ impl Iterator for QuadrantIterator { // https://stackoverflow.com/questions/1201200/fast-algorithm-for-drawing-filled-circles fn next(&mut self) -> Option { - // If border or stroke colour is `None`, treat entire object as transparent and exit early + // If border and stroke colour is `None`, treat entire object as transparent and exit early if self.style.stroke_color.is_none() && self.style.fill_color.is_none() { return None; } @@ -281,10 +281,11 @@ impl Iterator for QuadrantIterator { let inner_radius_sq = inner_radius * inner_radius; let outer_radius_sq = outer_radius * outer_radius; + //log::info!("GFX|OP: quaditerator {}, {:?}, {:?}, {:?}, {:?}, {:?}", self.radius, self.center, self.p, self.quad, self.clip, self.style); loop { let mut item = None; if self.clip.is_none() || // short-circuit evaluation makes this safe - (self.clip.unwrap().intersects_point(self.p)) { + (self.clip.unwrap().intersects_point(self.p + self.center)) { let t = self.p; let len = t.x * t.x + t.y * t.y; @@ -484,6 +485,7 @@ pub fn rounded_rectangle(fb: &mut LcdFB, rr: RoundedRectangle, clip: Option ! { let mut i = 1; for &s in history.iter() { // iterator is from oldest to newest, so do some math to go from newest to oldest + // TIL: there is a .rev() feature in iterators. Next time maybe use that instead. if (history.len() as u32 - i) == index { // decompose the string into a character-by-character sequence // and then stuff byte-by-byte, as fits, into the return array diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index a72dddc4d..e4718dc57 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -12,7 +12,9 @@ log-server = { path = "../log-server" } ticktimer-server = { path = "../ticktimer-server" } xous-names = { path = "../xous-names" } gam = {path="../gam"} +graphics-server = {path="../graphics-server"} ime-plugin-api = {path="../ime-plugin-api"} +content-plugin-api = {path="../content-plugin-api"} # all content canvas providers must provide this API log = "0.4" rkyv = { version = "0.3.1", default_features = false } diff --git a/services/shellchat/src/main.rs b/services/shellchat/src/main.rs index bd9a7f05e..5691accfa 100644 --- a/services/shellchat/src/main.rs +++ b/services/shellchat/src/main.rs @@ -4,28 +4,62 @@ use log::{error, info}; use core::fmt::Write; +use core::convert::TryFrom; use rkyv::Unarchive; use rkyv::archived_value; use core::pin::Pin; use ime_plugin_api::{ImeFrontEndApi, ImeFrontEnd}; +use graphics_server::{Gid, Point, Rectangle, TextBounds, TextView, DrawStyle, GlyphStyle, PixelColor}; use heapless::spsc::Queue; -use heapless::consts::U4; +use heapless::consts::U16; +#[derive(Debug)] +struct History { + // the history record + pub text: xous::String<1024>, + // if true, this was input from the user; if false, it's a response from the shell + pub is_input: bool, +} + +#[derive(Debug)] struct Repl { + // optional structures that indicate new input to the Repl loop per iteration + // an input string input: Option>, - history: Queue, U4>, + // examples of future inputs: returned values via other services + + // record our input history + history: Queue, + content: Gid, gam: xous::CID, + + // variables that define our graphical attributes + screensize: Point, + 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 } impl Repl { - fn new() -> Self { + fn new(my_server_name: &str) -> Self { let gam_conn = xous_names::request_connection_blocking(xous::names::SERVER_NAME_GAM).expect("SHCH: can't connect to GAM"); + let content = gam::request_content_canvas(gam_conn, my_server_name).expect("SHCH: couldn't get content canvas"); + let screensize = gam::get_canvas_bounds(gam_conn, content).expect("SHCH: couldn't get dimensions of content canvas"); Repl { input: None, history: Queue::new(), + content, gam: gam_conn, + screensize, + 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, } } @@ -39,30 +73,124 @@ impl Repl { Ok(()) } + fn circular_push(&mut self, item: History) { + if self.history.len() == self.history.capacity() { + self.history.dequeue().expect("SHCH: couldn't dequeue historye"); + } + self.history.enqueue(item).expect("SHCH: couldn't store input line"); + } + /// update the loop, in response to various inputs fn update(&mut self) -> Result<(), xous::Error> { + let debug1 = true; // if we had an input string, do something if let Some(local) = self.input { - if self.history.len() == self.history.capacity() { - self.history.dequeue().expect("SHCH: couldn't dequeue historye"); - } - self.history.enqueue(local).expect("SHCH: couldn't store input line"); - self.input = None; + let input_history = History { + text: local, + is_input: true, + }; + self.circular_push(input_history); + } - for &s in self.history.iter() { - info!("SHCH: command history {}", s); + // AT THIS POINT: if we have other inputs, update accordingly + // (PLACEHOLDER) + + // redraw UI once upon accepting input + self.redraw().expect("SHCH: can't redraw"); + + + ////// AT THIS POINT + // take the input and pass it on to the various command parsers, and attach result + // (PLACEHOLDER) + if let Some(local) = self.input { + let mut response = xous::String::<1024>::new(); + write!(response, "Command not recognized: {}", local.as_str().unwrap()).unwrap(); + let output_history = History { + text: response, + is_input: false + }; + self.circular_push(output_history); + } + + + // clear all the inputs to the loop, so we don't process them twice + self.input = None; + // redraw UI now that we've responded + self.redraw().expect("SHCH: can't redraw"); + + if debug1 { + for h in self.history.iter() { + info!("SHCH: command history is_input: {}, text:{}", h.is_input, h.text); } } - // other things to do based on other events... + Ok(()) + } + pub fn clear_area(&self) { + gam::draw_rectangle(self.gam, self.content, + Rectangle::new_with_style(Point::new(0, 0), self.screensize, + DrawStyle { + fill_color: Some(PixelColor::Light), + stroke_color: None, + stroke_width: 0 + } + )).expect("SHCH: can't clear content area"); + } + 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; + + // iterator returns from oldest to newest + // .rev() iterator is from newest to oldest + for h in self.history.iter().rev() { + let mut bubble_tv = + if h.is_input { + TextView::new(self.content, 255, + TextBounds::GrowableFromBr( + Point::new(self.screensize.x - self.margin.x, bubble_baseline), + self.bubble_width)) + } else { + TextView::new(self.content, 255, + TextBounds::GrowableFromBl( + Point::new(self.margin.x, bubble_baseline), + self.bubble_width)) + }; + if h.is_input { + bubble_tv.border_width = 1; + } else { + bubble_tv.border_width = 2; + } + bubble_tv.draw_border = true; + bubble_tv.clear_area = true; + bubble_tv.rounded_border = Some(self.bubble_radius); + bubble_tv.style = GlyphStyle::Small; + bubble_tv.margin = self.bubble_margin; + bubble_tv.ellipsis = false; bubble_tv.insertion = None; + write!(bubble_tv.text, "{}", h.text.as_str().expect("SHCH: couldn't convert history text")).expect("SHCH: couldn't write history text to TextView"); + gam::post_textview(self.gam, &mut bubble_tv).expect("SHCH: 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 + } + } Ok(()) } } #[xous::xous_main] fn xmain() -> ! { - let debug1 = true; + let debug1 = false; log_server::init_wait().unwrap(); info!("SHCH: my PID is {}", xous::process::id()); @@ -73,7 +201,7 @@ fn xmain() -> ! { let imef = ImeFrontEnd { connection: Some(imef_conn) }; imef.register_listener(xous::names::SERVER_NAME_SHELLCHAT).expect("SHCH: couldn't request events from IMEF"); - let mut repl = Repl::new(); + let mut repl = Repl::new(xous::names::SERVER_NAME_SHELLCHAT); let mut update_repl = false; info!("SHCH: starting main loop"); loop { @@ -93,6 +221,12 @@ fn xmain() -> ! { }, _ => panic!("SHCH: invalid response from server -- corruption occurred in MemoryMessage") }; + } else if let Ok(opcode) = content_plugin_api::Opcode::try_from(&envelope.body) { + match opcode { + content_plugin_api::Opcode::Redraw => { + repl.redraw().expect("SHCH: REPL couldn't redraw"); + } + } } else { error!("SHCH: couldn't convert message"); }