245 lines
7.9 KiB
Rust
245 lines
7.9 KiB
Rust
|
use orbtk::prelude::*;
|
||
|
use rhai::RegisterFn;
|
||
|
use rhai::{packages::*, Engine, EvalAltResult, INT};
|
||
|
use std::sync::mpsc::{Receiver, Sender};
|
||
|
use std::time::Duration;
|
||
|
pub mod parser;
|
||
|
use crate::parser::Value;
|
||
|
use crate::parser::QML;
|
||
|
use parser::Value::{QmlIdent, QmlNumber, QmlString};
|
||
|
|
||
|
#[macro_use]
|
||
|
extern crate pest_derive;
|
||
|
|
||
|
#[derive(AsAny)]
|
||
|
pub struct MainViewState {
|
||
|
pub qml: Vec<(String, QML)>,
|
||
|
pub rx: Option<Receiver<String>>,
|
||
|
pub state1: Option<MVState>,
|
||
|
pub state2: Option<MVState>,
|
||
|
}
|
||
|
|
||
|
impl Widget for MainViewState {
|
||
|
fn create() -> Self {
|
||
|
unimplemented!()
|
||
|
}
|
||
|
|
||
|
fn build(self, ctx: &mut BuildContext) -> Entity {
|
||
|
let top_level = self.qml.clone();
|
||
|
let widget = ctx.create_entity();
|
||
|
let this = render_ctx(widget, ctx, &top_level, 0, 0).unwrap();
|
||
|
ctx.register_state(this, Box::new(self.state2.unwrap()));
|
||
|
ctx.register_state(widget, Box::new(self.state1.unwrap()));
|
||
|
this
|
||
|
}
|
||
|
|
||
|
fn insert_handler(self, handler: impl Into<Rc<dyn EventHandler>>) -> Self {
|
||
|
self
|
||
|
}
|
||
|
|
||
|
fn child(self, child: Entity) -> Self {
|
||
|
self
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(AsAny)]
|
||
|
pub struct MVState {
|
||
|
pub engine: Engine,
|
||
|
pub tx: Option<Sender<(String, String, String)>>,
|
||
|
pub rx: Option<Receiver<(String, String, String)>>,
|
||
|
}
|
||
|
|
||
|
impl MVState {
|
||
|
fn action(&mut self, action: String) {
|
||
|
match self.tx.clone() {
|
||
|
Some(tx) => {
|
||
|
self.engine
|
||
|
.register_fn("update_property", move |x: &str, y: &str, z: &str| {
|
||
|
tx.send((String::from(x), String::from(y), String::from(z)));
|
||
|
});
|
||
|
let result = self.engine.eval::<()>(action.as_str());
|
||
|
}
|
||
|
_ => {}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl State for MVState {
|
||
|
fn init(&mut self, registry: &mut Registry, ctx: &mut Context) {
|
||
|
self.update(registry, ctx);
|
||
|
}
|
||
|
|
||
|
fn update(&mut self, registry: &mut Registry, ctx: &mut Context) {
|
||
|
match &self.rx {
|
||
|
Some(rx) => match rx.recv_timeout(Duration::from_millis(1)) {
|
||
|
Ok((x, y, z)) => {
|
||
|
ctx.child(x.as_str())
|
||
|
.set::<String16>(y.as_str(), String16::from("hijack"));
|
||
|
}
|
||
|
_ => {}
|
||
|
},
|
||
|
_ => {}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Template for MainViewState {}
|
||
|
|
||
|
fn parse_number(val: Option<&Value>) -> f64 {
|
||
|
match val {
|
||
|
Some(QmlNumber(num)) => num.clone(),
|
||
|
_ => 0.0,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn render_ctx(
|
||
|
id: Entity,
|
||
|
ctx: &mut BuildContext,
|
||
|
qml: &Vec<(String, QML)>,
|
||
|
row: u32,
|
||
|
col: u32,
|
||
|
) -> Option<Entity> {
|
||
|
for (ident, child) in qml.iter() {
|
||
|
match ident.as_str() {
|
||
|
"Grid" => {
|
||
|
let mut grid = Grid::create();
|
||
|
grid = grid.attach(Grid::row(row as usize));
|
||
|
grid = grid.attach(Grid::column(col as usize));
|
||
|
let rows = parse_number(child.properties.get("rows")) as u32;
|
||
|
let cols = parse_number(child.properties.get("cols")) as u32;
|
||
|
|
||
|
let mut grid_rows = Rows::create();
|
||
|
for i in 0..rows {
|
||
|
grid_rows = grid_rows.row("stretch");
|
||
|
}
|
||
|
grid = grid.rows(grid_rows.build());
|
||
|
|
||
|
let mut grid_cols = Columns::create();
|
||
|
for i in 0..rows {
|
||
|
grid_cols = grid_cols.column("stretch");
|
||
|
}
|
||
|
grid = grid.columns(grid_cols.build());
|
||
|
|
||
|
let mut grow = 0u32;
|
||
|
let mut gcol = 0u32;
|
||
|
//rect = rect.attach(Grid::column(gcol as usize));
|
||
|
for (i, s) in child.children.iter() {
|
||
|
match render_ctx(id, ctx, &vec![(i.clone(), s.clone())], grow, gcol) {
|
||
|
Some(entity) => {
|
||
|
grid = grid.child(entity);
|
||
|
grow += 1;
|
||
|
if grow as u32 == rows {
|
||
|
grow = 0;
|
||
|
gcol += 1;
|
||
|
}
|
||
|
}
|
||
|
_ => {}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return Some(grid.build(ctx));
|
||
|
}
|
||
|
"Rectangle" => {
|
||
|
let width = parse_number(child.properties.get("width"));
|
||
|
let height = parse_number(child.properties.get("height"));
|
||
|
|
||
|
let color = match child.properties.get("color") {
|
||
|
Some(QmlString(col)) => match col.as_str() {
|
||
|
"red" => Color::rgb(0xff, 00, 00),
|
||
|
"blue" => Color::rgb(0x00, 00, 0xff),
|
||
|
_ => Color::rgb(0xff, 0xff, 0xff),
|
||
|
},
|
||
|
_ => Color::rgb(0xff, 0xff, 0xff),
|
||
|
};
|
||
|
let mut rect = Container::create()
|
||
|
// .width(width)
|
||
|
// .height(height)
|
||
|
.background(color);
|
||
|
|
||
|
rect = rect.attach(Grid::row(row as usize));
|
||
|
rect = rect.attach(Grid::column(col as usize));
|
||
|
|
||
|
for (i, s) in child.children.iter() {
|
||
|
match render_ctx(id, ctx, &vec![(i.clone(), s.clone())], 0, 0) {
|
||
|
Some(entity) => {
|
||
|
rect = rect.child(entity);
|
||
|
}
|
||
|
_ => {}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return Some(rect.build(ctx));
|
||
|
}
|
||
|
"Button" => {
|
||
|
let mut button = Button::create();
|
||
|
button = button.attach(Grid::row(row as usize));
|
||
|
button = button.attach(Grid::column(col as usize));
|
||
|
|
||
|
let code = match child.properties.get("onclick").unwrap() {
|
||
|
QmlString(code) => code.clone(),
|
||
|
_ => String::new(),
|
||
|
};
|
||
|
|
||
|
button = button.on_click(move |states, _| -> bool {
|
||
|
state(id, states).action(code.clone());
|
||
|
return true;
|
||
|
});
|
||
|
|
||
|
let text = match child.properties.get("text").unwrap() {
|
||
|
QmlString(text) => text.clone(),
|
||
|
_ => String::new(),
|
||
|
};
|
||
|
|
||
|
match child.properties.get("anchors.centerIn") {
|
||
|
Some(QmlIdent(str)) => {
|
||
|
if str.eq("parent") {
|
||
|
button = button.vertical_alignment(Alignment::Center);
|
||
|
button = button.horizontal_alignment(Alignment::Center);
|
||
|
}
|
||
|
}
|
||
|
_ => {}
|
||
|
}
|
||
|
button = button.text(text);
|
||
|
|
||
|
return Some(button.build(ctx));
|
||
|
}
|
||
|
"Text" => {
|
||
|
let text = match child.properties.get("text") {
|
||
|
Some(QmlString(text)) => text.clone(),
|
||
|
_ => String::new(),
|
||
|
};
|
||
|
|
||
|
let mut tt = TextBlock::create();
|
||
|
tt = tt.text(text);
|
||
|
tt = tt.attach(Grid::row(row as usize));
|
||
|
tt = tt.attach(Grid::column(col as usize));
|
||
|
|
||
|
match child.properties.get("id") {
|
||
|
Some(QmlIdent(text)) => {
|
||
|
tt = tt.id(text.clone());
|
||
|
}
|
||
|
_ => {}
|
||
|
};
|
||
|
|
||
|
match child.properties.get("anchors.centerIn") {
|
||
|
Some(QmlIdent(str)) => {
|
||
|
if str.eq("parent") {
|
||
|
tt = tt.vertical_alignment(Alignment::Center);
|
||
|
tt = tt.horizontal_alignment(Alignment::Center);
|
||
|
}
|
||
|
}
|
||
|
_ => {}
|
||
|
}
|
||
|
let entity = tt.build(ctx);
|
||
|
return Some(entity);
|
||
|
}
|
||
|
_ => println!("unknown ident {}", ident),
|
||
|
}
|
||
|
}
|
||
|
None
|
||
|
}
|
||
|
|
||
|
fn state<'a>(id: Entity, states: &'a mut StatesContext) -> &'a mut MVState {
|
||
|
states.get_mut(id)
|
||
|
}
|