Skip to content
Open
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
17 changes: 14 additions & 3 deletions src/platform/win32.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::ffi::OsStr;
use std::io::BufRead;
use std::os::windows::ffi::OsStrExt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -2748,18 +2750,19 @@ fn save_document_at(
encoding_override: Option<TextEncoding>,
force_save_as: bool,
) -> Result<bool> {
let (encoding, existing_path) = {
let (encoding, existing_path, display_name) = {
let doc_tab = state
.docs
.get(index)
.ok_or_else(|| AppError::new("No active document."))?;
(
encoding_override.unwrap_or(doc_tab.doc.encoding),
doc_tab.doc.path.clone(),
doc_tab.doc.display_name.clone(),
)
};
let path = if force_save_as || existing_path.is_none() {
match save_file_dialog(hwnd)? {
match save_file_dialog(hwnd, &display_name)? {
Some(path) => path,
None => return Ok(false),
}
Expand Down Expand Up @@ -5862,16 +5865,24 @@ fn open_file_dialog(hwnd: HWND) -> Result<Option<PathBuf>> {
)))
}

fn save_file_dialog(hwnd: HWND) -> Result<Option<PathBuf>> {
fn save_file_dialog(hwnd: HWND, default_name: &str) -> Result<Option<PathBuf>> {
let mut buffer = vec![0u16; 1024];
let default_wide: Vec<u16> = OsStr::new(default_name)
.encode_wide()
.filter(|&unit| unit != 0)
.collect();
let copy_len = default_wide.len().min(buffer.len() - 1);
buffer[..copy_len].copy_from_slice(&default_wide[..copy_len]);
let filter = w!("All Files\0*.*\0\0");
let def_ext = w!("txt");

let mut ofn = OPENFILENAMEW {
lStructSize: std::mem::size_of::<OPENFILENAMEW>() as u32,
hwndOwner: hwnd,
lpstrFile: PWSTR(buffer.as_mut_ptr()),
nMaxFile: buffer.len() as u32,
lpstrFilter: PCWSTR(filter.as_ptr()),
lpstrDefExt: PCWSTR(def_ext.as_ptr()),
Flags: OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT,
..Default::default()
};
Expand Down