OS native dialogs for Windows, MacOS, and Linux

Related tags

GUI nfd2
Overview

💾 nfd2

Build Status Crates.io Docs Contributor Covenant Embark

nfd2 is a Rust binding to the nativefiledialog library, that provides a convenient cross-platform interface to opening file dialogs on Windows, MacOS, and Linux.

This is a fork!

The original nfd-rs crate appears essentially unmaintained by now, so we have made this fork with the intent of making sure that it is at least maintained and that bugs stay fixed so we can have something to rely on.

That being said, our ultimate goal with this crate is to eventually make it pure Rust, without a need for external C code or a build script at all.

Dependencies

Since this crate currently depends on nativefiledialog, you must have the dependencies for that project installed. See Compiling Your Programs in the README.md file for nativefiledialog for more information on the requirements for each platform.

Linux

Linux requires the GTK3 development package(s) to be installed before building:

  • Debian/Ubuntu: apt-get install libgtk-3-dev
  • Fedora: dnf install gtk3-devel

Other

Other platforms do not require additional installation.

Usage

Single File Dialog

use nfd2::Response;

fn main() {
    match nfd2::open_file_dialog(None, None).expect("oh no") {
        Response::Okay(file_path) => println!("File path = {:?}", file_path),
        Response::OkayMultiple(files) => println!("Files {:?}", files),
        Response::Cancel => println!("User canceled"),
    }
}

Multiple File Dialog

use nfd2::Response;

fn main() {
    /// Only show .jpg files
    let result = nfd2::dialog_multiple().filter("jpg").open().expect("oh no");

    match result {
        Response::Okay(file_path) => println!("File path = {:?}", file_path),
        Response::OkayMultiple(files) => println!("Files {:?}", files),
        Response::Cancel => println!("User canceled"),
    }
}

Contributing

We welcome community contributions to this project.

Please read our Contributor Guide for more information on how to get started.

License

MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) AND Zlib (Zlib or https://opensource.org/licenses/Zlib)

Comments
  • Rewrite in Rust

    Rewrite in Rust

    This crate basically provides a thin wrapper around the nativefiledialog C library, which itself is just a small abstraction on top of the OS dialogs. While nativefiledialog is small and easy to build, there's really no good reason why we can't go directly to the OS dialogs inside Rust without needing to have a submodule/wrapper for a C library.

    enhancement 
    opened by Jake-Shadle 10
  • fix FreeBSD support

    fix FreeBSD support

    Checklist

    • [x] I have read the Contributor Guide
    • [x] I have read and agree to the Code of Conduct
    • [x] I have added a description of my changes and why I'd like them included in the section below

    Description of Changes

    The libraries needed for FreeBSD support are not in the default search-path on the system, this patch adds /usr/local/lib to rustc's search path on FreeBSD.

    Related Issues

    List related issues here

    opened by Erk- 4
  • Implementing the Error trait on NFDError

    Implementing the Error trait on NFDError

    Checklist

    • [x] I have read the Contributor Guide
    • [x] I have read and agree to the Code of Conduct
    • [x] I have added a description of my changes and why I'd like them included in the section below

    Description of Changes

    Added an implementation of std::error::Error for NFDError. Used error::Error instead of just importing Error because I noticed that the rest of the file used a similar convention.

    Thanks for maintaining this crate! Please release a new version once this is merged. :)

    Related Issues

    Fixes #7

    opened by sunjay 3
  • Added Clone & PartialEq traits to Response.

    Added Clone & PartialEq traits to Response.

    Checklist

    • [x] I have read the Contributor Guide
    • [x] I have read and agree to the Code of Conduct
    • [x] I have added a description of my changes and why I'd like them included in the section below

    Description of Changes

    Added Clone & PartialEq traits to Response enum. I need to be able to compare against a Response directly (w/o using a match). As in:

    if Response::Cancel != result {
        ...
    }
    

    This requires only PartialEq. I added Clone just because. For the same reason no one though PartialEq was needed here Clone may be needed by someone in a situation no one though about. :)

    opened by virtualritz 2
  • Panic when using the library from an imgui-rs based application

    Panic when using the library from an imgui-rs based application

    Describe the bug imgui (https://github.com/Gekkio/imgui-rs/) does not provide a file picker. So I gave a try to nfd2. The following piece of code (included in the definition of a window)

    ...
            if ui.small_button(im_str!("...")) {
                if let Ok(nfd2::Response::Okay(fname)) = nfd2::open_file_dialog(
                    Some("bmp,png,jpg"), 
                    None)
                {
                    state.texture_import = fname.into();
                }
            }
    ...
    

    makes panic the application.

    The same code in a standalone application that does not use imgui-rs works perfectly. The issue exists also with the original nfdcrate.

    I have no idea if I have to report the bug there (I guess) or in ìmgui-rs` repository.

    Expected behavior No panic is expected.

    Trace

    thread 'main' panicked at 'already mutably borrowed: BorrowError', /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\src\libcore\cell.rs:798
    :9
    stack backtrace:
       0: core::fmt::write
                 at /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\/src\libcore\fmt\mod.rs:1069
       1: std::io::Write::write_fmt<std::sys::windows::stdio::Stderr>
                 at /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\/src\libstd\io\mod.rs:1427
       2: std::sys_common::backtrace::_print
                 at /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\/src\libstd\sys_common\backtrace.rs:62
       3: std::sys_common::backtrace::print
                 at /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\/src\libstd\sys_common\backtrace.rs:49
       4: std::panicking::default_hook::{{closure}}
                 at /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\/src\libstd\panicking.rs:198
       5: std::panicking::default_hook
                 at /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\/src\libstd\panicking.rs:218
       6: std::panicking::rust_panic_with_hook
                 at /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\/src\libstd\panicking.rs:511
       7: std::panicking::begin_panic_handler
                 at /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\/src\libstd\panicking.rs:419
       8: core::panicking::panic_fmt
                 at /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\/src\libcore\panicking.rs:111
       9: core::option::expect_none_failed
                 at /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\/src\libcore\option.rs:1211
      10: core::result::Result<core::cell::Ref<core::option::Option<winit::platform_impl::platform::event_loop::runner::EventLoopRunner<()>>>,
     core::cell::BorrowError>::expect<core::cell::Ref<core::option::Option<winit::platform_impl::platform::event_loop::runner::
                 at /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\src\libcore\result.rs:961
      11: core::cell::RefCell<core::option::Option<winit::platform_impl::platform::event_loop::runner::EventLoopRunner<()>>>::borrow<core::opt
    ion::Option<winit::platform_impl::platform::event_loop::runner::EventLoopRunner<()>>>
                 at /rustc/f509b26a7730d721ef87423a72b3fdf8724b4afa\src\libcore\cell.rs:798
      12: NtGdiExtSelectClipRgn
      13: winit::platform_impl::platform::event_loop::SubclassInput<()>::send_event<()>
                 at C:\Users\giotr\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.21.0\src\platform_impl\windows\event_loop.rs:104
      14: winit::platform_impl::platform::event_loop::public_window_callback<()>
                 at C:\Users\giotr\.cargo\registry\src\github.com-1ecc6299db9ec823\winit-0.21.0\src\platform_impl\windows\event_loop.rs:1382
      15: DefSubclassProc
      16: DefSubclassProc
      17: AddClipboardFormatListener
      18: GetParent
      19: CallWindowProcW
      20: glDebugEntry
      21: AddClipboardFormatListener
      22: GetParent
      23: GetParent
      24: IsRectEmpty
      25: KiUserCallbackDispatcher
      26: <unknown>
      27: DllGetClassObject
      28: <unknown>
      29: <unknown>
      30: <unknown>
      31: AddClipboardFormatListener
      32: PtInRect
      33: PtInRect
      34: PtInRect
      35: AddClipboardFormatListener
      36: GetParent
      37: GetParent
      38: IsRectEmpty
      39: KiUserCallbackDispatcher
      40: IsWindow
      41: DialogBoxIndirectParamAorW
      42: DialogBoxIndirectParamAorW
      43: DialogBoxIndirectParamW
      44: DllCanUnloadNow
      45: <unknown>
      46: NFD_OpenDialog
                 at C:\Users\giotr\.cargo\registry\src\github.com-1ecc6299db9ec823\nfd2-0.1.1\nativefiledialog\src\nfd_win.cpp:426
      47: nfd2::open_dialog
                 at C:\Users\giotr\.cargo\registry\src\github.com-1ecc6299db9ec823\nfd2-0.1.1\src\lib.rs:176
      48: nfd2::open_file_dialog
                 at C:\Users\giotr\.cargo\registry\src\github.com-1ecc6299db9ec823\nfd2-0.1.1\src\lib.rs:121
    note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
    error: process didn't exit successfully: `target\debug\magicrasters.exe` (exit code: 0xc0000409, STATUS_STACK_BUFFER_OVERRUN)
    

    Device:

    • OS: Window10
    • Browser [e.g. chrome, safari]
    • Version: 0.2.0 and 0.1.1 (not tested with previous one)
    bug 
    opened by rgiot 1
  • Response should return OsString

    Response should return OsString

    I don't think it is possible to return a file selection if the file name is not utf8 The Response returns a String, which must be valid utf8. The same constraint does not apply to files on the file system, consequently it is not possible to get a usable representation of the file name to such a file.

    Alternative methods should be added with a new Response type For instance a "OsResponse" enum with Okay(OsString) could be returned from the new method.

    enhancement 
    opened by snakehand 1
  • Cleanup

    Cleanup

    This PR updates nfd to the devel branch to take advantage of some changes that haven't hit master yet. When updating, I also noticed that there was a memory leak where the path was not being freed, so fixed that.

    This also fixes all the clippy lints as of 1.51.0.

    opened by Jake-Shadle 0
  • Update CHANGELOG

    Update CHANGELOG

    Checklist

    • [ ] I have read the Contributor Guide
    • [ ] I have read and agree to the Code of Conduct
    • [ ] I have added a description of my changes and why I'd like them included in the section below

    Description of Changes

    Describe your changes here

    Related Issues

    List related issues here

    opened by Jake-Shadle 0
  • Documenting required dependencies and extra steps when cloning the repository

    Documenting required dependencies and extra steps when cloning the repository

    Checklist

    • [x] I have read the Contributor Guide
    • [x] I have read and agree to the Code of Conduct
    • [x] I have added a description of my changes and why I'd like them included in the section below

    Description of Changes

    In creating my other PR to this repo (#9), I ran into a few things that I thought should be documented. Please feel free to nitpick my wording. Preferably using GitHub's new suggestions feature so I can quickly merge in the changes.

    Thanks for maintaining this crate!

    Related Issues

    N/A

    opened by sunjay 0
  • Change string inputs/outputs to Path/Bufs

    Change string inputs/outputs to Path/Bufs

    While the underlying C library only supports utf-8 strings, the API has been changed to use the more idiomatic Path and PathBuf types for working with the file system.

    Resolves: #4

    opened by Jake-Shadle 0
  • Archive this crate & repro

    Archive this crate & repro

    The new pure-Rust rfd crate replaces this crate, so let's close this project!

    Think we can do:

    • [ ] Push out a release of this where we mark it in the README.md and Cargo.toml as unmaintained & recommend rfd
    • [ ] Publish a RUSTSEC advisory about the same
    • [ ] Archive this github repo

    Anything else? We didn't have it listed on embark.dev/embark.rs as it was just a fork, otherwise we would have removed from there also.

    opened by repi 2
  • Missing top-level documentation

    Missing top-level documentation

    Crate is missing top-level documentation. This is the first thing users see when going to https://docs.rs/nfd2 or using rustdoc. Copying readme might be good start. Also, other items are missing (or have wrong) documentation. E.g. open_pick_folder is described as "Open save dialog".

    documentation enhancement 
    opened by Shadlock0133 0
Owner
Embark
The future belongs to the curious
Embark
A tiny, neat C library that portably invokes native file open and save dialogs.

Native File Dialog A tiny, neat C library that portably invokes native file open, folder select and save dialogs. Write dialog code once and have it p

Michael Labbe 1.5k Dec 28, 2022
A cross-platform Mod Manager for RimWorld intended to work with macOS, linux and Windows

TODOs are available here. Discussions, PRs and Issues are open for anyone who is willing to contribute. rrm Inspired by Spoons rmm. This is a cross-pl

Alejandro Osornio 7 Sep 5, 2022
Winsafe-examples - Examples of native Windows applications written in Rust with WinSafe.

WinSafe examples This repo contains several examples of native Win32 applications written in Rust with WinSafe. All examples follow the same program s

Rodrigo 40 Dec 14, 2022
Windows Native Undocumented API for Rust Language 🔥

Windows Native   The Windows-Native Rust library provides a convenient and safe way to access the native Windows undocumented APIs using the Rust prog

null 3 Aug 22, 2023
Send Windows 10 styled notifications on Windows 7.

win7-notifications Send Windows 10 styled notifications on Windows 7. Note: This crate requires a win32 event loop to be running, otherwise the notifi

Tauri 9 Aug 29, 2022
Simple and portable (but not inflexible) GUI library in C that uses the native GUI technologies of each platform it supports.

libui: a portable GUI library for C This README is being written. Status It has come to my attention that I have not been particularly clear about how

Pietro Gagliardi 10.4k Dec 31, 2022
Native Maps for Web, Mobile and Desktop

mapr Native Maps for Web, Mobile and Linux A map rendering library written in Rust. Example | Book | API | Chat in Matrix Space Project State This pro

MapLibre 942 Jan 2, 2023
A data-first Rust-native UI design toolkit.

Druid A data-first Rust-native UI toolkit. Druid is an experimental Rust-native UI toolkit. Its main goal is to offer a polished user experience. Ther

null 8.2k Dec 31, 2022
Rust bindings to the minimalist, native, cross-platform UI toolkit `libui`

Improved User Interface A cross-platform UI toolkit for Rust based on libui iui: ui-sys: iui is a simple (about 4 kLOC of Rust), small (about 800kb, i

Rust Native UI Group 865 Dec 27, 2022
Truly cross platform, truly native. multiple backend GUI for rust

WIP: Sauron-native a rust UI library that conquers all platforms ranging from desktop to mobile devices. An attempt to create a truly native, truly cr

Jovansonlee Cesar 627 Jan 5, 2023
Cross-platform native Rust menu library

A cross-platform Rust library for managing the native operating system menus.

Mads Marquart 16 Jan 6, 2023
Native Rust library for FastCGI

The FastCGI Rust implementation. Description gfcgi is a native Rust library for FastCGI. This library supports multithreaded socket listeners and HTTP

Andrey Gridchin 9 Aug 7, 2022
Example for Rust Android Native Development

Android console application example project based on rust & cargo-xdk

Zhuo Zhang 4 Mar 17, 2022
Generate Anchor IDL for Native Solana Programs.

Native Solana To Anchor IDL Autogenerate Anchor IDL from programs written in Native Solana. Disclaimer The instructions must follow strict set of rule

acheron 72 Nov 10, 2022
Provides core language-agnostic functionality for LiveView Native across platforms

LiveView Native Core This repository contains an implementation of the LiveView Native core library, which is intended to handle all the details which

LiveView Native 35 Dec 27, 2022
Crate for simple implementation of Component for Native API 1C:Enterprise written in rust

Гайд по использованию на русском языке можно посмотреть здесь и задать вопросы по использованию, но не оставляйте там комментарии об ошибках, т.к. там

Maxim Kozlov 40 Sep 30, 2023
A powerful desktop widget app for windows, built with Vue and Tauri.

DashboardX Widgets A powerful desktop widget app for windows, built with Vue and Tauri. Still in development Currently only runs on windows (uses nati

DashboardX Widgets 3 Oct 25, 2023
Unofficial Linux QQ client, based on GTK4 and libadwaita, developed with Rust and Relm4.

GTK QQ (WIP) Unofficial Linux QQ client, based on GTK4 and libadwaita, developed with Rust and Relm4. Screenshots Light Dark Note The two screenshots

Lomírus 198 Dec 24, 2022
Rust docs for the Windows API

Windows API documentation for Rust This is an experimental documentation generator for the Rust for Windows project. The documentation is published he

Microsoft 52 Dec 9, 2022