Skip to content

Commit

Permalink
Install loaders upon show
Browse files Browse the repository at this point in the history
  • Loading branch information
lampsitter committed Sep 13, 2023
1 parent 19c4a54 commit f33d95d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 34 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
- `CommonMarkCache` now implements `Debug` ([#7](https://github.com/lampsitter/egui_commonmark/pull/7) by [@ChristopherPerry6060](https://github.com/ChristopherPerry6060)).
- `CommonMarkCache::add_syntax_themes_from_folder`
- `CommonMarkCache::add_syntax_theme_from_bytes`
- `CommonMarkCache::without_image_loaders`

### Changed

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ r"# Hello world
* [ ] Checkbox
";
// Stores image handles between each frame
let mut cache = CommonMarkCache::new(&ctx);
let mut cache = CommonMarkCache::default();
CommonMarkViewer::new("viewer").show(ui, &mut cache, markdown);
```

Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn main() {
});

Box::new(App {
cache: CommonMarkCache::new(&cc.egui_ctx),
cache: CommonMarkCache::default(),
})
}),
)
Expand Down
5 changes: 3 additions & 2 deletions examples/link_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ fn main() {
eframe::run_native(
"Markdown link hooks",
eframe::NativeOptions::default(),
Box::new(|cc| {
let mut cache = CommonMarkCache::new(&cc.egui_ctx);
Box::new(|_| {
let mut cache = CommonMarkCache::default();
cache.add_link_hook("#next");
cache.add_link_hook("#prev");

Box::new(App {
cache,
curr_page: 0,
Expand Down
2 changes: 1 addition & 1 deletion examples/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn main() {
});

Box::new(App {
cache: CommonMarkCache::new(&cc.egui_ctx),
cache: CommonMarkCache::default(),
})
}),
)
Expand Down
51 changes: 23 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
//! ";
//! // Stores image handles between each frame
//! # __run_test_ui(|ui| {
//! # let ctx = ui.ctx();
//! let mut cache = CommonMarkCache::new(&ctx);
//! let mut cache = CommonMarkCache::default();
//! CommonMarkViewer::new("viewer").show(ui, &mut cache, markdown);
//! # });
//!
Expand Down Expand Up @@ -56,6 +55,7 @@ pub struct CommonMarkCache {
link_hooks: HashMap<String, bool>,

scroll: HashMap<Id, ScrollableCache>,
has_installed_loaders: bool,
}

#[cfg(not(feature = "syntax_highlighting"))]
Expand All @@ -82,37 +82,16 @@ impl std::fmt::Debug for CommonMarkCache {
}
}

impl CommonMarkCache {
/// Create a new [`CommonMarkCache`]. This automatically sets up the needed egui image loaders
/// for you.
pub fn new(ctx: &egui::Context) -> Self {
egui_extras::loaders::install(ctx);
Self::without_image_loaders()
}

/// Alternative to [`new`](Self::new). Only use this if it is too difficult for you to provide
/// a [`egui::Context`] when creating the cache. Can also be useful when writing tests.
///
/// # Beware!
///
/// To be able to load images you must manually call the following somewhere in you
/// application:
///
/// ```
/// # use egui_extras::*;
/// # use egui::__run_test_ctx;
/// # __run_test_ctx(|ctx| {
/// egui_extras::loaders::install(&ctx);
/// # });
/// ```
pub fn without_image_loaders() -> Self {
impl Default for CommonMarkCache {
fn default() -> Self {
Self {
#[cfg(feature = "syntax_highlighting")]
ps: SyntaxSet::load_defaults_newlines(),
#[cfg(feature = "syntax_highlighting")]
ts: ThemeSet::load_defaults(),
link_hooks: HashMap::new(),
scroll: Default::default(),
has_installed_loaders: false,
}
}
}
Expand Down Expand Up @@ -226,6 +205,22 @@ impl CommonMarkCache {
}
self.scroll.get_mut(id).unwrap()
}

/// Should be called before any rendering
fn prepare_show(&mut self, ctx: &egui::Context) {
if !self.has_installed_loaders {
// Even though the install function can be called multiple times, its not the cheapest
// so we ensure that we only call it once.
// This could be done at the creation of the cache, however it is better to keep the
// cache free from egui's Ui and Context types as this allows it to be created before
// any egui instances. It also keeps the API similar to before the introduction of the
// image loaders.
egui_extras::loaders::install(ctx);
self.has_installed_loaders = true;
}

self.deactivate_link_hooks();
}
}

#[cfg(feature = "syntax_highlighting")]
Expand Down Expand Up @@ -334,7 +329,7 @@ impl CommonMarkViewer {

/// Shows rendered markdown
pub fn show(self, ui: &mut egui::Ui, cache: &mut CommonMarkCache, text: &str) {
cache.deactivate_link_hooks();
cache.prepare_show(ui.ctx());
CommonMarkViewerInternal::new(self.source_id).show(ui, cache, &self.options, text, false);
}

Expand All @@ -353,7 +348,7 @@ impl CommonMarkViewer {
/// [`show`]: crate::CommonMarkViewer::show
#[doc(hidden)] // Buggy in scenarios more complex than the example application
pub fn show_scrollable(self, ui: &mut egui::Ui, cache: &mut CommonMarkCache, text: &str) {
cache.deactivate_link_hooks();
cache.prepare_show(ui.ctx());
CommonMarkViewerInternal::new(self.source_id).show_scrollable(
ui,
cache,
Expand Down

0 comments on commit f33d95d

Please sign in to comment.