Skip to content

Latest commit

 

History

History
160 lines (133 loc) · 6.02 KB

README.md

File metadata and controls

160 lines (133 loc) · 6.02 KB

osdialog-zig

Cross-platform utility module for Zig to open native dialogs for the filesystem, message boxes, color-picking.

Quickstart

Showcase

Linux Windows macOS
Linux File Dialog Windows File Dialog macOS File Dialog
More Examples Toggle visibility...
Linux Windows macOS
Linux Color Picker GTK3 Linux Message Linux Message with Yes and No Buttons Linux Input Prompt Linux Color Picker GTK2 Windows Color Picker Windows Message Windows Message with Yes and No Buttons Windows Input Prompt macOS Color Picker macOS Message with Yes and No Buttons macOS Input Prompt

Installation

# ~/<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"));
	// ...
}

Usage

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

Example

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

Credits