xous-core/services/gam/src/api.rs

90 lines
2.9 KiB
Rust
Raw Normal View History

use xous::{Message, ScalarMessage};
use graphics_server::api::{Rectangle, TextView, Gid, Line, RoundedRectangle, Circle, Point};
2021-01-17 09:02:40 +00:00
#[derive(Debug, rkyv::Archive, rkyv::Unarchive, Copy, Clone)]
pub enum GamObjectType {
Line(Line),
Circ(Circle),
Rect(Rectangle),
RoundRect(RoundedRectangle),
}
#[derive(Debug, rkyv::Archive, rkyv::Unarchive, Copy, Clone)]
pub struct GamObject {
pub canvas: Gid,
pub obj: GamObjectType,
}
#[derive(Debug, rkyv::Archive, rkyv::Unarchive, Copy, Clone)]
pub struct SetCanvasBoundsRequest {
pub canvas: Gid,
pub requested: Point,
pub granted: Option<Point>,
}
#[derive(Debug, rkyv::Archive, rkyv::Unarchive, Copy, Clone)]
// #[archive(derive(Copy, Clone))]
pub enum Opcode {
// clears a canvas with a given GID
2021-01-17 13:58:02 +00:00
ClearCanvas(Gid),
// return the dimensions of a canvas as a Point (the top left is always (0,0))
GetCanvasBounds(Gid),
// request a new size for my canvas.
// This normally will be denied, unless the requested Gid corresponds to a special canvas that allows resizing.
SetCanvasBounds(SetCanvasBoundsRequest),
// draws an object
RenderObject(GamObject),
// renders a TextView
RenderTextView(TextView),
/////// planned
// returns a GID to the "content" Canvas; requires an authentication token
2021-01-17 13:58:02 +00:00
RequestContentCanvas(Gid),
// hides a canvas with a given GID
2021-01-17 13:58:02 +00:00
HideCanvas(Gid),
// requests the GID to the "input" Canvas; call only works once (for the IME server), then maps out
2021-02-18 20:02:29 +00:00
RequestInputCanvas,
// indicates if the current UI layout requires an input field
HasInput(bool),
}
impl core::convert::TryFrom<&Message> for Opcode {
type Error = &'static str;
fn try_from(message: &Message) -> Result<Self, Self::Error> {
match message {
Message::Scalar(m) => match m.id {
2021-01-17 13:58:02 +00:00
0 => Ok(Opcode::ClearCanvas(Gid::new([m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _]))),
_ => Err("GAM api: unknown Scalar ID"),
},
Message::BlockingScalar(m) => match m.id {
0 => Ok(Opcode::GetCanvasBounds(Gid::new([m.arg1 as _, m.arg2 as _, m.arg3 as _, m.arg4 as _]))),
_ => Err("GAM api: unknown BlockingScalar ID"),
}
_ => Err("GAM api: unhandled message type"),
}
}
}
impl Into<Message> for Opcode {
fn into(self) -> Message {
match self {
Opcode::ClearCanvas(gid) => Message::Scalar(ScalarMessage {
2021-01-18 20:54:54 +00:00
id: 0, arg1: gid.gid()[0] as _, arg2: gid.gid()[1] as _, arg3: gid.gid()[2] as _, arg4: gid.gid()[3] as _
}),
Opcode::GetCanvasBounds(gid) => Message::BlockingScalar(ScalarMessage {
id: 0, arg1: gid.gid()[0] as _, arg2: gid.gid()[1] as _, arg3: gid.gid()[2] as _, arg4: gid.gid()[3] as _
}),
_ => panic!("GAM api: Opcode type not handled by Into(), maybe you meant to use a helper method?"),
}
}
}