gyscos / cursive

A Text User Interface library for the Rust programming language
MIT License
4.31k stars 245 forks source link

Setting a style for a TextView #515

Open robinkrahl opened 4 years ago

robinkrahl commented 4 years ago

This is a both a question and a feature request.

Problem description

I saw the TextView::set_effect method and wanted to have a set_style method too. I was about to implement it when I saw your comment in text_view.rs:

// TODO: remove now that we have styled text?
effect: Effect,

So I wanted to discuss this here first.

Yes, the user can just set the content to a styled string instead of setting the effect or style for the text view. But for views that are created once and then updated regularly and from multiple code locations (e. g. a status bar), it is useful to have a way to set a default style for the view. So I think it would make sense to keep the set_effect method and to even add a set_style(Into<Style) method. (The set_effect method could also be dropped in favor of set_style.) What do you think?

gyscos commented 4 years ago

Hi again!

I think there are two separate potential improvements:

There's also the more conceptual question "is style attached to the text content, or to the view itself?".

Some of this point to hierarchy of applied styles, maybe closer to CSS. I don't have a very clear idea of how to get there though, and the two improvements I mentioned are not exactly trivial to add (especially the second one), so for now having a dedicated style for the TextView itself which is merged with the content is perfectly fine.

robinkrahl commented 4 years ago

Alternatively, there could be a StyledView that wraps an arbitrary view and applies a style to the printer before drawing it, maybe together with a Styleable trait.

gyscos commented 3 years ago

On the latest commit there is now a set_style option to TextView.

We'll see later for a more general solution.