128 lines
3.8 KiB
Rust
128 lines
3.8 KiB
Rust
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
|
|
|
use std::{ops::Deref, sync::Arc};
|
|
|
|
use eframe::{
|
|
egui::{self, Slider},
|
|
egui_wgpu,
|
|
epaint::{
|
|
mutex::{Mutex, RwLock},
|
|
Rect, Vec2,
|
|
},
|
|
CreationContext,
|
|
};
|
|
use egui::{Align2, Pos2};
|
|
use egui_glyphon::{
|
|
glyphon::{Attrs, Family, FontSystem, Metrics, Shaping},
|
|
BufferWithTextArea, GlyphonRenderer, GlyphonRendererCallback,
|
|
};
|
|
use glyphon::{cosmic_text::Align, Buffer};
|
|
|
|
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>>,
|
|
buffers: Vec<Arc<RwLock<Buffer>>>,
|
|
}
|
|
|
|
impl Default for MyApp {
|
|
fn default() -> Self {
|
|
let mut font_system = FontSystem::new();
|
|
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();
|
|
|
|
Self {
|
|
font_system: Arc::new(Mutex::new(font_system)),
|
|
buffers,
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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)
|
|
});
|
|
|
|
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))
|
|
}
|
|
Some(Align::Left) | None => Rect::from_min_size(Pos2::ZERO, size),
|
|
}
|
|
}
|
|
|
|
impl eframe::App for MyApp {
|
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
|
egui::CentralPanel::default().show(ctx, |ui| {
|
|
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();
|
|
ui.painter().add(egui_wgpu::Callback::new_paint_callback(
|
|
ui.max_rect(),
|
|
GlyphonRendererCallback { buffers },
|
|
));
|
|
});
|
|
}
|
|
}
|