#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, flatipc::IpcSafe)]
pub struct Point {
pub x: i16,
pub y: i16,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, flatipc::IpcSafe)]
pub enum PixelColor {
#[default]
Dark,
Light,
}
impl From<bool> for PixelColor {
fn from(pc: bool) -> Self { if pc { PixelColor::Dark } else { PixelColor::Light } }
}
impl From<PixelColor> for bool {
fn from(pc: PixelColor) -> bool { if pc == PixelColor::Dark { true } else { false } }
}
impl From<usize> for PixelColor {
fn from(pc: usize) -> Self { if pc == 0 { PixelColor::Light } else { PixelColor::Dark } }
}
impl From<PixelColor> for usize {
fn from(pc: PixelColor) -> usize { if pc == PixelColor::Light { 0 } else { 1 } }
}
#[derive(Debug, Copy, Clone, Default, flatipc::IpcSafe)]
pub struct DrawStyle {
pub fill_color: Option<PixelColor>,
pub stroke_color: Option<PixelColor>,
pub stroke_width: i16,
}
#[derive(Debug, Clone, Copy, Default, flatipc::IpcSafe)]
pub struct Rectangle {
pub tl: Point,
pub br: Point,
pub style: DrawStyle,
}
#[derive(Debug, Copy, Clone, flatipc::IpcSafe)]
pub enum TextBounds {
BoundingBox(Rectangle),
GrowableFromBr(Point, u16),
GrowableFromTl(Point, u16),
GrowableFromBl(Point, u16),
GrowableFromTr(Point, u16),
CenteredTop(Rectangle),
CenteredBot(Rectangle),
}
impl Default for TextBounds {
fn default() -> Self { TextBounds::BoundingBox(Rectangle::default()) }
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default, flatipc::IpcSafe)]
pub struct Gid {
pub gid: [u32; 4],
}
#[derive(Debug, Copy, Clone, PartialEq, Default, flatipc::IpcSafe)]
pub enum TextOp {
#[default]
Nop,
Render,
ComputeBounds, }
#[derive(Copy, Clone, Debug, PartialEq, Default, flatipc::IpcSafe)]
pub enum GlyphStyle {
#[default]
Small = 0,
Regular = 1,
Bold = 2,
Monospace = 3,
Cjk = 4,
Large = 5,
ExtraLarge = 6,
Tall = 7,
}
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Default, flatipc::IpcSafe)]
pub struct Pt {
pub x: i16,
pub y: i16,
}
#[derive(Copy, Clone, Debug, PartialEq, Default, flatipc::IpcSafe)]
pub struct Cursor {
pub pt: Pt,
pub line_height: usize,
}
#[derive(Clone, Debug, Default, flatipc::Ipc)]
#[repr(C)]
pub struct TextView {
operation: TextOp,
canvas: Gid,
pub clip_rect: Option<Rectangle>,
pub untrusted: bool,
pub token: Option<[u32; 4]>,
pub invert: bool,
pub bounds_hint: TextBounds,
pub bounds_computed: Option<Rectangle>,
pub overflow: Option<bool>,
dry_run: bool,
pub style: GlyphStyle,
pub cursor: Cursor,
pub insertion: Option<i32>,
pub ellipsis: bool,
pub draw_border: bool,
pub clear_area: bool,
pub border_width: u16,
pub rounded_border: Option<u16>,
pub margin: Point,
pub selected: Option<[u32; 2]>,
pub busy_animation_state: Option<u32>,
pub text: flatipc::String<3000>,
}
#[test]
fn textview_general_test() {
use flatipc::{IntoIpc, Ipc};
let tv = TextView::default();
let mut tv_msg = tv.into_ipc();
tv_msg.draw_border = true;
use core::fmt::Write;
write!(&mut tv_msg.text, "Hello from the server!").unwrap();
let original_tv = tv_msg.into_original();
println!("Original textview draw_border: {} text: {}", original_tv.draw_border, original_tv.text);
}
#[test]
fn simple_ipc() {
#[derive(flatipc::Ipc, Debug)]
#[repr(C)]
pub enum SimpleIpc {
Single(u32),
}
impl Default for SimpleIpc {
fn default() -> Self { SimpleIpc::Single(0) }
}
let simple_ipc = SimpleIpc::default();
println!("Simple IPC: {:?}", simple_ipc);
}
#[test]
fn server_test() {
use flatipc::{IntoIpc, Ipc};
#[derive(flatipc::Ipc, Debug, PartialEq)]
#[repr(C)]
struct Incrementer {
value: u32,
}
let inc = Incrementer { value: 42 };
let adder_server = flatipc::backend::mock::Server::new(
Box::new(|opcode, a, b, buffer| {
let flattened = IpcIncrementer::from_slice(buffer, a).unwrap();
println!(
"In adder server. Opcode: {}. Current increment value: {} (a: {}, b: {})",
opcode, flattened.value, a, b
);
(0, 0)
}),
Box::new(|opcode, a, b, buffer| {
println!("LendMut opcode: {} (a: {}, b: {})", opcode, a, b);
let flattened = IpcIncrementer::from_slice_mut(buffer, a).unwrap();
flattened.value += 1;
(0, 0)
}),
);
#[derive(flatipc::Ipc, PartialEq, Debug)]
#[repr(C)]
struct Value(u32);
let x = Value(42).into_ipc();
let y = Value(42);
assert_eq!(*x, y);
let adder_server_connection =
flatipc::backend::mock::IPC_MACHINE.lock().unwrap().add_server(adder_server);
let mut lendable_inc = inc.into_ipc();
println!("Value before: {}", lendable_inc.value);
lendable_inc.lend(adder_server_connection, 0).unwrap();
println!("Value after: {}", lendable_inc.value);
println!("Value before mut: {}", lendable_inc.value);
lendable_inc.lend_mut(adder_server_connection, 0).unwrap();
println!("Value after mut: {}", lendable_inc.value);
println!("Does lendable_inc equal inc? {}", *lendable_inc == Incrementer { value: 43 });
let original_inc = lendable_inc.into_original();
println!("Original value: {}", original_inc.value);
}