Skip to content

windows: Apply the save dialog's selected file type to the file extension - #370

Open
intergrado-cg wants to merge 1 commit into
PolyMeilex:masterfrom
intergrado:windows-save-format-extension
Open

intergrado-cg wants to merge 1 commit into
PolyMeilex:masterfrom
intergrado:windows-save-format-extension

Conversation

@intergrado-cg

Copy link
Copy Markdown

Supersedes #368 (moved from one-bit/rfd to intergrado/rfd; same commits)

Summary

The Common Item Dialog doesn't always change the extension of the name when another file type is selected, and FileDialog doesn't say which type was selected. A save dialog opened with Untitled.psd and PSD, PNG and JPEG types can therefore return Untitled.psd for the PNG type.

After the dialog returns, this asks it for the selected file type (IFileDialog::GetFileTypeIndex, 1-based) and applies that type's first extension to the returned path when:

  • the name has no extension, or
  • it still has the extension of the name passed to set_file_name, and that extension isn't one of the selected type's.

A typed extension is kept (Untitled.png typed with the PSD type selected stays .png), and so is any extension the type lists, whatever its case. A type whose first extension is * or empty changes nothing. If the renamed path already exists, the path the dialog returned is kept, since the dialog only asked to overwrite that one. If the index can't be read, or names no type, the path is unchanged.

The rule is a function of paths (with_chosen_extension), and the lookup of the type by its index is another, so both have unit tests that don't need a dialog.

This is the Windows counterpart of #341, which applies the same rules to the XDG portal. It is independent of #341 and of the macOS change in the same area.

Testing

  • Unit tests for the rule and for the 1-based index: suggested extension replaced, typed extension kept, matching extensions kept (case-insensitive), missing extension added, */empty types ignored, an index of 0, past the end, or with no types changes nothing.
  • cargo check and cargo clippy --all-targets for x86_64-pc-windows-msvc, and cargo fmt --check.
  • By hand on Windows 11 (x64): a dialog opened with a name in one type and saved with another type selected wrote the file with the selected type's extension.

…sion

The save dialog does not always change the extension of the name when
another file type is selected, and `FileDialog` does not say which type
was selected. A save dialog opened with `Untitled.psd` and PSD, PNG and
JPEG types could therefore return `Untitled.psd` for the PNG type.

After the dialog returns, ask it for the selected file type with
`GetFileTypeIndex` and apply that type's first extension to the returned
path when:

- the name has no extension, or
- it still has the extension of the name passed to `set_file_name`, and
  that extension is not one of the selected type's.

A typed extension is kept, and so is any extension the type lists. A
type whose first extension is `*` or empty changes nothing. If the
renamed path already exists, the path the dialog returned is kept, since
the dialog only asked to overwrite that one.

The rule is a function of paths, with unit tests. It is the Windows
counterpart of the XDG portal rule in PolyMeilex#341.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants