-
Notifications
You must be signed in to change notification settings - Fork 70
/
main.rs
170 lines (151 loc) · 5.93 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use winit::{
event::{self, WindowEvent},
event_loop::EventLoopBuilder,
keyboard::{KeyCode, PhysicalKey},
};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
fn main() {
let event_loop = EventLoopBuilder::<String>::with_user_event()
.build()
.expect("Failed");
let builder = winit::window::WindowBuilder::new();
let window = builder.build(&event_loop).unwrap();
#[cfg(target_arch = "wasm32")]
{
use winit::platform::web::WindowExtWebSys;
web_sys::window()
.and_then(|win| win.document())
.and_then(|doc| doc.body())
.and_then(|body| {
body.append_child(&web_sys::Element::from(window.canvas()))
.ok()
})
.expect("couldn't append canvas to document body");
}
let event_loop_proxy = event_loop.create_proxy();
let executor = Executor::new();
event_loop
.run(move |event, target| match event {
event::Event::UserEvent(name) => {
#[cfg(target_arch = "wasm32")]
alert(&name);
#[cfg(not(target_arch = "wasm32"))]
println!("{}", name);
}
event::Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested { .. } => target.exit(),
WindowEvent::KeyboardInput {
event:
event::KeyEvent {
state: event::ElementState::Pressed,
physical_key: PhysicalKey::Code(KeyCode::KeyS),
..
},
..
} => {
let dialog = rfd::AsyncFileDialog::new()
.add_filter("midi", &["mid", "midi"])
.add_filter("rust", &["rs", "toml"])
.set_parent(&window)
.save_file();
let event_loop_proxy = event_loop_proxy.clone();
executor.execut(async move {
let file = dialog.await;
let file = if let Some(file) = file {
file.write(b"Hi! This is a test file").await.unwrap();
Some(file)
} else {
None
};
event_loop_proxy
.send_event(format!("saved file name: {:#?}", file))
.ok();
});
}
WindowEvent::KeyboardInput {
event:
event::KeyEvent {
state: event::ElementState::Pressed,
physical_key: PhysicalKey::Code(KeyCode::KeyF),
..
},
..
} => {
let dialog = rfd::AsyncFileDialog::new()
.add_filter("midi", &["mid", "midi"])
.add_filter("rust", &["rs", "toml"])
.set_parent(&window)
.pick_file();
let event_loop_proxy = event_loop_proxy.clone();
executor.execut(async move {
let files = dialog.await;
// let names: Vec<String> = files.into_iter().map(|f| f.file_name()).collect();
let names = files;
event_loop_proxy.send_event(format!("{:#?}", names)).ok();
});
}
WindowEvent::DroppedFile(file_path) => {
let dialog = rfd::AsyncMessageDialog::new()
.set_title("File dropped")
.set_description(format!("file path was: {:#?}", file_path))
.set_buttons(rfd::MessageButtons::YesNo)
.set_parent(&window)
.show();
let event_loop_proxy = event_loop_proxy.clone();
executor.execut(async move {
let val = dialog.await;
event_loop_proxy.send_event(format!("Msg: {}", val)).ok();
});
}
WindowEvent::KeyboardInput {
event:
event::KeyEvent {
state: event::ElementState::Pressed,
physical_key: PhysicalKey::Code(KeyCode::KeyM),
..
},
..
} => {
let dialog = rfd::AsyncMessageDialog::new()
.set_title("Msg!")
.set_description("Description!")
.set_buttons(rfd::MessageButtons::YesNo)
.set_parent(&window)
.show();
let event_loop_proxy = event_loop_proxy.clone();
executor.execut(async move {
let val = dialog.await;
event_loop_proxy.send_event(format!("Msg: {}", val)).ok();
});
}
_ => {}
},
_ => {}
})
.unwrap();
}
use std::future::Future;
struct Executor {
#[cfg(not(target_arch = "wasm32"))]
pool: futures::executor::ThreadPool,
}
impl Executor {
fn new() -> Self {
Self {
#[cfg(not(target_arch = "wasm32"))]
pool: futures::executor::ThreadPool::new().unwrap(),
}
}
#[cfg(not(target_arch = "wasm32"))]
fn execut<F: Future<Output = ()> + Send + 'static>(&self, f: F) {
self.pool.spawn_ok(f);
}
#[cfg(target_arch = "wasm32")]
fn execut<F: Future<Output = ()> + 'static>(&self, f: F) {
wasm_bindgen_futures::spawn_local(f);
}
}