slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
17.09k stars 575 forks source link

Add wrap anywhere functionality to text wrapping #5374

Closed danielstuart14 closed 3 months ago

danielstuart14 commented 3 months ago

Currently, Slint only supports word-wrap and no-wrap options for text wrapping. While word-wrap works for wrapping human text, when dealing with a console output (which could return large hexadecimal values, strings, etc) this kind of wrapping isn't ideal.

As so, a char-wrap mode would suit better for theses cases, always wrapping on the last character that fits the content.

Here is an example (from SlintPad, using TextEdit with word-wrap): image

And here is how it would look like with char-wrap: image

It does look like implementing this for the Qt backend should be easier, as Qt itself supports Qt_TextFlag_TextWrapAnywhere and slint already uses Qt_TextFlag_TextWordWrap. I'm still figuring out how text wrapping works with femtovg, as well as with skia (which doesn't seem to support wrapping right now).

danielstuart14 commented 3 months ago

Is a PR with support just for the Qt backend (the others falling back to word-wrap) something desired? Of course the other backends would need to be extended later to also support it.

ogoffart commented 3 months ago

CSS has overflow-wrap: anywhere; and overflow-wrap: break-word; (ref)

The difference between the two is the way the minimum with is computed. But note that we currently already return 0 minimum-width with word-wrap (which is maybe not optimal)

I think it's make sense to have wrap: break-word

Is a PR with support just for the Qt backend (the others falling back to word-wrap) something desired?

Yes, that would be a good start. Implementing that in other backends can be done later and shouldn't be too difficult.