Process for adding JUCE hooks? #147
Replies: 6 comments
-
Hey @jrlanglois , Could you give a little more context? Are you talking about instantiating a |
Beta Was this translation helpful? Give feedback.
-
Well, the context is about understanding how to integrate C++ code for React code to understand. I haven't a clue what the process would be! |
Beta Was this translation helpful? Give feedback.
-
Touche! This is one of those things we should document better. Generally there are two ways to cross this C++/React boundary: you can pass C++ functions into javascript and javascript functions into C++ and call them when you need, or you can create a custom native View and render it in your React component tree. Option 1To establish C++ functions in your js environment, see In the other direction, you can invoke js functions from C++ with global.myFunc = function(x) {
console.log('Hello from js', x);
}; Then you could invoke that You can also pass javascript functions as Option 2Now the other option is to define a totally custom native view, and register it with React. To do this, you can make your own C++ class that inherits class MyCoolView : public blueprint::View {
void paint(juce::Graphics& g) override {
// Do your own paint routine like usual.
// You can also treat this whole class instance like your normal juce::Components. Add children, `addAndMakeVisible`,
// `resized` and everything!
}
} Now once you've got your custom view implementation, you have to tell React about it so that you can use it. The end goal with this is to be able to write something like this: function MyReactApp(props) {
return (
<View>
<MyCoolView customProp="hi" otherCustomProp="bye" />
</View>
);
} With this, you can write your juce::Components like normal, but use React to compose these things together into your full application. So, how do we register your C++ class with React? All you need is mAppRoot.registerViewType("MyCoolView", []() {
// This lambda will be called when we need to construct one of your custom view instances. So, first, we make one:
auto view = std::make_unique<MyCoolView>();
// Then we need to construct a shadow view, which will govern the layout. 99% of the time, you can just use the
// default shadow view:
auto shadowView = std::make_unique<ShadowViewType>(view.get());
// Then just return these guys as a pair
return {std::move(view), std::move(shadowView)};
}); Boom! Now React can understand your function MyCoolView(props) {
return React.createElement("MyCoolView", props, props.children);
}
// Now I can use it elsewhere in JSX...
function MyApp(props) {
return (<MyCoolView {...props} />);
} Et voila! So, that was a lot, I realize, but that lays out all your options :). Better documentation is on the way! |
Beta Was this translation helpful? Give feedback.
-
Excellent, thanks! Yeah, it sounds like this outta be documented somehow. Maybe the Wiki would be useful here? |
Beta Was this translation helpful? Give feedback.
-
Definitely. We've moved documentation into the repo: https://github.com/nick-thompson/blueprint/tree/master/docs, which reminds me I should probably remove the wiki to avoid confusion :) |
Beta Was this translation helpful? Give feedback.
-
I just added the Discussions feature of github, which looks pretty neat! I'm going to mark this as a discussion, which will close the issue attached, but preserve the question/answer here for other users who find this helpful |
Beta Was this translation helpful? Give feedback.
-
What is needed to add hooks for doing things like creating/removing/moving/resizing windows, popup menus, custom components, and so forth?
Beta Was this translation helpful? Give feedback.
All reactions