File: simple.rs

package info (click to toggle)
rust-cursive-tabs 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 544 kB
  • sloc: sh: 89; makefile: 2
file content (71 lines) | stat: -rw-r--r-- 2,493 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use cursive::view::{Nameable, Resizable};
use cursive::views::{Button, LinearLayout, NamedView, PaddedView, TextArea, TextView};
use cursive_tabs::{Align, TabPanel};

const TAB_0: &str =
    "With using the TabPanel you get a TabView and TabBar, preconfigured for you to use!
Simply create it with:

`cursive_tabs::TabPanel::new()`";

const TAB_1: &str = "You then can add views and configure your panel.";

const TAB_2: &str =
    "Ofcourse you can also use the provided TabView without the panel, simply create it with:

`cursive_tabs::TabView::new()`";

const TAB_3: &str = "All you have to do is add:

cursive-tabs = \"^0\"

to your Cargo.toml!
";

fn main() {
    let mut siv = cursive::default();
    let mut panel = TabPanel::new()
        .with_tab(TextView::new(TAB_0).with_name("0"))
        .with_tab(TextView::new(TAB_1).with_name("1"))
        .with_tab(TextView::new(TAB_2).with_name("2"))
        .with_tab(TextView::new(TAB_3).with_name("3"))
        .with_tab(PaddedView::lrtb(2, 2, 1, 1, TextArea::new()).with_name("4"))
        .with_bar_alignment(Align::End);

    let view = panel
        .active_view_mut()
        .unwrap()
        .downcast_mut::<NamedView<PaddedView<TextArea>>>()
        .unwrap();
    view.get_mut()
        .get_inner_mut()
        .set_content("This is additional text, set after the creation of the view!");
    panel.set_active_tab("0").expect("View not found");

    siv.add_layer(
        LinearLayout::vertical()
            .child(panel.with_name("Tabs").fixed_size((50, 10)))
            .child(
                LinearLayout::horizontal()
                    .child(Button::new("Prev", |siv| {
                        let mut tabs: cursive::views::ViewRef<TabPanel> =
                            siv.find_name("Tabs").expect("id not found");
                        tabs.prev();
                    }))
                    .child(Button::new("Next", |siv| {
                        let mut tabs: cursive::views::ViewRef<TabPanel> =
                            siv.find_name("Tabs").expect("id not found");
                        tabs.next();
                    }))
                    .child(Button::new("Switch", |siv| {
                        let mut tabs: cursive::views::ViewRef<TabPanel> =
                            siv.find_name("Tabs").expect("id not found");
                        tabs.swap_tabs("1", "2");
                    })),
            ),
    );

    siv.add_global_callback('q', |siv| siv.quit());

    siv.run();
}