Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement markdown mutating checkboxes through CommonMarkViewer::show_mut #40

Merged
merged 10 commits into from
Mar 27, 2024

Conversation

crumblingstatue
Copy link
Contributor

Closes #38

This pull request neglects to implement the feature for the comrak backend. I haven't tested what happens when trying to use show_mut with the comrak backend.
Since the comrak backend's future is unclear, this is acceptable.

@@ -252,6 +252,8 @@ struct CommonMarkOptions {
use_explicit_uri_scheme: bool,
default_implicit_uri_scheme: String,
alerts: AlertBundle,
/// Whether to present a mutable ui for things like checkboxes
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this belong in CommonMarkOptions, or should it be somewhere else?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine for it to live there

/// Shows rendered markdown, and allows the rendered ui to mutate the source text.
///
/// The only currently implemented mutation is allowing checkboxes to be toggled through the ui.
pub fn show_mut(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You suggested show_mutable as the name, but I think show_mut is more idiomatic for a Rust API name.

@@ -17,15 +19,15 @@ pub struct ScrollableCache {
/// Parse events until a desired end tag is reached or no more events are found.
/// This is needed for multiple events that must be rendered inside a single widget
fn delayed_events<'e>(
events: &mut impl Iterator<Item = (usize, pulldown_cmark::Event<'e>)>,
events: &mut impl Iterator<Item = (usize, (pulldown_cmark::Event<'e>, Range<usize>))>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there could be a type alias for this, but I wasn't sure enough about it to make it one.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the exact same thing. I have some refactoring to do :)

checkbox_events: Vec<CheckboxClickEvent>,
}

pub(crate) struct CheckboxClickEvent {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there is a better name for this

@@ -204,23 +215,25 @@ impl CommonMarkViewerInternal {
options: &CommonMarkOptions,
text: &str,
populate_split_points: bool,
) -> egui::InnerResponse<()> {
) -> (egui::InnerResponse<()>, Vec<CheckboxClickEvent>) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It returns a Vec, but usually there is only one thing that the user clicks.
... Although there is multitouch and whatnot, so I think this is a safe option.

@@ -325,25 +341,26 @@ impl CommonMarkViewerInternal {
scroll_cache.split_points.clear();
}
}

#[allow(clippy::too_many_arguments)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clippy complained about too many arguments.
Unsure if I could somehow reduce the number of arguments, so I just added an allow

@@ -476,7 +494,19 @@ impl CommonMarkViewerInternal {
newline(ui);
}
pulldown_cmark::Event::TaskListMarker(mut checkbox) => {
ui.add(Checkbox::without_text(&mut checkbox));
if options.mutable {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immutable uses original checkbox widget that you made, while mutable uses normal egui checkbox widget.

Copy link
Owner

@lampsitter lampsitter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an example that demonstrates the feature? Just copy how hello_world.rs does it.

/// Shows rendered markdown, and allows the rendered ui to mutate the source text.
///
/// The only currently implemented mutation is allowing checkboxes to be toggled through the ui.
pub fn show_mut(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn show_mut(
#[cfg(feature = "pulldown_cmark")]
pub fn show_mut(

If you mark the entire function for the pulldown_cmark backend. Then you can remove the cfgs inside the function and that way it's obvious that it is not supported by the comrak backend.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I'll implement it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@crumblingstatue
Copy link
Contributor Author

crumblingstatue commented Mar 27, 2024

Can you add an example that demonstrates the feature? Just copy how hello_world.rs does it.

I'll try cooking something up

EDIT: I added a show_mut example.

@crumblingstatue
Copy link
Contributor Author

Let me know if you need any other change.

@lampsitter
Copy link
Owner

Thanks. This is great!

@lampsitter lampsitter merged commit 0143021 into lampsitter:master Mar 27, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Support for mutable checkboxes
2 participants