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
72
73
74
75
76
77
78
79
80
81
use std::marker::PhantomData;
use std::thread;

use crate::sys;
use crate::Ui;

pub struct ListClipper {
    items_count: i32,
    items_height: f32,
}

impl ListClipper {
    pub const fn new(items_count: i32) -> Self {
        ListClipper {
            items_count,
            items_height: -1.0,
        }
    }

    pub const fn items_height(mut self, items_height: f32) -> Self {
        self.items_height = items_height;
        self
    }

    pub fn begin<'ui>(self, ui: &Ui<'ui>) -> ListClipperToken<'ui> {
        let list_clipper = unsafe {
            let list_clipper = sys::ImGuiListClipper_ImGuiListClipper();
            sys::ImGuiListClipper_Begin(list_clipper, self.items_count, self.items_height);
            list_clipper
        };
        ListClipperToken::new(ui, list_clipper)
    }
}

pub struct ListClipperToken<'ui> {
    list_clipper: *mut sys::ImGuiListClipper,
    _phantom: PhantomData<&'ui Ui<'ui>>,
}

impl<'ui> ListClipperToken<'ui> {
    fn new(_: &Ui<'ui>, list_clipper: *mut sys::ImGuiListClipper) -> Self {
        Self {
            list_clipper,
            _phantom: PhantomData,
        }
    }

    pub fn step(&mut self) -> bool {
        unsafe { sys::ImGuiListClipper_Step(self.list_clipper) }
    }

    pub fn end(&mut self) {
        unsafe {
            sys::ImGuiListClipper_End(self.list_clipper);
        }
    }

    pub fn display_start(&self) -> i32 {
        unsafe { (*self.list_clipper).DisplayStart }
    }

    pub fn display_end(&self) -> i32 {
        unsafe { (*self.list_clipper).DisplayEnd }
    }
}

impl<'ui> Drop for ListClipperToken<'ui> {
    fn drop(&mut self) {
        if !self.step() {
            unsafe {
                sys::ImGuiListClipper_destroy(self.list_clipper);
            };
        } else if !thread::panicking() {
            panic!(
                "Forgot to call End(), or to Step() until false? \
            This is the only token in the repository which users must call `.end()` or `.step()` \
            with. See https://github.com/imgui-rs/imgui-rs/issues/438"
            );
        }
    }
}