nfd-zig

nfd-zig

nfd-zig is a Zig binding to the library nativefiledialog which provides a convenient cross-platform interface to opening file dialogs in Linux, macOS and Windows.

This library has been tested on Windows 10, macOS 11.1 and Linux.

Usage

You can run the demo with zig build run. The demo’s source is in src/demo.zig.

If you want to add the library to your own project:

  • Fetch the nfd dependency:
zig fetch --save git+https://github.com/fabioarnold/nfd-zig.git

This will add the nfd dependency to your build.zig.zon. e.g.,

.nfd = .{
    .url = "git+https://github.com/fabioarnold/nfd-zig.git#0ad2a0c092ffba0c98613d619b82100c991f5ad6",
    .hash = "nfd-0.1.0-11fxvN6IBgD5rvvfjrw1wPqibMsbUJ-h2ZcGR6FOEvrm",
},
  • Add the module in your build.zig:
const nfd = b.dependency("nfd", .{ .target = target, .optimize = optimize });
const nfd_mod = nfd.module("nfd");
exe.root_module.addImport("nfd", nfd_mod);
  • Import and use it your code, e.g., main.zig:
const nfd = @import("nfd");

pub fn main() !void {
    const filter = "txt";
    const default_path: ?[]const u8 = null;
    const path: ?[]const u8 = try nfd.openFileDialog(filter, default_path);
}

Screenshot

Open dialog on Windows 10 Open dialog on Linux with GTK 3 Open dialog on MacOS with Cocoa