mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-06-09 13:18:18 +00:00
macos: complete cimgui events
This commit is contained in:
@@ -815,6 +815,7 @@ pub const Inspector = struct {
|
||||
surface: *Surface,
|
||||
ig_ctx: *cimgui.c.ImGuiContext,
|
||||
backend: ?Backend = null,
|
||||
keymap_state: input.Keymap.State = .{},
|
||||
|
||||
/// Our previous instant used to calculate delta time for animations.
|
||||
instant: ?std.time.Instant = null,
|
||||
@@ -848,6 +849,12 @@ pub const Inspector = struct {
|
||||
cimgui.c.igDestroyContext(self.ig_ctx);
|
||||
}
|
||||
|
||||
/// Queue a render for the next frame.
|
||||
pub fn queueRender(self: *Inspector) void {
|
||||
// TODO
|
||||
_ = self;
|
||||
}
|
||||
|
||||
/// Initialize the inspector for a metal backend.
|
||||
pub fn initMetal(self: *Inspector, device: objc.Object) bool {
|
||||
defer device.msgSend(void, objc.sel("release"), .{});
|
||||
@@ -928,6 +935,162 @@ pub const Inspector = struct {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn mouseButtonCallback(
|
||||
self: *Inspector,
|
||||
action: input.MouseButtonState,
|
||||
button: input.MouseButton,
|
||||
mods: input.Mods,
|
||||
) void {
|
||||
_ = mods;
|
||||
|
||||
self.queueRender();
|
||||
cimgui.c.igSetCurrentContext(self.ig_ctx);
|
||||
const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO();
|
||||
|
||||
const imgui_button = switch (button) {
|
||||
.left => cimgui.c.ImGuiMouseButton_Left,
|
||||
.middle => cimgui.c.ImGuiMouseButton_Middle,
|
||||
.right => cimgui.c.ImGuiMouseButton_Right,
|
||||
else => return, // unsupported
|
||||
};
|
||||
|
||||
cimgui.c.ImGuiIO_AddMouseButtonEvent(io, imgui_button, action == .press);
|
||||
}
|
||||
|
||||
pub fn scrollCallback(
|
||||
self: *Inspector,
|
||||
xoff: f64,
|
||||
yoff: f64,
|
||||
mods: input.ScrollMods,
|
||||
) void {
|
||||
_ = mods;
|
||||
|
||||
self.queueRender();
|
||||
cimgui.c.igSetCurrentContext(self.ig_ctx);
|
||||
const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO();
|
||||
cimgui.c.ImGuiIO_AddMouseWheelEvent(
|
||||
io,
|
||||
@floatCast(xoff),
|
||||
@floatCast(yoff),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn cursorPosCallback(self: *Inspector, x: f64, y: f64) void {
|
||||
self.queueRender();
|
||||
cimgui.c.igSetCurrentContext(self.ig_ctx);
|
||||
const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO();
|
||||
cimgui.c.ImGuiIO_AddMousePosEvent(io, @floatCast(x), @floatCast(y));
|
||||
}
|
||||
|
||||
pub fn focusCallback(self: *Inspector, focused: bool) void {
|
||||
self.queueRender();
|
||||
cimgui.c.igSetCurrentContext(self.ig_ctx);
|
||||
const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO();
|
||||
cimgui.c.ImGuiIO_AddFocusEvent(io, focused);
|
||||
}
|
||||
|
||||
pub fn textCallback(self: *Inspector, text: [:0]const u8) void {
|
||||
self.queueRender();
|
||||
cimgui.c.igSetCurrentContext(self.ig_ctx);
|
||||
const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO();
|
||||
cimgui.c.ImGuiIO_AddInputCharactersUTF8(io, text.ptr);
|
||||
}
|
||||
|
||||
pub fn keyCallback(
|
||||
self: *Inspector,
|
||||
action: input.Action,
|
||||
keycode: u32,
|
||||
mods: input.Mods,
|
||||
) !void {
|
||||
// True if this is a key down event
|
||||
const is_down = action == .press or action == .repeat;
|
||||
|
||||
// Translate our key using the keymap for our localized keyboard layout.
|
||||
// We only translate for keydown events. Otherwise, we only care about
|
||||
// the raw keycode.
|
||||
var buf: [128]u8 = undefined;
|
||||
const result: input.Keymap.Translation = if (is_down) translate: {
|
||||
const result = try self.surface.app.keymap.translate(
|
||||
&buf,
|
||||
&self.keymap_state,
|
||||
@intCast(keycode),
|
||||
mods,
|
||||
);
|
||||
|
||||
// If this is a dead key, then we're composing a character and
|
||||
// we don't do anything.
|
||||
if (result.composing) return;
|
||||
|
||||
// If the text is just a single non-printable ASCII character
|
||||
// then we clear the text. We handle non-printables in the
|
||||
// key encoder manual (such as tab, ctrl+c, etc.)
|
||||
if (result.text.len == 1 and result.text[0] < 0x20) {
|
||||
break :translate .{ .composing = false, .text = "" };
|
||||
}
|
||||
|
||||
break :translate result;
|
||||
} else .{ .composing = false, .text = "" };
|
||||
|
||||
// We want to get the physical unmapped key to process keybinds.
|
||||
const physical_key = keycode: for (input.keycodes.entries) |entry| {
|
||||
if (entry.native == keycode) break :keycode entry.key;
|
||||
} else .invalid;
|
||||
|
||||
// If the resulting text has length 1 then we can take its key
|
||||
// and attempt to translate it to a key enum and call the key callback.
|
||||
// If the length is greater than 1 then we're going to call the
|
||||
// charCallback.
|
||||
//
|
||||
// We also only do key translation if this is not a dead key.
|
||||
const key = if (!result.composing) key: {
|
||||
// If our physical key is a keypad key, we use that.
|
||||
if (physical_key.keypad()) break :key physical_key;
|
||||
|
||||
// A completed key. If the length of the key is one then we can
|
||||
// attempt to translate it to a key enum and call the key
|
||||
// callback. First try plain ASCII.
|
||||
if (result.text.len > 0) {
|
||||
if (input.Key.fromASCII(result.text[0])) |key| {
|
||||
break :key key;
|
||||
}
|
||||
}
|
||||
|
||||
break :key physical_key;
|
||||
} else .invalid;
|
||||
|
||||
self.queueRender();
|
||||
cimgui.c.igSetCurrentContext(self.ig_ctx);
|
||||
const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO();
|
||||
|
||||
// Update all our modifiers
|
||||
cimgui.c.ImGuiIO_AddKeyEvent(io, cimgui.c.ImGuiKey_LeftShift, mods.shift);
|
||||
cimgui.c.ImGuiIO_AddKeyEvent(io, cimgui.c.ImGuiKey_LeftCtrl, mods.ctrl);
|
||||
cimgui.c.ImGuiIO_AddKeyEvent(io, cimgui.c.ImGuiKey_LeftAlt, mods.alt);
|
||||
cimgui.c.ImGuiIO_AddKeyEvent(io, cimgui.c.ImGuiKey_LeftSuper, mods.super);
|
||||
|
||||
// Send our keypress
|
||||
if (key.imguiKey()) |imgui_key| {
|
||||
cimgui.c.ImGuiIO_AddKeyEvent(
|
||||
io,
|
||||
imgui_key,
|
||||
action == .press or action == .repeat,
|
||||
);
|
||||
}
|
||||
|
||||
// Send any text
|
||||
if (result.text.len > 0) text: {
|
||||
const view = std.unicode.Utf8View.init(result.text) catch |err| {
|
||||
log.warn("cannot build utf8 view over input: {}", .{err});
|
||||
break :text;
|
||||
};
|
||||
var it = view.iterator();
|
||||
|
||||
while (it.nextCodepoint()) |cp| {
|
||||
cimgui.c.ImGuiIO_AddInputCharacter(io, cp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn newFrame(self: *Inspector) !void {
|
||||
const io: *cimgui.c.ImGuiIO = cimgui.c.igGetIO();
|
||||
|
||||
@@ -1252,6 +1415,69 @@ pub const CAPI = struct {
|
||||
ptr.updateContentScale(x, y);
|
||||
}
|
||||
|
||||
export fn ghostty_inspector_mouse_button(
|
||||
ptr: *Inspector,
|
||||
action: input.MouseButtonState,
|
||||
button: input.MouseButton,
|
||||
mods: c_int,
|
||||
) void {
|
||||
ptr.mouseButtonCallback(
|
||||
action,
|
||||
button,
|
||||
@bitCast(@as(
|
||||
input.Mods.Backing,
|
||||
@truncate(@as(c_uint, @bitCast(mods))),
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
export fn ghostty_inspector_mouse_pos(ptr: *Inspector, x: f64, y: f64) void {
|
||||
ptr.cursorPosCallback(x, y);
|
||||
}
|
||||
|
||||
export fn ghostty_inspector_mouse_scroll(
|
||||
ptr: *Inspector,
|
||||
x: f64,
|
||||
y: f64,
|
||||
scroll_mods: c_int,
|
||||
) void {
|
||||
ptr.scrollCallback(
|
||||
x,
|
||||
y,
|
||||
@bitCast(@as(u8, @truncate(@as(c_uint, @bitCast(scroll_mods))))),
|
||||
);
|
||||
}
|
||||
|
||||
export fn ghostty_inspector_key(
|
||||
ptr: *Inspector,
|
||||
action: input.Action,
|
||||
keycode: u32,
|
||||
c_mods: c_int,
|
||||
) void {
|
||||
ptr.keyCallback(
|
||||
action,
|
||||
keycode,
|
||||
@bitCast(@as(
|
||||
input.Mods.Backing,
|
||||
@truncate(@as(c_uint, @bitCast(c_mods))),
|
||||
)),
|
||||
) catch |err| {
|
||||
log.err("error processing key event err={}", .{err});
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
export fn ghostty_inspector_text(
|
||||
ptr: *Inspector,
|
||||
str: [*:0]const u8,
|
||||
) void {
|
||||
ptr.textCallback(std.mem.sliceTo(str, 0));
|
||||
}
|
||||
|
||||
export fn ghostty_inspector_set_focus(ptr: *Inspector, focused: bool) void {
|
||||
ptr.focusCallback(focused);
|
||||
}
|
||||
|
||||
/// Sets the window background blur on macOS to the desired value.
|
||||
/// I do this in Zig as an extern function because I don't know how to
|
||||
/// call these functions in Swift.
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const cimgui = @import("cimgui");
|
||||
|
||||
/// A generic key input event. This is the information that is necessary
|
||||
/// regardless of apprt in order to generate the proper terminal
|
||||
@@ -377,6 +378,137 @@ pub const Key = enum(c_int) {
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns the cimgui key constant for this key.
|
||||
pub fn imguiKey(self: Key) ?c_uint {
|
||||
return switch (self) {
|
||||
.a => cimgui.c.ImGuiKey_A,
|
||||
.b => cimgui.c.ImGuiKey_B,
|
||||
.c => cimgui.c.ImGuiKey_C,
|
||||
.d => cimgui.c.ImGuiKey_D,
|
||||
.e => cimgui.c.ImGuiKey_E,
|
||||
.f => cimgui.c.ImGuiKey_F,
|
||||
.g => cimgui.c.ImGuiKey_G,
|
||||
.h => cimgui.c.ImGuiKey_H,
|
||||
.i => cimgui.c.ImGuiKey_I,
|
||||
.j => cimgui.c.ImGuiKey_J,
|
||||
.k => cimgui.c.ImGuiKey_K,
|
||||
.l => cimgui.c.ImGuiKey_L,
|
||||
.m => cimgui.c.ImGuiKey_M,
|
||||
.n => cimgui.c.ImGuiKey_N,
|
||||
.o => cimgui.c.ImGuiKey_O,
|
||||
.p => cimgui.c.ImGuiKey_P,
|
||||
.q => cimgui.c.ImGuiKey_Q,
|
||||
.r => cimgui.c.ImGuiKey_R,
|
||||
.s => cimgui.c.ImGuiKey_S,
|
||||
.t => cimgui.c.ImGuiKey_T,
|
||||
.u => cimgui.c.ImGuiKey_U,
|
||||
.v => cimgui.c.ImGuiKey_V,
|
||||
.w => cimgui.c.ImGuiKey_W,
|
||||
.x => cimgui.c.ImGuiKey_X,
|
||||
.y => cimgui.c.ImGuiKey_Y,
|
||||
.z => cimgui.c.ImGuiKey_Z,
|
||||
|
||||
.zero => cimgui.c.ImGuiKey_0,
|
||||
.one => cimgui.c.ImGuiKey_1,
|
||||
.two => cimgui.c.ImGuiKey_2,
|
||||
.three => cimgui.c.ImGuiKey_3,
|
||||
.four => cimgui.c.ImGuiKey_4,
|
||||
.five => cimgui.c.ImGuiKey_5,
|
||||
.six => cimgui.c.ImGuiKey_6,
|
||||
.seven => cimgui.c.ImGuiKey_7,
|
||||
.eight => cimgui.c.ImGuiKey_8,
|
||||
.nine => cimgui.c.ImGuiKey_9,
|
||||
|
||||
.semicolon => cimgui.c.ImGuiKey_Semicolon,
|
||||
.space => cimgui.c.ImGuiKey_Space,
|
||||
.apostrophe => cimgui.c.ImGuiKey_Apostrophe,
|
||||
.comma => cimgui.c.ImGuiKey_Comma,
|
||||
.grave_accent => cimgui.c.ImGuiKey_GraveAccent,
|
||||
.period => cimgui.c.ImGuiKey_Period,
|
||||
.slash => cimgui.c.ImGuiKey_Slash,
|
||||
.minus => cimgui.c.ImGuiKey_Minus,
|
||||
.equal => cimgui.c.ImGuiKey_Equal,
|
||||
.left_bracket => cimgui.c.ImGuiKey_LeftBracket,
|
||||
.right_bracket => cimgui.c.ImGuiKey_RightBracket,
|
||||
.backslash => cimgui.c.ImGuiKey_Backslash,
|
||||
|
||||
.up => cimgui.c.ImGuiKey_UpArrow,
|
||||
.down => cimgui.c.ImGuiKey_DownArrow,
|
||||
.left => cimgui.c.ImGuiKey_LeftArrow,
|
||||
.right => cimgui.c.ImGuiKey_RightArrow,
|
||||
.home => cimgui.c.ImGuiKey_Home,
|
||||
.end => cimgui.c.ImGuiKey_End,
|
||||
.insert => cimgui.c.ImGuiKey_Insert,
|
||||
.delete => cimgui.c.ImGuiKey_Delete,
|
||||
.caps_lock => cimgui.c.ImGuiKey_CapsLock,
|
||||
.scroll_lock => cimgui.c.ImGuiKey_ScrollLock,
|
||||
.num_lock => cimgui.c.ImGuiKey_NumLock,
|
||||
.page_up => cimgui.c.ImGuiKey_PageUp,
|
||||
.page_down => cimgui.c.ImGuiKey_PageDown,
|
||||
.escape => cimgui.c.ImGuiKey_Escape,
|
||||
.enter => cimgui.c.ImGuiKey_Enter,
|
||||
.tab => cimgui.c.ImGuiKey_Tab,
|
||||
.backspace => cimgui.c.ImGuiKey_Backspace,
|
||||
.print_screen => cimgui.c.ImGuiKey_PrintScreen,
|
||||
.pause => cimgui.c.ImGuiKey_Pause,
|
||||
|
||||
.f1 => cimgui.c.ImGuiKey_F1,
|
||||
.f2 => cimgui.c.ImGuiKey_F2,
|
||||
.f3 => cimgui.c.ImGuiKey_F3,
|
||||
.f4 => cimgui.c.ImGuiKey_F4,
|
||||
.f5 => cimgui.c.ImGuiKey_F5,
|
||||
.f6 => cimgui.c.ImGuiKey_F6,
|
||||
.f7 => cimgui.c.ImGuiKey_F7,
|
||||
.f8 => cimgui.c.ImGuiKey_F8,
|
||||
.f9 => cimgui.c.ImGuiKey_F9,
|
||||
.f10 => cimgui.c.ImGuiKey_F10,
|
||||
.f11 => cimgui.c.ImGuiKey_F11,
|
||||
.f12 => cimgui.c.ImGuiKey_F12,
|
||||
|
||||
.kp_0 => cimgui.c.ImGuiKey_Keypad0,
|
||||
.kp_1 => cimgui.c.ImGuiKey_Keypad1,
|
||||
.kp_2 => cimgui.c.ImGuiKey_Keypad2,
|
||||
.kp_3 => cimgui.c.ImGuiKey_Keypad3,
|
||||
.kp_4 => cimgui.c.ImGuiKey_Keypad4,
|
||||
.kp_5 => cimgui.c.ImGuiKey_Keypad5,
|
||||
.kp_6 => cimgui.c.ImGuiKey_Keypad6,
|
||||
.kp_7 => cimgui.c.ImGuiKey_Keypad7,
|
||||
.kp_8 => cimgui.c.ImGuiKey_Keypad8,
|
||||
.kp_9 => cimgui.c.ImGuiKey_Keypad9,
|
||||
.kp_decimal => cimgui.c.ImGuiKey_KeypadDecimal,
|
||||
.kp_divide => cimgui.c.ImGuiKey_KeypadDivide,
|
||||
.kp_multiply => cimgui.c.ImGuiKey_KeypadMultiply,
|
||||
.kp_subtract => cimgui.c.ImGuiKey_KeypadSubtract,
|
||||
.kp_add => cimgui.c.ImGuiKey_KeypadAdd,
|
||||
.kp_enter => cimgui.c.ImGuiKey_KeypadEnter,
|
||||
.kp_equal => cimgui.c.ImGuiKey_KeypadEqual,
|
||||
|
||||
.left_shift => cimgui.c.ImGuiKey_LeftShift,
|
||||
.left_control => cimgui.c.ImGuiKey_LeftCtrl,
|
||||
.left_alt => cimgui.c.ImGuiKey_LeftAlt,
|
||||
.left_super => cimgui.c.ImGuiKey_LeftSuper,
|
||||
.right_shift => cimgui.c.ImGuiKey_RightShift,
|
||||
.right_control => cimgui.c.ImGuiKey_RightCtrl,
|
||||
.right_alt => cimgui.c.ImGuiKey_RightAlt,
|
||||
.right_super => cimgui.c.ImGuiKey_RightSuper,
|
||||
|
||||
.invalid,
|
||||
.f13,
|
||||
.f14,
|
||||
.f15,
|
||||
.f16,
|
||||
.f17,
|
||||
.f18,
|
||||
.f19,
|
||||
.f20,
|
||||
.f21,
|
||||
.f22,
|
||||
.f23,
|
||||
.f24,
|
||||
.f25,
|
||||
=> null,
|
||||
};
|
||||
}
|
||||
test "fromASCII should not return keypad keys" {
|
||||
const testing = std.testing;
|
||||
try testing.expect(Key.fromASCII('0').? == .zero);
|
||||
|
||||
Reference in New Issue
Block a user