-
-
Notifications
You must be signed in to change notification settings - Fork 78
feat: added SDL_SetWindowHitTest wrapper #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
revmischa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
| let boxed: Box<Box<dyn Fn(crate::rect::Point) -> HitTestResult>> = | ||
| Box::new(Box::new(hit_test)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is it in two boxes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrapped the closure twice to relax the trait bounds when dereferencing the callback from c_void pointer, as only Box'ing it once would yield
expected a `Fn(Point)` closure, found `c_void`
the trait `Fn(Point)` is not implemented for `c_void`
required for the cast from `*mut c_void` to `*mut dyn Fn(Point) -> HitTestResult
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let callback = &*(data as *const dyn Fn(Point) -> HitTestResult); might work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried your solution but it seems the compiler still stops us from casting it into a trait pointer...
expected a `Fn(Point)` closure, found `c_void`
the trait `Fn(Point)` is not implemented for `c_void`
required for the cast from `*mut c_void` to `*const dyn Fn(Point) -> HitTestResult
Casting the void pointer into the trait pointer still requires the void pointer to implement that trait (which c_void doesn't)
I think Box is still needed to relax the trait bounds
src/sdl3/video.rs
Outdated
|
|
||
| unsafe { | ||
| let result = | ||
| sys::video::SDL_SetWindowHitTest(self.context.raw, Some(hit_test_sys), userdata); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like the hit_test will never be freed, we could store the userdata pointer on the Window struct and free it on Window drop perhaps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved hit_test into the Window struct in 96e8623
This PR adds a wrapper function and required enums for the
SDL_SetWindowHitTestfunction in thevideosubmodule.The function is currently implemented with a trampoline function to wrap around bindgen's
extern "C"function pointer type.To replace
SDL_SetWindowHitTest(SDL_Window* win, const SDL_Point* area, void* data)'sdatavoid pointer, I've implemented the closure as animpl Fnto be able to capture variables as needed into the closure instead of passing them as function parameters.