Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment: pass the component handle as the first argument of callback #2035

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/7guis/booker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ slint::slint!(import { Booker } from "booker.slint";);

pub fn main() {
let booker = Booker::new();
booker.on_validate_date(|date: SharedString| {
booker.on_validate_date(|_, date: SharedString| {
NaiveDate::parse_from_str(date.as_str(), "%d.%m.%Y").is_ok()
});
booker.on_compare_date(|date1: SharedString, date2: SharedString| {
booker.on_compare_date(|_, date1: SharedString, date2: SharedString| {
let date1 = match NaiveDate::parse_from_str(date1.as_str(), "%d.%m.%Y") {
Err(_) => return false,
Ok(x) => x,
Expand Down
16 changes: 4 additions & 12 deletions examples/7guis/circledraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ pub fn main() {
{
let model = model.clone();
let undo_stack = undo_stack.clone();
let window_weak = main_window.as_weak();
main_window.on_background_clicked(move |x, y| {
main_window.on_background_clicked(move |main_window, x, y| {
let mut undo_stack = undo_stack.borrow_mut();
let main_window = window_weak.unwrap();

model.push(Circle { x: x as f32, y: y as f32, d: 30.0 });
undo_stack.push(Change::CircleAdded { row: model.row_count() - 1 });
Expand All @@ -107,10 +105,8 @@ pub fn main() {

{
let undo_stack = undo_stack.clone();
let window_weak = main_window.as_weak();
main_window.on_undo_clicked(move || {
main_window.on_undo_clicked(move |main_window| {
let mut undo_stack = undo_stack.borrow_mut();
let main_window = window_weak.unwrap();
undo_stack.undo();
main_window.set_undoable(undo_stack.undoable());
main_window.set_redoable(undo_stack.redoable());
Expand All @@ -119,10 +115,8 @@ pub fn main() {

{
let undo_stack = undo_stack.clone();
let window_weak = main_window.as_weak();
main_window.on_redo_clicked(move || {
main_window.on_redo_clicked(move |main_window| {
let mut undo_stack = undo_stack.borrow_mut();
let main_window = window_weak.unwrap();
undo_stack.redo();
main_window.set_undoable(undo_stack.undoable());
main_window.set_redoable(undo_stack.redoable());
Expand All @@ -132,11 +126,9 @@ pub fn main() {
{
let model = model.clone();
let undo_stack = undo_stack.clone();
let window_weak = main_window.as_weak();
main_window.on_circle_resized(move |row, diameter| {
main_window.on_circle_resized(move |main_window, row, diameter| {
let row = row as usize;
let mut undo_stack = undo_stack.borrow_mut();
let main_window = window_weak.unwrap();

let mut circle = model.row_data(row).unwrap();
let old_d = circle.d;
Expand Down
18 changes: 4 additions & 14 deletions examples/7guis/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ pub fn main() {
main_window.set_names_list(filtered_model.clone().into());

{
let main_window_weak = main_window.as_weak();
let model = model.clone();
main_window.on_createClicked(move || {
let main_window = main_window_weak.unwrap();
main_window.on_createClicked(move |main_window| {
let new_entry = Name {
first: main_window.get_name().to_string(),
last: main_window.get_surname().to_string(),
Expand All @@ -46,12 +44,9 @@ pub fn main() {
}

{
let main_window_weak = main_window.as_weak();
let model = model.clone();
let filtered_model = filtered_model.clone();
main_window.on_updateClicked(move || {
let main_window = main_window_weak.unwrap();

main_window.on_updateClicked(move |main_window| {
let updated_entry = Name {
first: main_window.get_name().to_string(),
last: main_window.get_surname().to_string(),
Expand All @@ -63,22 +58,17 @@ pub fn main() {
}

{
let main_window_weak = main_window.as_weak();
let model = model.clone();
let filtered_model = filtered_model.clone();
main_window.on_deleteClicked(move || {
let main_window = main_window_weak.unwrap();

main_window.on_deleteClicked(move |main_window| {
let index = filtered_model.unfiltered_row(main_window.get_current_item() as usize);
model.remove(index);
});
}

{
let main_window_weak = main_window.as_weak();
let filtered_model = filtered_model.clone();
main_window.on_prefixEdited(move || {
let main_window = main_window_weak.unwrap();
main_window.on_prefixEdited(move |main_window| {
*prefix.borrow_mut() = main_window.get_prefix();
filtered_model.reset();
});
Expand Down
2 changes: 1 addition & 1 deletion examples/imagefilter/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn main() {

main_window.set_filters(slint::ModelRc::from(filters.clone()));

main_window.on_filter_image(move |filter_index| {
main_window.on_filter_image(move |_, filter_index| {
let filter_fn = filters.0[filter_index as usize].apply_function;
let filtered_image = filter_fn(&source_image);
slint::Image::from_rgba8(slint::SharedPixelBuffer::clone_from_slice(
Expand Down
10 changes: 4 additions & 6 deletions examples/memory/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ pub fn main() {

main_window.set_memory_tiles(tiles_model.clone().into());

let main_window_weak = main_window.as_weak();

main_window.on_check_if_pair_solved(move || {
main_window.on_check_if_pair_solved(move |main_window| {
let mut flipped_tiles =
tiles_model.iter().enumerate().filter(|(_, tile)| tile.image_visible && !tile.solved);

Expand All @@ -48,11 +46,11 @@ pub fn main() {
t2.solved = true;
tiles_model.set_row_data(t2_idx, t2);
} else {
main_window_weak.unwrap().set_disable_tiles(true);
let main_window_weak = main_window_weak.clone();
main_window.set_disable_tiles(true);
let main_window = main_window.clone_strong();
let tiles_model = tiles_model.clone();
Timer::single_shot(Duration::from_secs(1), move || {
main_window_weak.unwrap().set_disable_tiles(false);
main_window.set_disable_tiles(false);
t1.image_visible = false;
tiles_model.set_row_data(t1_idx, t1);
t2.image_visible = false;
Expand Down
2 changes: 1 addition & 1 deletion examples/plotter/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn main() {

let main_window = MainWindow::new();

main_window.on_render_plot(render_plot);
main_window.on_render_plot(|_, p, y, a| render_plot(p, y, a));

main_window.run();
}
6 changes: 3 additions & 3 deletions examples/printerdemo/rust/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ pub fn main() {
});
main_window.global::<PrinterQueue>().set_printer_queue(printer_queue.data.clone().into());

main_window.on_quit(move || {
main_window.on_quit(move |_| {
#[cfg(not(target_arch = "wasm32"))]
std::process::exit(0);
});

let printer_queue_copy = printer_queue.clone();
main_window.global::<PrinterQueue>().on_start_job(move |title| {
main_window.global::<PrinterQueue>().on_start_job(move |_, title| {
printer_queue_copy.push_job(title);
});

let printer_queue_copy = printer_queue.clone();
main_window.global::<PrinterQueue>().on_cancel_job(move |idx| {
main_window.global::<PrinterQueue>().on_cancel_job(move |_, idx| {
printer_queue_copy.data.remove(idx as usize);
});

Expand Down
6 changes: 3 additions & 3 deletions examples/printerdemo_mcu/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ fn main() -> ! {
});
main_window.global::<PrinterQueue>().set_printer_queue(printer_queue.data.clone().into());

main_window.on_quit(move || {
main_window.on_quit(move |_| {
#[cfg(not(target_arch = "wasm32"))]
slint::quit_event_loop().unwrap();
});

let printer_queue_copy = printer_queue.clone();
main_window.global::<PrinterQueue>().on_start_job(move |title| {
main_window.global::<PrinterQueue>().on_start_job(move |_, title| {
printer_queue_copy.push_job(title);
});

let printer_queue_copy = printer_queue.clone();
main_window.global::<PrinterQueue>().on_cancel_job(move |idx| {
main_window.global::<PrinterQueue>().on_cancel_job(move |_, idx| {
printer_queue_copy.data.remove(idx as usize);
});

Expand Down
10 changes: 3 additions & 7 deletions examples/printerdemo_old/rust/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@ pub fn main() {
InkLevel { color: slint::Color::from_rgb_u8(0, 0, 0), level: 0.80 },
]));

let main_weak = main_window.as_weak();
main_window.on_fax_number_erase(move || {
let main_window = main_weak.unwrap();
main_window.on_fax_number_erase(move |main_window| {
let mut fax_number = main_window.get_fax_number().to_string();
fax_number.pop();
main_window.set_fax_number(fax_number.into());
});

let main_weak = main_window.as_weak();
main_window.on_fax_send(move || {
let main_window = main_weak.upgrade().unwrap();
main_window.on_fax_send(move |main_window| {
let fax_number = main_window.get_fax_number().to_string();
println!("Sending a fax to {}", fax_number);
main_window.set_fax_number(slint::SharedString::default());
});

main_window.on_quit(move || {
main_window.on_quit(move |_| {
#[cfg(not(target_arch = "wasm32"))]
std::process::exit(0);
});
Expand Down
10 changes: 5 additions & 5 deletions examples/slide_puzzle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ pub fn main() {
main_window.set_pieces(state.borrow().pieces.clone().into());

let state_copy = state.clone();
main_window.on_piece_clicked(move |p| {
main_window.on_piece_clicked(move |main_window, p| {
state_copy.borrow().auto_play_timer.stop();
state_copy.borrow().main_window.unwrap().set_auto_play(false);
main_window.set_auto_play(false);
if state_copy.borrow().finished {
return;
}
Expand All @@ -206,14 +206,14 @@ pub fn main() {
});

let state_copy = state.clone();
main_window.on_reset(move || {
main_window.on_reset(move |main_window| {
state_copy.borrow().auto_play_timer.stop();
state_copy.borrow().main_window.unwrap().set_auto_play(false);
main_window.set_auto_play(false);
state_copy.borrow_mut().randomize();
});

let state_copy = state;
main_window.on_enable_auto_mode(move |enabled| {
main_window.on_enable_auto_mode(move |_, enabled| {
if enabled {
let state_weak = Rc::downgrade(&state_copy);
state_copy.borrow().auto_play_timer.start(
Expand Down
12 changes: 4 additions & 8 deletions examples/todo/rust/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ pub fn main() {
let main_window = MainWindow::new();
main_window.on_todo_added({
let todo_model = todo_model.clone();
move |text| todo_model.push(TodoItem { checked: false, title: text })
move |_, text| todo_model.push(TodoItem { checked: false, title: text })
});
main_window.on_remove_done({
let todo_model = todo_model.clone();
move || {
move |_| {
let mut offset = 0;
for i in 0..todo_model.row_count() {
if todo_model.row_data(i - offset).unwrap().checked {
Expand All @@ -45,9 +45,7 @@ pub fn main() {
}
});

let weak_window = main_window.as_weak();
main_window.on_popup_confirmed(move || {
let window = weak_window.unwrap();
main_window.on_popup_confirmed(move |window| {
window.hide();
});

Expand All @@ -67,11 +65,9 @@ pub fn main() {
}

main_window.on_apply_sorting_and_filtering({
let weak_window = main_window.as_weak();
let todo_model = todo_model.clone();

move || {
let window = weak_window.unwrap();
move |window| {
window.set_todo_model(todo_model.clone().into());

if window.get_hide_done_items() {
Expand Down
7 changes: 5 additions & 2 deletions internal/compiler/generator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ fn public_api(
ctx: &EvaluationContext,
) -> TokenStream {
let mut property_and_callback_accessors: Vec<TokenStream> = vec![];
let public_component_id = public_component_id(&ctx.public_component.item_tree.root);

for p in public_properties {
let prop_ident = ident(&p.name);
let prop = access_member(&p.prop, ctx);
Expand All @@ -552,12 +554,13 @@ fn public_api(
let args_index = (0..callback_args.len()).map(proc_macro2::Literal::usize_unsuffixed);
property_and_callback_accessors.push(quote!(
#[allow(dead_code)]
pub fn #on_ident(&self, mut f: impl FnMut(#(#callback_args),*) -> #return_type + 'static) {
pub fn #on_ident(&self, mut f: impl FnMut(&#public_component_id, #(#callback_args),*) -> #return_type + 'static) {
let _self = #self_init;
let root_weak = _self.root.get().unwrap().clone();
#[allow(unused)]
#prop.set_handler(
// FIXME: why do i need to clone here?
move |args| f(#(args.#args_index.clone()),*)
move |args| f(&#public_component_id(root_weak.upgrade().unwrap()), #(args.#args_index.clone()),*)
)
}
));
Expand Down