move to tab if file already open

This commit is contained in:
Penwing 2024-01-26 20:53:16 +01:00
parent 94ef5675b7
commit fa2104b9de
2 changed files with 195 additions and 189 deletions

View file

@ -12,218 +12,226 @@ use crate::TIME_LABELS;
use tools::hex_str_to_color; use tools::hex_str_to_color;
impl Calcifer { impl Calcifer {
pub fn handle_confirm(&mut self) { pub fn handle_confirm(&mut self) {
if self.close_tab_confirm.proceed { if self.close_tab_confirm.proceed {
self.close_tab_confirm.close(); self.close_tab_confirm.close();
self.delete_tab(self.tab_to_close); self.delete_tab(self.tab_to_close);
} }
if self.refresh_confirm.proceed { if self.refresh_confirm.proceed {
self.refresh_confirm.close(); self.refresh_confirm.close();
self.tabs[self.selected_tab.to_index()].refresh(); self.tabs[self.selected_tab.to_index()].refresh();
} }
} }
pub fn save_tab(&self) -> Option<PathBuf> { pub fn save_tab(&self) -> Option<PathBuf> {
if self.tabs[self.selected_tab.to_index()] if self.tabs[self.selected_tab.to_index()]
.path .path
.file_name() .file_name()
.map_or(true, |name| name.to_string_lossy() == "untitled") .map_or(true, |name| name.to_string_lossy() == "untitled")
{ {
self.save_tab_as() self.save_tab_as()
} else { } else {
if let Err(err) = fs::write( if let Err(err) = fs::write(
&self.tabs[self.selected_tab.to_index()].path, &self.tabs[self.selected_tab.to_index()].path,
&self.tabs[self.selected_tab.to_index()].code, &self.tabs[self.selected_tab.to_index()].code,
) { ) {
eprintln!("Error writing file: {}", err); eprintln!("Error writing file: {}", err);
return None; return None;
} }
Some(self.tabs[self.selected_tab.to_index()].path.clone()) Some(self.tabs[self.selected_tab.to_index()].path.clone())
} }
} }
pub fn save_tab_as(&self) -> Option<PathBuf> { pub fn save_tab_as(&self) -> Option<PathBuf> {
if let Some(path) = rfd::FileDialog::new() if let Some(path) = rfd::FileDialog::new()
.set_directory(Path::new(&PATH_ROOT)) .set_directory(Path::new(&PATH_ROOT))
.save_file() .save_file()
{ {
if let Err(err) = fs::write(&path, &self.tabs[self.selected_tab.to_index()].code) { if let Err(err) = fs::write(&path, &self.tabs[self.selected_tab.to_index()].code) {
eprintln!("Error writing file: {}", err); eprintln!("Error writing file: {}", err);
return None; return None;
} }
return Some(path); return Some(path);
} }
None None
} }
pub fn handle_save_file(&mut self, path_option: Option<PathBuf>) { pub fn handle_save_file(&mut self, path_option: Option<PathBuf>) {
if let Some(path) = path_option { if let Some(path) = path_option {
println!("File saved successfully at: {:?}", path); println!("File saved successfully at: {:?}", path);
self.tabs[self.selected_tab.to_index()].path = path; self.tabs[self.selected_tab.to_index()].path = path;
self.tabs[self.selected_tab.to_index()].saved = true; self.tabs[self.selected_tab.to_index()].saved = true;
} else { } else {
println!("File save failed."); println!("File save failed.");
} }
} }
pub fn from_app_state(app_state: tools::AppState) -> Self { pub fn from_app_state(app_state: tools::AppState) -> Self {
let mut new = Self { let mut new = Self {
theme: DEFAULT_THEMES[min(app_state.theme, DEFAULT_THEMES.len() - 1)], theme: DEFAULT_THEMES[min(app_state.theme, DEFAULT_THEMES.len() - 1)],
tabs: Vec::new(), tabs: Vec::new(),
settings_menu: tools::settings::SettingsWindow::new(DEFAULT_THEMES[app_state.theme]), settings_menu: tools::settings::SettingsWindow::new(DEFAULT_THEMES[app_state.theme]),
..Default::default() ..Default::default()
}; };
for path in app_state.tabs { for path in app_state.tabs {
if !path if !path
.file_name() .file_name()
.map_or(true, |name| name.to_string_lossy() == "untitled") .map_or(true, |name| name.to_string_lossy() == "untitled")
{ {
new.open_file(Some(&path)); new.open_file(Some(&path));
} }
} }
if new.tabs == vec![] { if new.tabs == vec![] {
new.open_file(None); new.open_file(None);
} }
new new
} }
pub fn save_state(&self) { pub fn save_state(&self) {
let mut state_theme: usize = 0; let mut state_theme: usize = 0;
if let Some(theme) = DEFAULT_THEMES.iter().position(|&r| r == self.theme) { if let Some(theme) = DEFAULT_THEMES.iter().position(|&r| r == self.theme) {
state_theme = theme; state_theme = theme;
} }
let mut state_tabs = vec![]; let mut state_tabs = vec![];
for tab in &self.tabs { for tab in &self.tabs {
state_tabs.push(tab.path.clone()); state_tabs.push(tab.path.clone());
} }
let app_state = tools::AppState { let app_state = tools::AppState {
tabs: state_tabs, tabs: state_tabs,
theme: state_theme, theme: state_theme,
}; };
let _ = tools::save_state(&app_state, SAVE_PATH); let _ = tools::save_state(&app_state, SAVE_PATH);
} }
pub fn move_through_tabs(&mut self, forward: bool) { pub fn move_through_tabs(&mut self, forward: bool) {
let new_index = if forward { let new_index = if forward {
(self.selected_tab.to_index() + 1) % self.tabs.len() (self.selected_tab.to_index() + 1) % self.tabs.len()
} else { } else {
self.selected_tab self.selected_tab
.to_index() .to_index()
.checked_sub(1) .checked_sub(1)
.unwrap_or(self.tabs.len() - 1) .unwrap_or(self.tabs.len() - 1)
}; };
self.selected_tab = tools::TabNumber::from_index(new_index); self.selected_tab = tools::TabNumber::from_index(new_index);
} }
pub fn list_files(&mut self, ui: &mut egui::Ui, path: &Path) -> io::Result<()> { pub fn list_files(&mut self, ui: &mut egui::Ui, path: &Path) -> io::Result<()> {
if path.file_name().is_none() { if path.file_name().is_none() {
return Ok(()); return Ok(());
} }
let name = path let name = path
.file_name() .file_name()
.unwrap_or_else(|| OsStr::new("")) .unwrap_or_else(|| OsStr::new(""))
.to_string_lossy() .to_string_lossy()
.into_owned(); .into_owned();
if !path.is_dir() { if !path.is_dir() {
if ui.button(name).clicked() { if ui.button(name).clicked() {
self.open_file(Some(path)); self.open_file(Some(path));
} }
return Ok(()); return Ok(());
} }
egui::CollapsingHeader::new(name).show(ui, |ui| match fs::read_dir(path) { egui::CollapsingHeader::new(name).show(ui, |ui| match fs::read_dir(path) {
Err(err) => { Err(err) => {
ui.label(format!("Error reading directory: {}", err)); ui.label(format!("Error reading directory: {}", err));
} }
Ok(entries) => { Ok(entries) => {
let mut paths: Vec<Result<fs::DirEntry, io::Error>> = entries let mut paths: Vec<Result<fs::DirEntry, io::Error>> = entries
.map(|r| r.map_err(|e| io::Error::new(io::ErrorKind::Other, e))) .map(|r| r.map_err(|e| io::Error::new(io::ErrorKind::Other, e)))
.collect(); .collect();
paths.sort_by(|a, b| match (a, b) { paths.sort_by(|a, b| match (a, b) {
(Ok(entry_a), Ok(entry_b)) => tools::sort_directories_first(entry_a, entry_b), (Ok(entry_a), Ok(entry_b)) => tools::sort_directories_first(entry_a, entry_b),
(Err(_), Ok(_)) => std::cmp::Ordering::Greater, (Err(_), Ok(_)) => std::cmp::Ordering::Greater,
(Ok(_), Err(_)) => std::cmp::Ordering::Less, (Ok(_), Err(_)) => std::cmp::Ordering::Less,
(Err(_), Err(_)) => std::cmp::Ordering::Equal, (Err(_), Err(_)) => std::cmp::Ordering::Equal,
}); });
for result in paths { for result in paths {
match result { match result {
Ok(entry) => { Ok(entry) => {
let _ = self.list_files(ui, &entry.path()); let _ = self.list_files(ui, &entry.path());
} }
Err(err) => { Err(err) => {
ui.label(format!("Error processing directory entry: {}", err)); ui.label(format!("Error processing directory entry: {}", err));
} }
} }
} }
} }
}); });
Ok(()) Ok(())
} }
pub fn open_file(&mut self, path_option: Option<&Path>) { pub fn open_file(&mut self, path_option: Option<&Path>) {
if self.tabs.len() < MAX_TABS { if let Some(path) = path_option.clone() {
if let Some(path) = path_option { for (index, tab) in self.tabs.clone().iter().enumerate() {
self.tabs.push(tools::Tab::new(path.to_path_buf())); if tab.path == path {
} else { self.selected_tab = tools::TabNumber::from_index(index);
self.tabs.push(tools::Tab::default()); return
} }
self.selected_tab = tools::TabNumber::from_index(self.tabs.len() - 1); }
} }
} if self.tabs.len() < MAX_TABS {
if let Some(path) = path_option {
self.tabs.push(tools::Tab::new(path.to_path_buf()));
} else {
self.tabs.push(tools::Tab::default());
}
self.selected_tab = tools::TabNumber::from_index(self.tabs.len() - 1);
}
}
pub fn delete_tab(&mut self, index: usize) { pub fn delete_tab(&mut self, index: usize) {
self.tabs.remove(index); self.tabs.remove(index);
self.selected_tab = tools::TabNumber::from_index(min(index, self.tabs.len() - 1)); self.selected_tab = tools::TabNumber::from_index(min(index, self.tabs.len() - 1));
} }
pub fn toggle(&self, ui: &mut egui::Ui, display: bool, title: &str) -> bool { pub fn toggle(&self, ui: &mut egui::Ui, display: bool, title: &str) -> bool {
let bg_color: Color32; let bg_color: Color32;
let text_color: Color32; let text_color: Color32;
if display { if display {
bg_color = hex_str_to_color(self.theme.functions); bg_color = hex_str_to_color(self.theme.functions);
text_color = hex_str_to_color(self.theme.bg); text_color = hex_str_to_color(self.theme.bg);
} else { } else {
bg_color = hex_str_to_color(self.theme.bg); bg_color = hex_str_to_color(self.theme.bg);
text_color = hex_str_to_color(self.theme.literals); text_color = hex_str_to_color(self.theme.literals);
}; };
ui.style_mut().visuals.override_text_color = Some(text_color); ui.style_mut().visuals.override_text_color = Some(text_color);
if ui.add(egui::Button::new(title).fill(bg_color)).clicked() { if ui.add(egui::Button::new(title).fill(bg_color)).clicked() {
return !display; return !display;
} }
ui.style_mut().visuals.override_text_color = None; ui.style_mut().visuals.override_text_color = None;
display
}
display pub fn profiler(&self) -> String {
} if !self.profiler_visible {
return "".to_string();
}
let combined_string: Vec<String> = TIME_LABELS
.into_iter()
.zip(self.time_watch.clone())
.map(|(s, v)| format!("{} : {:.1} ms", s, v))
.collect();
pub fn profiler(&self) -> String { let mut result = combined_string.join(" ; ");
if !self.profiler_visible { result.push_str(&format!(
return "".to_string(); " total : {:.1} ms",
} self.time_watch.clone().iter().sum::<f32>()
let combined_string: Vec<String> = TIME_LABELS ));
.into_iter() result
.zip(self.time_watch.clone()) }
.map(|(s, v)| format!("{} : {:.1} ms", s, v))
.collect();
let mut result = combined_string.join(" ; ");
result.push_str(&format!(
" total : {:.1} ms",
self.time_watch.clone().iter().sum::<f32>()
));
result
}
} }

View file

@ -56,8 +56,6 @@ impl Tab {
.extension() .extension()
.and_then(|ext| ext.to_str()) .and_then(|ext| ext.to_str())
.unwrap_or_default(); .unwrap_or_default();
println!("language : {}", extension);
Self { Self {
path: file_path.clone(), path: file_path.clone(),