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

rich_text and markdown widgets #2508

Merged
merged 7 commits into from
Jul 18, 2024
Merged

rich_text and markdown widgets #2508

merged 7 commits into from
Jul 18, 2024

Conversation

hecrj
Copy link
Member

@hecrj hecrj commented Jul 18, 2024

This PR implements support for rich text on top of cosmic-text and also introduces markdown helpers in the widget module.

Rich Text

Rich text can be created on-the-fly with the span helper and the rich_text! macro (analogous to column! and row!):

use iced::widget::{rich_text, span};
use iced::Font;

rich_text!["Hello ", span("iced").font(Font::MONOSPACE).size(20)]

Or you can store the spans in your application state and borrow them directly with the rich_text function:

rich_text(&self.my_cached_spans)

This can be useful if you are generating rich text as a result of some expensive operation, like parsing.

Markdown

Markdown support can be enabled with the new markdown feature.

Then, you can parse markdown with the parse function:

use iced::widget::markdown;

self.items = markdown::parse("# Some markdown!", Theme::TokyoNight.palette()).collect();

For now, colors need to be attached to spans during construction; therefore, a theme::Palette is needed.

Once parsed, you can render it:

use iced::widget::markdown;

markdown(&self.items)

Further configuration options will be added eventually.


To showcase all of this, I have created a simple Markdown editor example:

image

Code blocks will be automatically highlighted if the highlighter feature is enabled.

@hecrj hecrj added feature New feature or request text widget addition labels Jul 18, 2024
@hecrj hecrj added this to the 0.13 milestone Jul 18, 2024
@hecrj hecrj merged commit 23ad153 into master Jul 18, 2024
24 checks passed
@hecrj hecrj deleted the feature/rich-text branch July 18, 2024 20:39
@iced-rs iced-rs locked as resolved and limited conversation to collaborators Jul 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants