1use makepad_platform::*;
2use crate::{Cx2d, CxDraw};
3
4pub trait MatchEvent{
5 fn handle_startup(&mut self, _cx: &mut Cx){}
6 fn handle_shutdown(&mut self, _cx: &mut Cx){}
7 fn handle_foreground(&mut self, _cx: &mut Cx){}
8 fn handle_background(&mut self, _cx: &mut Cx){}
9 fn handle_pause(&mut self, _cx: &mut Cx){}
10 fn handle_resume(&mut self, _cx: &mut Cx){}
11 fn handle_app_got_focus(&mut self, _cx: &mut Cx){}
12 fn handle_app_lost_focus(&mut self, _cx: &mut Cx){}
13 fn handle_next_frame(&mut self, _cx: &mut Cx, _e:&NextFrameEvent){}
14 fn handle_action(&mut self, _cx: &mut Cx, _e:&Action){}
15 fn handle_actions(&mut self, cx: &mut Cx, actions:&Actions){
16 for action in actions{
17 self.handle_action(cx, action);
18 }
19 }
20 fn handle_signal(&mut self, _cx: &mut Cx){}
21 fn handle_audio_devices(&mut self, _cx: &mut Cx, _e:&AudioDevicesEvent){}
22 fn handle_midi_ports(&mut self, _cx: &mut Cx, _e:&MidiPortsEvent){}
23 fn handle_video_inputs(&mut self, _cx: &mut Cx, _e:&VideoInputsEvent){}
24
25 fn handle_http_response(&mut self, _cx:&mut Cx, _request_id:LiveId, _response:&HttpResponse){}
26 fn handle_http_request_error(&mut self, _cx:&mut Cx, _request_id:LiveId, _err:&HttpError){}
27 fn handle_http_progress(&mut self, _cx:&mut Cx, _request_id:LiveId, _progress:&HttpProgress){}
28 fn handle_http_stream(&mut self, _cx:&mut Cx, _request_id:LiveId, _data:&HttpResponse){}
29 fn handle_http_stream_complete(&mut self, _cx:&mut Cx, _request_id:LiveId, _data:&HttpResponse){}
30
31 fn handle_network_responses(&mut self, cx: &mut Cx, e:&NetworkResponsesEvent ){
32 for e in e{
33 match &e.response{
34 NetworkResponse::HttpRequestError(err)=>{
35 self.handle_http_request_error(cx, e.request_id, err);
36 }
37 NetworkResponse::HttpResponse(res)=>{
38 self.handle_http_response(cx, e.request_id, res);
39 }
40 NetworkResponse::HttpProgress(progress)=>{
41 self.handle_http_progress(cx, e.request_id, progress);
42 }
43 NetworkResponse::HttpStreamResponse(data)=>{
44 self.handle_http_stream(cx, e.request_id, data);
45 }
46 NetworkResponse::HttpStreamComplete(res)=>{
47 self.handle_http_stream_complete(cx, e.request_id, res);
48 }
49 }
50 }
51 }
52
53 fn handle_draw(&mut self, _cx: &mut Cx, _e:&DrawEvent){}
54 fn handle_timer(&mut self, _cx: &mut Cx, _e:&TimerEvent){}
55 fn handle_draw_2d(&mut self, _cx: &mut Cx2d){}
56 fn handle_key_down(&mut self, _cx: &mut Cx, _e:&KeyEvent){}
57 fn handle_key_up(&mut self, _cx: &mut Cx, _e:&KeyEvent){}
58 fn handle_back_pressed(&mut self, _cx: &mut Cx) -> bool { false }
66
67 fn match_event(&mut self, cx:&mut Cx, event:&Event){
68 match event{
69 Event::Startup=>self.handle_startup(cx),
70 Event::Shutdown=>self.handle_shutdown(cx),
71 Event::Foreground=>self.handle_foreground(cx),
72 Event::Background=>self.handle_background(cx),
73 Event::Pause=>self.handle_pause(cx),
74 Event::Resume=>self.handle_resume(cx),
75 Event::Signal=>self.handle_signal(cx),
76 Event::AppGotFocus=>self.handle_app_got_focus(cx),
77 Event::Timer(te)=>self.handle_timer(cx, te),
78 Event::AppLostFocus=>self.handle_app_lost_focus(cx),
79 Event::NextFrame(e)=>self.handle_next_frame(cx, e),
80 Event::Actions(e)=>self.handle_actions(cx,e),
81 Event::Draw(e)=>self.handle_draw(cx, e),
82 Event::AudioDevices(e)=>self.handle_audio_devices(cx, e),
83 Event::MidiPorts(e)=>self.handle_midi_ports(cx, e),
84 Event::VideoInputs(e)=>self.handle_video_inputs(cx, e),
85 Event::NetworkResponses(e)=>self.handle_network_responses(cx, e),
86 Event::KeyDown(e)=>self.handle_key_down(cx, e),
87 Event::KeyUp(e)=>self.handle_key_up(cx, e),
88 Event::BackPressed { handled } if !handled.get() => {
89 let was_handled = self.handle_back_pressed(cx);
90 handled.set(was_handled);
91 }
92 _=>()
93 }
94 }
95
96 fn match_event_with_draw_2d(&mut self, cx:&mut Cx, event:&Event)->Result<(),()>{
97 match event{
98 Event::Draw(e)=>{
99 let mut cx_draw = CxDraw::new(cx, e);
100 let mut cx_2d = Cx2d::new(&mut cx_draw);
101 self.handle_draw_2d(&mut cx_2d);
102 Ok(())
103 }
104 e=>{
105 self.match_event(cx, e);
106 Err(())
107 }
108 }
109 }
110}