Files
codex/codex-rs/tui/src/app/footer_selection.rs
T
Eric Traut 1f9549e265 Support PRIMARY selection in Wayland sessions (#64192)
## Why

The TUI disabled PRIMARY whenever `WAYLAND_DISPLAY` was set, so it
couldn't publish selected text to PRIMARY or paste from it with a middle
click in Wayland sessions.

## What changed

Probe Wayland PRIMARY support in the background and use the result for
selection copying and middle-click paste. If the compositor lacks the
Wayland data-control protocol, allow XWayland fallback only when the
Wayland and X11 sockets have matching listener credentials.

## Testing

Add a unit test covering native Wayland support, the same-session
XWayland fallback, remote X11 displays, and unsuccessful probes.

GitOrigin-RevId: 9b5c1536762383c0fe770dd86d58a49a1f3633a8
2026-11-26 17:55:06 +00:00

83 lines
3.4 KiB
Rust

//! Route registered read-only selections before copy, interrupt, clicks, and paste shortcuts.
//! A stationary click is replayed to the existing widget handler after selection releases it.
use super::*;
use crate::clipboard_copy::CopyFormat;
use crate::keymap::ListAction;
use crate::rendered_selection::RegionAction;
use crate::rendered_selection::RenderedSelection;
use crate::transcript_view::ViewAction;
impl App {
pub(super) fn handle_rendered_selection_event(
&mut self,
tui: &mut tui::Tui,
event: &TuiEvent,
) -> Result<bool> {
let warning_active = self.chat_widget.keymap_contexts().is_warnings();
if !tui.is_owned_screen()
|| self.overlay.is_some()
|| (!warning_active
&& (!self.chat_widget.no_modal_or_popup_active()
|| self.chat_widget.is_external_writer_view()))
{
*self.chat_widget.rendered_selection.borrow_mut() = RenderedSelection::default();
return Ok(false);
}
let refresh = matches!(event, TuiEvent::Mouse(mouse)
if self.chat_widget.rendered_selection.borrow().needs_refresh(*mouse));
if refresh {
self.refresh_owned_transcript(tui)?;
}
let action = {
let mut selection = self.chat_widget.rendered_selection.borrow_mut();
if warning_active
&& let TuiEvent::Key(key) = event
&& matches!(
self.keymap.list.action_for(*key),
Some(ListAction::PageUp | ListAction::PageDown)
)
{
selection.clear();
return Ok(false);
}
selection.view.copy_on_select = self.local_settings.copy_on_select();
selection.view.copy_on_select_clear_selection =
self.local_settings.tui.copy_on_select_clear_selection;
selection.view.primary_selection = tui.primary_selection_available();
selection.handle_event(event, tui.terminal.previous_buffer())
};
let Some(action) = action else {
return Ok(false);
};
if warning_active {
self.chat_widget.record_warning_activity();
}
self.cancel_pending_key_chord();
match action {
RegionAction::Click(mouse) => {
self.chat_widget
.handle_warning_event(&TuiEvent::Mouse(mouse), &self.transcript_cells);
}
RegionAction::Selection(action) => {
self.transcript_view.end_selection(&self.transcript_cells);
self.transcript_view.cancel_search();
self.transcript_view.clear_activity_focus();
self.chat_widget.clear_composer_selection();
let copied = {
let mut selection = self.chat_widget.rendered_selection.borrow_mut();
let RenderedSelection { view, cells, .. } = &mut *selection;
view.copy_action(tui, cells, &action, CopyFormat::PlainText)
};
if let Some((characters, result)) = copied {
self.transcript_view.show_copy_feedback(&result, characters);
} else if let ViewAction::OpenLink(url) = action {
self.open_url_in_browser(url);
}
}
}
tui.frame_requester().schedule_frame();
Ok(true)
}
}