|
| 1 | +//! Add `light` or `dark` to the end of the command to specify theme. Default |
| 2 | +//! is light. `cargo r --example html -- dark` |
| 3 | +
|
| 4 | +use eframe::egui; |
| 5 | +use egui_commonmark::*; |
| 6 | +use std::cell::RefCell; |
| 7 | +use std::rc::Rc; |
| 8 | + |
| 9 | +struct App { |
| 10 | + cache: CommonMarkCache, |
| 11 | + /// To avoid id collisions |
| 12 | + counter: Rc<RefCell<usize>>, |
| 13 | +} |
| 14 | + |
| 15 | +impl eframe::App for App { |
| 16 | + fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { |
| 17 | + *self.counter.as_ref().borrow_mut() = 0; |
| 18 | + |
| 19 | + egui::CentralPanel::default().show(ctx, |ui| { |
| 20 | + egui::ScrollArea::vertical().show(ui, |ui| { |
| 21 | + CommonMarkViewer::new() |
| 22 | + .render_html_fn({ |
| 23 | + let counter = Rc::clone(&self.counter); |
| 24 | + Some(&move |ui, html| { |
| 25 | + // For simplicity lets just hide the content regardless of what kind of |
| 26 | + // node it is. |
| 27 | + ui.collapsing( |
| 28 | + format!("Collapsed {}", counter.as_ref().borrow()), |
| 29 | + |ui| { |
| 30 | + ui.label(html); |
| 31 | + }, |
| 32 | + ); |
| 33 | + |
| 34 | + *counter.as_ref().borrow_mut() += 1; |
| 35 | + }) |
| 36 | + }) |
| 37 | + .show(ui, &mut self.cache, EXAMPLE_TEXT); |
| 38 | + }); |
| 39 | + }); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +fn main() -> eframe::Result { |
| 44 | + let mut args = std::env::args(); |
| 45 | + args.next(); |
| 46 | + |
| 47 | + eframe::run_native( |
| 48 | + "Markdown viewer", |
| 49 | + eframe::NativeOptions::default(), |
| 50 | + Box::new(move |cc| { |
| 51 | + if let Some(theme) = args.next() { |
| 52 | + if theme == "light" { |
| 53 | + cc.egui_ctx.set_theme(egui::Theme::Light); |
| 54 | + } else if theme == "dark" { |
| 55 | + cc.egui_ctx.set_theme(egui::Theme::Dark); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + cc.egui_ctx.style_mut(|style| { |
| 60 | + // Show the url of a hyperlink on hover |
| 61 | + style.url_in_tooltip = true; |
| 62 | + }); |
| 63 | + |
| 64 | + Ok(Box::new(App { |
| 65 | + cache: CommonMarkCache::default(), |
| 66 | + counter: Rc::new(RefCell::new(0)), |
| 67 | + })) |
| 68 | + }), |
| 69 | + ) |
| 70 | +} |
| 71 | + |
| 72 | +const EXAMPLE_TEXT: &str = r#" |
| 73 | +# Customized rendering using html |
| 74 | +<p> |
| 75 | +some text |
| 76 | +</p> |
| 77 | +
|
| 78 | +<p> |
| 79 | +some text 2 |
| 80 | +</p> |
| 81 | +"#; |
0 commit comments