Add Ctrl+S and indicate when files are dirty

This commit is contained in:
2025-06-15 12:39:52 +02:00
parent 1ed278cc55
commit 7d234641cb
4 changed files with 111 additions and 54 deletions

View File

@ -8,8 +8,8 @@ use std::{
use crate::{file_editor::FileEditor, preferences::Preferences, util::GuiSender};
use egui::{
Align, Button, Color32, FontData, FontDefinitions, PointerButton, RichText, ScrollArea, Stroke,
Ui,
Align, Button, Color32, Context, FontData, FontDefinitions, Key, Modifiers, PointerButton,
RichText, ScrollArea, Stroke,
};
#[derive(serde::Deserialize, serde::Serialize)]
@ -36,8 +36,8 @@ pub struct Jobs {
}
impl Jobs {
fn start(&mut self, ui: &mut Ui, job: impl FnOnce() -> Option<Action> + Send + 'static) {
let ctx = ui.ctx().clone();
fn start(&mut self, ctx: &Context, job: impl FnOnce() -> Option<Action> + Send + 'static) {
let ctx = ctx.clone();
let actions_tx = self.actions_tx.clone();
self.handles.push(std::thread::spawn(move || {
// start rendering the spinner thingy
@ -68,6 +68,12 @@ impl Tab {
Tab::File(file_editor) => file_editor.title(),
}
}
pub fn is_dirty(&self) -> bool {
match self {
Tab::File(file_editor) => file_editor.is_dirty,
}
}
}
pub type TabId = usize;
@ -182,7 +188,7 @@ impl App {
Default::default()
}
fn actions_tx(&self, ctx: &egui::Context) -> GuiSender<Action> {
fn actions_tx(&self, ctx: &Context) -> GuiSender<Action> {
GuiSender::new(self.actions_tx.clone(), ctx)
}
@ -211,7 +217,7 @@ impl eframe::App for App {
eframe::set_value(storage, eframe::APP_KEY, self);
}
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
fn update(&mut self, ctx: &Context, _frame: &mut eframe::Frame) {
self.preferences.apply(ctx);
self.jobs.handles.retain(|job| !job.is_finished());
@ -224,11 +230,11 @@ impl eframe::App for App {
self.open_tab_index = Some(self.tabs.len().saturating_sub(1));
}
//ctx.input_mut(|input| {
// if input.consume_key(Modifiers::CTRL, Key::H) {
// self.buffer.push(BufferItem::Painting(Default::default()));
// }
//});
ctx.input_mut(|input| {
if input.consume_key(Modifiers::CTRL, Key::S) {
self.save_active_tab(ctx);
}
});
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
egui::containers::menu::Bar::new().ui(ui, |ui| {
@ -243,7 +249,7 @@ impl eframe::App for App {
#[cfg(not(target_arch = "wasm32"))]
if ui.button("Open File").clicked() {
self.jobs.start(ui, move || {
self.jobs.start(ui.ctx(), move || {
let file_path = rfd::FileDialog::new().pick_file()?;
let text = fs::read_to_string(&file_path)
@ -268,6 +274,19 @@ impl eframe::App for App {
}
}
let can_save_file = self
.open_tab_index
.and_then(|i| self.tabs.get(i))
.and_then(|(id, tab)| match tab {
Tab::File(file_editor) => Some((*id, file_editor)),
})
.and_then(|(_, file_editor)| file_editor.path().zip(Some(file_editor)))
.is_some();
if ui.add_enabled(can_save_file, Button::new("Save")).clicked() {
self.save_active_tab(ui.ctx());
}
let open_file =
self.open_tab_index
.and_then(|i| self.tabs.get(i))
@ -275,26 +294,6 @@ impl eframe::App for App {
Tab::File(file_editor) => Some((*id, file_editor)),
});
let open_file_with_path = open_file
.clone()
.and_then(|(_, file_editor)| file_editor.path().zip(Some(file_editor)));
if ui
.add_enabled(open_file_with_path.is_some(), Button::new("Save"))
.clicked()
{
if let Some((file_path, file_editor)) = open_file_with_path {
let text = file_editor.to_string();
let file_path = file_path.to_owned();
self.jobs.start(ui, move || {
if let Err(e) = fs::write(file_path, text.as_bytes()) {
log::error!("{e}");
};
None
});
}
}
#[cfg(not(target_arch = "wasm32"))]
if ui
.add_enabled(open_file.is_some(), Button::new("Save As"))
@ -303,7 +302,7 @@ impl eframe::App for App {
let (tab_id, editor) =
open_file.expect("We checked that open_file is_some");
let text = editor.to_string();
self.jobs.start(ui, move || {
self.jobs.start(ui.ctx(), move || {
let file_path = rfd::FileDialog::new().save_file()?;
fs::write(&file_path, text.as_bytes())
@ -331,15 +330,12 @@ impl eframe::App for App {
ui.add_space(16.0);
ui.add_space(16.0);
ScrollArea::horizontal().show(ui, |ui| {
for (i, (tab_id, tab)) in self.tabs.iter().enumerate() {
let selected = self.open_tab_index == Some(i);
let mut button = Button::new(tab.title()).selected(selected);
let dirty = i == 0; // TODO: mark as dirty when contents hasn't been saved
if dirty {
if tab.is_dirty() {
button = button.right_text(RichText::new("*").strong())
}
@ -387,4 +383,31 @@ impl App {
self.next_tab_id += 1;
self.tabs.insert(i, (id, tab));
}
fn save_active_tab(&mut self, ctx: &Context) {
let open_file = self
.open_tab_index
.and_then(|i| self.tabs.get_mut(i))
.and_then(|(id, tab)| match tab {
Tab::File(file_editor) => Some((*id, file_editor)),
})
.and_then(|(_, file_editor)| {
file_editor
.path()
.map(ToOwned::to_owned)
.zip(Some(file_editor))
});
if let Some((file_path, file_editor)) = open_file {
file_editor.is_dirty = false;
let text = file_editor.to_string();
let file_path = file_path.to_owned();
self.jobs.start(ctx, move || {
if let Err(e) = fs::write(file_path, text.as_bytes()) {
log::error!("{e}");
};
None
});
}
}
}