use super::text_of;
pub(crate) fn screen_sized(w: f32, h: f32) -> egui::RawInput {
egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(
egui::Pos2::ZERO,
egui::vec2(w, h),
)),
..Default::default()
}
}
fn screen() -> egui::RawInput {
screen_sized(1200.0, 2400.0)
}
pub(crate) fn paint(mut body: impl FnMut(&mut egui::Ui)) -> String {
let ctx = egui::Context::default();
let output = ctx.run(screen(), |ctx| {
egui::CentralPanel::default().show(ctx, &mut body);
});
text_of(&output)
}
pub(crate) struct Window {
ctx: egui::Context,
input: egui::RawInput,
}
impl Window {
pub(crate) fn sized(w: f32, h: f32) -> Self {
Self {
ctx: egui::Context::default(),
input: screen_sized(w, h),
}
}
pub(crate) fn new() -> Self {
Self::sized(1200.0, 2400.0)
}
pub(crate) fn frame(
&self,
events: Vec<egui::Event>,
mut body: impl FnMut(&egui::Context),
) -> egui::FullOutput {
let mut input = self.input.clone();
input.events = events;
self.ctx.run(input, |ctx| body(ctx))
}
pub(crate) fn focused(&self) -> Option<egui::Id> {
self.ctx.memory(egui::Memory::focused)
}
pub(crate) fn text(&self, body: impl FnMut(&egui::Context)) -> String {
text_of(&self.frame(Vec::new(), body))
}
}
pub(crate) fn click(window: &Window, pos: egui::Pos2, mut body: impl FnMut(&egui::Context)) {
window.frame(vec![egui::Event::PointerMoved(pos)], &mut body);
for pressed in [true, false] {
window.frame(vec![button(pos, pressed)], &mut body);
}
}
fn button(pos: egui::Pos2, pressed: bool) -> egui::Event {
egui::Event::PointerButton {
pos,
button: egui::PointerButton::Primary,
pressed,
modifiers: egui::Modifiers::NONE,
}
}
pub(crate) fn press(key: egui::Key) -> egui::Event {
egui::Event::Key {
key,
physical_key: None,
pressed: true,
repeat: false,
modifiers: egui::Modifiers::NONE,
}
}
pub(crate) fn locate_in(
window: &Window,
label: &str,
body: impl FnMut(&egui::Context),
) -> Option<egui::Pos2> {
super::locate(&window.frame(Vec::new(), body), label)
}