@@ -15,7 +15,7 @@ use crate::{
1515 icons,
1616 scene:: {
1717 MouseToMidiEventState , NuonRenderer , Scene ,
18- freeplay:: recorder:: { FreeplayRecorder , PreviewState } ,
18+ freeplay:: recorder:: { FreeplayRecorder , PreviewState , RecorderError , RecorderStatus } ,
1919 playing_scene:: { Keyboard , midi_player:: MidiPlayer } ,
2020 } ,
2121 song:: Song ,
@@ -56,7 +56,7 @@ pub struct FreeplayScene {
5656 mouse_to_midi_state : MouseToMidiEventState ,
5757 deduced_chord_name : String ,
5858 recorder : FreeplayRecorder ,
59- recorder_status : String ,
59+ recorder_status : RecorderStatus ,
6060 preview_state : Option < PreviewState > ,
6161
6262 context : std:: task:: Context < ' static > ,
@@ -102,7 +102,7 @@ impl FreeplayScene {
102102 mouse_to_midi_state : MouseToMidiEventState :: default ( ) ,
103103 deduced_chord_name : String :: new ( ) ,
104104 recorder : FreeplayRecorder :: default ( ) ,
105- recorder_status : String :: new ( ) ,
105+ recorder_status : RecorderStatus :: default ( ) ,
106106 preview_state : None ,
107107
108108 context : std:: task:: Context :: from_waker ( noop_waker_ref ( ) ) ,
@@ -197,7 +197,7 @@ impl FreeplayScene {
197197 self . keyboard . reset_notes ( ) ;
198198 }
199199
200- fn rebuild_preview ( & mut self , ctx : & Context ) -> Result < ( ) , String > {
200+ fn rebuild_preview ( & mut self , ctx : & Context ) -> Result < ( ) , RecorderError > {
201201 self . clear_preview ( ) ;
202202
203203 let song = self . recorder . to_song ( ) ?;
@@ -269,15 +269,7 @@ impl FreeplayScene {
269269 self . keyboard . reset_notes ( ) ;
270270 }
271271
272- fn preview_status_label ( & self ) -> String {
273- if self . recorder . is_recording ( ) {
274- return format ! ( "Recording {:.1}s" , self . recorder. duration( ) . as_secs_f32( ) , ) ;
275- }
276-
277- self . recorder_status . clone ( )
278- }
279-
280- fn stop_recording ( & mut self , ctx : & Context ) -> Result < ( ) , String > {
272+ fn stop_recording ( & mut self , ctx : & Context ) -> Result < ( ) , RecorderError > {
281273 self . recorder . stop ( ) ;
282274 self . rebuild_preview ( ctx)
283275 }
@@ -287,33 +279,30 @@ impl FreeplayScene {
287279 match self . stop_recording ( ctx) {
288280 Ok ( ( ) ) => {
289281 self . recorder_status =
290- format ! ( "Recorded {:.1}s" , self . recorder. duration( ) . as_secs_f32 ( ) ) ;
282+ RecorderStatus :: RecordingFinished ( self . recorder . duration ( ) ) ;
291283 }
292284 Err ( err) => {
293- self . recorder_status = err;
285+ self . recorder_status = RecorderStatus :: Error ( err) ;
294286 }
295287 }
296288
297289 return ;
298290 }
299291
300292 self . clear_preview ( ) ;
293+ self . recorder_status = RecorderStatus :: default ( ) ;
301294 self . recorder . start ( ) ;
302- self . recorder_status = "Recording in progress" . to_string ( ) ;
303295 }
304296
305297 fn handle_save_click ( & mut self , ctx : & Context ) {
306298 if self . recorder . is_recording ( )
307299 && let Err ( err) = self . stop_recording ( ctx)
308300 {
309- self . recorder_status = err;
301+ self . recorder_status = RecorderStatus :: Error ( err) ;
310302 return ;
311303 }
312304
313- if !self . recorder . has_note_events ( ) {
314- self . recorder_status = "Nothing recorded yet" . to_string ( ) ;
315- return ;
316- }
305+ debug_assert ! ( self . recorder. has_note_events( ) ) ;
317306
318307 let mut dialog = rfd:: AsyncFileDialog :: new ( )
319308 . add_filter ( "midi" , & [ "mid" , "midi" ] )
@@ -326,28 +315,26 @@ impl FreeplayScene {
326315 let smf = match self . recorder . to_smf ( ) {
327316 Ok ( smf) => smf,
328317 Err ( err) => {
329- self . recorder_status = err;
318+ self . recorder_status = RecorderStatus :: Error ( err) ;
330319 return ;
331320 }
332321 } ;
333322
334- self . futures . push ( on_async (
335- dialog. save_file ( ) ,
336- |path, state, _ctx| match path {
337- Some ( file) => match FreeplayRecorder :: save_to_path ( smf, file. path ( ) ) {
323+ self . futures
324+ . push ( on_async ( dialog. save_file ( ) , |file, state, _ctx| {
325+ let Some ( file) = file else {
326+ return ;
327+ } ;
328+
329+ match FreeplayRecorder :: save_to_path ( smf, file. path ( ) ) {
338330 Ok ( ( ) ) => {
339- state. recorder_status =
340- format ! ( "Saved recording to {}" , file. path( ) . display( ) ) ;
331+ state. recorder_status = RecorderStatus :: Saved ( file. path ( ) . to_owned ( ) ) ;
341332 }
342333 Err ( err) => {
343- state. recorder_status = err;
334+ state. recorder_status = RecorderStatus :: Error ( err) ;
344335 }
345- } ,
346- None => {
347- state. recorder_status = "Save canceled" . to_string ( ) ;
348336 }
349- } ,
350- ) ) ;
337+ } ) ) ;
351338 }
352339
353340 fn update_preview ( & mut self , ctx : & Context , delta : Duration ) -> Option < f32 > {
@@ -376,7 +363,11 @@ impl FreeplayScene {
376363 . map ( |s| s. player . is_paused ( ) )
377364 . unwrap_or ( true ) ;
378365
379- let status_label = self . preview_status_label ( ) ;
366+ let status_label = if self . recorder . is_recording ( ) {
367+ format ! ( "Recording {:.1}s" , self . recorder. duration( ) . as_secs_f32( ) )
368+ } else {
369+ self . recorder_status . to_string ( )
370+ } ;
380371
381372 enum Msg {
382373 TogglePlay ,
0 commit comments