From 0ca4ffd657f1a2884ca8422be8a78baa42f00de4 Mon Sep 17 00:00:00 2001 From: Jeff Zhao Date: Wed, 31 Aug 2022 11:11:06 -0400 Subject: use hashmap and uuid to store tabs (#194) This is preliminary changes in order to track preview threads and progress. The current setup is we just kick off a new thread to load the given directory whenever we see the directory content does not exist in history. We don't track these threads or which tab these requests came from. When the result is returned, we just assign it to the current tab, instead of the tab that actually initiated the request. By adding uuid, we can now track which tab requested the preview and assign it accordingly. This will also allow us to track the status of the preview, so we can display to the user a loading state, when a directory is taking longer than usual to load. This will also solve the problem of kicking off multiple threads to read the same directory for the same tab. Now these threads can be stored and tracked. - side: fix reload not honouring tab sort options - use tab specific options whenever we need to reload stuff --- src/context/tab_context.rs | 57 +++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 24 deletions(-) (limited to 'src/context') diff --git a/src/context/tab_context.rs b/src/context/tab_context.rs index 77f75a5..1393b06 100644 --- a/src/context/tab_context.rs +++ b/src/context/tab_context.rs @@ -1,50 +1,59 @@ -use std::slice::IterMut; +use std::collections::hash_map::IterMut; +use std::collections::HashMap; + +use uuid::Uuid; use crate::tab::JoshutoTab; #[derive(Default)] pub struct TabContext { pub index: usize, - tabs: Vec, + pub tab_order: Vec, + tabs: HashMap, } impl TabContext { pub fn new() -> Self { Self::default() } - pub fn len(&self) -> usize { - self.tabs.len() + self.tab_order.len() } - pub fn tab_ref(&self, i: usize) -> Option<&JoshutoTab> { - if i >= self.tabs.len() { - return None; - } - Some(&self.tabs[i]) + pub fn tab_ref(&self, id: &Uuid) -> Option<&JoshutoTab> { + self.tabs.get(id) } - pub fn tab_mut(&mut self, i: usize) -> Option<&mut JoshutoTab> { - if i >= self.tabs.len() { - return None; - } - Some(&mut self.tabs[i]) + pub fn tab_mut(&mut self, id: &Uuid) -> Option<&mut JoshutoTab> { + self.tabs.get_mut(id) } + pub fn curr_tab_id(&self) -> Uuid { + self.tab_order[self.index] + } pub fn curr_tab_ref(&self) -> &JoshutoTab { - &self.tabs[self.index] + let id = &self.tab_order[self.index]; + self.tabs.get(id).unwrap() } pub fn curr_tab_mut(&mut self) -> &mut JoshutoTab { - &mut self.tabs[self.index] - } - pub fn push_tab(&mut self, tab: JoshutoTab) { - self.tabs.push(tab); - self.index = self.tabs.len() - 1; - } - pub fn pop_tab(&mut self, index: usize) -> JoshutoTab { - self.tabs.remove(index) + let id = &self.tab_order[self.index]; + self.tabs.get_mut(id).unwrap() + } + pub fn insert_tab(&mut self, id: Uuid, tab: JoshutoTab) { + self.tabs.insert(id, tab); + self.tab_order.push(id); + } + pub fn remove_tab(&mut self, id: &Uuid) -> Option { + let tab = self.tabs.remove(id); + for i in 0..self.tab_order.len() { + if self.tab_order[i] == *id { + self.tab_order.remove(i); + break; + } + } + tab } - pub fn iter_mut(&mut self) -> IterMut { + pub fn iter_mut(&mut self) -> IterMut { self.tabs.iter_mut() } } -- cgit v1.2.3