removed wrapper

This commit is contained in:
2024-11-20 14:47:59 +01:00
parent a593829a81
commit e55606b9cf
4 changed files with 34 additions and 19 deletions
+13 -14
View File
@@ -6,23 +6,16 @@ use crate::{
};
use std::ffi::c_void;
struct ElementWrapper<'a>(&'a mut Element);
impl<'a> ElementWrapper<'a> {
pub unsafe fn from_raw(ptr: *mut c_void) -> &'a mut Element {
assert!(!ptr.is_null(), "Pointer to ElementWrapper is null");
let wrapper = ptr as *mut ElementWrapper;
(*wrapper).0
}
}
extern "C" {
fn c_run(element: *mut c_void) -> bool;
}
#[no_mangle]
pub extern "C" fn render(ptr: *mut c_void, state: u32) {
let element = unsafe { ElementWrapper::from_raw(ptr) };
let element = unsafe {
assert!(!ptr.is_null(), "Pointer is null");
&mut *(ptr as *mut Element)
};
let (width, height) = get_term_size();
let mut frame: Vec<String> = create_frame(width, height);
render_to_frame(State::from_u32(state), width, &mut frame, element);
@@ -34,7 +27,10 @@ pub extern "C" fn render(ptr: *mut c_void, state: u32) {
#[no_mangle]
pub extern "C" fn event(ptr: *mut c_void, state: *mut u32) {
let event = crossterm::event::read().unwrap();
let element = unsafe { ElementWrapper::from_raw(ptr) };
let element = unsafe {
assert!(!ptr.is_null(), "Pointer is null");
&mut *(ptr as *mut Element)
};
if let crossterm::event::Event::Resize(width, height) = event {
element.update_data(width as usize, height as usize);
}
@@ -44,7 +40,10 @@ pub extern "C" fn event(ptr: *mut c_void, state: *mut u32) {
#[no_mangle]
pub extern "C" fn init_event(ptr: *mut c_void, state: *mut u32) {
let event = crossterm::event::Event::FocusGained;
let element = unsafe { ElementWrapper::from_raw(ptr) };
let element = unsafe {
assert!(!ptr.is_null(), "Pointer is null");
&mut *(ptr as *mut Element)
};
let (width, height) = get_term_size();
element.update_data(width, height);
element.event(event, &StateChanger(state));
@@ -55,7 +54,7 @@ pub fn run(element: &mut Element) -> bool {
enable_raw_mode().unwrap();
clear();
unsafe {
let c = c_run(&mut ElementWrapper(element) as *mut ElementWrapper as *mut c_void);
let c = c_run(element as *mut Element as *mut c_void);
show_cursor();
disable_raw_mode().unwrap();
clear();