Skip to content

Commit

Permalink
Remove explicit Id (#63)
Browse files Browse the repository at this point in the history
Over time the need for the id has been reduced and it should be possible to remove it for the normal use case now.

* Remove explicit id

* Remove id from builder

* Update changelog
  • Loading branch information
lampsitter committed Sep 7, 2024
1 parent f180522 commit ff02bc5
Show file tree
Hide file tree
Showing 24 changed files with 127 additions and 122 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

### Changed

- `CommonMarkViewer::new` no longer takes in an id.
- `commonmark!` and `commonmark_str!` no longer takes in an id.
- `CommonMarkViewer::show_scrollable` takes in an id explicity.

- Updated pulldown-cmark to 0.12
- Newlines are no longer inserted before/after markdown ([#56](https://github.com/lampsitter/egui_commonmark/pull/56))
> For the old behaviour you can call `ui.label("");` before and and after
Expand Down
2 changes: 1 addition & 1 deletion egui_commonmark/examples/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl App {
egui::Frame::none()
.inner_margin(egui::Margin::symmetric(5.0, 0.0))
.show(ui, |ui| {
CommonMarkViewer::new("viewer")
CommonMarkViewer::new()
.default_width(Some(200))
.max_image_width(Some(512))
.show(
Expand Down
2 changes: 1 addition & 1 deletion egui_commonmark/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl eframe::App for App {
let text = include_str!("markdown/hello_world.md");
egui::CentralPanel::default().show(ctx, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
CommonMarkViewer::new("viewer")
CommonMarkViewer::new()
.max_image_width(Some(512))
.show(ui, &mut self.cache, text);
});
Expand Down
2 changes: 1 addition & 1 deletion egui_commonmark/examples/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl eframe::App for App {
.show_inside(ui, |ui| ui.text_edit_multiline(&mut self.markdown));
egui::CentralPanel::default().show_inside(ui, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
CommonMarkViewer::new("viewer").show(ui, &mut self.cache, &self.markdown);
CommonMarkViewer::new().show(ui, &mut self.cache, &self.markdown);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion egui_commonmark/examples/link_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Notice how the destination is not shown on [hover](#prev) unlike with [urls](htt

egui::CentralPanel::default().show(ctx, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
CommonMarkViewer::new("viewer").show(ui, &mut self.cache, p[self.curr_page]);
CommonMarkViewer::new().show(ui, &mut self.cache, p[self.curr_page]);
});
});
}
Expand Down
25 changes: 9 additions & 16 deletions egui_commonmark/examples/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,71 +16,64 @@ impl eframe::App for App {
egui::CentralPanel::default().show(ctx, |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
// Embed text directly
commonmark!("n1", ui, &mut self.cache, "Hello, world");
commonmark!(ui, &mut self.cache, "Hello, world");

// In cases like these it's better to use egui::Separator directly
commonmark!("n1-1", ui, &mut self.cache, "------------");
commonmark!(ui, &mut self.cache, "------------");

// From a file like include_str! NOTE: This does not cause a recompile when the
// file has changed!
commonmark_str!(
"n2",
ui,
&mut self.cache,
"egui_commonmark/examples/markdown/hello_world.md"
);
commonmark!("n4", ui, &mut self.cache, "------------");
commonmark!(ui, &mut self.cache, "------------");

commonmark_str!(
"n3",
ui,
&mut self.cache,
"egui_commonmark/examples/markdown/headers.md"
);
commonmark!("n5", ui, &mut self.cache, "------------");
commonmark!(ui, &mut self.cache, "------------");

commonmark_str!(
"n6",
ui,
&mut self.cache,
"egui_commonmark/examples/markdown/lists.md"
);

commonmark!("n6", ui, &mut self.cache, "------------");
commonmark!(ui, &mut self.cache, "------------");

commonmark_str!(
"n7",
ui,
&mut self.cache,
"egui_commonmark/examples/markdown/code-blocks.md"
);

commonmark!("n4", ui, &mut self.cache, "------------");
commonmark!(ui, &mut self.cache, "------------");

commonmark_str!(
"n9",
ui,
&mut self.cache,
"egui_commonmark/examples/markdown/blockquotes.md"
);

commonmark!("n10", ui, &mut self.cache, "------------");
commonmark!(ui, &mut self.cache, "------------");

commonmark_str!(
"n11",
ui,
&mut self.cache,
"egui_commonmark/examples/markdown/tables.md"
);
commonmark!("n12", ui, &mut self.cache, "------------");
commonmark!(ui, &mut self.cache, "------------");

commonmark_str!(
"n13",
ui,
&mut self.cache,
"egui_commonmark/examples/markdown/definition_list.md"
);
commonmark!("n14", ui, &mut self.cache, "------------");
commonmark!(ui, &mut self.cache, "------------");
});
});
}
Expand Down
9 changes: 9 additions & 0 deletions egui_commonmark/examples/markdown/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ item a2 | item b2
item a3 | item b3
item a4 | item b4




Column A | Column B
-----------|----------
`item` `a1` | item b1
item a2 | item b2
item a3 | item b3
item a4 | item b4
4 changes: 2 additions & 2 deletions egui_commonmark/examples/mixing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ macro_rules! m {
$ui.label("Label!");
#[cfg(feature = "macros")]
{
egui_commonmark_macros::commonmark!("n1", $ui, &mut $cache, $a);
egui_commonmark_macros::commonmark!($ui, &mut $cache, $a);
}
#[cfg(not(feature = "macros"))]
{
egui_commonmark::CommonMarkViewer::new("viewer").show($ui, &mut $cache, $a);
egui_commonmark::CommonMarkViewer::new().show($ui, &mut $cache, $a);
}
)*
};
Expand Down
4 changes: 2 additions & 2 deletions egui_commonmark/examples/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ vec.push(5);
text += &repeating.repeat(1024);

egui::CentralPanel::default().show(ctx, |ui| {
CommonMarkViewer::new("viewer")
CommonMarkViewer::new()
.max_image_width(Some(512))
.show_scrollable(ui, &mut self.cache, &text);
.show_scrollable("viewer", ui, &mut self.cache, &text);
});
}
}
Expand Down
8 changes: 5 additions & 3 deletions egui_commonmark/examples/show_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ impl eframe::App for App {
.code_editor()
.desired_width(f32::INFINITY),
);
CommonMarkViewer::new("viewer")
.max_image_width(Some(512))
.show_mut(ui, &mut self.cache, &mut self.text_buffer);
CommonMarkViewer::new().max_image_width(Some(512)).show_mut(
ui,
&mut self.cache,
&mut self.text_buffer,
);
});
});
}
Expand Down
44 changes: 25 additions & 19 deletions egui_commonmark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//!
//! # __run_test_ui(|ui| {
//! let mut cache = CommonMarkCache::default();
//! CommonMarkViewer::new("viewer").show(ui, &mut cache, markdown);
//! CommonMarkViewer::new().show(ui, &mut cache, markdown);
//! # });
//!
//! ```
Expand Down Expand Up @@ -47,7 +47,7 @@
//! use egui_commonmark::{CommonMarkCache, commonmark};
//! # egui::__run_test_ui(|ui| {
//! let mut cache = CommonMarkCache::default();
//! let _response = commonmark!("example", ui, &mut cache, "# ATX Heading Level 1");
//! let _response = commonmark!(ui, &mut cache, "# ATX Heading Level 1");
//! # });
//! ```
//!
Expand All @@ -60,7 +60,7 @@
//! use egui_commonmark::{CommonMarkCache, commonmark_str};
//! # egui::__run_test_ui(|ui| {
//! let mut cache = CommonMarkCache::default();
//! commonmark_str!("example_file", ui, &mut cache, "content.md");
//! commonmark_str!(ui, &mut cache, "content.md");
//! # });
//! ```
//!
Expand All @@ -86,18 +86,14 @@ pub use egui_commonmark_backend;

use egui_commonmark_backend::*;

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct CommonMarkViewer {
source_id: Id,
options: CommonMarkOptions,
}

impl CommonMarkViewer {
pub fn new(source_id: impl std::hash::Hash) -> Self {
Self {
source_id: Id::new(source_id),
options: CommonMarkOptions::default(),
}
pub fn new() -> Self {
Self::default()
}

/// The amount of spaces a bullet point is indented. By default this is 4
Expand Down Expand Up @@ -133,7 +129,7 @@ impl CommonMarkViewer {
/// # Example
/// ```
/// # use egui_commonmark::CommonMarkViewer;
/// CommonMarkViewer::new("viewer").default_implicit_uri_scheme("https://example.org/");
/// CommonMarkViewer::new().default_implicit_uri_scheme("https://example.org/");
/// ```
pub fn default_implicit_uri_scheme<S: Into<String>>(mut self, scheme: S) -> Self {
self.options.default_implicit_uri_scheme = scheme.into();
Expand Down Expand Up @@ -181,12 +177,12 @@ impl CommonMarkViewer {
) -> egui::InnerResponse<()> {
egui_commonmark_backend::prepare_show(cache, ui.ctx());

let (response, _) = parsers::pulldown::CommonMarkViewerInternal::new(self.source_id).show(
let (response, _) = parsers::pulldown::CommonMarkViewerInternal::new().show(
ui,
cache,
&self.options,
text,
false,
None,
);

response
Expand All @@ -204,10 +200,13 @@ impl CommonMarkViewer {
self.options.mutable = true;
egui_commonmark_backend::prepare_show(cache, ui.ctx());

let (response, checkmark_events) = parsers::pulldown::CommonMarkViewerInternal::new(
self.source_id,
)
.show(ui, cache, &self.options, text, false);
let (response, checkmark_events) = parsers::pulldown::CommonMarkViewerInternal::new().show(
ui,
cache,
&self.options,
text,
None,
);

// Update source text for checkmarks that were clicked
for ev in checkmark_events {
Expand Down Expand Up @@ -236,9 +235,16 @@ impl CommonMarkViewer {
/// [`show`]: crate::CommonMarkViewer::show
#[doc(hidden)] // Buggy in scenarios more complex than the example application
#[cfg(feature = "pulldown_cmark")]
pub fn show_scrollable(self, ui: &mut egui::Ui, cache: &mut CommonMarkCache, text: &str) {
pub fn show_scrollable(
self,
source_id: impl std::hash::Hash,
ui: &mut egui::Ui,
cache: &mut CommonMarkCache,
text: &str,
) {
egui_commonmark_backend::prepare_show(cache, ui.ctx());
parsers::pulldown::CommonMarkViewerInternal::new(self.source_id).show_scrollable(
parsers::pulldown::CommonMarkViewerInternal::new().show_scrollable(
Id::new(source_id),
ui,
cache,
&self.options,
Expand Down
Loading

0 comments on commit ff02bc5

Please sign in to comment.