|
| 1 | +import AppKit |
| 2 | +import Carbon.HIToolbox |
| 3 | +import Testing |
| 4 | + |
| 5 | +#if canImport(cmux_DEV) |
| 6 | + @testable import cmux_DEV |
| 7 | +#elseif canImport(cmux) |
| 8 | + @testable import cmux |
| 9 | +#endif |
| 10 | + |
| 11 | +/// Regression coverage for https://github.com/manaflow-ai/cmux/issues/5993: |
| 12 | +/// cmux ignored `macos-option-as-alt` left/right and captured Option before |
| 13 | +/// character composition. |
| 14 | +/// |
| 15 | +/// libghostty applies `macos-option-as-alt = left|right` (both in |
| 16 | +/// `ghostty_surface_key_translation_mods` and in the key encoder's |
| 17 | +/// Alt-prefix rules) from the `GHOSTTY_MODS_*_RIGHT` side bits of the mods |
| 18 | +/// cmux sends. If cmux maps both physical Option keys to the same generic |
| 19 | +/// `GHOSTTY_MODS_ALT`, every Option key looks like the left one: with |
| 20 | +/// `= left` the right Option can never compose characters (`…`, `@`, `ą`, |
| 21 | +/// `/`), and with `= right` the right Option is never treated as Alt. |
| 22 | +@MainActor |
| 23 | +@Suite struct GhosttyOptionAsAltModsTests { |
| 24 | + // MARK: NSEvent flags -> libghostty mods side bits |
| 25 | + |
| 26 | + @Test func rightOptionCarriesAltAndAltRightSideBit() { |
| 27 | + let raw = NSEvent.ModifierFlags.option.rawValue | UInt(NX_DEVICERALTKEYMASK) |
| 28 | + let mods = cmuxGhosttyModsFromFlags(modifierFlagsRawValue: raw) |
| 29 | + #expect(mods.rawValue & GHOSTTY_MODS_ALT.rawValue != 0) |
| 30 | + #expect( |
| 31 | + mods.rawValue & GHOSTTY_MODS_ALT_RIGHT.rawValue != 0, |
| 32 | + "right Option must set GHOSTTY_MODS_ALT_RIGHT so macos-option-as-alt = left|right can distinguish sides" |
| 33 | + ) |
| 34 | + } |
| 35 | + |
| 36 | + @Test func leftOptionCarriesAltWithoutAltRightSideBit() { |
| 37 | + let raw = NSEvent.ModifierFlags.option.rawValue | UInt(NX_DEVICELALTKEYMASK) |
| 38 | + let mods = cmuxGhosttyModsFromFlags(modifierFlagsRawValue: raw) |
| 39 | + #expect(mods.rawValue & GHOSTTY_MODS_ALT.rawValue != 0) |
| 40 | + #expect(mods.rawValue & GHOSTTY_MODS_ALT_RIGHT.rawValue == 0) |
| 41 | + } |
| 42 | + |
| 43 | + @Test func rightShiftCarriesShiftRightSideBit() { |
| 44 | + let raw = NSEvent.ModifierFlags.shift.rawValue | UInt(NX_DEVICERSHIFTKEYMASK) |
| 45 | + let mods = cmuxGhosttyModsFromFlags(modifierFlagsRawValue: raw) |
| 46 | + #expect(mods.rawValue & GHOSTTY_MODS_SHIFT.rawValue != 0) |
| 47 | + #expect(mods.rawValue & GHOSTTY_MODS_SHIFT_RIGHT.rawValue != 0) |
| 48 | + } |
| 49 | + |
| 50 | + @Test func rightControlCarriesCtrlRightSideBit() { |
| 51 | + let raw = NSEvent.ModifierFlags.control.rawValue | UInt(NX_DEVICERCTLKEYMASK) |
| 52 | + let mods = cmuxGhosttyModsFromFlags(modifierFlagsRawValue: raw) |
| 53 | + #expect(mods.rawValue & GHOSTTY_MODS_CTRL.rawValue != 0) |
| 54 | + #expect(mods.rawValue & GHOSTTY_MODS_CTRL_RIGHT.rawValue != 0) |
| 55 | + } |
| 56 | + |
| 57 | + @Test func rightCommandCarriesSuperRightSideBit() { |
| 58 | + let raw = NSEvent.ModifierFlags.command.rawValue | UInt(NX_DEVICERCMDKEYMASK) |
| 59 | + let mods = cmuxGhosttyModsFromFlags(modifierFlagsRawValue: raw) |
| 60 | + #expect(mods.rawValue & GHOSTTY_MODS_SUPER.rawValue != 0) |
| 61 | + #expect(mods.rawValue & GHOSTTY_MODS_SUPER_RIGHT.rawValue != 0) |
| 62 | + } |
| 63 | + |
| 64 | + @Test func genericModifiersMapWithoutSideBits() { |
| 65 | + let raw = NSEvent.ModifierFlags.shift.rawValue |
| 66 | + | NSEvent.ModifierFlags.control.rawValue |
| 67 | + | NSEvent.ModifierFlags.option.rawValue |
| 68 | + | NSEvent.ModifierFlags.command.rawValue |
| 69 | + let mods = cmuxGhosttyModsFromFlags(modifierFlagsRawValue: raw) |
| 70 | + #expect(mods.rawValue & GHOSTTY_MODS_SHIFT.rawValue != 0) |
| 71 | + #expect(mods.rawValue & GHOSTTY_MODS_CTRL.rawValue != 0) |
| 72 | + #expect(mods.rawValue & GHOSTTY_MODS_ALT.rawValue != 0) |
| 73 | + #expect(mods.rawValue & GHOSTTY_MODS_SUPER.rawValue != 0) |
| 74 | + #expect(mods.rawValue & GHOSTTY_MODS_SHIFT_RIGHT.rawValue == 0) |
| 75 | + #expect(mods.rawValue & GHOSTTY_MODS_CTRL_RIGHT.rawValue == 0) |
| 76 | + #expect(mods.rawValue & GHOSTTY_MODS_ALT_RIGHT.rawValue == 0) |
| 77 | + #expect(mods.rawValue & GHOSTTY_MODS_SUPER_RIGHT.rawValue == 0) |
| 78 | + } |
| 79 | + |
| 80 | + // MARK: libghostty translation mods -> AppKit translation flags |
| 81 | + |
| 82 | + @Test func translationFlagsDropOptionWhenGhosttyStripsAlt() { |
| 83 | + // macos-option-as-alt stripped Alt for this side: the AppKit |
| 84 | + // character translation must not apply Option (Alt/Meta encoding). |
| 85 | + let translated = cmuxTranslationModifierFlags( |
| 86 | + original: [.option], |
| 87 | + ghosttyTranslationMods: GHOSTTY_MODS_NONE |
| 88 | + ) |
| 89 | + #expect(!translated.contains(.option)) |
| 90 | + } |
| 91 | + |
| 92 | + @Test func translationFlagsKeepOptionWhenGhosttyKeepsAlt() { |
| 93 | + // Option on the composing side must stay available to AppKit so |
| 94 | + // Option-composed characters keep working. |
| 95 | + let translated = cmuxTranslationModifierFlags( |
| 96 | + original: [.option, .shift], |
| 97 | + ghosttyTranslationMods: ghostty_input_mods_e( |
| 98 | + rawValue: GHOSTTY_MODS_ALT.rawValue | GHOSTTY_MODS_SHIFT.rawValue |
| 99 | + ) |
| 100 | + ) |
| 101 | + #expect(translated.contains(.option)) |
| 102 | + #expect(translated.contains(.shift)) |
| 103 | + } |
| 104 | + |
| 105 | + @Test func translationFlagsPreserveFlagsGhosttyDoesNotModel() { |
| 106 | + let translated = cmuxTranslationModifierFlags( |
| 107 | + original: [.option, .function, .numericPad], |
| 108 | + ghosttyTranslationMods: GHOSTTY_MODS_NONE |
| 109 | + ) |
| 110 | + #expect(translated.contains(.function)) |
| 111 | + #expect(translated.contains(.numericPad)) |
| 112 | + #expect(!translated.contains(.option)) |
| 113 | + } |
| 114 | + |
| 115 | + // MARK: Option composition per keyboard layout (issue #5993 acceptance) |
| 116 | + |
| 117 | + @Test func usLayoutOptionSemicolonComposesEllipsis() throws { |
| 118 | + try expectOptionComposes( |
| 119 | + layoutID: "com.apple.keylayout.US", |
| 120 | + keyCode: UInt16(kVK_ANSI_Semicolon), |
| 121 | + expected: "…" |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + @Test func germanLayoutOptionLComposesAtSign() throws { |
| 126 | + try expectOptionComposes( |
| 127 | + layoutID: "com.apple.keylayout.German", |
| 128 | + keyCode: UInt16(kVK_ANSI_L), |
| 129 | + expected: "@" |
| 130 | + ) |
| 131 | + } |
| 132 | + |
| 133 | + @Test func polishProLayoutOptionAComposesAOgonek() throws { |
| 134 | + try expectOptionComposes( |
| 135 | + layoutID: "com.apple.keylayout.PolishPro", |
| 136 | + keyCode: UInt16(kVK_ANSI_A), |
| 137 | + expected: "ą" |
| 138 | + ) |
| 139 | + } |
| 140 | + |
| 141 | + @Test func canadianCSALayoutOptionComposesSlash() throws { |
| 142 | + // On Canadian-CSA the key at the ANSI-slash position types "é"; |
| 143 | + // Option/AltGr must still produce "/" (issue #5025). |
| 144 | + try expectOptionComposes( |
| 145 | + layoutID: "com.apple.keylayout.Canadian-CSA", |
| 146 | + keyCode: UInt16(kVK_ANSI_Slash), |
| 147 | + expected: "/" |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + private func expectOptionComposes( |
| 152 | + layoutID: String, |
| 153 | + keyCode: UInt16, |
| 154 | + expected: String |
| 155 | + ) throws { |
| 156 | + let composed = try #require( |
| 157 | + KeyboardLayout.textInputCharacter( |
| 158 | + forKeyCode: keyCode, |
| 159 | + modifierFlags: .option, |
| 160 | + inputSourceID: layoutID |
| 161 | + ), |
| 162 | + Comment(rawValue: "input source \(layoutID) unavailable or produced no character") |
| 163 | + ) |
| 164 | + #expect( |
| 165 | + composed == expected, |
| 166 | + Comment(rawValue: "Option translation on \(layoutID) produced \(composed) instead of \(expected)") |
| 167 | + ) |
| 168 | + } |
| 169 | +} |
0 commit comments