-
Finn Bear authored4fc9b16b
code.rs 2.17 KiB
use eframe::egui;
use eframe::egui::{Align2, Color32, Context, Vec2, Widget, Window};
#[derive(Clone)]
pub struct Code {
/// Must be unique.
pub name: &'static str,
/// Horizontal offset from middle.
pub offset_x: f32,
/// Vertical offset from middle.
pub offset_y: f32,
/// How tall and wide.
pub size_pixels: f32,
/// Background color. Transparent disables background.
pub background: Color32,
/// Code.
pub code: String,
}
impl Default for Code {
fn default() -> Self {
Self {
name: "grid",
offset_x: 0.0,
offset_y: 0.0,
size_pixels: 400.0,
background: Color32::from_rgb(230, 230, 230),
code: String::from(r#"println!("Hello world!");"#),
}
}
}
impl Code {
/// Render to UI.
pub fn show(&self, ctx: &Context) {
Window::new(self.name)
.title_bar(false)
//.frame(Frame::none())
.anchor(
Align2::CENTER_CENTER,
Vec2::new(self.offset_x, self.offset_y),
)
.fixed_size(Vec2::splat(self.size_pixels))
.show(ctx, |ui| {
//let (_id, _rect) = ui.allocate_space(Vec2::splat(self.size_pixels));
let theme = egui_demo_lib::syntax_highlighting::CodeTheme::from_memory(ui.ctx());
let mut layouter = |ui: &egui::Ui, string: &str, wrap_width: f32| {
let mut layout_job = egui_demo_lib::syntax_highlighting::highlight(
ui.ctx(),
&theme,
string,
"rs",
);
layout_job.wrap_width = wrap_width;
ui.fonts().layout_job(layout_job)
};
egui::TextEdit::multiline(&mut self.code.as_str())
.font(egui::TextStyle::Monospace) // for cursor height
.code_editor()
//.desired_rows(10)
.lock_focus(true)
.desired_width(f32::INFINITY)
.layouter(&mut layouter)
.ui(ui);
});
}
}