@@ -10,7 +10,10 @@ use neothesia_iced_widgets::{BarLayout, Layout, NeoBtn};
10
10
11
11
use crate :: { context:: Context , scene:: menu_scene:: icons, song:: Song } ;
12
12
13
- use super :: { page:: Page , top_padded, Data , Message , Step } ;
13
+ use super :: {
14
+ page:: { Page , PageMessage } ,
15
+ top_padded, Data , Message , Step ,
16
+ } ;
14
17
15
18
#[ derive( Debug , Clone ) ]
16
19
pub enum Event {
@@ -24,20 +27,24 @@ pub struct MainPage;
24
27
impl Page for MainPage {
25
28
type Event = Event ;
26
29
27
- fn update ( data : & mut Data , msg : Self :: Event , ctx : & mut Context ) -> Command < Message > {
30
+ fn update ( data : & mut Data , msg : Self :: Event , ctx : & mut Context ) -> PageMessage {
28
31
match msg {
29
32
Event :: Play => {
30
33
super :: play ( data, ctx) ;
31
34
}
32
35
Event :: GoToPage ( step) => {
33
- return Command :: perform ( async { } , move |_| Message :: GoToPage ( step) ) ;
36
+ return PageMessage :: go_to_page ( step) ;
34
37
}
35
38
Event :: MidiFilePicker ( msg) => {
36
- return midi_file_picker_update ( data, msg, ctx) ;
39
+ return PageMessage :: Command (
40
+ midi_file_picker_update ( data, msg, ctx)
41
+ . map ( Event :: MidiFilePicker )
42
+ . map ( Message :: MainPage ) ,
43
+ ) ;
37
44
}
38
45
} ;
39
46
40
- Command :: none ( )
47
+ PageMessage :: None
41
48
}
42
49
43
50
fn view < ' a > ( data : & ' a Data , ctx : & Context ) -> neothesia_iced_widgets:: Element < ' a , Self :: Event > {
@@ -154,19 +161,23 @@ impl MidiFilePickerMessage {
154
161
155
162
impl From < MidiFilePickerMessage > for Message {
156
163
fn from ( msg : MidiFilePickerMessage ) -> Self {
157
- Message :: MainPage ( super :: main :: Event :: MidiFilePicker ( msg) )
164
+ Message :: MainPage ( Event :: MidiFilePicker ( msg) )
158
165
}
159
166
}
160
167
161
168
fn midi_file_picker_update (
162
169
data : & mut Data ,
163
170
msg : MidiFilePickerMessage ,
164
171
ctx : & mut Context ,
165
- ) -> Command < Message > {
172
+ ) -> Command < MidiFilePickerMessage > {
166
173
match msg {
167
174
MidiFilePickerMessage :: OpenMidiFilePicker => {
168
175
data. is_loading = true ;
169
- return open_midi_file_picker ( |v| MidiFilePickerMessage :: MidiFileLoaded ( v) . into ( ) ) ;
176
+
177
+ return Command :: perform (
178
+ open_midi_file_picker ( ) ,
179
+ MidiFilePickerMessage :: MidiFileLoaded ,
180
+ ) ;
170
181
}
171
182
MidiFilePickerMessage :: MidiFileLoaded ( midi) => {
172
183
if let Some ( ( midi, path) ) = midi {
@@ -180,41 +191,34 @@ fn midi_file_picker_update(
180
191
Command :: none ( )
181
192
}
182
193
183
- fn open_midi_file_picker (
184
- f : impl FnOnce ( Option < ( midi_file:: MidiFile , PathBuf ) > ) -> Message + ' static + Send ,
185
- ) -> Command < Message > {
186
- Command :: perform (
187
- async {
188
- let file = rfd:: AsyncFileDialog :: new ( )
189
- . add_filter ( "midi" , & [ "mid" , "midi" ] )
190
- . pick_file ( )
191
- . await ;
192
-
193
- if let Some ( file) = file {
194
- log:: info!( "File path = {:?}" , file. path( ) ) ;
195
-
196
- let thread = async_thread:: Builder :: new ( )
197
- . name ( "midi-loader" . into ( ) )
198
- . spawn ( move || {
199
- let midi = midi_file:: MidiFile :: new ( file. path ( ) ) ;
200
-
201
- if let Err ( e) = & midi {
202
- log:: error!( "{}" , e) ;
203
- }
204
-
205
- midi. map ( |midi| ( midi, file. path ( ) . to_path_buf ( ) ) ) . ok ( )
206
- } ) ;
207
-
208
- if let Ok ( thread) = thread {
209
- thread. join ( ) . await . ok ( ) . flatten ( )
210
- } else {
211
- None
194
+ async fn open_midi_file_picker ( ) -> Option < ( midi_file:: MidiFile , PathBuf ) > {
195
+ let file = rfd:: AsyncFileDialog :: new ( )
196
+ . add_filter ( "midi" , & [ "mid" , "midi" ] )
197
+ . pick_file ( )
198
+ . await ;
199
+
200
+ if let Some ( file) = file {
201
+ log:: info!( "File path = {:?}" , file. path( ) ) ;
202
+
203
+ let thread = async_thread:: Builder :: new ( )
204
+ . name ( "midi-loader" . into ( ) )
205
+ . spawn ( move || {
206
+ let midi = midi_file:: MidiFile :: new ( file. path ( ) ) ;
207
+
208
+ if let Err ( e) = & midi {
209
+ log:: error!( "{}" , e) ;
212
210
}
213
- } else {
214
- log:: info!( "User canceled dialog" ) ;
215
- None
216
- }
217
- } ,
218
- f,
219
- )
211
+
212
+ midi. map ( |midi| ( midi, file. path ( ) . to_path_buf ( ) ) ) . ok ( )
213
+ } ) ;
214
+
215
+ if let Ok ( thread) = thread {
216
+ thread. join ( ) . await . ok ( ) . flatten ( )
217
+ } else {
218
+ None
219
+ }
220
+ } else {
221
+ log:: info!( "User canceled dialog" ) ;
222
+ None
223
+ }
220
224
}
0 commit comments