Comments (5)
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 thedid_finish_launching()
is a nuisance bit due to howNSMenu
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
andRefCell
might be your friend for some interior mutability here, although the0.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.
(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.
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.
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.
Fantastic, thanks for all the help and pointers, @ryanmcgrath!
from cacao.
Related Issues (20)
- [UISceneConfiguration isEqualToString:]: unrecognized selector error with 0.4.0-beta1 HOT 4
- ViewAnimatorProxy?
- Mixing runtimes HOT 4
- Story for unowned pointers in collections HOT 8
- NSMutableArray HOT 1
- Can't seem to make Button fire when clicked HOT 4
- Improved documentation for ListView HOT 16
- tvOS example HOT 5
- TextField not on edit mode at window opening HOT 2
- Method to access the layout anchors of a view HOT 3
- Get screen dimensions for positioning below toolbar
- Window closes on blocking operations HOT 2
- SplitViewController doesn't implement Layout (or super-trait ObjcAccess) - example code? HOT 4
- Setting window level HOT 6
- _action.request.absolute_url() exit with segmentation fault error HOT 8
- MenuItem's objc pointer is private HOT 2
- Capability to resize Images HOT 3
- missing CALayer.contents -> Any? HOT 3
- Enable focusing on an opened window
- Browser Example Broken HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cacao.