linux: Primary clipboard (#10534)

Implements copying from and pasting to the primary selection.

Release Notes:

- N/A
This commit is contained in:
apricotbucket28 2024-04-18 18:54:18 -03:00 committed by GitHub
parent 98db7fa61e
commit b31df39ab0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 173 additions and 16 deletions

View file

@ -1828,6 +1828,29 @@ impl Editor {
old_cursor_position: &Anchor,
cx: &mut ViewContext<Self>,
) {
// Copy selections to primary selection buffer
#[cfg(target_os = "linux")]
if local {
let selections = self.selections.all::<usize>(cx);
let buffer_handle = self.buffer.read(cx).read(cx);
let mut text = String::new();
for (index, selection) in selections.iter().enumerate() {
let text_for_selection = buffer_handle
.text_for_range(selection.start..selection.end)
.collect::<String>();
text.push_str(&text_for_selection);
if index != selections.len() - 1 {
text.push('\n');
}
}
if !text.is_empty() {
cx.write_to_primary(ClipboardItem::new(text));
}
}
if self.focus_handle.is_focused(cx) && self.leader_peer_id.is_none() {
self.buffer.update(cx, |buffer, cx| {
buffer.set_active_selections(

View file

@ -502,6 +502,34 @@ impl EditorElement {
cx.stop_propagation();
}
fn mouse_middle_down(
editor: &mut Editor,
event: &MouseDownEvent,
position_map: &PositionMap,
text_hitbox: &Hitbox,
cx: &mut ViewContext<Editor>,
) {
if !text_hitbox.is_hovered(cx) || editor.read_only(cx) {
return;
}
if let Some(item) = cx.read_from_primary() {
let point_for_position =
position_map.point_for_position(text_hitbox.bounds, event.position);
let position = point_for_position.previous_valid;
editor.select(
SelectPhase::Begin {
position,
add: false,
click_count: 1,
},
cx,
);
editor.insert(item.text(), cx);
}
}
fn mouse_up(
editor: &mut Editor,
event: &MouseUpEvent,
@ -2903,6 +2931,9 @@ impl EditorElement {
MouseButton::Right => editor.update(cx, |editor, cx| {
Self::mouse_right_down(editor, event, &position_map, &text_hitbox, cx);
}),
MouseButton::Middle => editor.update(cx, |editor, cx| {
Self::mouse_middle_down(editor, event, &position_map, &text_hitbox, cx);
}),
_ => {}
};
}