main.rs
1 use std::io; 2 3 mod app; 4 mod effects; 5 mod input_router; 6 mod reducer; 7 mod selectors; 8 mod ui; 9 10 use app::App; 11 12 #[cfg(not(target_arch = "wasm32"))] 13 use { 14 crossterm::{ 15 event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyEventKind}, 16 execute, 17 }, 18 ratatui::DefaultTerminal, 19 std::time::Duration, 20 }; 21 22 #[cfg(target_arch = "wasm32")] 23 use { 24 ratatui::Terminal, 25 ratzilla::{DomBackend, WebRenderer}, 26 std::{cell::RefCell, rc::Rc}, 27 }; 28 29 #[cfg(not(target_arch = "wasm32"))] 30 fn run_native(app: &mut App, terminal: &mut DefaultTerminal) -> io::Result<()> { 31 app.start_initial_load_native(); 32 33 while !app.should_exit() { 34 app.poll_native_background_messages(); 35 app.tick_native(); 36 terminal.draw(|frame| ui::render(frame, app))?; 37 38 if !event::poll(Duration::from_millis(50))? { 39 continue; 40 } 41 42 match event::read()? { 43 Event::Key(key_event) if key_event.kind == KeyEventKind::Press => { 44 let app_key_event = input_router::app_key_event_from_native(key_event); 45 if let Some(action) = app_key_event 46 .and_then(|key_event| input_router::map_key_event_to_action(app, key_event)) 47 { 48 effects::dispatch_native_action(app, action); 49 } 50 } 51 Event::Mouse(mouse_event) => { 52 if let Some(action) = input_router::map_native_mouse_event(mouse_event) { 53 effects::dispatch_native_action(app, action); 54 } 55 } 56 _ => {} 57 } 58 } 59 60 Ok(()) 61 } 62 63 #[cfg(not(target_arch = "wasm32"))] 64 fn main() -> io::Result<()> { 65 let mut terminal = ratatui::init(); 66 let mut app = App::new(); 67 68 execute!(io::stdout(), EnableMouseCapture)?; 69 let app_result = run_native(&mut app, &mut terminal); 70 71 let mouse_capture_restore_result = execute!(io::stdout(), DisableMouseCapture); 72 ratatui::restore(); 73 74 mouse_capture_restore_result?; 75 app_result 76 } 77 78 #[cfg(target_arch = "wasm32")] 79 fn main() -> io::Result<()> { 80 let app_state = Rc::new(RefCell::new(App::new())); 81 App::start_initial_load_web(app_state.clone()); 82 83 let terminal = Terminal::new(DomBackend::new()?)?; 84 terminal.on_key_event({ 85 let app_state = app_state.clone(); 86 move |key_event| { 87 let action = { 88 let app = app_state.borrow(); 89 input_router::app_key_event_from_web(key_event).and_then(|app_key_event| { 90 input_router::map_key_event_to_action(&app, app_key_event) 91 }) 92 }; 93 94 if let Some(action) = action { 95 effects::dispatch_web_action(&app_state, action); 96 } 97 } 98 }); 99 100 terminal.on_mouse_event({ 101 let app_state = app_state.clone(); 102 move |mouse_event| { 103 if let Some(action) = input_router::map_web_mouse_event(mouse_event) { 104 effects::dispatch_web_action(&app_state, action); 105 } 106 } 107 }); 108 109 terminal.draw_web(move |frame| { 110 ui::render(frame, &mut app_state.borrow_mut()); 111 }); 112 113 Ok(()) 114 }