Skip to main content

positioning/
positioning.rs

1use {
2    cursive::{align::*, view::*, views::*, *},
3    cursive_image::*,
4};
5
6// Here we'll demonstrate the various positioning configurations
7
8// Press "s" to switch sizing mode (shrink/fit/scale=3)
9// Press "h" to switch horizontal alignment mode (left/center/right)
10// Press "v" to switch vertical alignment mode (top/center/bottom)
11
12const FILE: &str = "assets/media/linux.png";
13const NAME: &str = "Image";
14
15fn main() {
16    let mut cursive = default();
17
18    cursive.add_layer(Panel::new(
19        ImageView::default()
20            // We're also showing the use of new_png_file(), which discovers the image size for us
21            // (requires the "png" feature)
22            .with_image(Image::new_png_file(FILE).expect("new_png_file"))
23            .with_name(NAME)
24            .fixed_size((50, 20)),
25    ));
26
27    cursive.add_global_callback('s', |cursive| {
28        _ = cursive.call_on_name(NAME, |image_view: &mut ImageView| {
29            image_view.set_sizing(match image_view.sizing() {
30                Sizing::Shrink => Sizing::Fit,
31                Sizing::Fit => Sizing::Scale(3.),
32                Sizing::Scale(_) => Sizing::Shrink,
33            })
34        });
35    });
36
37    cursive.add_global_callback('h', |cursive| {
38        _ = cursive.call_on_name(NAME, |image_view: &mut ImageView| {
39            image_view.set_align(match image_view.align() {
40                Align { h: HAlign::Left, v } => Align { h: HAlign::Center, v },
41                Align { h: HAlign::Center, v } => Align { h: HAlign::Right, v },
42                Align { h: HAlign::Right, v } => Align { h: HAlign::Left, v },
43            })
44        });
45    });
46
47    cursive.add_global_callback('v', |cursive| {
48        _ = cursive.call_on_name(NAME, |image_view: &mut ImageView| {
49            image_view.set_align(match image_view.align() {
50                Align { h, v: VAlign::Top } => Align { h, v: VAlign::Center },
51                Align { h, v: VAlign::Center } => Align { h, v: VAlign::Bottom },
52                Align { h, v: VAlign::Bottom } => Align { h, v: VAlign::Top },
53            })
54        });
55    });
56
57    cursive.add_global_callback('q', |cursive| cursive.quit());
58
59    cursive.run();
60}