state.rs (2230B)
1 use crate::timeline::TimelineTab; 2 use notedeck::debouncer::Debouncer; 3 use std::time::Duration; 4 5 #[derive(Debug, Eq, PartialEq)] 6 pub enum SearchState { 7 Typing, 8 Searched, 9 Navigating, 10 New, 11 } 12 13 #[derive(Debug, Eq, PartialEq)] 14 pub enum FocusState { 15 /// Get ready to focus 16 Navigating, 17 18 /// We should request focus when we stop navigating 19 ShouldRequestFocus, 20 21 /// We already focused, we don't need to do that again 22 RequestedFocus, 23 } 24 25 /// Search query state that exists between frames 26 #[derive(Debug)] 27 pub struct SearchQueryState { 28 /// This holds our search query while we're updating it 29 pub string: String, 30 31 /// When the debouncer timer elapses, we execute the search and mark 32 /// our state as searchd. This will make sure we don't try to search 33 /// again next frames 34 pub state: SearchState, 35 36 /// A bit of context to know if we're navigating to the view. We 37 /// can use this to know when to request focus on the textedit 38 pub focus_state: FocusState, 39 40 /// When was the input updated? We use this to debounce searches 41 pub debouncer: Debouncer, 42 43 /// The search results 44 pub notes: TimelineTab, 45 } 46 47 impl Default for SearchQueryState { 48 fn default() -> Self { 49 SearchQueryState::new() 50 } 51 } 52 53 impl SearchQueryState { 54 pub fn new() -> Self { 55 Self { 56 string: "".to_string(), 57 state: SearchState::New, 58 notes: TimelineTab::default(), 59 focus_state: FocusState::Navigating, 60 debouncer: Debouncer::new(Duration::from_millis(200)), 61 } 62 } 63 64 pub fn should_search(&self) -> bool { 65 self.state == SearchState::Typing && self.debouncer.should_act() 66 } 67 68 /// Mark the search as updated. This will update our debouncer and clear 69 /// the searched flag, enabling us to search again. This should be 70 /// called when the search box changes 71 pub fn mark_updated(&mut self) { 72 self.state = SearchState::Typing; 73 self.debouncer.bounce(); 74 } 75 76 /// Call this when you are about to do a search so that we don't try 77 /// to search again next frame 78 pub fn mark_searched(&mut self, state: SearchState) { 79 self.state = state; 80 } 81 }