105 lines
2.6 KiB
Zig
105 lines
2.6 KiB
Zig
const std = @import("std");
|
|
const builtin = @import("builtin");
|
|
|
|
const Allocator = std.mem.Allocator;
|
|
const log = std.log.scoped(.platform);
|
|
|
|
const OSPlatform = switch (builtin.os.tag) {
|
|
.windows => @import("windows.zig"),
|
|
else => @panic("Platform layer doesn't support OS")
|
|
};
|
|
|
|
var impl: OSPlatform = undefined;
|
|
|
|
pub const OpenFileOptions = struct {
|
|
pub const Filter = struct {
|
|
const Name = std.BoundedArray(u8, 32);
|
|
const Format = std.BoundedArray(u8, 32);
|
|
|
|
name: Name = .{},
|
|
format: Format = .{}
|
|
};
|
|
|
|
pub const Style = enum { open, save };
|
|
pub const Kind = enum { file, folder };
|
|
|
|
filters: std.BoundedArray(Filter, 4) = .{},
|
|
default_filter: ?usize = null,
|
|
file_must_exist: bool = false,
|
|
prompt_creation: bool = false,
|
|
prompt_overwrite: bool = false,
|
|
kind: Kind = .file,
|
|
style: Style = .open,
|
|
|
|
pub fn appendFilter(self: *OpenFileOptions, name: []const u8, format: []const u8) !void {
|
|
try self.filters.append(Filter{
|
|
.name = try Filter.Name.fromSlice(name),
|
|
.format = try Filter.Format.fromSlice(format)
|
|
});
|
|
}
|
|
};
|
|
|
|
pub const FilePickerId = usize;
|
|
pub const FilePickerStatus = enum {
|
|
in_progress,
|
|
success,
|
|
canceled,
|
|
failure
|
|
};
|
|
|
|
pub fn toggleConsoleWindow() void {
|
|
impl.toggleConsoleWindow();
|
|
}
|
|
|
|
pub fn spawnFilePicker(opts: *const OpenFileOptions) !FilePickerId {
|
|
return impl.spawnFilePicker(opts);
|
|
}
|
|
|
|
pub fn isFilePickerOpen() bool {
|
|
return impl.isFilePickerOpen();
|
|
}
|
|
|
|
pub fn checkFilePicker(id: FilePickerId) ?FilePickerStatus {
|
|
return impl.checkFilePicker(id);
|
|
}
|
|
|
|
pub fn getFilePickerPath(allocator: std.mem.Allocator, id: FilePickerId) ![]u8 {
|
|
return impl.getFilePickerPath(allocator, id);
|
|
}
|
|
|
|
pub fn waitUntilFilePickerDone(allocator: std.mem.Allocator, optional_id: *?FilePickerId) ?[]u8 {
|
|
const id = optional_id.* orelse return null;
|
|
|
|
const status = checkFilePicker(id) orelse {
|
|
optional_id.* = null;
|
|
return null;
|
|
};
|
|
|
|
if (status == .in_progress) {
|
|
return null;
|
|
}
|
|
|
|
defer optional_id.* = null;
|
|
|
|
if (status == .success) {
|
|
if (getFilePickerPath(allocator, id)) |path| {
|
|
return path;
|
|
} else |e| {
|
|
log.err("getFilePickerPath(): {}", .{ e });
|
|
}
|
|
} else if (status == .canceled) {
|
|
// Ignore this
|
|
} else if (status == .failure) {
|
|
log.err("Unknown error occured while file picker was open", .{ });
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
pub fn init(allocator: std.mem.Allocator) !void {
|
|
try impl.init(allocator);
|
|
}
|
|
|
|
pub fn deinit(allocator: std.mem.Allocator) void {
|
|
impl.deinit(allocator);
|
|
} |