fix(plugins): send events to plugins properly in a multiuser env (#986)

This commit is contained in:
Aram Drevekenin 2022-01-03 16:55:21 +01:00 committed by GitHub
parent 7dad61e529
commit 153ed175af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 43 deletions

View file

@ -306,20 +306,20 @@ impl Pane for PluginPane {
self.geom.y -= count; self.geom.y -= count;
self.should_render = true; self.should_render = true;
} }
fn scroll_up(&mut self, count: usize) { fn scroll_up(&mut self, count: usize, client_id: ClientId) {
self.send_plugin_instructions self.send_plugin_instructions
.send(PluginInstruction::Update( .send(PluginInstruction::Update(
Some(self.pid), Some(self.pid),
None, Some(client_id),
Event::Mouse(Mouse::ScrollUp(count)), Event::Mouse(Mouse::ScrollUp(count)),
)) ))
.unwrap(); .unwrap();
} }
fn scroll_down(&mut self, count: usize) { fn scroll_down(&mut self, count: usize, client_id: ClientId) {
self.send_plugin_instructions self.send_plugin_instructions
.send(PluginInstruction::Update( .send(PluginInstruction::Update(
Some(self.pid), Some(self.pid),
None, Some(client_id),
Event::Mouse(Mouse::ScrollDown(count)), Event::Mouse(Mouse::ScrollDown(count)),
)) ))
.unwrap(); .unwrap();
@ -327,29 +327,30 @@ impl Pane for PluginPane {
fn clear_scroll(&mut self) { fn clear_scroll(&mut self) {
unimplemented!(); unimplemented!();
} }
fn start_selection(&mut self, start: &Position) { fn start_selection(&mut self, start: &Position, client_id: ClientId) {
log::info!("plugin pane send left click plugin instruction");
self.send_plugin_instructions self.send_plugin_instructions
.send(PluginInstruction::Update( .send(PluginInstruction::Update(
Some(self.pid), Some(self.pid),
None, Some(client_id),
Event::Mouse(Mouse::LeftClick(start.line.0, start.column.0)), Event::Mouse(Mouse::LeftClick(start.line.0, start.column.0)),
)) ))
.unwrap(); .unwrap();
} }
fn update_selection(&mut self, position: &Position) { fn update_selection(&mut self, position: &Position, client_id: ClientId) {
self.send_plugin_instructions self.send_plugin_instructions
.send(PluginInstruction::Update( .send(PluginInstruction::Update(
Some(self.pid), Some(self.pid),
None, Some(client_id),
Event::Mouse(Mouse::Hold(position.line.0, position.column.0)), Event::Mouse(Mouse::Hold(position.line.0, position.column.0)),
)) ))
.unwrap(); .unwrap();
} }
fn end_selection(&mut self, end: Option<&Position>) { fn end_selection(&mut self, end: Option<&Position>, client_id: ClientId) {
self.send_plugin_instructions self.send_plugin_instructions
.send(PluginInstruction::Update( .send(PluginInstruction::Update(
Some(self.pid), Some(self.pid),
None, Some(client_id),
Event::Mouse(Mouse::Release( Event::Mouse(Mouse::Release(
end.map(|Position { line, column }| (line.0, column.0)), end.map(|Position { line, column }| (line.0, column.0)),
)), )),
@ -379,11 +380,11 @@ impl Pane for PluginPane {
fn borderless(&self) -> bool { fn borderless(&self) -> bool {
self.borderless self.borderless
} }
fn handle_right_click(&mut self, to: &Position) { fn handle_right_click(&mut self, to: &Position, client_id: ClientId) {
self.send_plugin_instructions self.send_plugin_instructions
.send(PluginInstruction::Update( .send(PluginInstruction::Update(
Some(self.pid), Some(self.pid),
None, Some(client_id),
Event::Mouse(Mouse::RightClick(to.line.0, to.column.0)), Event::Mouse(Mouse::RightClick(to.line.0, to.column.0)),
)) ))
.unwrap(); .unwrap();

View file

@ -414,11 +414,11 @@ impl Pane for TerminalPane {
self.geom.y -= count; self.geom.y -= count;
self.reflow_lines(); self.reflow_lines();
} }
fn scroll_up(&mut self, count: usize) { fn scroll_up(&mut self, count: usize, _client_id: ClientId) {
self.grid.move_viewport_up(count); self.grid.move_viewport_up(count);
self.set_should_render(true); self.set_should_render(true);
} }
fn scroll_down(&mut self, count: usize) { fn scroll_down(&mut self, count: usize, _client_id: ClientId) {
self.grid.move_viewport_down(count); self.grid.move_viewport_down(count);
self.set_should_render(true); self.set_should_render(true);
} }
@ -452,12 +452,12 @@ impl Pane for TerminalPane {
self.grid.pending_messages_to_pty.drain(..).collect() self.grid.pending_messages_to_pty.drain(..).collect()
} }
fn start_selection(&mut self, start: &Position) { fn start_selection(&mut self, start: &Position, client_id: ClientId) {
self.grid.start_selection(start); self.grid.start_selection(start);
self.set_should_render(true); self.set_should_render(true);
} }
fn update_selection(&mut self, to: &Position) { fn update_selection(&mut self, to: &Position, _client_id: ClientId) {
let should_scroll = self.selection_scrolled_at.elapsed() let should_scroll = self.selection_scrolled_at.elapsed()
>= time::Duration::from_millis(SELECTION_SCROLL_INTERVAL_MS); >= time::Duration::from_millis(SELECTION_SCROLL_INTERVAL_MS);
// TODO: check how far up/down mouse is relative to pane, to increase scroll lines? // TODO: check how far up/down mouse is relative to pane, to increase scroll lines?
@ -474,7 +474,7 @@ impl Pane for TerminalPane {
self.set_should_render(true); self.set_should_render(true);
} }
fn end_selection(&mut self, end: Option<&Position>) { fn end_selection(&mut self, end: Option<&Position>, _client_id: ClientId) {
self.grid.end_selection(end); self.grid.end_selection(end);
self.set_should_render(true); self.set_should_render(true);
} }

View file

@ -8,6 +8,7 @@ use std::fmt::Write;
#[test] #[test]
pub fn scrolling_inside_a_pane() { pub fn scrolling_inside_a_pane() {
let fake_client_id = 1;
let mut fake_win_size = PaneGeom::default(); let mut fake_win_size = PaneGeom::default();
fake_win_size.cols.set_inner(121); fake_win_size.cols.set_inner(121);
fake_win_size.rows.set_inner(20); fake_win_size.rows.set_inner(20);
@ -20,9 +21,9 @@ pub fn scrolling_inside_a_pane() {
writeln!(&mut text_to_fill_pane, "\rline {}", i + 1).unwrap(); writeln!(&mut text_to_fill_pane, "\rline {}", i + 1).unwrap();
} }
terminal_pane.handle_pty_bytes(text_to_fill_pane.into_bytes()); terminal_pane.handle_pty_bytes(text_to_fill_pane.into_bytes());
terminal_pane.scroll_up(10); terminal_pane.scroll_up(10, fake_client_id);
assert_snapshot!(format!("{:?}", terminal_pane.grid)); assert_snapshot!(format!("{:?}", terminal_pane.grid));
terminal_pane.scroll_down(3); terminal_pane.scroll_down(3, fake_client_id);
assert_snapshot!(format!("{:?}", terminal_pane.grid)); assert_snapshot!(format!("{:?}", terminal_pane.grid));
terminal_pane.clear_scroll(); terminal_pane.clear_scroll();
assert_snapshot!(format!("{:?}", terminal_pane.grid)); assert_snapshot!(format!("{:?}", terminal_pane.grid));

View file

@ -931,7 +931,7 @@ pub(crate) fn screen_thread_main(
screen screen
.get_active_tab_mut(client_id) .get_active_tab_mut(client_id)
.unwrap() .unwrap()
.scroll_terminal_up(&point, 3); .scroll_terminal_up(&point, 3, client_id);
screen.render(); screen.render();
} }
@ -947,7 +947,7 @@ pub(crate) fn screen_thread_main(
screen screen
.get_active_tab_mut(client_id) .get_active_tab_mut(client_id)
.unwrap() .unwrap()
.scroll_terminal_down(&point, 3); .scroll_terminal_down(&point, 3, client_id);
screen.render(); screen.render();
} }

View file

@ -177,8 +177,8 @@ pub trait Pane {
fn push_right(&mut self, count: usize); fn push_right(&mut self, count: usize);
fn pull_left(&mut self, count: usize); fn pull_left(&mut self, count: usize);
fn pull_up(&mut self, count: usize); fn pull_up(&mut self, count: usize);
fn scroll_up(&mut self, count: usize); fn scroll_up(&mut self, count: usize, client_id: ClientId);
fn scroll_down(&mut self, count: usize); fn scroll_down(&mut self, count: usize, client_id: ClientId);
fn clear_scroll(&mut self); fn clear_scroll(&mut self);
fn is_scrolled(&self) -> bool; fn is_scrolled(&self) -> bool;
fn active_at(&self) -> Instant; fn active_at(&self) -> Instant;
@ -194,9 +194,9 @@ pub trait Pane {
None => self.position_and_size().contains(position), None => self.position_and_size().contains(position),
} }
} }
fn start_selection(&mut self, _start: &Position) {} fn start_selection(&mut self, _start: &Position, _client_id: ClientId) {}
fn update_selection(&mut self, _position: &Position) {} fn update_selection(&mut self, _position: &Position, _client_id: ClientId) {}
fn end_selection(&mut self, _end: Option<&Position>) {} fn end_selection(&mut self, _end: Option<&Position>, _client_id: ClientId) {}
fn reset_selection(&mut self) {} fn reset_selection(&mut self) {}
fn get_selected_text(&self) -> Option<String> { fn get_selected_text(&self) -> Option<String> {
None None
@ -265,7 +265,7 @@ pub trait Pane {
} }
fn set_borderless(&mut self, borderless: bool); fn set_borderless(&mut self, borderless: bool);
fn borderless(&self) -> bool; fn borderless(&self) -> bool;
fn handle_right_click(&mut self, _to: &Position) {} fn handle_right_click(&mut self, _to: &Position, _client_id: ClientId) {}
} }
macro_rules! resize_pty { macro_rules! resize_pty {
@ -1696,7 +1696,7 @@ impl Tab {
.panes .panes
.get_mut(&PaneId::Terminal(active_terminal_id)) .get_mut(&PaneId::Terminal(active_terminal_id))
.unwrap(); .unwrap();
active_terminal.scroll_up(1); active_terminal.scroll_up(1, client_id);
} }
} }
pub fn scroll_active_terminal_down(&mut self, client_id: ClientId) { pub fn scroll_active_terminal_down(&mut self, client_id: ClientId) {
@ -1705,7 +1705,7 @@ impl Tab {
.panes .panes
.get_mut(&PaneId::Terminal(active_terminal_id)) .get_mut(&PaneId::Terminal(active_terminal_id))
.unwrap(); .unwrap();
active_terminal.scroll_down(1); active_terminal.scroll_down(1, client_id);
if !active_terminal.is_scrolled() { if !active_terminal.is_scrolled() {
self.process_pending_vte_events(active_terminal_id); self.process_pending_vte_events(active_terminal_id);
} }
@ -1719,7 +1719,7 @@ impl Tab {
.unwrap(); .unwrap();
// prevent overflow when row == 0 // prevent overflow when row == 0
let scroll_rows = active_terminal.rows().max(1) - 1; let scroll_rows = active_terminal.rows().max(1) - 1;
active_terminal.scroll_up(scroll_rows); active_terminal.scroll_up(scroll_rows, client_id);
} }
} }
pub fn scroll_active_terminal_down_page(&mut self, client_id: ClientId) { pub fn scroll_active_terminal_down_page(&mut self, client_id: ClientId) {
@ -1730,7 +1730,7 @@ impl Tab {
.unwrap(); .unwrap();
// prevent overflow when row == 0 // prevent overflow when row == 0
let scroll_rows = active_terminal.rows().max(1) - 1; let scroll_rows = active_terminal.rows().max(1) - 1;
active_terminal.scroll_down(scroll_rows); active_terminal.scroll_down(scroll_rows, client_id);
if !active_terminal.is_scrolled() { if !active_terminal.is_scrolled() {
self.process_pending_vte_events(active_terminal_id); self.process_pending_vte_events(active_terminal_id);
} }
@ -1744,7 +1744,7 @@ impl Tab {
.unwrap(); .unwrap();
// prevent overflow when row == 0 // prevent overflow when row == 0
let scroll_rows = (active_terminal.rows().max(1) - 1) / 2; let scroll_rows = (active_terminal.rows().max(1) - 1) / 2;
active_terminal.scroll_up(scroll_rows); active_terminal.scroll_up(scroll_rows, client_id);
} }
} }
pub fn scroll_active_terminal_down_half_page(&mut self, client_id: ClientId) { pub fn scroll_active_terminal_down_half_page(&mut self, client_id: ClientId) {
@ -1755,7 +1755,7 @@ impl Tab {
.unwrap(); .unwrap();
// prevent overflow when row == 0 // prevent overflow when row == 0
let scroll_rows = (active_terminal.rows().max(1) - 1) / 2; let scroll_rows = (active_terminal.rows().max(1) - 1) / 2;
active_terminal.scroll_down(scroll_rows); active_terminal.scroll_down(scroll_rows, client_id);
if !active_terminal.is_scrolled() { if !active_terminal.is_scrolled() {
self.process_pending_vte_events(active_terminal_id); self.process_pending_vte_events(active_terminal_id);
} }
@ -1785,14 +1785,14 @@ impl Tab {
} }
} }
} }
pub fn scroll_terminal_up(&mut self, point: &Position, lines: usize) { pub fn scroll_terminal_up(&mut self, point: &Position, lines: usize, client_id: ClientId) {
if let Some(pane) = self.get_pane_at(point, false) { if let Some(pane) = self.get_pane_at(point, false) {
pane.scroll_up(lines); pane.scroll_up(lines, client_id);
} }
} }
pub fn scroll_terminal_down(&mut self, point: &Position, lines: usize) { pub fn scroll_terminal_down(&mut self, point: &Position, lines: usize, client_id: ClientId) {
if let Some(pane) = self.get_pane_at(point, false) { if let Some(pane) = self.get_pane_at(point, false) {
pane.scroll_down(lines); pane.scroll_down(lines, client_id);
if !pane.is_scrolled() { if !pane.is_scrolled() {
if let PaneId::Terminal(pid) = pane.pid() { if let PaneId::Terminal(pid) = pane.pid() {
self.process_pending_vte_events(pid); self.process_pending_vte_events(pid);
@ -1832,7 +1832,7 @@ impl Tab {
if let Some(pane) = self.get_pane_at(position, false) { if let Some(pane) = self.get_pane_at(position, false) {
let relative_position = pane.relative_position(position); let relative_position = pane.relative_position(position);
pane.start_selection(&relative_position); pane.start_selection(&relative_position, client_id);
self.selecting_with_mouse = true; self.selecting_with_mouse = true;
}; };
} }
@ -1841,7 +1841,7 @@ impl Tab {
if let Some(pane) = self.get_pane_at(position, false) { if let Some(pane) = self.get_pane_at(position, false) {
let relative_position = pane.relative_position(position); let relative_position = pane.relative_position(position);
pane.handle_right_click(&relative_position); pane.handle_right_click(&relative_position, client_id);
}; };
} }
fn focus_pane_at(&mut self, point: &Position, client_id: ClientId) { fn focus_pane_at(&mut self, point: &Position, client_id: ClientId) {
@ -1869,14 +1869,14 @@ impl Tab {
if active_pane_id != self.get_pane_id_at(position, true) { if active_pane_id != self.get_pane_id_at(position, true) {
if let Some(active_pane_id) = active_pane_id { if let Some(active_pane_id) = active_pane_id {
if let Some(active_pane) = self.panes.get_mut(&active_pane_id) { if let Some(active_pane) = self.panes.get_mut(&active_pane_id) {
active_pane.end_selection(None); active_pane.end_selection(None, client_id);
selected_text = active_pane.get_selected_text(); selected_text = active_pane.get_selected_text();
active_pane.reset_selection(); active_pane.reset_selection();
} }
} }
} else if let Some(pane) = self.get_pane_at(position, true) { } else if let Some(pane) = self.get_pane_at(position, true) {
let relative_position = pane.relative_position(position); let relative_position = pane.relative_position(position);
pane.end_selection(Some(&relative_position)); pane.end_selection(Some(&relative_position), client_id);
selected_text = pane.get_selected_text(); selected_text = pane.get_selected_text();
pane.reset_selection(); pane.reset_selection();
} }
@ -1890,7 +1890,7 @@ impl Tab {
if let Some(active_pane_id) = self.get_active_pane_id(client_id) { if let Some(active_pane_id) = self.get_active_pane_id(client_id) {
if let Some(active_pane) = self.panes.get_mut(&active_pane_id) { if let Some(active_pane) = self.panes.get_mut(&active_pane_id) {
let relative_position = active_pane.relative_position(position_on_screen); let relative_position = active_pane.relative_position(position_on_screen);
active_pane.update_selection(&relative_position); active_pane.update_selection(&relative_position, client_id);
} }
} }
} }

View file

@ -141,7 +141,8 @@ pub(crate) fn wasm_thread_main(
if subs.contains(&event_type) if subs.contains(&event_type)
&& ((pid.is_none() && cid.is_none()) && ((pid.is_none() && cid.is_none())
|| (pid.is_none() && cid == Some(client_id)) || (pid.is_none() && cid == Some(client_id))
|| (cid.is_none() && pid == Some(plugin_id))) || (cid.is_none() && pid == Some(plugin_id))
|| (cid == Some(client_id) && pid == Some(plugin_id)))
{ {
let update = instance.exports.get_function("update").unwrap(); let update = instance.exports.get_function("update").unwrap();
wasi_write_object(&plugin_env.wasi_env, &event); wasi_write_object(&plugin_env.wasi_env, &event);