Closed jjant closed 2 years ago
Heya, cool to see someone using the custom drawing code... I don't think anyone has dug that deep yet!
I went ahead and made an example in the repo: https://github.com/ryanmcgrath/cacao/blob/trunk/examples/custom_image_drawing.rs
Basically, you were hitting a few things here:
App::activate()
in the did_finish_launching()
is a nuisance bit due to how NSMenu
works.did_finish_launching()
scope. Cacao goes out of its way to avoid retaining various ObjC objects to avoid any odd issues with leaking, and because it kind of clashes with Rust's borrowing model to do so. Rc
and RefCell
might be your friend for some interior mutability here, although the 0.3
branch (for Airyx etc) changes some methods to be &mut self
to avoid the need for a lot of those tricks.Feel free to grab the example in the repo and go crazy - it just renders a generic file icon, but hopefully it's helpful!
(This is also probably a great candidate project for custom drawing code on a CALayer
or something, so if you wind up looking into that... pull requests welcome!)
Hey man, thank you so much for the detailed answer!
Unfortunately I'm not smart enough to get what I want to do to work...
I noticed that CGContext
has a .data()
function that I think lets me access the pixel data directly, but on the Image::draw
callback I get a CGContextRef
(which I assume is a C CGContext*
? Apple docs are not the clearest thing...).
I can get a CGContext
from the ref with to_owned()
, but modifying that doesn't seem to do anything (I assume I'm getting a copy, rather than the actual original thing).
I saw there's also CGContext::from_existing_context_ptr()
but it takes a different CGContextRef (which appears to come from some sys crate and not core-foundation-rs).
I can also build a context by itself, with CGContext::create_bitmap_context
, but I can't seem to understand how to turn that into a cacao image.
A different question altogether, but I don't understand what the bool returned from the Image::draw
callback is, should it always be true
?
Re: the returned boolean value, the Image::draw
method is mostly a mapping over NSImage
draw handlers - which return a BOOL
in their handler method. I don't exactly think it's the greatest API ever, but I wanted to keep it more in line with it from the Rust side for clarity's sake.
See: https://developer.apple.com/documentation/appkit/nsimage/1519860-imagewithsize?language=objc
Regarding understanding the CGContextRef
pieces, you probably wanna scope out image/image.rs
line ~179ish (the draw
method). It grabs the current graphics context and does some basic calls before passing it to you - hopefully this helps with whatever you're trying to do?
Fantastic, thanks for all the help and pointers, @ryanmcgrath!
Hey there, I'm pretty new to mac/cocoa programming, I'm trying to basically write a renderer, ending up with some kind of array of pixel data, and display that to a window.
It seems like I could use Bitmap contexts/
Image::draw
to achieve this somehow, but so far I haven't been able to.I tried stuff like this, which doesn't render anything to the screen:
And many variations of it (without really achieving anything), any ideas?