libscroll_re/
lib.rs

1extern crate libscroll;
2extern crate num;
3
4
5
6#[macro_use]
7extern crate ocaml;
8
9static BAD_PTR: &str = "ERROR: null/invalid ptr passed as scrollview handle";
10
11//use ocaml::core::memory;
12use ocaml::{ToValue, Value};
13
14caml!(libscroll_new() {
15    //std::mem::transmute::<*mut libscroll::Scrollview, Value>(Box::into_raw(Box::new(libscroll::Scrollview::new())))
16
17    caml_local!(result);
18
19    //result = std::mem::transmute(Box::into_raw(Box::new(libscroll::Scrollview::new())));
20
21    //return result;
22    Value::ptr(Box::into_raw(Box::new(libscroll::Scrollview::new())))
23
24});
25
26caml!(libscroll_del(scrollview) {
27    let _: Box<libscroll::Scrollview> = Box::from_raw(std::mem::transmute(scrollview));
28    // drops
29
30    Value::unit()
31});
32
33caml!(libscroll_set_geometry(
34        scrollview,
35        content_height,
36        content_width,
37        viewport_height,
38        viewport_width
39) {
40    //let mut scrollview: Box<libscroll::Scrollview> = Box::from_raw(std::mem::transmute(scrollview));
41    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
42    let content_height = content_height.int64_val() as u64;
43    let content_width = content_width.int64_val() as u64;
44    let viewport_width = viewport_width.int64_val() as u64;
45    let viewport_height = viewport_height.int64_val() as u64;
46
47    scrollview
48        .as_mut()
49        .expect(BAD_PTR)
50        .set_geometry(
51            content_height,
52            content_width,
53            viewport_height,
54            viewport_width,
55        );
56
57    Value::unit()
58});
59
60caml!(libscroll_animating(scrollview) {
61    //let scrollview: Box<libscroll::Scrollview> = Box::from_raw(std::mem::transmute(scrollview));
62    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
63
64    let result: isize = scrollview
65        .as_mut()
66        .expect(BAD_PTR)
67        .animating()
68        .into();
69
70    //return scrollview.animating().bool_val()
71    Value::nativeint(result)
72});
73
74caml!(libscroll_step_frame(scrollview) {
75    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
76
77    scrollview
78        .as_mut()
79        .expect(BAD_PTR)
80        .step_frame(None);
81
82
83    Value::unit()
84});
85
86caml!(libscroll_set_avg_frametime(scrollview, milliseconds) {
87    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
88    let milliseconds = milliseconds.f64_val();
89
90    scrollview
91        .as_mut()
92        .expect(BAD_PTR)
93        .set_avg_frametime(milliseconds);
94
95    Value::unit()
96});
97
98caml!(libscroll_set_next_frame_predict(scrollview, milliseconds) {
99    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
100    let milliseconds = milliseconds.f64_val();
101
102    scrollview
103        .as_mut()
104        .expect(BAD_PTR)
105        .set_next_frame_predict(milliseconds);
106
107    Value::unit()
108});
109
110caml!(libscroll_get_position_absolute(scrollview) {
111    let scrollview = scrollview.ptr_val::<libscroll::Scrollview>();
112
113    let pos = scrollview
114        .as_ref()
115        .expect(BAD_PTR)
116        .get_position_absolute();
117
118    tuple!(pos.x, pos.y).into()
119});
120
121caml!(libscroll_push_pan(scrollview, axis, amount) {
122    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
123    let axis = axis.usize_val();
124    let axis = match axis {
125        0 => libscroll::Axis::Horizontal,
126        1 => libscroll::Axis::Vertical,
127        other => panic!("Bad input for axis bounds {}, expected 0 (horizontal) or 1 (vertical)", other),
128    };
129
130    let amount = amount.f64_val();
131
132    scrollview
133        .as_mut()
134        .expect(BAD_PTR)
135        .push_pan(axis, amount, None);
136
137    Value::unit()
138});
139
140caml!(libscroll_push_fling(scrollview) {
141    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
142
143    scrollview
144        .as_mut()
145        .expect(BAD_PTR)
146        .push_fling(None);
147
148    Value::unit()
149});
150
151caml!(libscroll_push_interrupt(scrollview) {
152    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
153
154    scrollview
155        .as_mut()
156        .expect(BAD_PTR)
157        .push_interrupt(None);
158
159    Value::unit()
160});
161
162caml!(libscroll_set_source(scrollview, source) {
163    let scrollview = scrollview.mut_ptr_val::<libscroll::Scrollview>();
164    //let source: libscroll::Source = std::mem::transmute(source.nativeint_val());
165    //let source: Option<libscroll::Source> = num::FromPrimitive::from_isize(source.nativeint_val());
166    let source: libscroll::Source = std::mem::transmute(source.usize_val() as u8);
167
168    scrollview
169        .as_mut()
170        .expect(BAD_PTR)
171        .set_source(source);
172
173    Value::unit()
174});