arcdps_imgui/
list_clipper.rs
1use std::marker::PhantomData;
2use std::thread;
3
4use crate::sys;
5use crate::Ui;
6
7pub struct ListClipper {
8 items_count: i32,
9 items_height: f32,
10}
11
12impl ListClipper {
13 pub const fn new(items_count: i32) -> Self {
14 ListClipper {
15 items_count,
16 items_height: -1.0,
17 }
18 }
19
20 pub const fn items_height(mut self, items_height: f32) -> Self {
21 self.items_height = items_height;
22 self
23 }
24
25 pub fn begin<'ui>(self, ui: &Ui<'ui>) -> ListClipperToken<'ui> {
26 let list_clipper = unsafe {
27 let list_clipper = sys::ImGuiListClipper_ImGuiListClipper();
28 sys::ImGuiListClipper_Begin(list_clipper, self.items_count, self.items_height);
29 list_clipper
30 };
31 ListClipperToken::new(ui, list_clipper)
32 }
33}
34
35pub struct ListClipperToken<'ui> {
36 list_clipper: *mut sys::ImGuiListClipper,
37 _phantom: PhantomData<&'ui Ui<'ui>>,
38}
39
40impl<'ui> ListClipperToken<'ui> {
41 fn new(_: &Ui<'ui>, list_clipper: *mut sys::ImGuiListClipper) -> Self {
42 Self {
43 list_clipper,
44 _phantom: PhantomData,
45 }
46 }
47
48 pub fn step(&mut self) -> bool {
49 unsafe { sys::ImGuiListClipper_Step(self.list_clipper) }
50 }
51
52 pub fn end(&mut self) {
53 unsafe {
54 sys::ImGuiListClipper_End(self.list_clipper);
55 }
56 }
57
58 pub fn display_start(&self) -> i32 {
59 unsafe { (*self.list_clipper).DisplayStart }
60 }
61
62 pub fn display_end(&self) -> i32 {
63 unsafe { (*self.list_clipper).DisplayEnd }
64 }
65}
66
67impl<'ui> Drop for ListClipperToken<'ui> {
68 fn drop(&mut self) {
69 if !self.step() {
70 unsafe {
71 sys::ImGuiListClipper_destroy(self.list_clipper);
72 };
73 } else if !thread::panicking() {
74 panic!(
75 "Forgot to call End(), or to Step() until false? \
76 This is the only token in the repository which users must call `.end()` or `.step()` \
77 with. See https://github.com/imgui-rs/imgui-rs/issues/438"
78 );
79 }
80 }
81}