From 19b2edb0f4484f173e8ebf8f7028cd844e4f1b33 Mon Sep 17 00:00:00 2001 From: dbr Date: Sun, 1 Aug 2021 12:38:37 +1000 Subject: [PATCH 1/3] Add example of using list clipper API Somewhat relates to #438 --- imgui-examples/examples/long_list.rs | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 imgui-examples/examples/long_list.rs diff --git a/imgui-examples/examples/long_list.rs b/imgui-examples/examples/long_list.rs new file mode 100644 index 0000000..ffa4dba --- /dev/null +++ b/imgui-examples/examples/long_list.rs @@ -0,0 +1,33 @@ +/// Demonstrates using the "list clipper" to efficiently display long +/// lists in a scrolling area. +/// +/// You specify the height per item, and the `ListClipper` API will +/// provide which item index are visible. This avoids having to create +/// thousands of items only for them to not be made visible. +/// +/// Note this requires a fixed (or easily computable) height per item. +use imgui::*; + +mod support; + +fn main() { + let lots_of_words: Vec = (0..10000) + .map(|x| format!("Line {}", x).into()) + .collect(); + + let system = support::init(file!()); + system.main_loop(move |_, ui| { + Window::new(im_str!("Hello long world")) + .size([300.0, 110.0], Condition::FirstUseEver) + .build(ui, || { + let mut clipper = imgui::ListClipper::new(lots_of_words.len() as i32) + .items_height(ui.current_font_size()) + .begin(ui); + while clipper.step() { + for row_num in clipper.display_start()..clipper.display_end() { + ui.text(&lots_of_words[row_num as usize]); + } + } + }); + }); +} From 15c28266907517051d234b19cbec5d8926aae3e0 Mon Sep 17 00:00:00 2001 From: dbr Date: Sat, 4 Sep 2021 12:21:00 +1000 Subject: [PATCH 2/3] Fix fmt --- imgui-examples/examples/long_list.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/imgui-examples/examples/long_list.rs b/imgui-examples/examples/long_list.rs index ffa4dba..62791cf 100644 --- a/imgui-examples/examples/long_list.rs +++ b/imgui-examples/examples/long_list.rs @@ -11,9 +11,7 @@ use imgui::*; mod support; fn main() { - let lots_of_words: Vec = (0..10000) - .map(|x| format!("Line {}", x).into()) - .collect(); + let lots_of_words: Vec = (0..10000).map(|x| format!("Line {}", x).into()).collect(); let system = support::init(file!()); system.main_loop(move |_, ui| { From d7470caa4c6e94d9d9ed3b7a80407d1b818c2eaf Mon Sep 17 00:00:00 2001 From: Jack Spira Date: Sun, 5 Sep 2021 12:12:00 -0700 Subject: [PATCH 3/3] rebased and minor clippy fix --- imgui-examples/examples/long_list.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imgui-examples/examples/long_list.rs b/imgui-examples/examples/long_list.rs index 62791cf..79278af 100644 --- a/imgui-examples/examples/long_list.rs +++ b/imgui-examples/examples/long_list.rs @@ -11,7 +11,7 @@ use imgui::*; mod support; fn main() { - let lots_of_words: Vec = (0..10000).map(|x| format!("Line {}", x).into()).collect(); + let lots_of_words: Vec = (0..10000).map(|x| format!("Line {}", x)).collect(); let system = support::init(file!()); system.main_loop(move |_, ui| {