Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 103 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/edit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ harness = false
debug-latency = []

[dependencies]
dirs = "6.0.0"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project avoids dependencies intentionally.

lsh.workspace = true
stdext.workspace = true

Expand Down
2 changes: 1 addition & 1 deletion crates/edit/src/bin/edit/draw_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub fn draw_handle_save(ctx: &mut Context, state: &mut State) {
}
} else {
// No path? Show the file picker.
state.wants_file_picker = StateFilePicker::SaveAs;
show_file_picker(state, StateFilePicker::SaveAs);
state.wants_save = false;
ctx.needs_rerender();
}
Expand Down
4 changes: 2 additions & 2 deletions crates/edit/src/bin/edit/draw_menubar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ fn draw_menu_file(ctx: &mut Context, state: &mut State) {
draw_add_untitled_document(ctx, state);
}
if ctx.menubar_menu_button(loc(LocId::FileOpen), 'O', kbmod::CTRL | vk::O) {
state.wants_file_picker = StateFilePicker::Open;
show_file_picker(state, StateFilePicker::Open);
}
if state.documents.active().is_some() {
if ctx.menubar_menu_button(loc(LocId::FileSave), 'S', kbmod::CTRL | vk::S) {
state.wants_save = true;
}
if ctx.menubar_menu_button(loc(LocId::FileSaveAs), 'A', vk::NULL) {
state.wants_file_picker = StateFilePicker::SaveAs;
show_file_picker(state, StateFilePicker::SaveAs);
}
}
#[allow(irrefutable_let_patterns)]
Expand Down
9 changes: 6 additions & 3 deletions crates/edit/src/bin/edit/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,10 @@ fn handle_args(state: &mut State) -> apperr::Result<bool> {
dir = Some(parent.to_path_buf());
}

state.file_picker_pending_dir = DisplayablePathBuf::from_path(dir.unwrap_or(cwd));
// EN: With no explicit file or directory, file dialogs begin on the desktop.
// 中文:未指定檔案或目錄時,檔案對話框預設由桌面開始。
let default_dir = sys::desktop_dir().unwrap_or(cwd);
state.file_picker_pending_dir = DisplayablePathBuf::from_path(dir.unwrap_or(default_dir));
Ok(false)
}

Expand Down Expand Up @@ -381,11 +384,11 @@ fn draw(ctx: &mut Context, state: &mut State) {
if key == kbmod::CTRL | vk::N {
draw_add_untitled_document(ctx, state);
} else if key == kbmod::CTRL | vk::O {
state.wants_file_picker = StateFilePicker::Open;
show_file_picker(state, StateFilePicker::Open);
} else if key == kbmod::CTRL | vk::S {
state.wants_save = true;
} else if key == kbmod::CTRL_SHIFT | vk::S {
state.wants_file_picker = StateFilePicker::SaveAs;
show_file_picker(state, StateFilePicker::SaveAs);
} else if key == kbmod::CTRL | vk::W {
state.wants_close = true;
} else if key == kbmod::CTRL | vk::P {
Expand Down
36 changes: 36 additions & 0 deletions crates/edit/src/bin/edit/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use edit::{buffer, icu};
use crate::apperr;
use crate::documents::DocumentManager;
use crate::localization::*;
use crate::sys;

#[repr(transparent)]
pub struct FormatApperr(apperr::Error);
Expand Down Expand Up @@ -243,6 +244,41 @@ impl State {
}
}

/// EN: Opens a file picker and selects a useful cross-platform starting directory.
/// 中文:開啟檔案選擇器,並選擇適用於各平台的起始資料夾。
pub fn show_file_picker(state: &mut State, picker: StateFilePicker) {
debug_assert!(matches!(picker, StateFilePicker::Open | StateFilePicker::SaveAs));

// EN: Open starts on the desktop. Save As keeps a named document beside its source,
// EN: while a new untitled document starts on the desktop as well.
// 中文:開啟舊檔由桌面開始;另存已命名檔案時沿用來源目錄,未命名檔案則由桌面開始。
let target_dir = if picker == StateFilePicker::Open {
sys::desktop_dir().ok()
} else {
state
.documents
.active()
.and_then(|doc| doc.path.as_deref())
.and_then(Path::parent)
.map(Path::to_path_buf)
.or_else(|| sys::desktop_dir().ok())
};

if let Some(target_dir) = target_dir
&& state.file_picker_pending_dir.as_path() != target_dir
{
state.file_picker_pending_dir = DisplayablePathBuf::from_path(target_dir);
state.file_picker_pending_dir_revision =
state.file_picker_pending_dir_revision.wrapping_add(1);
}

state.wants_file_picker = picker;
state.file_picker_pending_name.clear();
state.file_picker_entries = None;
state.file_picker_overwrite_warning = None;
state.file_picker_autocomplete.clear();
}

pub fn draw_add_untitled_document(ctx: &mut Context, state: &mut State) {
if let Err(err) = state.documents.add_untitled() {
error_log_add(ctx, state, err);
Expand Down
8 changes: 8 additions & 0 deletions crates/edit/src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ pub use std::fs::canonicalize;
pub use unix::*;
#[cfg(windows)]
pub use windows::*;

/// EN: Returns the user's desktop directory through the platform-neutral directory provider.
/// 中文:透過跨平台目錄提供者取得使用者的桌面資料夾。
pub fn desktop_dir() -> std::io::Result<std::path::PathBuf> {
dirs::desktop_dir().ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::NotFound, "desktop directory is unavailable")
})
}