Spawn the Open dialog without filename

This commit is contained in:
Leonard Hecker
2025-05-13 00:52:20 +02:00
parent 89fc267f81
commit 4d2729a44f
5 changed files with 32 additions and 18 deletions

View File

@@ -17,8 +17,9 @@ pub enum Arena {
impl Drop for Arena {
fn drop(&mut self) {
if let Arena::Delegated { delegate, borrow } = self {
assert_eq!(*borrow, delegate.borrows.get());
delegate.borrows.update(|b| b - 1);
let borrows = delegate.borrows.get();
assert_eq!(*borrow, borrows);
delegate.borrows.set(borrows - 1);
}
}
}

View File

@@ -260,10 +260,7 @@ pub fn draw_handle_wants_close(ctx: &mut Context, state: &mut State) {
match action {
Action::None => return,
Action::Save => {
state.file_picker_pending_name = doc.filename.clone();
state.wants_save = true;
}
Action::Save => state.wants_save = true,
Action::Discard => state.documents.remove_active(),
Action::Cancel => state.wants_exit = false,
}

View File

@@ -13,9 +13,21 @@ use crate::loc::*;
use crate::state::*;
pub fn draw_file_picker(ctx: &mut Context, state: &mut State) {
// The save dialog is pre-filled with the current document filename.
if state.file_picker_pending_name.is_empty()
&& state.wants_file_picker == StateFilePicker::SaveAs
{
state.file_picker_pending_name = state
.documents
.active()
.map_or("Untitled.txt", |doc| doc.filename.as_str())
.to_string();
}
let width = (ctx.size().width - 20).max(10);
let height = (ctx.size().height - 10).max(10);
let mut doit = None;
let mut done = false;
ctx.modal_begin(
"file-picker",
@@ -110,7 +122,7 @@ pub fn draw_file_picker(ctx: &mut Context, state: &mut State) {
}
}
if ctx.modal_end() {
state.wants_file_picker = StateFilePicker::None;
done = true;
}
if state.file_picker_overwrite_warning.is_some() {
@@ -157,19 +169,26 @@ pub fn draw_file_picker(ctx: &mut Context, state: &mut State) {
}
if let Some(path) = doit {
let res = if state.wants_file_picker == StateFilePicker::SaveAs {
if let Some(doc) = state.documents.active_mut() { doc.save(Some(path)) } else { Ok(()) }
} else {
let res = if state.wants_file_picker == StateFilePicker::Open {
state.documents.add_file_path(&path).map(|_| ())
} else if let Some(doc) = state.documents.active_mut() {
doc.save(Some(path))
} else {
Ok(())
};
match res {
Ok(..) => {
ctx.needs_rerender();
state.wants_file_picker = StateFilePicker::None;
done = true;
}
Err(err) => error_log_add(ctx, state, err),
}
}
if done {
state.wants_file_picker = StateFilePicker::None;
state.file_picker_pending_name = String::new();
}
}
// Returns Some(path) if the path refers to a file.

View File

@@ -243,21 +243,18 @@ fn handle_args(state: &mut State) -> apperr::Result<bool> {
}
}
let doc;
if let Some(mut file) = sys::open_stdin_if_redirected() {
doc = state.documents.add_untitled()?;
let doc = state.documents.add_untitled()?;
let mut tb = doc.buffer.borrow_mut();
tb.read_file(&mut file, None)?;
tb.mark_as_dirty();
} else if let Some(path) = path {
doc = state.documents.add_file_path(&path)?;
state.documents.add_file_path(&path)?;
} else {
doc = state.documents.add_untitled()?;
state.documents.add_untitled()?;
}
state.file_picker_pending_dir = DisplayablePathBuf::new(cwd);
state.file_picker_pending_name = doc.filename.clone();
Ok(false)
}

View File

@@ -121,7 +121,7 @@ pub struct State {
pub wants_file_picker: StateFilePicker,
pub file_picker_pending_dir: DisplayablePathBuf,
pub file_picker_pending_name: String, // This could be PathBuf, if `tui` would expose its TextBuffer for editline.
pub file_picker_pending_name: String,
pub file_picker_entries: Option<Vec<DisplayablePathBuf>>,
pub file_picker_overwrite_warning: Option<PathBuf>, // The path the warning is about.