Skip to content

Commit

Permalink
Use weak references in closures / callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
fengalin committed Aug 26, 2020
1 parent 30b5305 commit ebebec1
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 66 deletions.
41 changes: 22 additions & 19 deletions ui/src/audio_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl UIDispatcher for AudioDispatcher {
) {
// draw
audio_ctrl.drawingarea.connect_draw(clone!(
@strong main_ctrl_rc => move |drawing_area, cairo_ctx| {
@weak main_ctrl_rc => @default-panic, move |drawing_area, cairo_ctx| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
if let Some(position) = main_ctrl.audio_ctrl.draw(drawing_area, cairo_ctx) {
main_ctrl.refresh_info(position);
Expand All @@ -37,8 +37,9 @@ impl UIDispatcher for AudioDispatcher {

// widget size changed
audio_ctrl.drawingarea.connect_size_allocate(
clone!(@strong main_ctrl_rc => move |_, alloc| {
if let Ok(mut main_ctrl) = main_ctrl_rc.try_borrow_mut() {
clone!(@weak main_ctrl_rc => move |_, alloc| {
let main_ctrl = main_ctrl_rc.try_borrow_mut();
if let Ok(mut main_ctrl) = main_ctrl {
let mut audio_ctrl = &mut main_ctrl.audio_ctrl;
audio_ctrl.area_height = f64::from(alloc.height);
audio_ctrl.area_width = f64::from(alloc.width);
Expand All @@ -49,7 +50,7 @@ impl UIDispatcher for AudioDispatcher {

// Move cursor over drawing_area
audio_ctrl.drawingarea.connect_motion_notify_event(
clone!(@strong main_ctrl_rc => move |_, event_motion| {
clone!(@weak main_ctrl_rc => @default-panic, move |_, event_motion| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
if let Some((boundary, position)) =
main_ctrl.audio_ctrl.motion_notify(&event_motion)
Expand All @@ -67,8 +68,9 @@ impl UIDispatcher for AudioDispatcher {

// Leave drawing_area
audio_ctrl.drawingarea.connect_leave_notify_event(
clone!(@strong main_ctrl_rc => move |_, _event_crossing| {
match main_ctrl_rc.try_borrow_mut() {
clone!(@weak main_ctrl_rc => @default-panic, move |_, _event_crossing| {
let main_ctrl = main_ctrl_rc.try_borrow_mut();
match main_ctrl {
Ok(mut main_ctrl) => {
main_ctrl.audio_ctrl.leave_drawing_area();
Inhibit(true)
Expand All @@ -80,7 +82,7 @@ impl UIDispatcher for AudioDispatcher {

// button press in drawing_area
audio_ctrl.drawingarea.connect_button_press_event(
clone!(@strong main_ctrl_rc => move |_, event_button| {
clone!(@weak main_ctrl_rc => @default-panic, move |_, event_button| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
main_ctrl.audio_ctrl.button_pressed(event_button);
Inhibit(true)
Expand All @@ -89,7 +91,7 @@ impl UIDispatcher for AudioDispatcher {

// button release in drawing_area
audio_ctrl.drawingarea.connect_button_release_event(
clone!(@strong main_ctrl_rc => move |_, event_button| {
clone!(@weak main_ctrl_rc => @default-panic, move |_, event_button| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
main_ctrl.audio_ctrl.button_released(event_button);
Inhibit(true)
Expand All @@ -100,7 +102,7 @@ impl UIDispatcher for AudioDispatcher {
app.add_action(&audio_ctrl.zoom_in_action);
audio_ctrl
.zoom_in_action
.connect_activate(clone!(@strong main_ctrl_rc => move |_, _| {
.connect_activate(clone!(@weak main_ctrl_rc => move |_, _| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
main_ctrl.audio_ctrl.zoom_in()
}));
Expand All @@ -109,28 +111,28 @@ impl UIDispatcher for AudioDispatcher {
app.add_action(&audio_ctrl.zoom_out_action);
audio_ctrl
.zoom_out_action
.connect_activate(clone!(@strong main_ctrl_rc => move |_, _| {
.connect_activate(clone!(@weak main_ctrl_rc => move |_, _| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
main_ctrl.audio_ctrl.zoom_out()
}));

// Register Step forward action
app.add_action(&audio_ctrl.step_forward_action);
audio_ctrl.step_forward_action.connect_activate(
clone!(@strong main_ctrl_rc => move |_, _| {
audio_ctrl
.step_forward_action
.connect_activate(clone!(@weak main_ctrl_rc => move |_, _| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
if let Some(current_ts) = main_ctrl.current_ts() {
let seek_target = current_ts + main_ctrl.audio_ctrl.seek_step;
main_ctrl.seek(seek_target, gst::SeekFlags::ACCURATE);
}
}),
);
}));

// Register Step back action
app.add_action(&audio_ctrl.step_back_action);
audio_ctrl
.step_back_action
.connect_activate(clone!(@strong main_ctrl_rc => move |_, _| {
.connect_activate(clone!(@weak main_ctrl_rc => move |_, _| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
if let Some(ts) = main_ctrl.current_ts() {
let seek_pos = {
Expand All @@ -146,18 +148,19 @@ impl UIDispatcher for AudioDispatcher {
}));

// Update conditions asynchronously
audio_ctrl.update_conditions_async =
Some(Box::new(clone!(@strong main_ctrl_rc => move || {
audio_ctrl.update_conditions_async = Some(Box::new(
clone!(@weak main_ctrl_rc => @default-panic, move || {
let main_ctrl_rc = Rc::clone(&main_ctrl_rc);
async move {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
main_ctrl.audio_ctrl.update_conditions();
}.boxed_local()
})));
}),
));

// Tick callback
audio_ctrl.tick_cb = Some(Rc::new(
clone!(@strong main_ctrl_rc => move |_da, _frame_clock| {
clone!(@weak main_ctrl_rc => move |_da, _frame_clock| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
main_ctrl.audio_ctrl.tick();
}),
Expand Down
13 changes: 6 additions & 7 deletions ui/src/info_bar_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ impl InfoBarController {
self.close_info_bar_action
.connect_activate(move |_, _| info_bar.emit_close());
} else {
self.close_info_bar_action.connect_activate(
clone!(@strong main_ctrl_rc => move |_, _| {
main_ctrl_rc.borrow_mut().quit()
}),
);
self.close_info_bar_action
.connect_activate(clone!(@weak main_ctrl_rc => move |_, _| {
main_ctrl_rc.borrow_mut().quit();
}));

self.info_bar
.connect_response(clone!(@strong main_ctrl_rc => move |_, _| {
main_ctrl_rc.borrow_mut().quit()
.connect_response(clone!(@weak main_ctrl_rc => move |_, _| {
main_ctrl_rc.borrow_mut().quit();
}));
}
}
Expand Down
26 changes: 13 additions & 13 deletions ui/src/info_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl UIDispatcher for InfoDispatcher {
});

info_ctrl.show_chapters_btn.connect_toggled(clone!(
@strong main_ctrl_rc => move |toggle_button| {
@weak main_ctrl_rc => move |toggle_button| {
let main_ctrl = main_ctrl_rc.borrow();
let info_ctrl = &main_ctrl.info_ctrl;
if toggle_button.get_active() {
Expand All @@ -47,7 +47,7 @@ impl UIDispatcher for InfoDispatcher {

// Draw thumnail image
info_ctrl.drawingarea.connect_draw(clone!(
@strong main_ctrl_rc => move |drawingarea, cairo_ctx| {
@weak main_ctrl_rc => @default-panic, move |drawingarea, cairo_ctx| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
main_ctrl.info_ctrl.draw_thumbnail(drawingarea, cairo_ctx);
Inhibit(true)
Expand All @@ -56,7 +56,7 @@ impl UIDispatcher for InfoDispatcher {

// Scale seek
info_ctrl.timeline_scale.connect_change_value(
clone!(@strong main_ctrl_rc => move |_, _, value| {
clone!(@weak main_ctrl_rc => @default-panic, move |_, _, value| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
main_ctrl.seek((value as u64).into(), gst::SeekFlags::KEY_UNIT);
Inhibit(true)
Expand All @@ -65,7 +65,7 @@ impl UIDispatcher for InfoDispatcher {

// TreeView seek
info_ctrl.chapter_treeview.connect_row_activated(
clone!(@strong main_ctrl_rc => move |_, tree_path, _| {
clone!(@weak main_ctrl_rc => move |_, tree_path, _| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
let info_ctrl = &mut main_ctrl.info_ctrl;
if let Some(chapter) = &info_ctrl.chapter_manager.chapter_from_path(tree_path) {
Expand All @@ -87,7 +87,7 @@ impl UIDispatcher for InfoDispatcher {
});

title_renderer.connect_edited(clone!(
@strong main_ctrl_rc => move |_, _tree_path, new_title| {
@weak main_ctrl_rc => move |_, _tree_path, new_title| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
main_ctrl
.info_ctrl
Expand All @@ -104,7 +104,7 @@ impl UIDispatcher for InfoDispatcher {
app.add_action(&info_ctrl.add_chapter_action);
info_ctrl
.add_chapter_action
.connect_activate(clone!(@strong main_ctrl_rc => move |_, _| {
.connect_activate(clone!(@weak main_ctrl_rc => move |_, _| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
if let Some(ts) = main_ctrl.current_ts() {
main_ctrl.info_ctrl.add_chapter(ts);
Expand All @@ -116,7 +116,7 @@ impl UIDispatcher for InfoDispatcher {
app.add_action(&info_ctrl.del_chapter_action);
info_ctrl
.del_chapter_action
.connect_activate(clone!(@strong main_ctrl_rc => move |_, _| {
.connect_activate(clone!(@weak main_ctrl_rc => move |_, _| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
main_ctrl.info_ctrl.remove_chapter();
main_ctrl.ui_event().update_focus();
Expand All @@ -132,15 +132,16 @@ impl UIDispatcher for InfoDispatcher {

info_ctrl
.repeat_btn
.connect_clicked(clone!(@strong main_ctrl_rc => move |button| {
.connect_clicked(clone!(@weak main_ctrl_rc => move |button| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
main_ctrl.info_ctrl.repeat_chapter = button.get_active();
}));

// Register next chapter action
app.add_action(&info_ctrl.next_chapter_action);
info_ctrl.next_chapter_action.connect_activate(
clone!(@strong main_ctrl_rc => move |_, _| {
info_ctrl
.next_chapter_action
.connect_activate(clone!(@weak main_ctrl_rc => move |_, _| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
let seek_pos = main_ctrl
.info_ctrl
Expand All @@ -151,13 +152,12 @@ impl UIDispatcher for InfoDispatcher {
if let Some(seek_pos) = seek_pos {
main_ctrl.seek(seek_pos, gst::SeekFlags::ACCURATE);
}
}),
);
}));

// Register previous chapter action
app.add_action(&info_ctrl.previous_chapter_action);
info_ctrl.previous_chapter_action.connect_activate(clone!(
@strong main_ctrl_rc => move |_, _| {
@weak main_ctrl_rc => move |_, _| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
if let Some(cur_ts) = main_ctrl.current_ts() {
let cur_start = main_ctrl
Expand Down
23 changes: 12 additions & 11 deletions ui/src/main_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl MainDispatcher {
// About
let about = gio::SimpleAction::new("about", None);
app.add_action(&about);
about.connect_activate(clone!(@strong main_ctrl_rc => move |_, _| {
about.connect_activate(clone!(@weak main_ctrl_rc => move |_, _| {
main_ctrl_rc.borrow().about();
}));
app.set_accels_for_action("app.about", &["<Ctrl>A"]);
Expand All @@ -44,23 +44,23 @@ impl MainDispatcher {
// Quit
let quit = gio::SimpleAction::new("quit", None);
app.add_action(&quit);
quit.connect_activate(clone!(@strong main_ctrl_rc => move |_, _| {
quit.connect_activate(clone!(@weak main_ctrl_rc => move |_, _| {
main_ctrl_rc.borrow_mut().quit();
}));
app.set_accels_for_action("app.quit", &["<Ctrl>Q"]);
app_section.append(Some(&gettext("Quit")), Some("app.quit"));

main_ctrl
.window
.connect_delete_event(clone!(@strong main_ctrl_rc => move |_, _| {
main_ctrl.window.connect_delete_event(
clone!(@weak main_ctrl_rc => @default-panic, move |_, _| {
main_ctrl_rc.borrow_mut().quit();
Inhibit(false)
}));
}),
);

let ui_event = main_ctrl.ui_event().clone();
if gstreamer::init().is_ok() {
main_ctrl.new_media_event_handler =
Some(Box::new(clone!(@strong main_ctrl_rc => move |receiver| {
main_ctrl.new_media_event_handler = Some(Box::new(
clone!(@weak main_ctrl_rc => @default-panic, move |receiver| {
let main_ctrl_rc = Rc::clone(&main_ctrl_rc);
async move {
let mut receiver = receiver;
Expand All @@ -73,7 +73,8 @@ impl MainDispatcher {
}
debug!("Media event handler terminated");
}.boxed_local()
})));
}),
));

let ui_event_clone = ui_event.clone();
let _ = PlaybackPipeline::check_requirements()
Expand All @@ -85,7 +86,7 @@ impl MainDispatcher {
// Register Open action
let open = gio::SimpleAction::new("open", None);
app.add_action(&open);
open.connect_activate(clone!(@strong main_ctrl_rc => move |_, _| {
open.connect_activate(clone!(@weak main_ctrl_rc => move |_, _| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
match main_ctrl.state {
ControllerState::Playing | ControllerState::EOS => {
Expand All @@ -103,7 +104,7 @@ impl MainDispatcher {
// Register Play/Pause action
let play_pause = gio::SimpleAction::new("play_pause", None);
app.add_action(&play_pause);
play_pause.connect_activate(clone!(@strong main_ctrl_rc => move |_, _| {
play_pause.connect_activate(clone!(@weak main_ctrl_rc => move |_, _| {
main_ctrl_rc.borrow_mut().play_pause();
}));
main_ctrl.play_pause_btn.set_sensitive(true);
Expand Down
30 changes: 16 additions & 14 deletions ui/src/output_base_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ where
ui_event_clone.switch_to(Impl::CtrlImpl::FOCUS_CONTEXT);
});

ctrl.btn.connect_clicked(clone!(@strong main_ctrl_rc => move |_| {
ctrl.btn.connect_clicked(clone!(@weak main_ctrl_rc => move |_| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
main_ctrl.pause_and_callback(Box::new(|main_ctrl: &mut MainController| {
if !Impl::ctrl_mut(main_ctrl).is_busy {
Expand All @@ -75,7 +75,7 @@ where
}));

ctrl.new_media_event_handler = Some(Box::new(
clone!(@strong main_ctrl_rc => move |receiver| {
clone!(@weak main_ctrl_rc => @default-panic, move |receiver| {
let main_ctrl_rc = Rc::clone(&main_ctrl_rc);
async move {
let mut receiver = receiver;
Expand All @@ -96,22 +96,24 @@ where
}),
));

ctrl.new_progress_updater = Some(Box::new(clone!(@strong main_ctrl_rc => move || {
let main_ctrl_rc = Rc::clone(&main_ctrl_rc);
async move {
let mut stream = glib::interval_stream(PROGRESS_TIMER_PERIOD);
loop {
let _ = stream.next().await;
if Impl::ctrl_ref_mut(&main_ctrl_rc).update_progress().is_err() {
break;
ctrl.new_progress_updater = Some(Box::new(
clone!(@weak main_ctrl_rc => @default-panic, move || {
let main_ctrl_rc = Rc::clone(&main_ctrl_rc);
async move {
let mut stream = glib::interval_stream(PROGRESS_TIMER_PERIOD);
loop {
let _ = stream.next().await;
if Impl::ctrl_ref_mut(&main_ctrl_rc).update_progress().is_err() {
break;
}
}
}
}.boxed_local()
})));
}.boxed_local()
}),
));

let btn = ctrl.btn.clone();
ctrl.new_processing_state_handler = Some(Box::new(clone!(
@strong main_ctrl_rc, @strong ui_event => move |state| {
@weak main_ctrl_rc, @strong ui_event => @default-panic, move |state| {
let main_ctrl_rc = Rc::clone(&main_ctrl_rc);
let ui_event = ui_event.clone();
let btn = btn.clone();
Expand Down
2 changes: 1 addition & 1 deletion ui/src/streams_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ macro_rules! register_on_export_toggled(
.unwrap()
.downcast::<gtk::CellRendererToggle>()
.expect("Unexpected `CellRenderer` type for `export` column")
.connect_toggled(clone!(@strong main_ctrl_rc => move |_, tree_path| {
.connect_toggled(clone!(@weak main_ctrl_rc => move |_, tree_path| {
let mut main_ctrl = main_ctrl_rc.borrow_mut();
let store = main_ctrl.streams_ctrl.$store.clone();

Expand Down
2 changes: 1 addition & 1 deletion ui/src/video_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl UIDispatcher for VideoDispatcher {
.set_events(gdk::EventMask::BUTTON_PRESS_MASK);

video_ctrl.container.connect_button_press_event(
clone!(@strong main_ctrl_rc => move |_, _| {
clone!(@weak main_ctrl_rc => @default-panic, move |_, _| {
main_ctrl_rc.borrow_mut().play_pause();
Inhibit(true)
}),
Expand Down

0 comments on commit ebebec1

Please sign in to comment.