Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): add plugin APIs to affect other panes #3576

Merged
merged 5 commits into from
Aug 29, 2024
Merged
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
51 changes: 51 additions & 0 deletions default-plugins/fixture-plugin-for-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,57 @@ impl ZellijPlugin for State {
BareKey::Char('d') if key.has_modifiers(&[KeyModifier::Alt]) => {
rerun_command_pane(1);
},
BareKey::Char('e') if key.has_modifiers(&[KeyModifier::Alt]) => {
resize_pane_with_id(
ResizeStrategy::new(Resize::Increase, Some(Direction::Left)),
PaneId::Terminal(2),
);
},
BareKey::Char('f') if key.has_modifiers(&[KeyModifier::Alt]) => {
edit_scrollback_for_pane_with_id(PaneId::Terminal(2));
},
BareKey::Char('g') if key.has_modifiers(&[KeyModifier::Alt]) => {
write_to_pane_id(vec![102, 111, 111], PaneId::Terminal(2));
},
BareKey::Char('h') if key.has_modifiers(&[KeyModifier::Alt]) => {
write_chars_to_pane_id("foo\n", PaneId::Terminal(2));
},
BareKey::Char('i') if key.has_modifiers(&[KeyModifier::Alt]) => {
move_pane_with_pane_id(PaneId::Terminal(2));
},
BareKey::Char('j') if key.has_modifiers(&[KeyModifier::Alt]) => {
move_pane_with_pane_id_in_direction(PaneId::Terminal(2), Direction::Left);
},
BareKey::Char('k') if key.has_modifiers(&[KeyModifier::Alt]) => {
clear_screen_for_pane_id(PaneId::Terminal(2));
},
BareKey::Char('l') if key.has_modifiers(&[KeyModifier::Alt]) => {
scroll_up_in_pane_id(PaneId::Terminal(2));
},
BareKey::Char('m') if key.has_modifiers(&[KeyModifier::Alt]) => {
scroll_down_in_pane_id(PaneId::Terminal(2));
},
BareKey::Char('n') if key.has_modifiers(&[KeyModifier::Alt]) => {
scroll_to_top_in_pane_id(PaneId::Terminal(2));
},
BareKey::Char('o') if key.has_modifiers(&[KeyModifier::Alt]) => {
scroll_to_bottom_in_pane_id(PaneId::Terminal(2));
},
BareKey::Char('p') if key.has_modifiers(&[KeyModifier::Alt]) => {
page_scroll_up_in_pane_id(PaneId::Terminal(2));
},
BareKey::Char('q') if key.has_modifiers(&[KeyModifier::Alt]) => {
page_scroll_down_in_pane_id(PaneId::Terminal(2));
},
BareKey::Char('r') if key.has_modifiers(&[KeyModifier::Alt]) => {
toggle_pane_id_fullscreen(PaneId::Terminal(2));
},
BareKey::Char('s') if key.has_modifiers(&[KeyModifier::Alt]) => {
toggle_pane_embed_or_eject_for_pane_id(PaneId::Terminal(2));
},
BareKey::Char('t') if key.has_modifiers(&[KeyModifier::Alt]) => {
close_tab_with_index(2);
},
_ => {},
},
Event::CustomMessage(message, payload) => {
Expand Down
152 changes: 86 additions & 66 deletions zellij-server/src/panes/floating_panes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,34 +408,40 @@ impl FloatingPanes {
strategy: &ResizeStrategy,
) -> Result<bool> {
// true => successfully resized
let err_context =
|| format!("failed to {strategy} for active floating pane for client {client_id}");

if let Some(active_floating_pane_id) = self.active_panes.get(&client_id) {
return self.resize_pane_with_id(*strategy, *active_floating_pane_id);
}
Ok(false)
}
pub fn resize_pane_with_id(
&mut self,
strategy: ResizeStrategy,
pane_id: PaneId,
) -> Result<bool> {
// true => successfully resized
let err_context = || format!("Failed to resize pane with id: {:?}", pane_id);
let display_area = *self.display_area.borrow();
let viewport = *self.viewport.borrow();
if let Some(active_floating_pane_id) = self.active_panes.get(&client_id) {
let mut floating_pane_grid = FloatingPaneGrid::new(
&mut self.panes,
&mut self.desired_pane_positions,
display_area,
viewport,
);
floating_pane_grid
.change_pane_size(
active_floating_pane_id,
strategy,
(RESIZE_INCREMENT_WIDTH, RESIZE_INCREMENT_HEIGHT),
)
.with_context(err_context)?;
let mut floating_pane_grid = FloatingPaneGrid::new(
&mut self.panes,
&mut self.desired_pane_positions,
display_area,
viewport,
);
floating_pane_grid
.change_pane_size(
&pane_id,
&strategy,
(RESIZE_INCREMENT_WIDTH, RESIZE_INCREMENT_HEIGHT),
)
.with_context(err_context)?;

for pane in self.panes.values_mut() {
resize_pty!(pane, os_api, self.senders, self.character_cell_size)
.with_context(err_context)?;
}
self.set_force_render();
return Ok(true);
for pane in self.panes.values_mut() {
resize_pty!(pane, os_api, self.senders, self.character_cell_size)
.with_context(err_context)?;
}
Ok(false)
self.set_force_render();
Ok(true)
}

fn set_pane_active_at(&mut self, pane_id: PaneId) {
Expand Down Expand Up @@ -545,69 +551,83 @@ impl FloatingPanes {
}

pub fn move_active_pane_down(&mut self, client_id: ClientId) {
let display_area = *self.display_area.borrow();
let viewport = *self.viewport.borrow();
if let Some(active_pane_id) = self.active_panes.get(&client_id) {
let mut floating_pane_grid = FloatingPaneGrid::new(
&mut self.panes,
&mut self.desired_pane_positions,
display_area,
viewport,
);
floating_pane_grid.move_pane_down(active_pane_id).unwrap();
self.set_force_render();
self.move_pane_down(*active_pane_id);
}
}
pub fn move_active_pane_up(&mut self, client_id: ClientId) {
pub fn move_pane_down(&mut self, pane_id: PaneId) {
let display_area = *self.display_area.borrow();
let viewport = *self.viewport.borrow();
let mut floating_pane_grid = FloatingPaneGrid::new(
&mut self.panes,
&mut self.desired_pane_positions,
display_area,
viewport,
);
floating_pane_grid.move_pane_down(&pane_id).non_fatal();
self.set_force_render();
}
pub fn move_active_pane_up(&mut self, client_id: ClientId) {
if let Some(active_pane_id) = self.active_panes.get(&client_id) {
let mut floating_pane_grid = FloatingPaneGrid::new(
&mut self.panes,
&mut self.desired_pane_positions,
display_area,
viewport,
);
floating_pane_grid.move_pane_up(active_pane_id).unwrap();
self.set_force_render();
self.move_pane_up(*active_pane_id);
}
}
pub fn move_active_pane_left(&mut self, client_id: ClientId) {
pub fn move_pane_up(&mut self, pane_id: PaneId) {
let display_area = *self.display_area.borrow();
let viewport = *self.viewport.borrow();
let mut floating_pane_grid = FloatingPaneGrid::new(
&mut self.panes,
&mut self.desired_pane_positions,
display_area,
viewport,
);
floating_pane_grid.move_pane_up(&pane_id).non_fatal();
self.set_force_render();
}
pub fn move_active_pane_left(&mut self, client_id: ClientId) {
if let Some(active_pane_id) = self.active_panes.get(&client_id) {
let mut floating_pane_grid = FloatingPaneGrid::new(
&mut self.panes,
&mut self.desired_pane_positions,
display_area,
viewport,
);
floating_pane_grid.move_pane_left(active_pane_id).unwrap();
self.set_force_render();
self.move_pane_left(*active_pane_id);
}
}
pub fn move_active_pane_right(&mut self, client_id: ClientId) {
pub fn move_pane_left(&mut self, pane_id: PaneId) {
let display_area = *self.display_area.borrow();
let viewport = *self.viewport.borrow();
let mut floating_pane_grid = FloatingPaneGrid::new(
&mut self.panes,
&mut self.desired_pane_positions,
display_area,
viewport,
);
floating_pane_grid.move_pane_left(&pane_id).unwrap();
self.set_force_render();
}
pub fn move_active_pane_right(&mut self, client_id: ClientId) {
if let Some(active_pane_id) = self.active_panes.get(&client_id) {
let mut floating_pane_grid = FloatingPaneGrid::new(
&mut self.panes,
&mut self.desired_pane_positions,
display_area,
viewport,
);
floating_pane_grid.move_pane_right(active_pane_id).unwrap();
self.set_force_render();
self.move_pane_right(*active_pane_id);
}
}
pub fn move_pane_right(&mut self, pane_id: PaneId) {
let display_area = *self.display_area.borrow();
let viewport = *self.viewport.borrow();
let mut floating_pane_grid = FloatingPaneGrid::new(
&mut self.panes,
&mut self.desired_pane_positions,
display_area,
viewport,
);
floating_pane_grid.move_pane_right(&pane_id).unwrap();
self.set_force_render();
}
pub fn move_active_pane(
&mut self,
search_backwards: bool,
_os_api: &mut Box<dyn ServerOsApi>,
client_id: ClientId,
) {
let active_pane_id = self.get_active_pane_id(client_id).unwrap();

self.move_pane(search_backwards, active_pane_id)
}
pub fn move_pane(&mut self, search_backwards: bool, pane_id: PaneId) {
let new_position_id = {
let pane_grid = FloatingPaneGrid::new(
&mut self.panes,
Expand All @@ -616,13 +636,13 @@ impl FloatingPanes {
*self.viewport.borrow(),
);
if search_backwards {
pane_grid.previous_selectable_pane_id(&active_pane_id)
pane_grid.previous_selectable_pane_id(&pane_id)
} else {
pane_grid.next_selectable_pane_id(&active_pane_id)
pane_grid.next_selectable_pane_id(&pane_id)
}
};
if let Some(new_position_id) = new_position_id {
let current_position = self.panes.get(&active_pane_id).unwrap();
let current_position = self.panes.get(&pane_id).unwrap();
let prev_geom = current_position.position_and_size();
let prev_geom_override = current_position.geom_override();

Expand All @@ -635,7 +655,7 @@ impl FloatingPanes {
}
new_position.set_should_render(true);

let current_position = self.panes.get_mut(&active_pane_id).unwrap();
let current_position = self.panes.get_mut(&pane_id).unwrap();
current_position.set_geom(next_geom);
if let Some(geom) = next_geom_override {
current_position.set_geom_override(geom);
Expand Down
2 changes: 1 addition & 1 deletion zellij-server/src/panes/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ impl Grid {
self.mark_for_rerender();
}
/// Dumps all lines above terminal vieport and the viewport itself to a string
pub fn dump_screen(&mut self, full: bool) -> String {
pub fn dump_screen(&self, full: bool) -> String {
let viewport: String = dump_screen!(self.viewport);
if !full {
return viewport;
Expand Down
2 changes: 1 addition & 1 deletion zellij-server/src/panes/terminal_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ impl Pane for TerminalPane {
self.geom.y -= count;
self.reflow_lines();
}
fn dump_screen(&mut self, _client_id: ClientId, full: bool) -> String {
fn dump_screen(&self, full: bool) -> String {
self.grid.dump_screen(full)
}
fn clear_screen(&mut self) {
Expand Down
Loading
Loading