apprt/embedded: ghostty_surface_binding_action can now run any action

This commit is contained in:
Mitchell Hashimoto
2023-08-30 21:46:38 -07:00
parent 17e46bf0f4
commit c71979804e
4 changed files with 31 additions and 24 deletions

View File

@ -230,13 +230,6 @@ typedef enum {
GHOSTTY_KEY_RIGHT_SUPER,
} ghostty_input_key_e;
typedef enum {
GHOSTTY_BINDING_COPY_TO_CLIPBOARD,
GHOSTTY_BINDING_PASTE_FROM_CLIPBOARD,
GHOSTTY_BINDING_NEW_TAB,
GHOSTTY_BINDING_NEW_WINDOW,
} ghostty_binding_action_e;
// Fully defined types. This MUST be kept in sync with equivalent Zig
// structs. To find the Zig struct, grep for this type name. The documentation
// for all of these types is available in the Zig source.
@ -315,7 +308,7 @@ void ghostty_surface_ime_point(ghostty_surface_t, double *, double *);
void ghostty_surface_request_close(ghostty_surface_t);
void ghostty_surface_split(ghostty_surface_t, ghostty_split_direction_e);
void ghostty_surface_split_focus(ghostty_surface_t, ghostty_split_focus_direction_e);
void ghostty_surface_binding_action(ghostty_surface_t, ghostty_binding_action_e, void *);
bool ghostty_surface_binding_action(ghostty_surface_t, const char *, uintptr_t);
// APIs I'd like to get rid of eventually but are still needed for now.
// Don't use these unless you know what you're doing.

View File

@ -140,11 +140,17 @@ extension Ghostty {
}
func newTab(surface: ghostty_surface_t) {
ghostty_surface_binding_action(surface, GHOSTTY_BINDING_NEW_TAB, nil)
let action = "new_tab"
if (!ghostty_surface_binding_action(surface, action, UInt(action.count))) {
AppDelegate.logger.warning("action failed action=\(action)")
}
}
func newWindow(surface: ghostty_surface_t) {
ghostty_surface_binding_action(surface, GHOSTTY_BINDING_NEW_WINDOW, nil)
let action = "new_window"
if (!ghostty_surface_binding_action(surface, action, UInt(action.count))) {
AppDelegate.logger.warning("action failed action=\(action)")
}
}
func split(surface: ghostty_surface_t, direction: ghostty_split_direction_e) {

View File

@ -375,17 +375,26 @@ extension Ghostty {
@IBAction func copy(_ sender: Any?) {
guard let surface = self.surface else { return }
ghostty_surface_binding_action(surface, GHOSTTY_BINDING_COPY_TO_CLIPBOARD, nil)
let action = "copy_to_clipboard"
if (!ghostty_surface_binding_action(surface, action, UInt(action.count))) {
AppDelegate.logger.warning("action failed action=\(action)")
}
}
@IBAction func paste(_ sender: Any?) {
guard let surface = self.surface else { return }
ghostty_surface_binding_action(surface, GHOSTTY_BINDING_PASTE_FROM_CLIPBOARD, nil)
let action = "paste_from_clipboard"
if (!ghostty_surface_binding_action(surface, action, UInt(action.count))) {
AppDelegate.logger.warning("action failed action=\(action)")
}
}
@IBAction func pasteAsPlainText(_ sender: Any?) {
guard let surface = self.surface else { return }
ghostty_surface_binding_action(surface, GHOSTTY_BINDING_PASTE_FROM_CLIPBOARD, nil)
let action = "paste_from_clipboard"
if (!ghostty_surface_binding_action(surface, action, UInt(action.count))) {
AppDelegate.logger.warning("action failed action=\(action)")
}
}
// MARK: NSTextInputClient

View File

@ -882,22 +882,21 @@ pub const CAPI = struct {
/// Invoke an action on the surface.
export fn ghostty_surface_binding_action(
ptr: *Surface,
key: input.Binding.Key,
unused: *anyopaque,
) void {
// For future arguments
_ = unused;
const action: input.Binding.Action = switch (key) {
.copy_to_clipboard => .{ .copy_to_clipboard = {} },
.paste_from_clipboard => .{ .paste_from_clipboard = {} },
.new_tab => .{ .new_tab = {} },
.new_window => .{ .new_window = {} },
action_ptr: [*]const u8,
action_len: usize,
) bool {
const action_str = action_ptr[0..action_len];
const action = input.Binding.Action.parse(action_str) catch |err| {
log.err("error parsing binding action action={s} err={}", .{ action_str, err });
return false;
};
ptr.core_surface.performBindingAction(action) catch |err| {
log.err("error performing binding action action={} err={}", .{ action, err });
return false;
};
return true;
}
/// Sets the window background blur on macOS to the desired value.