From 008736c3bf8f7e45bdcf435749fe9cb7d4820c33 Mon Sep 17 00:00:00 2001 From: xdBronch <51252236+xdBronch@users.noreply.github.com> Date: Wed, 1 Nov 2023 19:19:30 -0400 Subject: [PATCH 1/2] add support for file dropping to glfw runtime --- src/apprt/glfw.zig | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index bf96e1e6c..65ab3cd57 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -353,6 +353,7 @@ pub const Surface = struct { win.setScrollCallback(scrollCallback); win.setCursorPosCallback(cursorPosCallback); win.setMouseButtonCallback(mouseButtonCallback); + win.setDropCallback(dropCallback); // Build our result self.* = .{ @@ -964,4 +965,38 @@ pub const Surface = struct { return; }; } + + fn dropCallback(window: glfw.Window, paths: [][*:0]const u8) void { + const tracy = trace(@src()); + defer tracy.end(); + + const surface = window.getUserPointer(CoreSurface) orelse return; + + var list = std.ArrayList(u8).init(surface.alloc); + defer list.deinit(); + + for (paths) |path| { + const path_slice = std.mem.span(path); + const writer = list.writer(); + + list.ensureTotalCapacity(path_slice.len * 2 + 1) catch |err| { // preallocate worst case of escaping every char + space + log.err("error in drop callback err={}", .{err}); + return; + }; + + for (path_slice) |c| { + if (std.mem.indexOfScalar(u8, "\\ ()[]{}<>\"'`!#$&;|*?\t", c)) |_| { + writer.print("\\{c}", .{c}) catch unreachable; // only error is OOM, memory preallocated + } else writer.writeByte(c) catch unreachable; // same here + } + writer.writeByte(' ') catch unreachable; // separate paths + + surface.textCallback(list.items) catch |err| { + log.err("error in drop callback err={}", .{err}); + return; + }; + + list.clearRetainingCapacity(); // avoid unnecessary reallocations + } + } }; From 657111c4101264c4d69020b0d3000e4a97e4e504 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 2 Nov 2023 21:34:43 -0700 Subject: [PATCH 2/2] apprt/glfw: small line length fixes --- src/apprt/glfw.zig | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index 65ab3cd57..be4103f77 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -977,16 +977,17 @@ pub const Surface = struct { for (paths) |path| { const path_slice = std.mem.span(path); - const writer = list.writer(); - list.ensureTotalCapacity(path_slice.len * 2 + 1) catch |err| { // preallocate worst case of escaping every char + space + // preallocate worst case of escaping every char + space + list.ensureTotalCapacity(path_slice.len * 2 + 1) catch |err| { log.err("error in drop callback err={}", .{err}); return; }; + const writer = list.writer(); for (path_slice) |c| { if (std.mem.indexOfScalar(u8, "\\ ()[]{}<>\"'`!#$&;|*?\t", c)) |_| { - writer.print("\\{c}", .{c}) catch unreachable; // only error is OOM, memory preallocated + writer.print("\\{c}", .{c}) catch unreachable; // memory preallocated } else writer.writeByte(c) catch unreachable; // same here } writer.writeByte(' ') catch unreachable; // separate paths