tree refresh

This commit is contained in:
WanderingPenwing 2024-02-28 15:33:23 +01:00
parent 43f2ebb2ae
commit b84cde8e2b
3 changed files with 385 additions and 378 deletions

View file

@ -15,410 +15,412 @@ use crate::TERMINAL_RANGE;
use editor::{CodeEditor, Syntax}; use editor::{CodeEditor, Syntax};
impl Calcifer { impl Calcifer {
pub fn draw_settings(&mut self, ctx: &egui::Context) { pub fn draw_settings(&mut self, ctx: &egui::Context) {
egui::SidePanel::left("settings") egui::SidePanel::left("settings")
.resizable(false) .resizable(false)
.exact_width(self.font_size * 1.8) .exact_width(self.font_size * 1.8)
.show(ctx, |ui| { .show(ctx, |ui| {
ui.vertical(|ui| { ui.vertical(|ui| {
if ui.add(egui::Button::new("📁")).clicked() { if ui.add(egui::Button::new("📁")).clicked() {
if let Some(path) = rfd::FileDialog::new() if let Some(path) = rfd::FileDialog::new()
.set_directory(self.home.as_path()) .set_directory(self.home.as_path())
.pick_file() .pick_file()
{ {
self.open_file(Some(&path)); self.open_file(Some(&path));
} }
} }
ui.separator(); ui.separator();
self.tree_visible = self.toggle(ui, self.tree_visible, "📦"); self.tree_visible = self.toggle(ui, self.tree_visible, "📦");
ui.separator(); ui.separator();
self.terminal_visible = self.toggle(ui, self.terminal_visible, "🖵"); self.terminal_visible = self.toggle(ui, self.terminal_visible, "🖵");
ui.separator(); ui.separator();
self.search_menu.visible = self.toggle(ui, self.search_menu.visible, "🔍"); self.search_menu.visible = self.toggle(ui, self.search_menu.visible, "🔍");
ui.separator(); ui.separator();
self.settings_menu.visible = self.toggle(ui, self.settings_menu.visible, ""); self.settings_menu.visible = self.toggle(ui, self.settings_menu.visible, "");
ui.separator(); ui.separator();
self.shortcuts_menu.visible = self.toggle(ui, self.shortcuts_menu.visible, ""); self.shortcuts_menu.visible = self.toggle(ui, self.shortcuts_menu.visible, "");
ui.separator(); ui.separator();
self.profiler_visible = self.toggle(ui, self.profiler_visible, ""); self.profiler_visible = self.toggle(ui, self.profiler_visible, "");
if self.tabs[self.selected_tab].language == PROJECT_EXTENSION { if self.tabs[self.selected_tab].language == PROJECT_EXTENSION {
ui.separator(); ui.separator();
self.project_content.item_window.visible = self.project_content.item_window.visible =
self.toggle(ui, self.project_content.item_window.visible, "🖊"); self.toggle(ui, self.project_content.item_window.visible, "🖊");
} }
}); });
}); });
} }
pub fn draw_tree_panel(&mut self, ctx: &egui::Context) { pub fn draw_tree_panel(&mut self, ctx: &egui::Context) {
if !self.tree_visible { if !self.tree_visible {
return; return;
} }
if self.file_tree.is_none() { let mut init_update : bool = false;
self.file_tree = Some(panels::generate_folder_entry(self.home.as_path())); if self.file_tree.is_none() {
} self.file_tree = Some(panels::generate_folder_entry(self.home.as_path()));
let mut n_files: usize = 0; init_update = true
egui::SidePanel::left("file_tree_panel").show(ctx, |ui| { }
ui.horizontal(|ui| { let mut n_files: usize = 0;
ui.label("Bookshelf "); egui::SidePanel::left("file_tree_panel").show(ctx, |ui| {
}); ui.horizontal(|ui| {
ui.separator(); ui.label("Bookshelf ");
ui.label(format!("{} files displayed", self.n_file_displayed)); });
ui.separator(); ui.separator();
egui::ScrollArea::vertical().show(ui, |ui| { ui.label(format!("{} files displayed", self.n_file_displayed));
if let Some(file_tree) = self.file_tree.clone() { ui.separator();
let update_requested = self.list_files(ui, &file_tree, &mut n_files); egui::ScrollArea::vertical().show(ui, |ui| {
if update_requested { if let Some(file_tree) = self.file_tree.clone() {
self.file_tree = Some(panels::update_file_tree( let update_requested = self.list_files(ui, &file_tree, &mut n_files);
file_tree, if update_requested || init_update {
self.tree_dir_opened.clone(), self.file_tree = Some(panels::update_file_tree(
)); file_tree,
} self.tree_dir_opened.clone(),
} else { ));
ui.label("No book on the Bookshelf"); }
} } else {
ui.separator(); ui.label("No book on the Bookshelf");
}); }
}); ui.separator();
self.n_file_displayed = n_files; });
} });
self.n_file_displayed = n_files;
}
pub fn draw_bottom_tray(&mut self, ctx: &egui::Context) { pub fn draw_bottom_tray(&mut self, ctx: &egui::Context) {
egui::TopBottomPanel::bottom("tray") egui::TopBottomPanel::bottom("tray")
.default_height(self.font_size * 1.2) .default_height(self.font_size * 1.2)
.resizable(false) .resizable(false)
.show(ctx, |ui| { .show(ctx, |ui| {
ui.label(self.profiler()); ui.label(self.profiler());
}); });
} }
pub fn draw_terminal_panel(&mut self, ctx: &egui::Context) { pub fn draw_terminal_panel(&mut self, ctx: &egui::Context) {
if !self.terminal_visible { if !self.terminal_visible {
return; return;
} }
egui::TopBottomPanel::bottom("terminal") egui::TopBottomPanel::bottom("terminal")
.default_height(TERMINAL_HEIGHT) .default_height(TERMINAL_HEIGHT)
.height_range(Rangef::new(TERMINAL_RANGE.start, TERMINAL_RANGE.end)) .height_range(Rangef::new(TERMINAL_RANGE.start, TERMINAL_RANGE.end))
.resizable(true) .resizable(true)
.show(ctx, |ui| { .show(ctx, |ui| {
ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| { ui.with_layout(egui::Layout::bottom_up(egui::Align::LEFT), |ui| {
let command_color = core::hex_str_to_color(self.theme.functions); let command_color = core::hex_str_to_color(self.theme.functions);
let entry_color = core::hex_str_to_color(self.theme.literals); let entry_color = core::hex_str_to_color(self.theme.literals);
let bg_color = core::hex_str_to_color(self.theme.bg); let bg_color = core::hex_str_to_color(self.theme.bg);
ui.label(""); ui.label("");
ui.horizontal(|ui| { ui.horizontal(|ui| {
if ui.add(egui::Button::new("")).clicked() { if ui.add(egui::Button::new("")).clicked() {
self.command_history.retain(|e| !e.finished); self.command_history.retain(|e| !e.finished);
} }
ui.style_mut().visuals.extreme_bg_color = bg_color; ui.style_mut().visuals.extreme_bg_color = bg_color;
let Self { command, .. } = self; let Self { command, .. } = self;
ui.colored_label( ui.colored_label(
command_color, command_color,
format_path(&env::current_dir().unwrap_or_else(|_| PathBuf::from("/"))), format_path(&env::current_dir().unwrap_or_else(|_| PathBuf::from("/"))),
); );
let response = ui.add( let response = ui.add(
egui::TextEdit::singleline(command) egui::TextEdit::singleline(command)
.desired_width(f32::INFINITY) .desired_width(f32::INFINITY)
.lock_focus(true), .lock_focus(true),
); );
if response.lost_focus() && ctx.input(|i| i.key_pressed(egui::Key::Enter)) { if response.lost_focus() && ctx.input(|i| i.key_pressed(egui::Key::Enter)) {
self.command_history self.command_history
.push(panels::send_command(self.command.clone())); .push(panels::send_command(self.command.clone()));
self.command = "".into(); self.command = "".into();
response.request_focus(); response.request_focus();
} }
}); });
ui.separator(); ui.separator();
egui::ScrollArea::vertical() egui::ScrollArea::vertical()
.stick_to_bottom(true) .stick_to_bottom(true)
.show(ui, |ui| { .show(ui, |ui| {
ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| { ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| {
ui.separator(); ui.separator();
ui.spacing_mut().item_spacing.y = 0.0; ui.spacing_mut().item_spacing.y = 0.0;
ui.style_mut().visuals.hyperlink_color = ui.style_mut().visuals.hyperlink_color =
core::hex_str_to_color(self.theme.keywords); core::hex_str_to_color(self.theme.keywords);
let mut running_command = false; let mut running_command = false;
for entry in &mut self.command_history { for entry in &mut self.command_history {
ui.label(""); ui.label("");
ui.horizontal(|ui| { ui.horizontal(|ui| {
if !entry.finished { if !entry.finished {
running_command = true; running_command = true;
entry.update(); entry.update();
let _ = ui.link("(⌛)"); let _ = ui.link("(⌛)");
} else if ui.link("(🗐)").clicked() { } else if ui.link("(🗐)").clicked() {
entry.copy_error_code(); entry.copy_error_code();
} }
ui.colored_label( ui.colored_label(
command_color, command_color,
format!("{} {}", entry.env, entry.command), format!("{} {}", entry.env, entry.command),
); );
}); });
for line in &entry.result { for line in &entry.result {
let color = if line.error { RED } else { entry_color }; let color = if line.error { RED } else { entry_color };
ui.add( ui.add(
egui::Label::new( egui::Label::new(
egui::RichText::new(&line.text) egui::RichText::new(&line.text)
.monospace() .monospace()
.color(color), .color(color),
), //.selectable(true) ), //.selectable(true)
); );
} }
} }
self.running_command = running_command; self.running_command = running_command;
}); });
}); });
}); });
}); });
} }
pub fn draw_tab_panel(&mut self, ctx: &egui::Context) { pub fn draw_tab_panel(&mut self, ctx: &egui::Context) {
egui::TopBottomPanel::top("tabs") egui::TopBottomPanel::top("tabs")
.resizable(false) .resizable(false)
.show(ctx, |ui| { .show(ctx, |ui| {
ui.painter().rect_filled( ui.painter().rect_filled(
ui.available_rect_before_wrap(), ui.available_rect_before_wrap(),
0.0, 0.0,
core::hex_str_to_color(self.theme.bg), core::hex_str_to_color(self.theme.bg),
); );
StripBuilder::new(ui) StripBuilder::new(ui)
.sizes(Size::remainder(), max(10, self.tabs.len() + 1)) .sizes(Size::remainder(), max(10, self.tabs.len() + 1))
.sense(egui::Sense::click()) .sense(egui::Sense::click())
.horizontal(|mut strip| { .horizontal(|mut strip| {
for (index, tab) in self.tabs.clone().iter().enumerate() { for (index, tab) in self.tabs.clone().iter().enumerate() {
strip.cell(|ui| { strip.cell(|ui| {
let mut color = core::hex_str_to_color(self.theme.comments); let mut color = core::hex_str_to_color(self.theme.comments);
if self.selected_tab == index { if self.selected_tab == index {
ui.painter().rect_filled( ui.painter().rect_filled(
ui.available_rect_before_wrap(), ui.available_rect_before_wrap(),
0.0, 0.0,
core::hex_str_to_color(self.theme.functions), core::hex_str_to_color(self.theme.functions),
); );
color = core::hex_str_to_color(self.theme.bg) color = core::hex_str_to_color(self.theme.bg)
} }
let unsaved_indicator = if tab.saved { "" } else { "~ " }; let unsaved_indicator = if tab.saved { "" } else { "~ " };
ui.with_layout( ui.with_layout(
egui::Layout::right_to_left(egui::Align::TOP), egui::Layout::right_to_left(egui::Align::TOP),
|ui| { |ui| {
ui.label(""); ui.label("");
if ui if ui
.add( .add(
egui::Label::new( egui::Label::new(
egui::RichText::new(" X ").color(color), egui::RichText::new(" X ").color(color),
) )
.sense(egui::Sense::click()), .sense(egui::Sense::click()),
) )
.clicked() .clicked()
&& !self.close_tab_confirm.visible && !self.close_tab_confirm.visible
{ {
if self.tabs.len() > 1 { if self.tabs.len() > 1 {
if tab.saved { if tab.saved {
self.delete_tab(index); self.delete_tab(index);
} else { } else {
self.close_tab_confirm.ask(); self.close_tab_confirm.ask();
self.tab_to_close = index; self.tab_to_close = index;
} }
} else { } else {
egui::Context::send_viewport_cmd( egui::Context::send_viewport_cmd(
ctx, ctx,
egui::ViewportCommand::Close, egui::ViewportCommand::Close,
); );
} }
} }
ui.with_layout( ui.with_layout(
egui::Layout::left_to_right(egui::Align::TOP), egui::Layout::left_to_right(egui::Align::TOP),
|ui| { |ui| {
if ui if ui
.add( .add(
egui::Label::new( egui::Label::new(
egui::RichText::new(format!( egui::RichText::new(format!(
" {}{}", " {}{}",
unsaved_indicator, unsaved_indicator,
tab.get_name() tab.get_name()
)) ))
.color(color), .color(color),
) )
.truncate(true) .truncate(true)
.sense(egui::Sense::click()), .sense(egui::Sense::click()),
) )
.clicked() .clicked()
{ {
self.selected_tab = index; self.selected_tab = index;
} }
}, },
); );
}, },
); );
}); });
} }
strip.cell(|ui| { strip.cell(|ui| {
if ui if ui
.add(egui::Label::new(" +").sense(egui::Sense::click())) .add(egui::Label::new(" +").sense(egui::Sense::click()))
.clicked() .clicked()
{ {
self.open_file(None); self.open_file(None);
} }
}); });
}); });
}); });
} }
pub fn draw_content_panel(&mut self, ctx: &egui::Context) { pub fn draw_content_panel(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
if ui if ui
.add(egui::Button::new("open directory in terminal")) .add(egui::Button::new("open directory in terminal"))
.clicked() .clicked()
{ {
let mut path = self.tabs[self.selected_tab].path.clone(); let mut path = self.tabs[self.selected_tab].path.clone();
path.pop(); path.pop();
panels::send_command(format!("cd {}", path.display())); panels::send_command(format!("cd {}", path.display()));
} }
ui.label("Picked file:"); ui.label("Picked file:");
ui.monospace( ui.monospace(
self.tabs[self.selected_tab] self.tabs[self.selected_tab]
.path .path
.to_string_lossy() .to_string_lossy()
.to_string(), .to_string(),
); );
}); });
ui.separator(); ui.separator();
if self.tabs[self.selected_tab].language == PROJECT_EXTENSION { if self.tabs[self.selected_tab].language == PROJECT_EXTENSION {
self.draw_project_file(ui); self.draw_project_file(ui);
} else { } else {
self.draw_code_file(ui); self.draw_code_file(ui);
} }
}); });
} }
fn draw_code_file(&mut self, ui: &mut egui::Ui) { fn draw_code_file(&mut self, ui: &mut egui::Ui) {
let current_tab = &mut self.tabs[self.selected_tab]; let current_tab = &mut self.tabs[self.selected_tab];
let lines = current_tab.code.chars().filter(|&c| c == '\n').count() + 1; let lines = current_tab.code.chars().filter(|&c| c == '\n').count() + 1;
let mut override_cursor: Option<CCursorRange> = None; let mut override_cursor: Option<CCursorRange> = None;
if !self.search_menu.result_selected { if !self.search_menu.result_selected {
override_cursor = Some(CCursorRange::two( override_cursor = Some(CCursorRange::two(
CCursor::new(self.search_menu.get_cursor_start()), CCursor::new(self.search_menu.get_cursor_start()),
CCursor::new(self.search_menu.get_cursor_end()), CCursor::new(self.search_menu.get_cursor_end()),
)); ));
self.search_menu.result_selected = true; self.search_menu.result_selected = true;
} }
CodeEditor::default() CodeEditor::default()
.id_source("code editor") .id_source("code editor")
.with_rows(max(45, lines)) .with_rows(max(45, lines))
.with_fontsize(self.font_size) .with_fontsize(self.font_size)
.with_theme(self.theme) .with_theme(self.theme)
.with_syntax(to_syntax(&current_tab.language)) .with_syntax(to_syntax(&current_tab.language))
.with_numlines(true) .with_numlines(true)
.show( .show(
ui, ui,
&mut current_tab.code, &mut current_tab.code,
&mut current_tab.saved, &mut current_tab.saved,
&mut current_tab.last_cursor, &mut current_tab.last_cursor,
&mut current_tab.scroll_offset, &mut current_tab.scroll_offset,
override_cursor, override_cursor,
); );
} }
fn draw_project_file(&mut self, ui: &mut egui::Ui) { fn draw_project_file(&mut self, ui: &mut egui::Ui) {
let current_tab = &mut self.tabs[self.selected_tab]; let current_tab = &mut self.tabs[self.selected_tab];
self.project_content self.project_content
.update_from_code(current_tab.code.clone()); .update_from_code(current_tab.code.clone());
panels::draw_project(ui, self.theme, &mut self.project_content); panels::draw_project(ui, self.theme, &mut self.project_content);
match self.project_content.save_to_code() { match self.project_content.save_to_code() {
Ok(code) => current_tab.code = code, Ok(code) => current_tab.code = code,
Err(_err) => (), Err(_err) => (),
} }
} }
pub fn draw_windows(&mut self, ctx: &egui::Context) { pub fn draw_windows(&mut self, ctx: &egui::Context) {
if self.project_content.item_window.visible { if self.project_content.item_window.visible {
if self.project_content.categories.len() > 1 if self.project_content.categories.len() > 1
&& !self.project_content.categories[self.project_content.selected_item.category] && !self.project_content.categories[self.project_content.selected_item.category]
.content .content
.is_empty() .is_empty()
{ {
self.project_content.item_window.show( self.project_content.item_window.show(
ctx, ctx,
&mut self.project_content.categories &mut self.project_content.categories
[self.project_content.selected_item.category] [self.project_content.selected_item.category]
.content[self.project_content.selected_item.row], .content[self.project_content.selected_item.row],
); );
} else { } else {
self.project_content.item_window.visible = false; self.project_content.item_window.visible = false;
} }
} }
if self.search_menu.visible { if self.search_menu.visible {
self.search_menu self.search_menu
.show(ctx, &mut self.tabs, &mut self.selected_tab); .show(ctx, &mut self.tabs, &mut self.selected_tab);
} }
if self.close_tab_confirm.visible { if self.close_tab_confirm.visible {
self.close_tab_confirm.show(ctx); self.close_tab_confirm.show(ctx);
} }
if self.refresh_confirm.visible { if self.refresh_confirm.visible {
self.refresh_confirm.show(ctx); self.refresh_confirm.show(ctx);
} }
if self.exit_confirm.visible { if self.exit_confirm.visible {
self.exit_confirm.show(ctx); self.exit_confirm.show(ctx);
} }
if self.exit_confirm.proceed { if self.exit_confirm.proceed {
for tab in self.tabs.iter_mut() { for tab in self.tabs.iter_mut() {
tab.saved = true; tab.saved = true;
} }
egui::Context::send_viewport_cmd(ctx, egui::ViewportCommand::Close); egui::Context::send_viewport_cmd(ctx, egui::ViewportCommand::Close);
} }
if self.shortcuts_menu.visible { if self.shortcuts_menu.visible {
self.shortcuts_menu.show(ctx); self.shortcuts_menu.show(ctx);
} }
if self.settings_menu.visible { if self.settings_menu.visible {
self.settings_menu.show(ctx); self.settings_menu.show(ctx);
} }
if self.settings_menu.updated { if self.settings_menu.updated {
self.theme = self.settings_menu.theme; self.theme = self.settings_menu.theme;
} }
self.handle_confirm(); self.handle_confirm();
} }
} }
fn to_syntax(language: &str) -> Syntax { fn to_syntax(language: &str) -> Syntax {
match language { match language {
"py" => Syntax::python(), "py" => Syntax::python(),
"rs" => Syntax::rust(), "rs" => Syntax::rust(),
_ => Syntax::shell(), _ => Syntax::shell(),
} }
} }
pub fn format_path(path: &Path) -> String { pub fn format_path(path: &Path) -> String {
let components: Vec<&OsStr> = path let components: Vec<&OsStr> = path
.components() .components()
.rev() .rev()
.take(DISPLAY_PATH_DEPTH) .take(DISPLAY_PATH_DEPTH)
.filter_map(|component| match component { .filter_map(|component| match component {
Component::RootDir | Component::CurDir => None, Component::RootDir | Component::CurDir => None,
_ => Some(component.as_os_str()), _ => Some(component.as_os_str()),
}) })
.collect(); .collect();
format!( format!(
"{}>", "{}>",
components components
.iter() .iter()
.rev() .rev()
.map(|&c| c.to_string_lossy()) .map(|&c| c.to_string_lossy())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("/") .join("/")
) )
} }

View file

View file

@ -17,7 +17,7 @@ const TITLE: &str = " debug";
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
const TITLE: &str = ""; const TITLE: &str = "";
const ALLOWED_FILE_EXTENSIONS: [&str; 7] = ["", "rs", "toml", "txt", "project", "sh", "md", "html", "js", "css", "php", "py"]; const ALLOWED_FILE_EXTENSIONS: [&str; 12] = ["", "rs", "toml", "txt", "project", "sh", "md", "html", "js", "css", "php", "py"];
const PROJECT_EXTENSION: &str = "project"; const PROJECT_EXTENSION: &str = "project";
const TERMINAL_HEIGHT: f32 = 200.0; const TERMINAL_HEIGHT: f32 = 200.0;
const TERMINAL_RANGE: Range<f32> = 100.0..600.0; const TERMINAL_RANGE: Range<f32> = 100.0..600.0;
@ -186,6 +186,11 @@ impl eframe::App for Calcifer {
self.handle_save_file(self.save_tab()); self.handle_save_file(self.save_tab());
} }
if ctx.input(|i| i.key_pressed(egui::Key::T) && i.modifiers.ctrl) {
self.file_tree = None;
self.tree_dir_opened = vec![];
}
if ctx.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl && i.modifiers.shift) { if ctx.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl && i.modifiers.shift) {
self.handle_save_file(self.save_tab_as()); self.handle_save_file(self.save_tab_as());
} }