From 688ab846617b4196fff9726c06506c7c22c65cc6 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 9 Aug 2023 14:52:22 -0700 Subject: [PATCH] apprt/embedded: allow noting that selection clipboard is not supported --- include/ghostty.h | 1 + macos/Sources/Ghostty/AppState.swift | 1 + src/Surface.zig | 8 ++++++++ src/apprt/embedded.zig | 13 +++++++++++++ 4 files changed, 23 insertions(+) diff --git a/include/ghostty.h b/include/ghostty.h index da767c37f..664ae8011 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -253,6 +253,7 @@ typedef void (*ghostty_runtime_toggle_fullscreen_cb)(void *, bool); typedef struct { void *userdata; + bool supports_selection_clipboard; ghostty_runtime_wakeup_cb wakeup_cb; ghostty_runtime_reload_config_cb reload_config_cb; ghostty_runtime_set_title_cb set_title_cb; diff --git a/macos/Sources/Ghostty/AppState.swift b/macos/Sources/Ghostty/AppState.swift index 3d15c3bc5..3bc180288 100644 --- a/macos/Sources/Ghostty/AppState.swift +++ b/macos/Sources/Ghostty/AppState.swift @@ -54,6 +54,7 @@ extension Ghostty { // uses to interface with the application runtime environment. var runtime_cfg = ghostty_runtime_config_s( userdata: Unmanaged.passUnretained(self).toOpaque(), + supports_selection_clipboard: false, wakeup_cb: { userdata in AppState.wakeup(userdata) }, reload_config_cb: { userdata in AppState.reloadConfig(userdata) }, set_title_cb: { userdata, title in AppState.setTitle(userdata, title: title) }, diff --git a/src/Surface.zig b/src/Surface.zig index 572fbc721..d40a1dfa5 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -849,6 +849,14 @@ fn setSelection(self: *Surface, sel_: ?terminal.Selection) void { const sel = sel_ orelse return; if (prev_) |prev| if (std.meta.eql(sel, prev)) return; + // Check if our runtime supports the selection clipboard at all. + // We can save a lot of work if it doesn't. + if (@hasDecl(apprt.runtime.Surface, "supportsClipboard")) { + if (!self.rt_surface.supportsClipboard(clipboard)) { + return; + } + } + var buf = self.io.terminal.screen.selectionString( self.alloc, sel, diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index fe9016ab6..ca6b0eaa9 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -32,6 +32,9 @@ pub const App = struct { /// Userdata that is passed to all the callbacks. userdata: AppUD = null, + /// True if the selection clipboard is supported. + supports_selection_clipboard: bool = false, + /// Callback called to wakeup the event loop. This should trigger /// a full tick of the app loop. wakeup: *const fn (AppUD) callconv(.C) void, @@ -239,6 +242,16 @@ pub const Surface = struct { ); } + pub fn supportsClipboard( + self: *const Surface, + clipboard_type: apprt.Clipboard, + ) bool { + return switch (clipboard_type) { + .standard => true, + .selection => self.app.opts.supports_selection_clipboard, + }; + } + pub fn getClipboardString( self: *const Surface, clipboard_type: apprt.Clipboard,