mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-15 16:26:08 +03:00
Merge pull request #783 from xdBronch/glfw-drop
add support for file dropping to glfw runtime
This commit is contained in:
@ -353,6 +353,7 @@ pub const Surface = struct {
|
|||||||
win.setScrollCallback(scrollCallback);
|
win.setScrollCallback(scrollCallback);
|
||||||
win.setCursorPosCallback(cursorPosCallback);
|
win.setCursorPosCallback(cursorPosCallback);
|
||||||
win.setMouseButtonCallback(mouseButtonCallback);
|
win.setMouseButtonCallback(mouseButtonCallback);
|
||||||
|
win.setDropCallback(dropCallback);
|
||||||
|
|
||||||
// Build our result
|
// Build our result
|
||||||
self.* = .{
|
self.* = .{
|
||||||
@ -964,4 +965,39 @@ pub const Surface = struct {
|
|||||||
return;
|
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);
|
||||||
|
|
||||||
|
// 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; // 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
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user