use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::Arc;
use egui::{
Color32, ColorImage, Frame, Grid, Image, ImageData, ScrollArea, Slider, TextureHandle,
TextureId, TextureOptions, Vec2, Window,
};
use xur_runtime::convert::into_value;
use xur_runtime::registry::ModuleBuilder;
use xur_runtime::{Registry, RuntimeError, Value, XurStr};
use super::types::{EguiCtx, EguiUi, Texture, with_ctx, with_ui};
static TEXTURES: Mutex<Option<HashMap<u64, TextureHandle>>> = Mutex::new(None);
pub(crate) fn register_ctx_methods(_registry: &Registry, m: &mut ModuleBuilder<'_>) {
let mut ty = m.ty::<EguiCtx>("Ctx");
ty.raw_function("window", |cx, args| {
let [this, Value::Str(title), body] = args else {
return Err(RuntimeError::new(
"`window` takes (title: str, body: fn(ui))",
));
};
if !matches!(body, Value::Closure(_) | Value::Fn(_)) {
return Err(RuntimeError::new("body must be a function"));
}
let body = body.clone();
let title = title.clone();
let cx_ptr = cx as *mut dyn xur_runtime::registry::Context;
with_ctx(this, |ctx| {
let frame =
Frame::window(&ctx.style_of(ctx.theme())).fill(Color32::from_rgb(24, 24, 28));
let mut ret = Value::Unit;
Window::new(title.as_ref())
.frame(frame)
.default_width(400.0)
.show(ctx, |ui| {
let cx = unsafe { &mut *cx_ptr };
let ui_val = into_value(cx, EguiUi::from_ui(ui)).expect("EguiUi registered");
match cx.call(&body, &[ui_val]) {
Ok(val) => ret = val,
Err(e) => {
tracing::error!("{}", e.message);
ui.colored_label(Color32::RED, format!("error: {}", e.message));
}
}
});
Ok(ret)
})
});
ty.raw_function("window_sized", |cx, args| {
let [this, Value::Str(title), Value::Float(width), body] = args else {
return Err(RuntimeError::new(
"`window_sized` takes (title: str, width: float, body: fn(ui))",
));
};
if !matches!(body, Value::Closure(_) | Value::Fn(_)) {
return Err(RuntimeError::new("body must be a function"));
}
let body = body.clone();
let title = title.clone();
let w = width.as_f32();
let cx_ptr = cx as *mut dyn xur_runtime::registry::Context;
with_ctx(this, |ctx| {
let frame =
Frame::window(&ctx.style_of(ctx.theme())).fill(Color32::from_rgb(24, 24, 28));
let mut ret = Value::Unit;
Window::new(title.as_ref())
.frame(frame)
.default_width(w)
.max_width(w)
.show(ctx, |ui| {
let cx = unsafe { &mut *cx_ptr };
let ui_val = into_value(cx, EguiUi::from_ui(ui)).expect("EguiUi registered");
match cx.call(&body, &[ui_val]) {
Ok(val) => ret = val,
Err(e) => {
tracing::error!("{}", e.message);
ui.colored_label(Color32::RED, format!("error: {}", e.message));
}
}
});
Ok(ret)
})
});
ty.method(
"load_texture_rgba",
|wrapper: &EguiCtx,
name: String,
width: i64,
height: i64,
rgba: Vec<u8>|
-> Result<Texture, String> {
let w = width as usize;
let h = height as usize;
if rgba.len() != w * h * 4 {
return Err(format!(
"expected {} bytes ({}x{}x4), got {}",
w * h * 4,
w,
h,
rgba.len()
));
}
let image = ColorImage::from_rgba_unmultiplied([w, h], &rgba);
let ctx = wrapper.ctx();
let tex_mngr = ctx.tex_manager();
let tex_id = tex_mngr.write().alloc(
name,
ImageData::Color(Arc::new(image)),
TextureOptions::LINEAR,
);
let TextureId::Managed(id) = tex_id else {
return Err("unexpected texture id type".into());
};
let handle = TextureHandle::new(tex_mngr, tex_id);
let mut map = TEXTURES.lock().unwrap();
map.get_or_insert_with(HashMap::new).insert(id, handle);
Ok(Texture::Managed(id as i64))
},
);
ty.method("free_texture", |_wrapper: &EguiCtx, tex: Texture| {
if let Texture::Managed(id) = tex {
let mut map = TEXTURES.lock().unwrap();
if let Some(m) = map.as_mut() {
m.remove(&(id as u64));
}
}
});
}
pub(crate) fn register_ui_methods(_registry: &Registry, m: &mut ModuleBuilder<'_>) {
let mut ty = m.ty::<EguiUi>("Ui");
ty.method("label", |wrapper: &EguiUi, text: XurStr| {
wrapper.ui().label(&*text);
});
ty.method("heading", |wrapper: &EguiUi, text: XurStr| {
wrapper.ui().heading(&*text);
});
ty.method("small", |wrapper: &EguiUi, text: XurStr| {
wrapper.ui().small(&*text);
});
ty.method("button", |wrapper: &EguiUi, text: XurStr| -> bool {
wrapper.ui().button(&*text).clicked()
});
ty.method("separator", |wrapper: &EguiUi| {
wrapper.ui().separator();
});
ty.method(
"checkbox",
|wrapper: &EguiUi, checked: bool, label: XurStr| -> bool {
let mut val = checked;
wrapper.ui().checkbox(&mut val, &*label);
val
},
);
ty.method(
"slider_float",
|wrapper: &EguiUi, label: XurStr, value: f64, min: f64, max: f64| -> f64 {
let mut val = value as f32;
wrapper
.ui()
.add(Slider::new(&mut val, min as f32..=max as f32).text(&*label));
val as f64
},
);
ty.method(
"slider_int",
|wrapper: &EguiUi, label: XurStr, value: i64, min: i64, max: i64| -> i64 {
let mut val = value;
wrapper
.ui()
.add(Slider::new(&mut val, min..=max).text(&*label));
val
},
);
ty.method("text_edit", |wrapper: &EguiUi, text: String| -> String {
let mut buf = text;
wrapper.ui().text_edit_singleline(&mut buf);
buf
});
ty.method(
"text_edit_multiline",
|wrapper: &EguiUi, text: String| -> String {
let mut buf = text;
wrapper.ui().text_edit_multiline(&mut buf);
buf
},
);
ty.raw_function("horizontal", |cx, args| {
let [this, body] = args else {
return Err(RuntimeError::new("`horizontal` takes a function"));
};
if !matches!(body, Value::Closure(_) | Value::Fn(_)) {
return Err(RuntimeError::new("body must be a function"));
}
let body = body.clone();
let cx_ptr = cx as *mut dyn xur_runtime::registry::Context;
with_ui(this, |ui| {
let mut ret = Value::Unit;
ui.horizontal(|inner_ui| {
let cx = unsafe { &mut *cx_ptr };
let inner = into_value(cx, EguiUi::from_ui(inner_ui)).expect("EguiUi registered");
match cx.call(&body, &[inner]) {
Ok(val) => ret = val,
Err(e) => {
tracing::error!("gui callback error: {}", e.message);
}
}
});
Ok(ret)
})
});
ty.raw_function("vertical", |cx, args| {
let [this, body] = args else {
return Err(RuntimeError::new("`vertical` takes a function"));
};
if !matches!(body, Value::Closure(_) | Value::Fn(_)) {
return Err(RuntimeError::new("body must be a function"));
}
let body = body.clone();
let cx_ptr = cx as *mut dyn xur_runtime::registry::Context;
with_ui(this, |ui| {
let mut ret = Value::Unit;
ui.vertical(|inner_ui| {
let cx = unsafe { &mut *cx_ptr };
let inner = into_value(cx, EguiUi::from_ui(inner_ui)).expect("EguiUi registered");
match cx.call(&body, &[inner]) {
Ok(val) => ret = val,
Err(e) => {
tracing::error!("gui callback error: {}", e.message);
}
}
});
Ok(ret)
})
});
ty.raw_function("collapsing", |cx, args| {
let [this, Value::Str(heading), body] = args else {
return Err(RuntimeError::new(
"`collapsing` takes (heading: str, body: fn)",
));
};
if !matches!(body, Value::Closure(_) | Value::Fn(_)) {
return Err(RuntimeError::new("body must be a function"));
}
let body = body.clone();
let heading = heading.clone();
let cx_ptr = cx as *mut dyn xur_runtime::registry::Context;
with_ui(this, |ui| {
let mut ret = Value::Unit;
ui.collapsing(heading.as_ref(), |inner_ui| {
let cx = unsafe { &mut *cx_ptr };
let inner = into_value(cx, EguiUi::from_ui(inner_ui)).expect("EguiUi registered");
match cx.call(&body, &[inner]) {
Ok(val) => ret = val,
Err(e) => {
tracing::error!("gui callback error: {}", e.message);
}
}
});
Ok(ret)
})
});
ty.raw_function("hover_text", |cx, args| {
let [this, Value::Str(tip), body] = args else {
return Err(RuntimeError::new("`hover_text` takes (tip: str, body: fn)"));
};
if !matches!(body, Value::Closure(_) | Value::Fn(_)) {
return Err(RuntimeError::new("body must be a function"));
}
let body = body.clone();
let tip = tip.clone();
let cx_ptr = cx as *mut dyn xur_runtime::registry::Context;
with_ui(this, |ui| {
let mut ret = Value::Unit;
let scoped = ui.scope(|inner_ui| {
let cx = unsafe { &mut *cx_ptr };
let inner = into_value(cx, EguiUi::from_ui(inner_ui)).expect("EguiUi registered");
match cx.call(&body, &[inner]) {
Ok(val) => ret = val,
Err(e) => {
tracing::error!("gui callback error: {}", e.message);
}
}
});
if !tip.is_empty() {
let id = scoped.response.id.with("hover_text");
ui.interact(scoped.response.rect, id, egui::Sense::hover())
.on_hover_text(tip.as_ref());
}
Ok(ret)
})
});
ty.raw_function("scroll", |cx, args| {
let cx_ptr = cx as *mut dyn xur_runtime::registry::Context;
let (this_val, id_salt, max_height, body_fn) = match args {
[this_val, Value::Str(id), Value::Float(h), b] if matches!(b, Value::Closure(_) | Value::Fn(_)) => {
(this_val, Some(id.clone()), Some(h.as_f32()), b.clone())
}
[this_val, Value::Str(id), b] if matches!(b, Value::Closure(_) | Value::Fn(_)) => {
(this_val, Some(id.clone()), None, b.clone())
}
[this_val, Value::Float(h), b] if matches!(b, Value::Closure(_) | Value::Fn(_)) => {
(this_val, None, Some(h.as_f32()), b.clone())
}
[this_val, b] if matches!(b, Value::Closure(_) | Value::Fn(_)) => {
(this_val, None, None, b.clone())
}
_ => {
return Err(RuntimeError::new(
"`scroll` takes fn(ui), (max_height, fn(ui)), (id, fn(ui)), or (id, max_height, fn(ui))"
));
}
};
with_ui(this_val, |ui| {
let mut scroll = ScrollArea::vertical().auto_shrink([false, true]);
if let Some(id) = &id_salt {
scroll = scroll.id_salt(&**id);
}
if let Some(h) = max_height {
scroll = scroll.max_height(h);
}
let mut ret = Value::Unit;
scroll.show(ui, |inner_ui| {
let cx = unsafe { &mut *cx_ptr };
let inner = into_value(cx, EguiUi::from_ui(inner_ui)).expect("EguiUi registered");
match cx.call(&body_fn, &[inner]) {
Ok(val) => ret = val,
Err(e) => { tracing::error!("gui callback error: {}", e.message); }
}
});
Ok(ret)
})
});
ty.raw_function("grid", |cx, args| {
let [this, Value::Str(id), Value::Int(cols), body] = args else {
return Err(RuntimeError::new(
"`grid` takes (id: str, columns: int, body: fn(ui))",
));
};
if !matches!(body, Value::Closure(_) | Value::Fn(_)) {
return Err(RuntimeError::new("body must be a function"));
}
let id = id.clone();
let cols = cols.to_usize().unwrap_or(1);
let body = body.clone();
let cx_ptr = cx as *mut dyn xur_runtime::registry::Context;
with_ui(this, |ui| {
let mut ret = Value::Unit;
Grid::new(id.as_ref())
.num_columns(cols)
.striped(true)
.show(ui, |grid_ui| {
let cx = unsafe { &mut *cx_ptr };
let inner =
into_value(cx, EguiUi::from_ui(grid_ui)).expect("EguiUi registered");
match cx.call(&body, &[inner]) {
Ok(val) => ret = val,
Err(e) => {
tracing::error!("gui callback error: {}", e.message);
}
}
});
Ok(ret)
})
});
ty.method("end_row", |wrapper: &EguiUi| {
wrapper.ui().end_row();
});
ty.method(
"selectable",
|wrapper: &EguiUi, selected: bool, text: XurStr| -> bool {
wrapper.ui().selectable_label(selected, &*text).clicked()
},
);
ty.method(
"colored_label",
|wrapper: &EguiUi, r: i64, g: i64, b: i64, text: XurStr| {
wrapper
.ui()
.colored_label(Color32::from_rgb(r as u8, g as u8, b as u8), &*text);
},
);
ty.method("add_space", |wrapper: &EguiUi, amount: f64| {
wrapper.ui().add_space(amount as f32);
});
ty.method(
"image",
|wrapper: &EguiUi, tex: Texture, width: f64, height: f64| {
let ui = wrapper.ui();
let sized = egui::load::SizedTexture::new(
tex.texture_id(),
Vec2::new(width as f32, height as f32),
);
ui.add(Image::new(sized));
},
);
}