folderSelect
Block while showing a native folder select dialog. Return the selected folder path or null if cancelled.
Not thread safe, but can be used from any thread.
Returned string is created by passed allocator. Not implemented for web (returns null).
Parameters
- alloc:std.mem.Allocator
- std.mem.Allocator
- opts:FolderDialogOptions
- FolderDialogOptions
Source
Implementation
pub fn folderSelect(alloc: std.mem.Allocator, opts: FolderDialogOptions) std.mem.Allocator.Error!?[]const u8 {
if (comptime !dvui.useTinyFileDialogs) return null;
var backing: [500]u8 = undefined;
var buf: []u8 = &backing;
var title: ?[*:0]const u8 = null;
if (opts.title) |t| {
const dupe = std.fmt.bufPrintSentinel(buf, "{s}", .{t}, 0) catch null;
if (dupe) |dt| {
title = dt.ptr;
buf = buf[dt.len + 1 ..];
}
}
var path: ?[*:0]const u8 = null;
if (opts.path) |p| {
const dupe = std.fmt.bufPrintSentinel(buf, "{s}", .{p}, 0) catch null;
if (dupe) |dp| {
path = dp.ptr;
buf = buf[dp.len + 1 ..];
}
}
var result: ?[]const u8 = null;
const tfd_ret = dvui.c.tinyfd_selectFolderDialog(title, path);
if (tfd_ret) |r| {
result = try alloc.dupe(u8, std.mem.sliceTo(r, 0));
}
// TODO: tinyfd maintains malloced memory from call to call, and we should
// figure out a way to get it to release that.
return result;
}