From e31e25f54d2721ddb1819a35e826a8b33ff9fa6c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 3 May 2024 20:17:22 -0700 Subject: [PATCH] apprt/embedded: add draw now API --- include/ghostty.h | 1 + macos/Sources/Ghostty/SurfaceView_AppKit.swift | 2 +- src/Surface.zig | 7 +++++++ src/apprt/embedded.zig | 13 +++++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/ghostty.h b/include/ghostty.h index d65eb7203..270dcff6e 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -504,6 +504,7 @@ ghostty_app_t ghostty_surface_app(ghostty_surface_t); bool ghostty_surface_transparent(ghostty_surface_t); bool ghostty_surface_needs_confirm_quit(ghostty_surface_t); void ghostty_surface_refresh(ghostty_surface_t); +void ghostty_surface_draw(ghostty_surface_t); void ghostty_surface_set_content_scale(ghostty_surface_t, double, double); void ghostty_surface_set_focus(ghostty_surface_t, bool); void ghostty_surface_set_occlusion(ghostty_surface_t, bool); diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index c6cddd30f..60f930c8a 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -439,7 +439,7 @@ extension Ghostty { override func updateLayer() { guard let surface = self.surface else { return } - ghostty_surface_refresh(surface); + ghostty_surface_draw(surface); } override func acceptsFirstMouse(for event: NSEvent?) -> Bool { diff --git a/src/Surface.zig b/src/Surface.zig index f142515c5..053d28dd1 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -553,6 +553,13 @@ pub fn close(self: *Surface) void { self.rt_surface.close(self.needsConfirmQuit()); } +/// Forces the surface to render. This is useful for when the surface +/// is in the middle of animation (such as a resize, etc.) or when +/// the render timer is managed manually by the apprt. +pub fn draw(self: *Surface) !void { + try self.renderer_thread.draw_now.notify(); +} + /// Activate the inspector. This will begin collecting inspection data. /// This will not affect the GUI. The GUI must use performAction to /// show/hide the inspector UI. diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index f3fa2f737..a90934b1c 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -653,6 +653,13 @@ pub const Surface = struct { }; } + pub fn draw(self: *Surface) void { + self.core_surface.draw() catch |err| { + log.err("error in draw err={}", .{err}); + return; + }; + } + pub fn updateContentScale(self: *Surface, x: f64, y: f64) void { // We are an embedded API so the caller can send us all sorts of // garbage. We want to make sure that the float values are valid @@ -1525,6 +1532,12 @@ pub const CAPI = struct { surface.refresh(); } + /// Tell the surface that it needs to schedule a render + /// call as soon as possible (NOW if possible). + export fn ghostty_surface_draw(surface: *Surface) void { + surface.draw(); + } + /// Update the size of a surface. This will trigger resize notifications /// to the pty and the renderer. export fn ghostty_surface_set_size(surface: *Surface, w: u32, h: u32) void {