Added right-clik copy/paste selection in terminals
Moorly CI / windows-build (push) Successful in 3m18s
Moorly CI / linux-build (push) Successful in 4m15s

This commit is contained in:
Squid committed 2026-11-12 19:44:53 +01:00
1 parent 64a152133e
commit 5b2e2c0d1f
1 file changed
+158 -22
+158 -22
View File
@@ -1731,12 +1731,13 @@ impl TerminalSelection {
} }
} }
/// Copies the selected terminal text to the clipboard. /// Copies the selected terminal text to the clipboard and clears the
fn copy_terminal_selection(state: &Moorly, id: u32) -> Task<Message> { /// selection.
let Some(terminal) = state.terminal_sessions.get(&id) else { fn copy_terminal_selection(state: &mut Moorly, id: u32) -> Task<Message> {
let Some(terminal) = state.terminal_sessions.get_mut(&id) else {
return Task::none(); return Task::none();
}; };
let Some(selection) = terminal.selection else { let Some(selection) = terminal.selection.take() else {
return Task::none(); return Task::none();
}; };
@@ -1749,6 +1750,58 @@ fn copy_terminal_selection(state: &Moorly, id: u32) -> Task<Message> {
} }
} }
/// Reads the clipboard and hands it back for a single paste into the terminal.
fn paste_terminal_clipboard(id: u32) -> Task<Message> {
iced::clipboard::read()
.map(move |contents| Message::TerminalPasteContents(id, contents))
}
/// Clipboard bytes for a paste, ignoring a missing or empty clipboard. Line
/// endings are normalised the same way pressing Enter is sent to the shell.
fn paste_contents(contents: Option<String>) -> Option<Vec<u8>> {
let text = contents.filter(|text| !text.is_empty())?;
let text = text.replace("\r\n", "\n").replace('\r', "\n");
Some(text.replace('\n', "\r").into_bytes())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TerminalClipboardAction {
Copy,
Paste,
}
/// Terminal clipboard shortcuts, shared by every terminal transport.
fn terminal_clipboard_action(
key: &keyboard::Key,
modifiers: keyboard::Modifiers,
) -> Option<TerminalClipboardAction> {
if !modifiers.command() || !modifiers.shift() {
return None;
}
let keyboard::Key::Character(character) = key else {
return None;
};
if character.eq_ignore_ascii_case("c") {
Some(TerminalClipboardAction::Copy)
} else if character.eq_ignore_ascii_case("v") {
Some(TerminalClipboardAction::Paste)
} else {
None
}
}
/// Right-click copies a selection, or pastes when nothing is selected.
fn terminal_right_click_action(has_selection: bool) -> TerminalClipboardAction {
if has_selection {
TerminalClipboardAction::Copy
} else {
TerminalClipboardAction::Paste
}
}
/// Text covered by a selection, in reading order and with the end cell /// Text covered by a selection, in reading order and with the end cell
/// included. /// included.
fn terminal_selection_text(screen: &vt100::Screen, selection: TerminalSelection) -> String { fn terminal_selection_text(screen: &vt100::Screen, selection: TerminalSelection) -> String {
@@ -4945,6 +4998,8 @@ enum Message {
TerminalTick, TerminalTick,
TerminalSelectionStart(u32, u16, u16), TerminalSelectionStart(u32, u16, u16),
TerminalSelectionUpdate(u32, u16, u16), TerminalSelectionUpdate(u32, u16, u16),
TerminalRightClick(u32),
TerminalPasteContents(u32, Option<String>),
TerminalKey { TerminalKey {
id: u32, id: u32,
key: keyboard::Key, key: keyboard::Key,
@@ -5269,31 +5324,37 @@ fn update(state: &mut Moorly, message: Message) -> Task<Message> {
selection.focus = (row, col); selection.focus = (row, col);
} }
} }
Message::TerminalRightClick(id) => {
let has_selection = state
.terminal_sessions
.get(&id)
.is_some_and(|terminal| terminal.selection.is_some());
return match terminal_right_click_action(has_selection) {
TerminalClipboardAction::Copy => copy_terminal_selection(state, id),
TerminalClipboardAction::Paste => paste_terminal_clipboard(id),
};
}
Message::TerminalPasteContents(id, contents) => {
if let Some(bytes) = paste_contents(contents)
&& let Some(terminal) = state.terminal_sessions.get_mut(&id)
&& terminal.exit.is_none()
{
let _ = terminal.session.write(&bytes);
}
}
Message::TerminalKey { Message::TerminalKey {
id, id,
key, key,
text, text,
modifiers, modifiers,
} => { } => {
// Copy the selection with Ctrl+Shift+C, or with Ctrl+C while a match terminal_clipboard_action(&key, modifiers) {
// selection exists (then clear it so the next Ctrl+C interrupts). Some(TerminalClipboardAction::Copy) => {
let copy = modifiers.command() return copy_terminal_selection(state, id);
&& matches!(&key, keyboard::Key::Character(character) if character.eq_ignore_ascii_case("c"))
&& state
.terminal_sessions
.get(&id)
.is_some_and(|terminal| terminal.selection.is_some());
if copy {
let task = copy_terminal_selection(state, id);
if !modifiers.shift()
&& let Some(terminal) = state.terminal_sessions.get_mut(&id)
{
terminal.selection = None;
} }
Some(TerminalClipboardAction::Paste) => return paste_terminal_clipboard(id),
return task; None => {}
} }
if let Some(terminal) = state.terminal_sessions.get_mut(&id) if let Some(terminal) = state.terminal_sessions.get_mut(&id)
@@ -11302,6 +11363,9 @@ impl canvas::Program<Message> for TerminalCanvas<'_> {
.and_capture(), .and_capture(),
) )
} }
canvas::Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Right)) => Some(
canvas::Action::publish(Message::TerminalRightClick(self.id)).and_capture(),
),
canvas::Event::Mouse(mouse::Event::CursorMoved { .. }) => { canvas::Event::Mouse(mouse::Event::CursorMoved { .. }) => {
if !*dragging { if !*dragging {
return None; return None;
@@ -12421,6 +12485,78 @@ mod tests {
); );
} }
#[test]
fn terminal_selection_copy_preserves_unicode_and_lines() {
let mut parser = vt100::Parser::new(4.try_into().unwrap(), 20.try_into().unwrap(), 0);
parser.process("åäö\r\nrad två".as_bytes());
let screen = parser.screen();
let selection = super::TerminalSelection {
anchor: (0, 0),
focus: (1, 6),
};
assert_eq!(
super::terminal_selection_text(screen, selection),
"åäö\nrad två"
);
}
#[test]
fn terminal_clipboard_shortcuts_need_both_modifiers() {
use super::TerminalClipboardAction::{Copy, Paste};
use super::{terminal_clipboard_action, terminal_right_click_action};
let key = |character: &str| keyboard::Key::Character(character.into());
let ctrl = keyboard::Modifiers::CTRL;
let shift = keyboard::Modifiers::SHIFT;
assert_eq!(terminal_clipboard_action(&key("C"), ctrl | shift), Some(Copy));
assert_eq!(terminal_clipboard_action(&key("v"), ctrl | shift), Some(Paste));
// Plain Ctrl+C stays an interrupt and Ctrl+V stays normal input.
assert_eq!(terminal_clipboard_action(&key("c"), ctrl), None);
assert_eq!(terminal_clipboard_action(&key("v"), ctrl), None);
assert_eq!(terminal_clipboard_action(&key("x"), ctrl | shift), None);
assert_eq!(terminal_right_click_action(true), Copy);
assert_eq!(terminal_right_click_action(false), Paste);
}
#[test]
fn pasting_keeps_unicode_and_multiline_text() {
assert_eq!(
super::paste_contents(Some("åäö\r\nrad 2\nrad 3".to_owned())),
Some("åäö\rrad 2\rrad 3".as_bytes().to_vec())
);
assert_eq!(super::paste_contents(Some(String::new())), None);
assert_eq!(super::paste_contents(None), None);
}
#[test]
fn copying_a_terminal_selection_clears_it() {
let (mut state, _, id) = terminal_for_session_end(super::SessionEnd::Keep);
let selection = super::TerminalSelection {
anchor: (0, 0),
focus: (0, 2),
};
state.terminal_sessions.get_mut(&id).unwrap().selection = Some(selection);
let _ = update(
&mut state,
Message::TerminalKey {
id,
key: keyboard::Key::Character("C".into()),
text: Some("C".into()),
modifiers: keyboard::Modifiers::CTRL | keyboard::Modifiers::SHIFT,
},
);
assert!(state.terminal_sessions[&id].selection.is_none());
state.terminal_sessions.get_mut(&id).unwrap().selection = Some(selection);
let _ = update(&mut state, Message::TerminalRightClick(id));
assert!(state.terminal_sessions[&id].selection.is_none());
}
#[test] #[test]
fn quick_power_shells_are_numbered_separately() { fn quick_power_shells_are_numbered_separately() {
let entries = vec![ let entries = vec![