feat: display indicator when need saving
This commit is contained in:
@@ -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 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"))
|
||||
.padding(1)
|
||||
.on_press(Message::FileActionSelected(FileAction::Close(Some(index))));
|
||||
|
||||
+11
@@ -6,6 +6,7 @@ use std::{
|
||||
use iced::widget::{markdown, text_editor};
|
||||
|
||||
pub struct File {
|
||||
needs_saving: bool,
|
||||
content: text_editor::Content,
|
||||
path: Option<PathBuf>,
|
||||
markdown: Vec<markdown::Item>,
|
||||
@@ -14,6 +15,7 @@ pub struct File {
|
||||
impl Default for File {
|
||||
fn default() -> Self {
|
||||
File {
|
||||
needs_saving: false,
|
||||
content: text_editor::Content::new(),
|
||||
path: None,
|
||||
markdown: Vec::new(),
|
||||
@@ -27,6 +29,7 @@ impl File {
|
||||
let markdown = markdown::parse(content).collect();
|
||||
|
||||
File {
|
||||
needs_saving: false,
|
||||
content: text_editor_content,
|
||||
path,
|
||||
markdown,
|
||||
@@ -90,4 +93,12 @@ impl File {
|
||||
.map(|p| p.to_string_lossy().to_string())
|
||||
.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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ pub fn saved_file(state: &mut State, result: Result<PathBuf, String>) -> Task<Me
|
||||
match result {
|
||||
Ok(path) => {
|
||||
state.set_active_file_path(path);
|
||||
state.set_active_file_save_status(false);
|
||||
}
|
||||
Err(_error) => {}
|
||||
};
|
||||
|
||||
+9
-1
@@ -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) {
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user