state.rs (1734B)
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 New, 10 } 11 12 /// Search query state that exists between frames 13 #[derive(Debug)] 14 pub struct SearchQueryState { 15 /// This holds our search query while we're updating it 16 pub string: String, 17 18 /// When the debouncer timer elapses, we execute the search and mark 19 /// our state as searchd. This will make sure we don't try to search 20 /// again next frames 21 pub state: SearchState, 22 23 /// When was the input updated? We use this to debounce searches 24 pub debouncer: Debouncer, 25 26 /// The search results 27 pub notes: TimelineTab, 28 } 29 30 impl Default for SearchQueryState { 31 fn default() -> Self { 32 SearchQueryState::new() 33 } 34 } 35 36 impl SearchQueryState { 37 pub fn new() -> Self { 38 Self { 39 string: "".to_string(), 40 state: SearchState::New, 41 notes: TimelineTab::default(), 42 debouncer: Debouncer::new(Duration::from_millis(200)), 43 } 44 } 45 46 pub fn should_search(&self) -> bool { 47 self.state == SearchState::Typing && self.debouncer.should_act() 48 } 49 50 /// Mark the search as updated. This will update our debouncer and clear 51 /// the searched flag, enabling us to search again. This should be 52 /// called when the search box changes 53 pub fn mark_updated(&mut self) { 54 self.state = SearchState::Typing; 55 self.debouncer.bounce(); 56 } 57 58 /// Call this when you are about to do a search so that we don't try 59 /// to search again next frame 60 pub fn mark_searched(&mut self, state: SearchState) { 61 self.state = state; 62 } 63 }