Giter VIP home page Giter VIP logo

Comments (5)

ryanmcgrath avatar ryanmcgrath commented on July 20, 2024

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:

  • Calling App::activate() in the did_finish_launching() is a nuisance bit due to how NSMenu works.
  • Retaining your view(s) is what enables them to actually render properly, they were all dropping at the end of the 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!

from cacao.

ryanmcgrath avatar ryanmcgrath commented on July 20, 2024

(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!)

from cacao.

jjant avatar jjant commented on July 20, 2024

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?

from cacao.

ryanmcgrath avatar ryanmcgrath commented on July 20, 2024

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?

from cacao.

jjant avatar jjant commented on July 20, 2024

Fantastic, thanks for all the help and pointers, @ryanmcgrath!

from cacao.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.