egui-glyphon/examples/hello-world.rs

129 lines
3.8 KiB
Rust
Raw Normal View History

2024-02-05 11:30:20 -05:00
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
2024-03-18 18:31:38 -04:00
use std::{ops::Deref, sync::Arc};
2024-02-05 11:30:20 -05:00
use eframe::{
egui::{self, Slider},
egui_wgpu,
epaint::{
mutex::{Mutex, RwLock},
Rect, Vec2,
},
CreationContext,
};
2024-03-18 18:31:38 -04:00
use egui::{Align2, Pos2};
2024-02-05 11:30:20 -05:00
use egui_glyphon::{
glyphon::{Attrs, Family, FontSystem, Metrics, Shaping},
BufferWithTextArea, GlyphonRenderer, GlyphonRendererCallback,
};
2024-03-18 18:31:38 -04:00
use glyphon::{cosmic_text::Align, Buffer};
2024-02-05 11:30:20 -05:00
fn main() -> Result<(), eframe::Error> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
..Default::default()
};
eframe::run_native(
"My egui App",
options,
Box::new(|cc| Box::new(MyApp::new(cc))),
)
}
struct MyApp {
font_system: Arc<Mutex<FontSystem>>,
2024-03-18 18:31:38 -04:00
buffers: Vec<Arc<RwLock<Buffer>>>,
2024-02-05 11:30:20 -05:00
}
impl Default for MyApp {
fn default() -> Self {
let mut font_system = FontSystem::new();
2024-03-18 18:31:38 -04:00
let buffers = cosmic_jotdown::jotdown_into_buffers(
r#"# Header
Test _test_ *test* 'test' "`test`""#,
&mut font_system,
Metrics::new(30.0, 42.0),
f32::MAX,
)
.map(|buffer| Arc::new(RwLock::new(buffer)))
.collect();
2024-02-05 11:30:20 -05:00
Self {
font_system: Arc::new(Mutex::new(font_system)),
2024-03-18 18:31:38 -04:00
buffers,
2024-02-05 11:30:20 -05:00
}
}
}
impl MyApp {
fn new(cc: &CreationContext<'_>) -> Self {
let app = Self::default();
if let Some(ref wgpu) = cc.wgpu_render_state {
GlyphonRenderer::insert(wgpu, Arc::clone(&app.font_system));
}
app
}
}
2024-03-18 18:31:38 -04:00
pub fn measure_buffer(buffer: &Buffer, vb: Vec2) -> Rect {
let mut rtl = false;
let (width, total_lines) =
buffer
.layout_runs()
.fold((0.0, 0usize), |(width, total_lines), run| {
if run.rtl {
rtl = true;
}
(run.line_w.max(width), total_lines + 1)
});
2024-02-05 11:30:20 -05:00
2024-03-18 18:31:38 -04:00
let (max_width, max_height) = buffer.size();
let size = Vec2::new(
if rtl { vb.x } else { width.min(max_width) },
(total_lines as f32 * buffer.metrics().line_height).min(max_height),
);
match buffer.lines[0].align() {
Some(Align::Right) | Some(Align::End) => {
Align2::RIGHT_TOP.align_size_within_rect(size, Rect::from_min_size(Pos2::ZERO, vb))
}
Some(Align::Center) | Some(Align::Justified) => {
Align2::CENTER_TOP.align_size_within_rect(size, Rect::from_min_size(Pos2::ZERO, vb))
2024-02-05 11:30:20 -05:00
}
2024-03-18 18:31:38 -04:00
Some(Align::Left) | None => Rect::from_min_size(Pos2::ZERO, size),
}
}
2024-02-05 11:30:20 -05:00
2024-03-18 18:31:38 -04:00
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
2024-02-05 11:30:20 -05:00
egui::CentralPanel::default().show(ctx, |ui| {
2024-03-18 18:31:38 -04:00
let mut rect = Rect::from_min_size(ui.cursor().min, Vec2::INFINITY);
let buffers: Vec<BufferWithTextArea> = self
.buffers
.iter()
.cloned()
.map(|buffer| {
let buffer_size = measure_buffer(buffer.read().deref(), ui.max_rect().size());
let this_rect = rect;
rect.min.y += buffer_size.height() + 48.0;
BufferWithTextArea::new(
buffer,
this_rect,
1.0,
egui_glyphon::glyphon::Color::rgb(255, 255, 255),
ui.ctx(),
)
})
.collect();
2024-02-05 11:30:20 -05:00
ui.painter().add(egui_wgpu::Callback::new_paint_callback(
ui.max_rect(),
GlyphonRendererCallback { buffers },
));
});
}
}