-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add basic support for text highlighting #1276
Conversation
native/src/widget/text.rs
Outdated
let quad = renderer::Quad { | ||
bounds: Rectangle { | ||
x: x + width_before_start, | ||
y, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems highlighting will not work with a Text
of multiple lines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I should clarify that this a very basic and naive implementation thus far; there's a lot of trivial cases that break it.
I think this is related to #156. We are not only interested in highlighthing support, but also changing color, font, size, and other text properties without affecting layout. |
renderer.measure(above_lines, size, font.clone(), Size::INFINITY); | ||
|
||
// If the highlight crosses over multiple lines, draw a seperate rect on each line | ||
// BUG: This ignores single `\r` but Iced's text layouting does not (See above) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rust only considers \n
and \r\n
as line endings, but Iced's text layouting also considers a standalone \r
as a line ending.
This isn't exactly a hard blocker, but I'm unaware of the consensus of if this behaviour should be kept or not.
// If the highlight crosses over multiple lines, draw a seperate rect on each line | ||
// BUG: This ignores single `\r` but Iced's text layouting does not (See above) | ||
// BUG #2: Text wrapping caused by the text not being given wide enough bounds is not handled at all | ||
// (And furthermore it currently _can't_ be handled because there's no way to get information about it) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is a way to handle this, please correct me, but if not, this would be a blocker on ensuring correct behaviour.
It's worth considering what the API for this should look like. If Iced ever gets full rich text support, this will likely be integrated with that, but until then this will need its own API. Currently the API accepts grapheme indexes; graphemes are the most intuitive way to deal with partitioning rendered text, and indeed, there is no intuitive answer as to what a highlight should look like if it were to be specified by byte or char index instead, and ended up cutting a grapheme in half. The issue with this is the std library provides no way to observe strings via their graphemes (Iced itself uses this crate), instead dealing with The API could be changed to accept On the other hand, keeping the API as-is would pass these burdens on to the user, requiring them to convert their data into grapheme indexes. We could expose helper methods to aid with this, but doing so could be scope creep. I am certainly not acquainted enough with Iced's architecture and design philosophies to make any kind of informed decision on my own (Not only about API but the other topics mentioned in my above comments), so I'm willing to delegate to the opinion of someone more familiar with Iced. |
@Pixelstormer What do you think of this crate? The |
I think there is still a lot of discussion that needs to happen before we can start thinking about code here. I have some text rendering improvements coming soon in the pipeline. I am aware I am the main bottleneck here! Once those improvements land, we can review this approach and evaluate if it's suitable or whether we can reuse any parts or not. |
Adds support for changing the background colour of various parts of text, to enable things like highlighting search results, marking outstanding regions of text, etc.
You could achieve a similar effect before this PR by splitting your text into multiple
Text
structs and nesting the parts you want to highlight in aContainer
with a background colour set, but this is not a viable solution as doing so interferes with text layouting, as demonstrated in the below videos.Text highlighting using the aforementioned containers-based method (Notice how the text fidgets around as the highlighting changes):
before.mp4
Text highlighting using the
highlight
method introduced by this PR:after.mp4