apprt: clipboard

This commit is contained in:
Mitchell Hashimoto
2022-12-30 15:29:36 -08:00
parent 8907104e7c
commit 0e73c5eb93
2 changed files with 18 additions and 4 deletions

View File

@ -543,7 +543,7 @@ fn clipboardRead(self: *const Window, kind: u8) !void {
return; return;
} }
const data = glfw.getClipboardString() catch |err| { const data = self.windowing_system.getClipboardString() catch |err| {
log.warn("error reading clipboard: {}", .{err}); log.warn("error reading clipboard: {}", .{err});
return; return;
}; };
@ -591,7 +591,7 @@ fn clipboardWrite(self: *const Window, data: []const u8) !void {
try dec.decode(buf, data); try dec.decode(buf, data);
assert(buf[buf.len] == 0); assert(buf[buf.len] == 0);
glfw.setClipboardString(buf) catch |err| { self.windowing_system.setClipboardString(buf) catch |err| {
log.err("error setting clipboard string err={}", .{err}); log.err("error setting clipboard string err={}", .{err});
return; return;
}; };
@ -821,7 +821,7 @@ pub fn keyCallback(
}; };
defer self.alloc.free(buf); defer self.alloc.free(buf);
glfw.setClipboardString(buf) catch |err| { self.windowing_system.setClipboardString(buf) catch |err| {
log.err("error setting clipboard string err={}", .{err}); log.err("error setting clipboard string err={}", .{err});
return; return;
}; };
@ -829,7 +829,7 @@ pub fn keyCallback(
}, },
.paste_from_clipboard => { .paste_from_clipboard => {
const data = glfw.getClipboardString() catch |err| { const data = self.windowing_system.getClipboardString() catch |err| {
log.warn("error reading clipboard: {}", .{err}); log.warn("error reading clipboard: {}", .{err});
return; return;
}; };

View File

@ -194,6 +194,20 @@ pub const Window = struct {
try self.window.setTitle(slice.ptr); try self.window.setTitle(slice.ptr);
} }
/// Read the clipboard. The windowing system is responsible for allocating
/// a buffer as necessary. This should be a stable pointer until the next
/// time getClipboardString is called.
pub fn getClipboardString(self: *const Window) ![:0]const u8 {
_ = self;
return try glfw.getClipboardString();
}
/// Set the clipboard.
pub fn setClipboardString(self: *const Window, val: [:0]const u8) !void {
_ = self;
try glfw.setClipboardString(val);
}
/// The cursor position from glfw directly is in screen coordinates but /// The cursor position from glfw directly is in screen coordinates but
/// all our interface works in pixels. /// all our interface works in pixels.
fn cursorPosToPixels(self: *const Window, pos: glfw.Window.CursorPos) !glfw.Window.CursorPos { fn cursorPosToPixels(self: *const Window, pos: glfw.Window.CursorPos) !glfw.Window.CursorPos {