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
12// We're also showing the use of new_png_file(), which discovers the image size for us
13// (requires the "png" feature)
14
15const NAME: &str = "Image";
16const FILE: &str = "assets/media/linux.png";
17
18fn main() {
19    let mut cursive = default();
20
21    cursive.add_layer(Panel::new(
22        ImageView::default()
23            .with_image(Image::new_png_file(FILE).expect("new_png_file"))
24            .with_name(NAME)
25            .fixed_size((50, 20)),
26    ));
27
28    cursive.add_global_callback('s', |cursive| {
29        _ = cursive.call_on_name(NAME, |image_view: &mut ImageView| {
30            image_view.set_sizing(match image_view.sizing() {
31                Sizing::Shrink => Sizing::Fit,
32                Sizing::Fit => Sizing::Scale(3.),
33                Sizing::Scale(_) => Sizing::Shrink,
34            })
35        });
36    });
37
38    cursive.add_global_callback('h', |cursive| {
39        _ = cursive.call_on_name(NAME, |image_view: &mut ImageView| {
40            image_view.set_align(match image_view.align() {
41                Align { h: HAlign::Left, v } => Align { h: HAlign::Center, v },
42                Align { h: HAlign::Center, v } => Align { h: HAlign::Right, v },
43                Align { h: HAlign::Right, v } => Align { h: HAlign::Left, v },
44            })
45        });
46    });
47
48    cursive.add_global_callback('v', |cursive| {
49        _ = cursive.call_on_name(NAME, |image_view: &mut ImageView| {
50            image_view.set_align(match image_view.align() {
51                Align { h, v: VAlign::Top } => Align { h, v: VAlign::Center },
52                Align { h, v: VAlign::Center } => Align { h, v: VAlign::Bottom },
53                Align { h, v: VAlign::Bottom } => Align { h, v: VAlign::Top },
54            })
55        });
56    });
57
58    cursive.add_global_callback('q', |cursive| cursive.quit());
59
60    cursive.run();
61}