added tab structure

This commit is contained in:
Penwing 2024-01-19 19:21:16 +01:00
parent a18cf073c7
commit ea2c422888
2 changed files with 92 additions and 16 deletions

View file

@ -2,10 +2,7 @@
mod tools; mod tools;
use eframe::egui; use eframe::egui;
use std::path::Path; use std::{path::Path, fs, io, env};
use std::fs;
use std::io;
use std::env;
const TERMINAL_HEIGHT : f32 = 200.0; const TERMINAL_HEIGHT : f32 = 200.0;
const RED : egui::Color32 = egui::Color32::from_rgb(235, 108, 99); const RED : egui::Color32 = egui::Color32::from_rgb(235, 108, 99);
@ -32,6 +29,8 @@ struct MyApp {
picked_path: Option<String>, picked_path: Option<String>,
language: String, language: String,
code: String, code: String,
selected_tab : tools::TabNumber,
tabs: Vec<tools::Tab>,
command: String, command: String,
command_history: Vec<tools::CommandEntry>, command_history: Vec<tools::CommandEntry>,
} }
@ -43,6 +42,8 @@ impl Default for MyApp {
picked_path: None, picked_path: None,
language: "rs".into(), language: "rs".into(),
code: "// write here".into(), code: "// write here".into(),
selected_tab : tools::TabNumber::None,
tabs: Vec::new(),
command: "".into(), command: "".into(),
command_history: Vec::new(), command_history: Vec::new(),
} }
@ -54,6 +55,7 @@ impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
self.draw_tree_panel(ctx); self.draw_tree_panel(ctx);
self.draw_terminal_panel(ctx); self.draw_terminal_panel(ctx);
self.draw_tab_panel(ctx);
self.draw_code_panel(ctx); self.draw_code_panel(ctx);
} }
} }
@ -113,14 +115,30 @@ impl MyApp {
}); });
} }
fn draw_tab_panel(&mut self, ctx: &egui::Context) {
egui::TopBottomPanel::top("tabs")
.resizable(false)
.show(ctx, |ui| {
ui.horizontal(|ui| {
for (index, tab) in self.tabs.iter().enumerate() {
ui.selectable_value(&mut self.selected_tab, tools::TabNumber::get_from_n(index), tab.get_name());
}
if tools::TabNumber::get_from_n(self.tabs.len()) != tools::TabNumber::None {
ui.selectable_value(&mut self.selected_tab, tools::TabNumber::Open, "+");
}
if self.selected_tab == tools::TabNumber::Open {
if let Some(path) = rfd::FileDialog::new().pick_file() {
self.selected_tab = self.open_file(&path);
} else {
self.selected_tab = tools::TabNumber::None;
}
}
});
});
}
fn draw_code_panel(&mut self, ctx: &egui::Context) { fn draw_code_panel(&mut self, ctx: &egui::Context) {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
if ui.button("Open file…").clicked() {
if let Some(path) = rfd::FileDialog::new().pick_file() {
self.open_file(&path);
}
}
if let Some(picked_path) = &self.picked_path { if let Some(picked_path) = &self.picked_path {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Picked file:"); ui.label("Picked file:");
@ -169,18 +187,30 @@ impl MyApp {
} else { } else {
//ui.label(name.to_string_lossy()); //ui.label(name.to_string_lossy());
if ui.button(name.to_string_lossy()).clicked() { if ui.button(name.to_string_lossy()).clicked() {
self.open_file(&path) self.selected_tab = self.open_file(&path);
} }
} }
} }
Ok(()) Ok(())
} }
fn open_file(&mut self, path: &Path) { fn open_file(&mut self, path: &Path) -> tools::TabNumber {
if tools::TabNumber::get_from_n(self.tabs.len()) == tools::TabNumber::None {
return tools::TabNumber::None
}
self.picked_path = Some(path.display().to_string()); self.picked_path = Some(path.display().to_string());
let file_path = Path::new(self.picked_path.as_deref().unwrap_or_default()); let file_path = Path::new(self.picked_path.as_deref().unwrap_or_default());
self.code = fs::read_to_string(file_path).expect("Not able to read the file"); self.code = fs::read_to_string(file_path).expect("Not able to read the file");
self.language = file_path.to_str().unwrap().split('.').last().unwrap().into(); self.language = file_path.to_str().unwrap().split('.').last().unwrap().into();
let new_tab = tools::Tab {
path: path.into(),
code: fs::read_to_string(file_path).expect("Not able to read the file"),
language: file_path.to_str().unwrap().split('.').last().unwrap().into(),
};
self.tabs.push(new_tab);
return tools::TabNumber::get_from_n(self.tabs.len() - 1)
} }
} }

View file

@ -1,11 +1,55 @@
//use eframe::egui; //use eframe::egui;
//use std::io; //use std::io;
use std::process::Command; use std::{process::Command, cmp::Ordering, env, path::PathBuf};
use std::cmp::Ordering;
use std::env;
//use std::path::Path;
//use std::fs; //use std::fs;
#[derive(Debug, PartialEq, Eq)]
pub enum TabNumber {
None,
Open,
Zero,
One,
Two,
Three,
Four,
Five,
Six,
Seven,
}
impl TabNumber {
pub fn get_from_n( n: usize) -> TabNumber {
match n {
0 => TabNumber::Zero,
1 => TabNumber::One,
2 => TabNumber::Two,
3 => TabNumber::Three,
4 => TabNumber::Four,
5 => TabNumber::Five,
6 => TabNumber::Six,
7 => TabNumber::Seven,
_ => TabNumber::None,
}
}
}
pub struct Tab {
pub path : PathBuf,
pub code : String,
pub language : String,
}
impl Tab {
pub fn get_name(&self) -> String {
self.path.file_name().expect("Could not get Tab Name").to_string_lossy().to_string()
}
}
pub struct CommandEntry { pub struct CommandEntry {
pub env : String, pub env : String,
pub command : String, pub command : String,
@ -13,6 +57,7 @@ pub struct CommandEntry {
pub error : String, pub error : String,
} }
impl Default for CommandEntry { impl Default for CommandEntry {
fn default() -> Self { fn default() -> Self {
Self { Self {
@ -24,6 +69,7 @@ impl Default for CommandEntry {
} }
} }
pub fn loaded() { pub fn loaded() {
println!("Tools loaded"); println!("Tools loaded");
} }