gyscos / cursive

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

[BUG] Limit subview size #775

Closed cdxjcl closed 6 months ago

cdxjcl commented 6 months ago

Describe the bug I want to limit the size of subviews by wrapping layout method but the result has longer width.

To Reproduce

let mut siv = cursive::default();
let text = TextView::new("ABCDABCDABCD");
let wrap_view = OnLayoutView::new(
    Layer::new(text),
    |layout, size| {
        layout.layout(Vec2::from((4, 1)));
    },
);
siv.screen_mut().add_transparent_layer(wrap_view);
siv.run();

Expected behavior TextView should only display "ABCD".

Screenshots

image

Expect: image

Environment

gyscos commented 6 months ago

Hi, and thanks for the report!

To override the size of another view, you actually need to wrap 3 methods:

The ResizedView wraps all these, and gives you convenient ways to configure what new size the view should have. For example:

ResizeView::with_fixed_size((4, 1), Layout::new(text))

(Other methods exist to only affect the width or height, and to specify min/max values rather than fixed ones.)

With the convenience Resizable trait, you can use chainable methods:

Layout::new(text).fixed_size((4, 1))
cdxjcl commented 6 months ago

Thank you for your reply. I used ResizedView and it solved my problem.