Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 27 additions & 5 deletions Sources/GhosttyTerminalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3257,6 +3257,15 @@ class GhosttyApp {
#endif
return false
}
// Shift+click: bypass embedded browser, open in system default browser.
if NSEvent.modifierFlags.contains(.shift) {
#if DEBUG
dlog("link.openURL shift=true, forcing system browser url=\(target.url)")
#endif
return performOnMain {
NSWorkspace.shared.open(target.url)
}
Comment on lines +3260 to +3267
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🌐 Web query:

In AppKit, does NSEvent.modifierFlags return the current global modifier state, or the modifier flags from the event that triggered a later callback?

💡 Result:

In AppKit, NSEvent.modifierFlags (the instance property on an NSEvent object) returns the modifier flags from the specific event that triggered the callback, not the current global modifier state.

Citations:


🏁 Script executed:

cd Sources && sed -n '3240,3280p' GhosttyTerminalView.swift | cat -n

Repository: manaflow-ai/cmux

Length of output: 2054


🏁 Script executed:

cd Sources && sed -n '3200,3280p' GhosttyTerminalView.swift | cat -n

Repository: manaflow-ai/cmux

Length of output: 4608


🏁 Script executed:

cd Sources && sed -n '3050,3150p' GhosttyTerminalView.swift | cat -n

Repository: manaflow-ai/cmux

Length of output: 5577


🏁 Script executed:

cd Sources && sed -n '2900,2950p' GhosttyTerminalView.swift | cat -n

Repository: manaflow-ai/cmux

Length of output: 2400


🏁 Script executed:

cd Sources && grep -n "func handleAction" GhosttyTerminalView.swift | head -5

Repository: manaflow-ai/cmux

Length of output: 156


🏁 Script executed:

cd Sources && sed -n '2857,2900p' GhosttyTerminalView.swift | cat -n

Repository: manaflow-ai/cmux

Length of output: 2723


🏁 Script executed:

cd Sources && rg -n "GHOSTTY_ACTION_OPEN_URL" --type swift | head -20

Repository: manaflow-ai/cmux

Length of output: 128


🏁 Script executed:

cd Sources && rg -n "handleAction\(" GhosttyTerminalView.swift | head -10

Repository: manaflow-ai/cmux

Length of output: 243


🏁 Script executed:

cd Sources && sed -n '1660,1680p' GhosttyTerminalView.swift | cat -n

Repository: manaflow-ai/cmux

Length of output: 1256


🏁 Script executed:

cd Sources && sed -n '7400,7430p' GhosttyTerminalView.swift | cat -n

Repository: manaflow-ai/cmux

Length of output: 1915


🏁 Script executed:

cd Sources && sed -n '7500,7600p' GhosttyTerminalView.swift | cat -n

Repository: manaflow-ai/cmux

Length of output: 4667


🏁 Script executed:

cd Sources && rg "NSEvent\.modifierFlags" GhosttyTerminalView.swift -B 2 -A 2

Repository: manaflow-ai/cmux

Length of output: 330


🏁 Script executed:

cd Sources && rg "ghostty_action_s" GhosttyTerminalView.swift | head -20

Repository: manaflow-ai/cmux

Length of output: 482


🏁 Script executed:

cd Sources && rg "class var modifierFlags\|static var modifierFlags" --type swift

Repository: manaflow-ai/cmux

Length of output: 42


🏁 Script executed:

cd Sources && python3 << 'EOF'
# Check if NSEvent.modifierFlags is being called as class property (which returns current state)
# or instance property (which returns event's flags)
import subprocess
result = subprocess.run(['rg', 'NSEvent\\.modifierFlags', '-A', '5', '-B', '5'], 
                       capture_output=True, text=True)
print("Search results for NSEvent.modifierFlags:")
print(result.stdout)
EOF

Repository: manaflow-ai/cmux

Length of output: 102


🏁 Script executed:

cd Sources && rg "ghostty_action_s\|ghostty_action_open_url" --type swift -B 2 -A 10 | head -50

Repository: manaflow-ai/cmux

Length of output: 42


Latch the click's Shift bit instead of reading global modifier state.

Line 3262 uses NSEvent.modifierFlags (the class property), which returns the current global modifier state, not the modifiers from the original click event. The handleAction callback receives only a ghostty_action_s struct—the original NSEvent is unavailable. If Shift is released between mouseUp and this callback, the same Cmd+Shift+click can fall back to the embedded browser instead of opening externally.

Capture the Shift state from the event during mouseDown/mouseUp and propagate it through the Ghostty callback chain (or store it temporarily) so this check reads the latched value instead of global state.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Sources/GhosttyTerminalView.swift` around lines 3260 - 3267, The callback is
reading NSEvent.modifierFlags (global state) instead of the latched Shift from
the original click; modify the input path so the Shift bit is captured at
mouseDown/mouseUp and carried to the Ghostty callback (or stored on
GhosttyTerminalView) and then read in the link-opening code instead of
NSEvent.modifierFlags. Concretely: add a boolean (e.g., shiftLatched) to the
data passed through ghostty_action_s or as a temporary property on
GhosttyTerminalView set during mouseDown/mouseUp, set it from
event.modifierFlags.contains(.shift) at the original event, propagate it into
handleAction, and replace uses of NSEvent.modifierFlags.contains(.shift) with
that latched boolean (update any relevant functions that construct or consume
the ghostty_action_s).

}
if !BrowserLinkOpenSettings.openTerminalLinksInCmuxBrowser() {
#if DEBUG
dlog("link.openURL cmuxBrowser=disabled, opening externally url=\(target.url)")
Expand Down Expand Up @@ -7044,7 +7053,7 @@ class GhosttyNSView: NSView, NSUserInterfaceValidations {
var keyEvent = ghostty_input_key_s()
keyEvent.action = action
keyEvent.keycode = UInt32(event.keyCode)
keyEvent.mods = modsFromEvent(event)
keyEvent.mods = linkModsFromEvent(event)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 linkModsFromEvent misapplied to keyboard modifier-tracking events

flagsChanged sends Ghostty a modifier-key event to keep its internal modifier state in sync. When Shift is pressed while Cmd is held, linkModsFromEvent strips Shift from the mods field, so Ghostty receives a PRESS event for the Shift keycode but with a modifier state that doesn't include Shift — a self-contradictory event. Ghostty may use this field when generating modifier-based escape sequences for subsequent keyboard input, risking wrong output.

The stripping is only necessary for the mouse paths (ghostty_surface_mouse_pos / ghostty_surface_mouse_button) so Ghostty treats Cmd+Shift as a plain Cmd link click. For this keyboard modifier-tracking event, modsFromEvent(event) (the prior behavior) is correct. Consider reverting just this line to modsFromEvent(event).

keyEvent.consumed_mods = GHOSTTY_MODS_NONE
keyEvent.text = nil
keyEvent.composing = false
Expand Down Expand Up @@ -7098,11 +7107,24 @@ class GhosttyNSView: NSView, NSUserInterfaceValidations {
return ghostty_surface_has_selection(surface)
}

// Strip Shift when Cmd is held so Ghostty treats Cmd+Shift+click as a
// link click. The real Shift state is checked in the OPEN_URL handler.
private func linkModsFromEvent(_ event: NSEvent) -> ghostty_input_mods_e {
let flags = event.modifierFlags
if flags.contains(.command), flags.contains(.shift) {
return modsFromFlags(flags.subtracting(.shift))
}
return modsFromFlags(flags)
}

private func hoverModsFromFlags(
_ flags: NSEvent.ModifierFlags,
suppressCommandPathHover: Bool
) -> ghostty_input_mods_e {
let effectiveFlags = suppressCommandPathHover ? flags.subtracting(.command) : flags
var effectiveFlags = suppressCommandPathHover ? flags.subtracting(.command) : flags
if effectiveFlags.contains(.command), effectiveFlags.contains(.shift) {
effectiveFlags = effectiveFlags.subtracting(.shift)
}
#if DEBUG
if suppressCommandPathHover, flags.contains(.command) {
_ = CmuxUITestCapture.mutateJSONObjectIfConfigured(
Expand Down Expand Up @@ -7383,9 +7405,9 @@ class GhosttyNSView: NSView, NSUserInterfaceValidations {
// Only update mouse position on the first click to prevent unwanted cursor
// movement during double-click selection (issue #1698)
if event.clickCount == 1 {
ghostty_surface_mouse_pos(surface, eventPoint.x, bounds.height - eventPoint.y, modsFromEvent(event))
ghostty_surface_mouse_pos(surface, eventPoint.x, bounds.height - eventPoint.y, linkModsFromEvent(event))
}
_ = ghostty_surface_mouse_button(surface, GHOSTTY_MOUSE_PRESS, GHOSTTY_MOUSE_LEFT, modsFromEvent(event))
_ = ghostty_surface_mouse_button(surface, GHOSTTY_MOUSE_PRESS, GHOSTTY_MOUSE_LEFT, linkModsFromEvent(event))
Comment on lines +7408 to +7410
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 linkModsFromEvent called twice in mouseDown

linkModsFromEvent(event) is a pure function but is called twice, once for mouse_pos and once for mouse_button. Caching the result in a local variable avoids the redundant call.

Suggested change
ghostty_surface_mouse_pos(surface, eventPoint.x, bounds.height - eventPoint.y, linkModsFromEvent(event))
}
_ = ghostty_surface_mouse_button(surface, GHOSTTY_MOUSE_PRESS, GHOSTTY_MOUSE_LEFT, modsFromEvent(event))
_ = ghostty_surface_mouse_button(surface, GHOSTTY_MOUSE_PRESS, GHOSTTY_MOUSE_LEFT, linkModsFromEvent(event))
let mouseMods = linkModsFromEvent(event)
if event.clickCount == 1 {
ghostty_surface_mouse_pos(surface, eventPoint.x, bounds.height - eventPoint.y, mouseMods)
}
_ = ghostty_surface_mouse_button(surface, GHOSTTY_MOUSE_PRESS, GHOSTTY_MOUSE_LEFT, mouseMods)

}

override func mouseUp(with event: NSEvent) {
Expand All @@ -7394,7 +7416,7 @@ class GhosttyNSView: NSView, NSUserInterfaceValidations {
#endif
guard let surface = surface else { return }
let point = convert(event.locationInWindow, from: nil)
let consumed = ghostty_surface_mouse_button(surface, GHOSTTY_MOUSE_RELEASE, GHOSTTY_MOUSE_LEFT, modsFromEvent(event))
let consumed = ghostty_surface_mouse_button(surface, GHOSTTY_MOUSE_RELEASE, GHOSTTY_MOUSE_LEFT, linkModsFromEvent(event))
_ = handleCommandClickRelease(at: point, modifierFlags: event.modifierFlags, ghosttyConsumed: consumed)
}

Expand Down
12 changes: 12 additions & 0 deletions Sources/Panels/BrowserPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6171,6 +6171,18 @@ private class BrowserNavigationDelegate: NSObject, WKNavigationDelegate {
return
}

// Shift+click: bypass embedded browser and open in system default browser.
if navigationAction.modifierFlags.contains(.shift),
navigationAction.navigationType == .linkActivated,
let url = navigationAction.request.url {
#if DEBUG
dlog("browser.nav.decidePolicy.action kind=shiftClickExternal url=\(url.absoluteString)")
#endif
NSWorkspace.shared.open(url)
decisionHandler(.cancel)
return
}
Comment on lines +6174 to +6184
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Handle failed external-open gracefully before canceling navigation.

If NSWorkspace.shared.open(url) fails, this path still cancels the navigation, so the click does nothing. Please only cancel on successful handoff (or fall back to .allow).

💡 Suggested fix
         if navigationAction.modifierFlags.contains(.shift),
            navigationAction.navigationType == .linkActivated,
            let url = navigationAction.request.url {
             `#if` DEBUG
             dlog("browser.nav.decidePolicy.action kind=shiftClickExternal url=\(url.absoluteString)")
             `#endif`
-            NSWorkspace.shared.open(url)
-            decisionHandler(.cancel)
-            return
+            let opened = NSWorkspace.shared.open(url)
+            if opened {
+                decisionHandler(.cancel)
+                return
+            }
+            decisionHandler(.allow)
+            return
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Sources/Panels/BrowserPanel.swift` around lines 6174 - 6184, The shift-click
external-open path currently unconditionally calls NSWorkspace.shared.open(url)
then decisionHandler(.cancel); update the logic in the BrowserPanel navigation
handler so you capture the Bool result from NSWorkspace.shared.open(url) and
only call decisionHandler(.cancel) when it returns true, otherwise call
decisionHandler(.allow) (and optionally log the failure via dlog in DEBUG).
Locate the shift-click branch handling navigationAction, the
NSWorkspace.shared.open call and the decisionHandler invocation to implement
this conditional fallback.


// Cmd+click and middle-click on regular links should always open in a new tab.
if shouldOpenInNewTab,
let url = navigationAction.request.url {
Expand Down
11 changes: 11 additions & 0 deletions Sources/Panels/BrowserPopupWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,17 @@ private class PopupNavigationDelegate: NSObject, WKNavigationDelegate {
return
}

// Shift+click: bypass embedded browser and open in system default browser.
if navigationAction.modifierFlags.contains(.shift),
navigationAction.navigationType == .linkActivated {
#if DEBUG
dlog("popup.nav.shiftClickExternal url=\(url.absoluteString)")
#endif
NSWorkspace.shared.open(url)
decisionHandler(.cancel)
return
}
Comment on lines +571 to +580
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Shift+click bypasses insecure HTTP warning in popup

The Shift+click check here fires before the insecure HTTP guard (line 583), so Shift+clicking an http:// link in a popup opens it directly in the system browser without the security alert. The main BrowserPanel places the insecure HTTP check before the Shift+click check, so those two paths are inconsistent. Since the user's intent with Shift+click is explicitly to delegate to the system browser (which has its own HTTP warnings), this is arguably harmless — but worth aligning for consistency.


// Insecure HTTP → show same prompt as main browser
if browserShouldBlockInsecureHTTPURL(url) {
#if DEBUG
Expand Down