C++: add on_* funciton to connect to signals

This commit is contained in:
Olivier Goffart 2020-08-03 13:18:38 +02:00
parent a4efac920a
commit b2a1a72450
2 changed files with 24 additions and 9 deletions

View file

@ -5,16 +5,14 @@ int main()
{
static Hello component;
component.foobar.set_handler([](auto...) { std::cout << "Hello from C++" << std::endl; });
component.on_foobar([](auto...) { std::cout << "Hello from C++" << std::endl; });
component.plus_clicked.set_handler([]() {
auto &counter = component.counter;
counter.set(counter.get() + 1);
component.on_plus_clicked([]() {
component.set_counter(component.get_counter() + 1);
});
component.minus_clicked.set_handler([]() {
auto &counter = component.counter;
counter.set(counter.get() - 1);
component.on_minus_clicked([]() {
component.set_counter(component.get_counter() - 1);
});
sixtyfps::ComponentWindow window;

View file

@ -97,11 +97,16 @@ mod cpp_ast {
/// The list of statement instead the function. When None, this is just a function
/// declaration without the definition
pub statements: Option<Vec<String>>,
/// What's inside template<...> if any
pub template_parameters: Option<String>,
}
impl Display for Function {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
indent(f)?;
if let Some(tpl) = &self.template_parameters {
write!(f, "template<{}> ", tpl)?;
}
if self.is_static {
write!(f, "static ")?;
}
@ -428,8 +433,7 @@ fn generate_component(
for (cpp_name, property_decl) in component.root_element.borrow().property_declarations.iter() {
let ty = if property_decl.property_type == Type::Signal {
if property_decl.expose_in_public_api && is_root {
let signal_emitter: Vec<String> = vec![format!("{}.emit();", cpp_name)];
let signal_emitter = vec![format!("{}.emit();", cpp_name)];
component_struct.members.push((
Access::Public,
Declaration::Function(Function {
@ -439,6 +443,19 @@ fn generate_component(
..Default::default()
}),
));
component_struct.members.push((
Access::Public,
Declaration::Function(Function {
name: format!("on_{}", cpp_name),
template_parameters: Some("typename Functor".into()),
signature: "(Functor && signal_handler)".into(),
statements: Some(vec![format!(
"{}.set_handler(std::forward<Functor>(signal_handler));",
cpp_name
)]),
..Default::default()
}),
));
}
"sixtyfps::Signal".into()