Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions Sources/GhosttyTerminalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,49 @@ private func cmuxTransparentWindowBaseColor() -> NSColor {
// avoids visual artifacts that can happen with a fully clear window background.
NSColor.white.withAlphaComponent(0.001)
}

// `flagsChanged` is used for both modifier presses and releases on macOS.
// Returning the wrong edge leaves Ghostty with a phantom held modifier until
// a later focus loss flushes release events into the PTY.
func cmuxGhosttyModifierActionForFlagsChanged(
keyCode: UInt16,
modifierFlagsRawValue: UInt
) -> ghostty_input_action_e? {
let flags = NSEvent.ModifierFlags(rawValue: modifierFlagsRawValue)
let modifierActive: Bool
switch keyCode {
case 0x39:
modifierActive = flags.contains(.capsLock)
case 0x38, 0x3C:
modifierActive = flags.contains(.shift)
case 0x3B, 0x3E:
modifierActive = flags.contains(.control)
case 0x3A, 0x3D:
modifierActive = flags.contains(.option)
case 0x37, 0x36:
modifierActive = flags.contains(.command)
default:
return nil
}

guard modifierActive else { return GHOSTTY_ACTION_RELEASE }

let sidePressed: Bool
switch keyCode {
case 0x3C:
sidePressed = modifierFlagsRawValue & UInt(NX_DEVICERSHIFTKEYMASK) != 0
case 0x3E:
sidePressed = modifierFlagsRawValue & UInt(NX_DEVICERCTLKEYMASK) != 0
case 0x3D:
sidePressed = modifierFlagsRawValue & UInt(NX_DEVICERALTKEYMASK) != 0
case 0x36:
sidePressed = modifierFlagsRawValue & UInt(NX_DEVICERCMDKEYMASK) != 0
default:
sidePressed = true
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
}
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

return sidePressed ? GHOSTTY_ACTION_PRESS : GHOSTTY_ACTION_RELEASE
Comment thread
austinywang marked this conversation as resolved.
Resolved
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
#endif

private func cmuxRuntimeReadClipboardCallback(
Expand Down Expand Up @@ -6981,14 +7024,17 @@ class GhosttyNSView: NSView, NSUserInterfaceValidations {
return
}

var keyEvent = ghostty_input_key_s()
keyEvent.action = GHOSTTY_ACTION_PRESS
keyEvent.keycode = UInt32(event.keyCode)
keyEvent.mods = modsFromEvent(event)
keyEvent.consumed_mods = GHOSTTY_MODS_NONE
keyEvent.text = nil
keyEvent.composing = false
_ = ghostty_surface_key(surface, keyEvent)
if !hasMarkedText(),
let action = cmuxGhosttyModifierActionForFlagsChanged(
keyCode: event.keyCode,
modifierFlagsRawValue: event.modifierFlags.rawValue
) {
var keyEvent = ghosttyKeyEvent(for: event, surface: surface)
Comment thread
austinywang marked this conversation as resolved.
Resolved
keyEvent.action = action
keyEvent.text = nil
keyEvent.composing = false
_ = sendGhosttyKey(surface, keyEvent)
}

let selectionActive = ghostty_surface_has_selection(surface)
let suppressCommandPathHover = event.modifierFlags.contains(.command) && selectionActive
Expand Down
78 changes: 78 additions & 0 deletions cmuxTests/TerminalAndGhosttyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3898,6 +3898,84 @@ final class GhosttyTerminalViewVisibilityPolicyTests: XCTestCase {
}
}

final class GhosttyModifierFlagsChangedActionTests: XCTestCase {
func testLeftShiftPressReturnsPress() {
XCTAssertEqual(
cmuxGhosttyModifierActionForFlagsChanged(
keyCode: 0x38,
modifierFlagsRawValue: NSEvent.ModifierFlags.shift.rawValue
),
GHOSTTY_ACTION_PRESS
)
}

func testLeftShiftReleaseReturnsRelease() {
XCTAssertEqual(
cmuxGhosttyModifierActionForFlagsChanged(
keyCode: 0x38,
modifierFlagsRawValue: 0
),
GHOSTTY_ACTION_RELEASE
)
}

func testRightShiftRequiresRightSideDeviceMaskForPress() {
XCTAssertEqual(
cmuxGhosttyModifierActionForFlagsChanged(
keyCode: 0x3C,
modifierFlagsRawValue: NSEvent.ModifierFlags.shift.rawValue | UInt(NX_DEVICERSHIFTKEYMASK)
),
GHOSTTY_ACTION_PRESS
)
}

func testRightShiftWithoutRightSideDeviceMaskReturnsRelease() {
XCTAssertEqual(
cmuxGhosttyModifierActionForFlagsChanged(
keyCode: 0x3C,
modifierFlagsRawValue: NSEvent.ModifierFlags.shift.rawValue
),
GHOSTTY_ACTION_RELEASE
)
}
Comment thread
austinywang marked this conversation as resolved.
Resolved

func testRightCommandRequiresRightSideDeviceMaskForPress() {
XCTAssertEqual(
cmuxGhosttyModifierActionForFlagsChanged(
keyCode: 0x36,
modifierFlagsRawValue: NSEvent.ModifierFlags.command.rawValue | UInt(NX_DEVICERCMDKEYMASK)
),
GHOSTTY_ACTION_PRESS
)
}

func testCapsLockUsesLogicalModifierState() {
XCTAssertEqual(
cmuxGhosttyModifierActionForFlagsChanged(
keyCode: 0x39,
modifierFlagsRawValue: NSEvent.ModifierFlags.capsLock.rawValue
),
GHOSTTY_ACTION_PRESS
)
XCTAssertEqual(
cmuxGhosttyModifierActionForFlagsChanged(
keyCode: 0x39,
modifierFlagsRawValue: 0
),
GHOSTTY_ACTION_RELEASE
)
}

func testNonModifierKeyReturnsNil() {
XCTAssertNil(
cmuxGhosttyModifierActionForFlagsChanged(
keyCode: 0x00,
modifierFlagsRawValue: NSEvent.ModifierFlags.shift.rawValue
)
)
}
}


final class TerminalControllerSocketListenerHealthTests: XCTestCase {
func testStableSocketBindPermissionFailureFallsBackToUserScopedSocket() {
Expand Down
Loading