Cross-platform utility module for Zig to open native dialogs for the filesystem, message boxes, color-picking.
Linux | Windows | macOS |
---|---|---|
# ~/<ProjectsPath>/your-awesome-projct
zig fetch --save https://github.com/ttytm/osdialog-zig/archive/main.tar.gz
// your-awesome-projct/build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
// ..
const osdialog_dep = b.dependency("osdialog", .{});
const exe = b.addExecutable(.{
.name = "your-awesome-projct",
// ..
});
exe.root_module.addImport("osdialog", osdialog_dep.module("osdialog"));
// ...
}
Ref.: osdialog-zig/src/lib.zig
/// Opens a message box and returns `true` if `OK` or `Yes` was pressed.
pub fn message(text: [*:0]const u8, opts: MessageOptions) bool
/// Opens an input prompt with an "OK" and "Cancel" button.
pub fn prompt(allocator: std.mem.Allocator, text: [*:0]const u8, opts: PromptOptions) ?[:0]u8
/// Opens an RGBA color picker and returns the selected `Color` or `null` if the selection was canceled.
pub fn color(options: ColorPickerOptions) ?Color
/// Opens a file dialog and returns the selected path or `null` if the selection was canceled.
pub fn path(allocator: std.mem.Allocator, action: PathAction, options: PathOptions) ?[:0]u8
Ref.: osdialog-zig/examples/src/main.zig
const std = @import("std");
const osd = @import("osdialog");
pub fn main() void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer _ = gpa.deinit();
_ = osd.message("Hello, World!", .{});
if (!osd.message("Do you want to continue?", .{ .buttons = .yes_no })) {
std.process.exit(0);
}
if (osd.prompt(allocator, "Give me some input", .{})) |input| {
defer allocator.free(input);
std.debug.print("Input: {s}\n", .{input});
}
if (osd.color(.{ .color = .{ .r = 247, .g = 163, .b = 29, .a = 255 } })) |selected| {
std.debug.print("Color RRR,GGG,BBB,AAA: {d},{d},{d},{d}\n", .{ selected.r, selected.g, selected.b, selected.a });
}
if (osd.path(allocator, .open, .{})) |path| {
defer allocator.free(path);
std.debug.print("Selected file: {s}\n", .{path});
}
if (osd.path(allocator, .open_dir, .{})) |path| {
defer allocator.free(path);
}
if (osd.path(allocator, .save, .{ .path = ".", .filename = "myfile.txt" })) |path| {
defer allocator.free(path);
std.debug.print("Save location: {s}\n", .{path});
}
}
# osdialog/examples
zig build run
- AndrewBelt/osdialog - The C project this library is leveraging