mod.rs (2026B)
1 pub mod library_ui; 2 pub mod setting_ui; 3 4 use crossterm::event::KeyEvent; 5 use ratatui::{Frame, widgets::ListState}; 6 7 use crate::app::App; 8 9 #[derive(PartialEq, Eq, Clone, Copy)] 10 pub enum Focus { 11 Artist, 12 Album, 13 Track, 14 Settings, 15 } 16 17 pub struct AppState { 18 pub focus: Focus, 19 pub artist_state: ListState, 20 pub album_state: ListState, 21 pub track_state: ListState, 22 pub settings_state: ListState, 23 pub is_device_dialog_open: bool, 24 pub available_devices: Vec<String>, 25 pub device_dialog_state: ListState, 26 } 27 28 impl AppState { 29 pub fn new() -> Self { 30 let mut artist_state = ListState::default(); 31 artist_state.select(Some(0)); 32 let mut settings_state = ListState::default(); 33 settings_state.select(Some(0)); 34 let mut device_dialog_state = ListState::default(); 35 device_dialog_state.select(Some(0)); 36 Self { 37 focus: Focus::Artist, 38 artist_state, 39 album_state: ListState::default(), 40 track_state: ListState::default(), 41 settings_state, 42 is_device_dialog_open: false, 43 available_devices: Vec::new(), 44 device_dialog_state, 45 } 46 } 47 48 pub fn open_device_dialog(&mut self, devices: Vec<String>) { 49 self.available_devices = devices; 50 self.is_device_dialog_open = true; 51 self.device_dialog_state.select(Some(0)); 52 } 53 54 pub fn close_device_dialog(&mut self) { 55 self.is_device_dialog_open = false; 56 } 57 } 58 59 pub fn render(f: &mut Frame, app: &mut App) { 60 if app.state.focus == Focus::Settings { 61 setting_ui::render(f, app); 62 } else { 63 library_ui::render(f, app); 64 } 65 } 66 67 pub fn handle_key_event(app: &mut App, key: KeyEvent) -> Result<bool, Box<dyn std::error::Error>> { 68 if app.state.focus == Focus::Settings { 69 setting_ui::handle_key_event(app, key) 70 } else { 71 library_ui::handle_key_event(app, key) 72 } 73 }