gyscos / cursive

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

layout using percentages #370

Open strawberry-choco opened 5 years ago

strawberry-choco commented 5 years ago

Asking for help.

Problem description

Is it possible to use percentages to layout? For example, I want to have two views, one left and one right, whereas the left one uses 40% of the available width and the right one 60% like in the screenshot below.

I have searched solutions on doc.rs and issues on github, but did not find anything relevant, thus this question.

image

gyscos commented 5 years ago

Thanks for the report!

Currently there is not easy way to get that. LinearLayout has a weight parameter for each child which should enable this, but it's not implemented yet - I'll have another look and see how quick we can get that.

Once it's implemented, you would make each view "fullscreen", and give them proportional weights (for example 2 and 3, or 40 and 60).

strawberry-choco commented 5 years ago

Ah, okay. So that's why the weight parameters did nothing when I changed them. Thanks for the info! :+1:

jaygazula27 commented 4 years ago

@gyscos In the meantime, do you have any recommendations on how we can achieve this sort of layout even if it's not necessarily easy?
The only thing I can come up with is somehow calculate the total width of screen and then adjust each of the (left and right) panels' width accordingly during a resize event. But I'm approaching it from a web UI point of view, and I'm not sure if that can be translated in a TUI.

gyscos commented 4 years ago

One way to achieve it is to basically re-implement a simplified version of LinearLayout which ignores the children's preferences and just give them a percentage of the available space.

If you want to use fixed percentages, you might be also be able to implement this using FixedLayout wrapped in an OnLayoutView: on each call to layout, update the children position in the FixedLayout using the percentages.

jaygazula27 commented 4 years ago

@gyscos Thanks for that information! I'll try to follow what you said and figure out a decent solution. Appreciate the help.

llambdaa commented 2 years ago

What is the state of this feature? Are you planning to implement it in the near future, so does it have some priority? Having relative layouts using ratios would be a major feature and probably very appreciated!

I figure, for that, you must be able to enforce width and height onto components that are respected when drawing it so that e.g. a button actually renders with the given width instead of shrinking to the bare minimum it needs. Resolving the ratios inside a LinearLayout and calculating the resulting width and or height (that could then be given as a requirement to the child components) shouldn't be that much of an issue.