feat: display indicator when need saving

This commit is contained in:
Stevan Freeborn
2026-02-21 05:08:48 -06:00
parent 268325adfa
commit 5faa42a3e1
4 changed files with 28 additions and 2 deletions
+7 -1
View File
@@ -10,7 +10,13 @@ pub fn view<'a>(files: &'a [File], active_index: usize) -> Element<'a, Message>
let tabs = files.iter().enumerate().map(|(index, file)| { let tabs = files.iter().enumerate().map(|(index, file)| {
let is_focused = index == active_index; let is_focused = index == active_index;
let label = text(file.display_name()); let label_text = if file.needs_saving() {
format!("{}", file.display_name())
} else {
file.display_name().to_owned()
};
let label = text(label_text);
let close_btn = button(text("x")) let close_btn = button(text("x"))
.padding(1) .padding(1)
.on_press(Message::FileActionSelected(FileAction::Close(Some(index)))); .on_press(Message::FileActionSelected(FileAction::Close(Some(index))));
+11
View File
@@ -6,6 +6,7 @@ use std::{
use iced::widget::{markdown, text_editor}; use iced::widget::{markdown, text_editor};
pub struct File { pub struct File {
needs_saving: bool,
content: text_editor::Content, content: text_editor::Content,
path: Option<PathBuf>, path: Option<PathBuf>,
markdown: Vec<markdown::Item>, markdown: Vec<markdown::Item>,
@@ -14,6 +15,7 @@ pub struct File {
impl Default for File { impl Default for File {
fn default() -> Self { fn default() -> Self {
File { File {
needs_saving: false,
content: text_editor::Content::new(), content: text_editor::Content::new(),
path: None, path: None,
markdown: Vec::new(), markdown: Vec::new(),
@@ -27,6 +29,7 @@ impl File {
let markdown = markdown::parse(content).collect(); let markdown = markdown::parse(content).collect();
File { File {
needs_saving: false,
content: text_editor_content, content: text_editor_content,
path, path,
markdown, markdown,
@@ -90,4 +93,12 @@ impl File {
.map(|p| p.to_string_lossy().to_string()) .map(|p| p.to_string_lossy().to_string())
.unwrap_or_default() .unwrap_or_default()
} }
pub fn needs_saving(&self) -> bool {
self.needs_saving
}
pub fn set_needs_saving(&mut self, state: bool) {
self.needs_saving = state
}
} }
+1
View File
@@ -62,6 +62,7 @@ pub fn saved_file(state: &mut State, result: Result<PathBuf, String>) -> Task<Me
match result { match result {
Ok(path) => { Ok(path) => {
state.set_active_file_path(path); state.set_active_file_path(path);
state.set_active_file_save_status(false);
} }
Err(_error) => {} Err(_error) => {}
}; };
+9 -1
View File
@@ -37,8 +37,16 @@ impl State {
} }
} }
pub fn set_active_file_save_status(&mut self, status: bool) {
self.files[self.current_file].set_needs_saving(status);
}
pub fn apply_edit(&mut self, action: text_editor::Action) { pub fn apply_edit(&mut self, action: text_editor::Action) {
self.files[self.current_file].content_mut().perform(action); self.files[self.current_file].content_mut().perform(action.clone());
if matches!(action, text_editor::Action::Edit(_)) {
self.files[self.current_file].set_needs_saving(true);
}
} }
pub fn new_file(&mut self) { pub fn new_file(&mut self) {