Pass application context to GUI. (#11)
This commit was merged in pull request #11.
This commit is contained in:
@@ -19,35 +19,20 @@ pub struct EditorTab {
|
||||
}
|
||||
|
||||
impl EditorTab {
|
||||
fn id() -> &'static str {
|
||||
"EditorTab"
|
||||
}
|
||||
pub const ID: &str = "EditorTab";
|
||||
|
||||
pub fn new(path: Option<&std::path::PathBuf>) -> Self {
|
||||
trace!(target:Self::id(), "Building EditorTab with path {path:?}");
|
||||
match path {
|
||||
None => Self {
|
||||
editors: HashMap::new(),
|
||||
tab_order: Vec::new(),
|
||||
current_editor: 0,
|
||||
},
|
||||
Some(path) => {
|
||||
let tab_order = vec![path.to_string_lossy().to_string()];
|
||||
Self {
|
||||
editors: HashMap::from([(
|
||||
tab_order.first().unwrap().to_owned(),
|
||||
Editor::new(path),
|
||||
)]),
|
||||
tab_order,
|
||||
current_editor: 0,
|
||||
}
|
||||
}
|
||||
pub fn new() -> Self {
|
||||
trace!(target:Self::ID, "Building {}", Self::ID);
|
||||
Self {
|
||||
editors: HashMap::new(),
|
||||
tab_order: Vec::new(),
|
||||
current_editor: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_editor(&mut self) {
|
||||
let next = (self.current_editor + 1) % self.tab_order.len();
|
||||
trace!(target:Self::id(), "Moving from {} to next editor tab at {}", self.current_editor, next);
|
||||
trace!(target:Self::ID, "Moving from {} to next editor tab at {}", self.current_editor, next);
|
||||
self.set_tab_focus(Focus::Active, next);
|
||||
self.current_editor = next;
|
||||
}
|
||||
@@ -57,7 +42,7 @@ impl EditorTab {
|
||||
.current_editor
|
||||
.checked_sub(1)
|
||||
.unwrap_or(self.tab_order.len() - 1);
|
||||
trace!(target:Self::id(), "Moving from {} to previous editor tab at {}", self.current_editor, prev);
|
||||
trace!(target:Self::ID, "Moving from {} to previous editor tab at {}", self.current_editor, prev);
|
||||
self.set_tab_focus(Focus::Active, prev);
|
||||
self.current_editor = prev;
|
||||
}
|
||||
@@ -66,7 +51,7 @@ impl EditorTab {
|
||||
match self.tab_order.get(index) {
|
||||
None => {
|
||||
if !self.tab_order.is_empty() {
|
||||
error!(target:Self::id(), "Failed to get editor tab key with invalid index {index}");
|
||||
error!(target:Self::ID, "Failed to get editor tab key with invalid index {index}");
|
||||
}
|
||||
None
|
||||
}
|
||||
@@ -84,16 +69,16 @@ impl EditorTab {
|
||||
}
|
||||
|
||||
pub fn set_current_tab_focus(&mut self, focus: Focus) {
|
||||
trace!(target:Self::id(), "Setting current tab {} focus to {:?}", self.current_editor, focus);
|
||||
trace!(target:Self::ID, "Setting current tab {} focus to {:?}", self.current_editor, focus);
|
||||
self.set_tab_focus(focus, self.current_editor)
|
||||
}
|
||||
|
||||
pub fn set_tab_focus(&mut self, focus: Focus, index: usize) {
|
||||
trace!(target:Self::id(), "Setting tab {} focus to {:?}", index, focus);
|
||||
trace!(target:Self::ID, "Setting tab {} focus to {:?}", index, focus);
|
||||
if focus == Focus::Active && index != self.current_editor {
|
||||
// If we are setting another tab to active, disable the current one.
|
||||
trace!(
|
||||
target:Self::id(),
|
||||
target:Self::ID,
|
||||
"New tab {} focus set to Active; Setting current tab {} to Inactive",
|
||||
index,
|
||||
self.current_editor
|
||||
@@ -102,12 +87,12 @@ impl EditorTab {
|
||||
}
|
||||
match self.get_editor_key(index) {
|
||||
None => {
|
||||
error!(target:Self::id(), "Failed setting tab focus for invalid key {index}");
|
||||
error!(target:Self::ID, "Failed setting tab focus for invalid key {index}");
|
||||
}
|
||||
Some(key) => match self.editors.get_mut(&key) {
|
||||
None => {
|
||||
error!(
|
||||
target:Self::id(),
|
||||
target:Self::ID,
|
||||
"Failed to update tab focus at index {} with invalid key: {}",
|
||||
self.current_editor,
|
||||
self.tab_order[self.current_editor]
|
||||
@@ -118,13 +103,13 @@ impl EditorTab {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_tab(&mut self, path: &std::path::PathBuf) -> Result<()> {
|
||||
trace!(target:Self::id(), "Opening new EditorTab with path {:?}", path);
|
||||
pub fn open_tab(&mut self, path: &std::path::Path) -> Result<()> {
|
||||
trace!(target:Self::ID, "Opening new EditorTab with path {:?}", path);
|
||||
if self
|
||||
.editors
|
||||
.contains_key(&path.to_string_lossy().to_string())
|
||||
{
|
||||
warn!(target:Self::id(), "EditorTab already opened with this file");
|
||||
warn!(target:Self::ID, "EditorTab already opened with this file");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -145,18 +130,16 @@ impl EditorTab {
|
||||
let key = self
|
||||
.tab_order
|
||||
.get(index)
|
||||
.ok_or(anyhow!(
|
||||
"Failed to get tab order with invalid index {index}"
|
||||
))?
|
||||
.ok_or_else(|| anyhow!("Failed to get tab order with invalid index {index}"))?
|
||||
.to_owned();
|
||||
match self.editors.remove(&key) {
|
||||
None => {
|
||||
error!(target:Self::id(), "Failed to remove editor tab {key} with invalid index {index}")
|
||||
error!(target:Self::ID, "Failed to remove editor tab {key} with invalid index {index}")
|
||||
}
|
||||
Some(_) => {
|
||||
self.prev_editor();
|
||||
self.tab_order.remove(index);
|
||||
info!(target:Self::id(), "Closed editor tab {key} at index {index}")
|
||||
info!(target:Self::ID, "Closed editor tab {key} at index {index}")
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -170,10 +153,10 @@ impl EditorTab {
|
||||
// TODO: Only file name is displayed in tab title, so files with the same name in different
|
||||
// directories will appear confusing.
|
||||
let tab_titles = self.tab_order.iter().map(|t| {
|
||||
std::path::PathBuf::from(t)
|
||||
std::path::Path::new(t)
|
||||
.file_name()
|
||||
.map(|f| f.to_string_lossy().to_string())
|
||||
.unwrap_or("Unknown".to_string())
|
||||
.unwrap_or_else(|| String::from("Unknown"))
|
||||
});
|
||||
// Don't set border color based on ComponentState::focus, the Editor renders the border.
|
||||
Tabs::new(tab_titles)
|
||||
|
||||
Reference in New Issue
Block a user