Skip to content

Commit

Permalink
refactor: use new clone macro syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
SeaDve committed Jul 13, 2024
1 parent 690a897 commit 7bd67e8
Show file tree
Hide file tree
Showing 19 changed files with 639 additions and 392 deletions.
12 changes: 8 additions & 4 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,14 @@ impl Application {
dialog.set_response_appearance(QUIT_RESPONSE_ID, adw::ResponseAppearance::Suggested);
dialog.connect_response(
Some(QUIT_RESPONSE_ID),
clone!(@weak self as obj => move |_, response| match response {
QUIT_RESPONSE_ID => obj.quit(),
_ => unreachable!(),
}),
clone!(
#[weak(rename_to = obj)]
self,
move |_, response| match response {
QUIT_RESPONSE_ID => obj.quit(),
_ => unreachable!(),
}
),
);
dialog.present(Some(parent));
}
Expand Down
9 changes: 6 additions & 3 deletions src/inspector_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ impl InspectorPage {
}
}),
)));
imp.provider_row
.connect_selected_notify(clone!(@weak self as obj => move |provider_row| {
imp.provider_row.connect_selected_notify(clone!(
#[weak(rename_to = obj)]
self,
move |provider_row| {
if let Some(ref item) = provider_row.selected_item() {
ProviderSettings::lock().active = item
.downcast_ref::<adw::EnumListItem>()
Expand All @@ -142,7 +144,8 @@ impl InspectorPage {
ProviderSettings::lock().active = ProviderType::default();
}
obj.update_test_rows_sensitivity();
}));
}
));

imp.test_provider_mode_row.set_selected(
imp.test_provider_mode_model
Expand Down
61 changes: 38 additions & 23 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,23 @@ mod imp {

let obj = self.obj();

let bus_watch_guard = self.gst_play
let bus_watch_guard = self
.gst_play
.message_bus()
.add_watch_local(
clone!(@weak obj => @default-return glib::ControlFlow::Continue, move |_, message| {
.add_watch_local(clone!(
#[weak]
obj,
#[upgrade_or_panic]
move |_, message| {
if gst_play::Play::is_play_message(message) {
let play_message = gst_play::PlayMessage::parse(message).unwrap();
obj.handle_gst_play_message(play_message);
} else {
tracing::trace!("Received other bus message: {:?}", message.view());
}
glib::ControlFlow::Continue
}),
)
}
))
.unwrap();
self.bus_watch_guard.set(bus_watch_guard).unwrap();
}
Expand Down Expand Up @@ -238,36 +242,47 @@ impl Player {
fn mpris_properties_changed(&self, property: impl IntoIterator<Item = Property> + 'static) {
utils::spawn(
glib::Priority::default(),
clone!(@weak self as obj => async move {
match obj.mpris_server().await {
Ok(server) => {
if let Err(err) = server.properties_changed(property).await {
tracing::error!("Failed to emit MPRIS properties changed: {:?}", err);
clone!(
#[weak(rename_to = obj)]
self,
async move {
match obj.mpris_server().await {
Ok(server) => {
if let Err(err) = server.properties_changed(property).await {
tracing::error!(
"Failed to emit MPRIS properties changed: {:?}",
err
);
}
}
Err(err) => {
tracing::error!("Failed to get MPRIS server: {:?}", err);
}
}
Err(err) => {
tracing::error!("Failed to get MPRIS server: {:?}", err);
}
}
}),
),
);
}

fn mpris_seeked(&self, position: Time) {
utils::spawn(
glib::Priority::default(),
clone!(@weak self as obj => async move {
match obj.mpris_server().await {
Ok(server) => {
if let Err(err) = server.emit(Signal::Seeked { position }).await {
tracing::error!("Failed to emit MPRIS seeked: {:?}", err);
clone!(
#[weak(rename_to = obj)]
self,
async move {
match obj.mpris_server().await {
Ok(server) => {
if let Err(err) = server.emit(Signal::Seeked { position }).await {
tracing::error!("Failed to emit MPRIS seeked: {:?}", err);
}
}
Err(err) => {
tracing::error!("Failed to get MPRIS server: {:?}", err);
}
}
Err(err) => {
tracing::error!("Failed to get MPRIS server: {:?}", err);
}
}
}),
),
);
}

Expand Down
23 changes: 13 additions & 10 deletions src/preferences_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,23 @@ impl PreferencesDialog {
])));
imp.audio_source_type_row
.set_selected(settings.audio_source_type().as_position());
imp.audio_source_type_row.connect_selected_notify(
clone!(@weak self as obj => move |provider_row| {
imp.audio_source_type_row.connect_selected_notify(clone!(
#[weak(rename_to = obj)]
self,
move |provider_row| {
obj.settings()
.set_audio_source_type(AudioSourceType::from_position(
provider_row.selected(),
));
}),
);
.set_audio_source_type(AudioSourceType::from_position(provider_row.selected()));
}
));

imp.aud_d_api_token_row
.set_text(&settings.aud_d_api_token());
imp.aud_d_api_token_row
.connect_apply(clone!(@weak self as obj => move |row| {
imp.aud_d_api_token_row.connect_apply(clone!(
#[weak(rename_to = obj)]
self,
move |row| {
obj.settings().set_aud_d_api_token(&row.text());
}));
}
));
}
}
58 changes: 37 additions & 21 deletions src/recognizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,22 @@ impl Recognizer {
.expect("saved recordings must be bound only once");

let network_monitor = gio::NetworkMonitor::default();
network_monitor.connect_connectivity_notify(clone!(@weak self as obj => move |monitor| {
tracing::debug!(connectivity = ?monitor.connectivity());

obj.update_offline_mode();

// TODO don't just call when network is available, but also for every
// interval if there is network, there are still saved recordings, and
// there is currently no recognition in progress.
//
// This should also be triggered when token is updated.
obj.try_recognize_saved_recordings();
}));
network_monitor.connect_connectivity_notify(clone!(
#[weak(rename_to = obj)]
self,
move |monitor| {
tracing::debug!(connectivity = ?monitor.connectivity());

obj.update_offline_mode();

// TODO don't just call when network is available, but also for every
// interval if there is network, there are still saved recordings, and
// there is currently no recognition in progress.
//
// This should also be triggered when token is updated.
obj.try_recognize_saved_recordings();
}
));

self.update_offline_mode();

Expand Down Expand Up @@ -250,16 +254,24 @@ impl Recognizer {
imp.recorder
.start(
Application::get().settings().audio_source_type(),
clone!(@weak self as obj => move |peak| {
obj.emit_recording_peak_changed(peak);
}),
clone!(
#[weak(rename_to = obj)]
self,
move |peak| {
obj.emit_recording_peak_changed(peak);
}
),
)
.context("Failed to start recording")?;
let recorded_time = DateTime::now_utc();

cancellable.connect_cancelled_local(clone!(@weak _finally => move |_| {
let _ = _finally.take();
}));
cancellable.connect_cancelled_local(clone!(
#[weak]
_finally,
move |_| {
let _ = _finally.take();
}
));

let provider = ProviderSettings::lock().active.to_provider();
let listen_duration = provider.listen_duration();
Expand Down Expand Up @@ -343,9 +355,13 @@ impl Recognizer {
// TODO recognize recordings concurrently, but not too many at once (at most 3?)
utils::spawn(
glib::Priority::default(),
clone!(@weak self as obj => async move {
obj.try_recognize_saved_recordings_inner().await;
}),
clone!(
#[weak(rename_to = obj)]
self,
async move {
obj.try_recognize_saved_recordings_inner().await;
}
),
);
}

Expand Down
11 changes: 6 additions & 5 deletions src/recognizer/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ impl Recorder {
let bus_watch_guard = pipeline
.bus()
.unwrap()
.add_watch_local(
clone!(@weak pipeline => @default-return glib::ControlFlow::Break, move |_, message| {
handle_bus_message(&pipeline, message, &peak_callback)
}),
)
.add_watch_local(clone!(
#[weak]
pipeline,
#[upgrade_or_panic]
move |_, message| handle_bus_message(&pipeline, message, &peak_callback)
))
.unwrap();
self.pipeline
.replace(Some((pipeline.clone(), bus_watch_guard, output_stream)));
Expand Down
46 changes: 26 additions & 20 deletions src/recognizer/recordings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,27 +206,33 @@ impl Recordings {
unsafe {
let handler_id = recording.connect_notify_local(
None,
clone!(@weak self as obj, @strong recording_id => move |recording, pspec| {
tracing::debug!("Recording property `{}` notified", pspec.name());

let (env, db) = obj.db();
if let Err(err) = env.with_write_txn(|wtxn| {
debug_assert!(
db.get(wtxn, &recording_id).unwrap().is_some(),
"recording must exist in the db"
);

db.put(wtxn, &recording_id, recording)
.context("Failed to put recording to db")?;

Ok(())
}) {
tracing::error!("Failed to update recording in database: {:?}", err);
clone!(
#[weak(rename_to = obj)]
self,
#[strong]
recording_id,
move |recording, pspec| {
tracing::debug!("Recording property `{}` notified", pspec.name());

let (env, db) = obj.db();
if let Err(err) = env.with_write_txn(|wtxn| {
debug_assert!(
db.get(wtxn, &recording_id).unwrap().is_some(),
"recording must exist in the db"
);

db.put(wtxn, &recording_id, recording)
.context("Failed to put recording to db")?;

Ok(())
}) {
tracing::error!("Failed to update recording in database: {:?}", err);
}

let index = obj.imp().list.borrow().get_index_of(&recording_id).unwrap();
obj.items_changed(index as u32, 1, 1);
}

let index = obj.imp().list.borrow().get_index_of(&recording_id).unwrap();
obj.items_changed(index as u32, 1, 1);
}),
),
);
recording.set_data(RECORDING_NOTIFY_HANDLER_ID_KEY, handler_id);
}
Expand Down
40 changes: 22 additions & 18 deletions src/song_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,28 @@ impl SongList {
unsafe {
let handler_id = song.connect_notify_local(
None,
clone!(@weak self as obj => move |song, pspec| {
tracing::debug!("Song property `{}` notified", pspec.name());

let (env, db) = obj.db();
if let Err(err) = env.with_write_txn(|wtxn| {
debug_assert!(
db.get(wtxn, song.id_ref()).unwrap().is_some(),
"song must exist in the db"
);

db.put(wtxn, song.id_ref(), song)
.context("Failed to put song to db")?;

Ok(())
}) {
tracing::error!("Failed to update song in database: {:?}", err);
};
}),
clone!(
#[weak(rename_to = obj)]
self,
move |song, pspec| {
tracing::debug!("Song property `{}` notified", pspec.name());

let (env, db) = obj.db();
if let Err(err) = env.with_write_txn(|wtxn| {
debug_assert!(
db.get(wtxn, song.id_ref()).unwrap().is_some(),
"song must exist in the db"
);

db.put(wtxn, song.id_ref(), song)
.context("Failed to put song to db")?;

Ok(())
}) {
tracing::error!("Failed to update song in database: {:?}", err);
};
}
),
);
song.set_data(SONG_NOTIFY_HANDLER_ID_KEY, handler_id);
}
Expand Down
Loading

0 comments on commit 7bd67e8

Please sign in to comment.