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

Refactor Pane #3764

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
18 changes: 9 additions & 9 deletions zellij-server/src/panes/active_panes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::tab::Pane;
use crate::tab::PaneTrait;

use crate::{os_input_output::ServerOsApi, panes::PaneId, ClientId};
use std::collections::{BTreeMap, HashMap};
Expand Down Expand Up @@ -30,13 +30,13 @@ impl ActivePanes {
&mut self,
client_id: ClientId,
pane_id: PaneId,
panes: &mut BTreeMap<PaneId, Box<dyn Pane>>,
panes: &mut BTreeMap<PaneId, Box<dyn PaneTrait>>,
) {
self.unfocus_pane_for_client(client_id, panes);
self.active_panes.insert(client_id, pane_id);
self.focus_pane(pane_id, panes);
}
pub fn clear(&mut self, panes: &mut BTreeMap<PaneId, Box<dyn Pane>>) {
pub fn clear(&mut self, panes: &mut BTreeMap<PaneId, Box<dyn PaneTrait>>) {
for pane_id in self.active_panes.values() {
self.unfocus_pane(*pane_id, panes);
}
Expand All @@ -54,19 +54,19 @@ impl ActivePanes {
pub fn remove(
&mut self,
client_id: &ClientId,
panes: &mut BTreeMap<PaneId, Box<dyn Pane>>,
panes: &mut BTreeMap<PaneId, Box<dyn PaneTrait>>,
) -> Option<PaneId> {
if let Some(pane_id_to_unfocus) = self.active_panes.get(&client_id) {
self.unfocus_pane(*pane_id_to_unfocus, panes);
}
self.active_panes.remove(client_id)
}
pub fn unfocus_all_panes(&self, panes: &mut BTreeMap<PaneId, Box<dyn Pane>>) {
pub fn unfocus_all_panes(&self, panes: &mut BTreeMap<PaneId, Box<dyn PaneTrait>>) {
for (_client_id, pane_id) in &self.active_panes {
self.unfocus_pane(*pane_id, panes);
}
}
pub fn focus_all_panes(&self, panes: &mut BTreeMap<PaneId, Box<dyn Pane>>) {
pub fn focus_all_panes(&self, panes: &mut BTreeMap<PaneId, Box<dyn PaneTrait>>) {
for (_client_id, pane_id) in &self.active_panes {
self.focus_pane(*pane_id, panes);
}
Expand All @@ -80,13 +80,13 @@ impl ActivePanes {
fn unfocus_pane_for_client(
&self,
client_id: ClientId,
panes: &mut BTreeMap<PaneId, Box<dyn Pane>>,
panes: &mut BTreeMap<PaneId, Box<dyn PaneTrait>>,
) {
if let Some(pane_id_to_unfocus) = self.active_panes.get(&client_id) {
self.unfocus_pane(*pane_id_to_unfocus, panes);
}
}
fn unfocus_pane(&self, pane_id: PaneId, panes: &mut BTreeMap<PaneId, Box<dyn Pane>>) {
fn unfocus_pane(&self, pane_id: PaneId, panes: &mut BTreeMap<PaneId, Box<dyn PaneTrait>>) {
if let PaneId::Terminal(terminal_id) = pane_id {
if let Some(focus_event) = panes.get(&pane_id).and_then(|p| p.unfocus_event()) {
let _ = self
Expand All @@ -95,7 +95,7 @@ impl ActivePanes {
}
}
}
fn focus_pane(&self, pane_id: PaneId, panes: &mut BTreeMap<PaneId, Box<dyn Pane>>) {
fn focus_pane(&self, pane_id: PaneId, panes: &mut BTreeMap<PaneId, Box<dyn PaneTrait>>) {
if let PaneId::Terminal(terminal_id) = pane_id {
if let Some(focus_event) = panes.get(&pane_id).and_then(|p| p.focus_event()) {
let _ = self
Expand Down
20 changes: 10 additions & 10 deletions zellij-server/src/panes/floating_panes/floating_pane_grid.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::tab::{MIN_TERMINAL_HEIGHT, MIN_TERMINAL_WIDTH};
use crate::{panes::PaneId, tab::Pane};
use crate::{panes::PaneId, tab::PaneTrait};
use std::cmp::Ordering;
use std::collections::HashMap;
use zellij_utils::data::{Direction, ResizeStrategy};
Expand All @@ -20,15 +20,15 @@ fn no_pane_id(pane_id: &PaneId) -> String {
}

pub struct FloatingPaneGrid<'a> {
panes: Rc<RefCell<HashMap<PaneId, &'a mut Box<dyn Pane>>>>,
panes: Rc<RefCell<HashMap<PaneId, &'a mut Box<dyn PaneTrait>>>>,
desired_pane_positions: Rc<RefCell<&'a mut HashMap<PaneId, PaneGeom>>>,
display_area: Size, // includes all panes (including eg. the status bar and tab bar in the default layout)
viewport: Viewport, // includes all non-UI panes
}

impl<'a> FloatingPaneGrid<'a> {
pub fn new(
panes: impl IntoIterator<Item = (&'a PaneId, &'a mut Box<dyn Pane>)>,
panes: impl IntoIterator<Item = (&'a PaneId, &'a mut Box<dyn PaneTrait>)>,
desired_pane_positions: &'a mut HashMap<PaneId, PaneGeom>,
display_area: Size,
viewport: Viewport,
Expand Down Expand Up @@ -602,7 +602,7 @@ impl<'a> FloatingPaneGrid<'a> {
pub fn next_selectable_pane_id_to_the_left(&self, current_pane_id: &PaneId) -> Option<PaneId> {
let panes = self.panes.borrow();
let current_pane = panes.get(current_pane_id)?;
let panes: Vec<(PaneId, &&mut Box<dyn Pane>)> = panes
let panes: Vec<(PaneId, &&mut Box<dyn PaneTrait>)> = panes
.iter()
.filter(|(_, p)| p.selectable())
.map(|(p_id, p)| (*p_id, p))
Expand All @@ -627,7 +627,7 @@ impl<'a> FloatingPaneGrid<'a> {
}
pub fn pane_id_on_edge(&self, direction: Direction) -> Option<PaneId> {
let panes = self.panes.borrow();
let panes: Vec<(PaneId, &&mut Box<dyn Pane>)> = panes
let panes: Vec<(PaneId, &&mut Box<dyn PaneTrait>)> = panes
.iter()
.filter(|(_, p)| p.selectable())
.map(|(p_id, p)| (*p_id, p))
Expand Down Expand Up @@ -672,7 +672,7 @@ impl<'a> FloatingPaneGrid<'a> {
pub fn next_selectable_pane_id_below(&self, current_pane_id: &PaneId) -> Option<PaneId> {
let panes = self.panes.borrow();
let current_pane = panes.get(current_pane_id)?;
let panes: Vec<(PaneId, &&mut Box<dyn Pane>)> = panes
let panes: Vec<(PaneId, &&mut Box<dyn PaneTrait>)> = panes
.iter()
.filter(|(_, p)| p.selectable())
.map(|(p_id, p)| (*p_id, p))
Expand All @@ -698,7 +698,7 @@ impl<'a> FloatingPaneGrid<'a> {
pub fn next_selectable_pane_id_above(&self, current_pane_id: &PaneId) -> Option<PaneId> {
let panes = self.panes.borrow();
let current_pane = panes.get(current_pane_id)?;
let panes: Vec<(PaneId, &&mut Box<dyn Pane>)> = panes
let panes: Vec<(PaneId, &&mut Box<dyn PaneTrait>)> = panes
.iter()
.filter(|(_, p)| p.selectable())
.map(|(p_id, p)| (*p_id, p))
Expand All @@ -724,7 +724,7 @@ impl<'a> FloatingPaneGrid<'a> {
pub fn next_selectable_pane_id_to_the_right(&self, current_pane_id: &PaneId) -> Option<PaneId> {
let panes = self.panes.borrow();
let current_pane = panes.get(current_pane_id)?;
let panes: Vec<(PaneId, &&mut Box<dyn Pane>)> = panes
let panes: Vec<(PaneId, &&mut Box<dyn PaneTrait>)> = panes
.iter()
.filter(|(_, p)| p.selectable())
.map(|(p_id, p)| (*p_id, p))
Expand All @@ -749,7 +749,7 @@ impl<'a> FloatingPaneGrid<'a> {
}
pub fn next_selectable_pane_id(&self, current_pane_id: &PaneId) -> Option<PaneId> {
let panes = self.panes.borrow();
let mut panes: Vec<(PaneId, &&mut Box<dyn Pane>)> = panes
let mut panes: Vec<(PaneId, &&mut Box<dyn PaneTrait>)> = panes
.iter()
.filter(|(_, p)| p.selectable())
.map(|(p_id, p)| (*p_id, p))
Expand All @@ -775,7 +775,7 @@ impl<'a> FloatingPaneGrid<'a> {
}
pub fn previous_selectable_pane_id(&self, current_pane_id: &PaneId) -> Option<PaneId> {
let panes = self.panes.borrow();
let mut panes: Vec<(PaneId, &&mut Box<dyn Pane>)> = panes
let mut panes: Vec<(PaneId, &&mut Box<dyn PaneTrait>)> = panes
.iter()
.filter(|(_, p)| p.selectable())
.map(|(p_id, p)| (*p_id, p))
Expand Down
36 changes: 18 additions & 18 deletions zellij-server/src/panes/floating_panes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use zellij_utils::{
};

use crate::resize_pty;
use crate::tab::{pane_info_for_pane, Pane};
use crate::tab::{pane_info_for_pane, PaneTrait};
use floating_pane_grid::FloatingPaneGrid;

use crate::{
Expand Down Expand Up @@ -33,7 +33,7 @@ const RESIZE_INCREMENT_WIDTH: usize = 5;
const RESIZE_INCREMENT_HEIGHT: usize = 2;

pub struct FloatingPanes {
panes: BTreeMap<PaneId, Box<dyn Pane>>,
panes: BTreeMap<PaneId, Box<dyn PaneTrait>>,
display_area: Rc<RefCell<Size>>,
viewport: Rc<RefCell<Viewport>>,
connected_clients: Rc<RefCell<HashSet<ClientId>>>,
Expand Down Expand Up @@ -101,17 +101,17 @@ impl FloatingPanes {
pub fn pane_ids(&self) -> impl Iterator<Item = &PaneId> {
self.panes.keys()
}
pub fn add_pane(&mut self, pane_id: PaneId, pane: Box<dyn Pane>) {
pub fn add_pane(&mut self, pane_id: PaneId, pane: Box<dyn PaneTrait>) {
self.desired_pane_positions
.insert(pane_id, pane.position_and_size());
self.panes.insert(pane_id, pane);
self.z_indices.push(pane_id);
}
pub fn replace_active_pane(
&mut self,
pane: Box<dyn Pane>,
pane: Box<dyn PaneTrait>,
client_id: ClientId,
) -> Result<Box<dyn Pane>> {
) -> Result<Box<dyn PaneTrait>> {
self.active_panes
.get(&client_id)
.with_context(|| format!("failed to determine active pane for client {client_id}"))
Expand All @@ -122,8 +122,8 @@ impl FloatingPanes {
pub fn replace_pane(
&mut self,
pane_id: PaneId,
mut with_pane: Box<dyn Pane>,
) -> Result<Box<dyn Pane>> {
mut with_pane: Box<dyn PaneTrait>,
) -> Result<Box<dyn PaneTrait>> {
let err_context = || format!("failed to replace pane {pane_id:?} with pane");

let with_pane_id = with_pane.pid();
Expand Down Expand Up @@ -160,7 +160,7 @@ impl FloatingPanes {
let _ = self.set_pane_frames();
removed_pane
}
pub fn remove_pane(&mut self, pane_id: PaneId) -> Option<Box<dyn Pane>> {
pub fn remove_pane(&mut self, pane_id: PaneId) -> Option<Box<dyn PaneTrait>> {
self.z_indices.retain(|p_id| *p_id != pane_id);
self.desired_pane_positions.remove(&pane_id);
self.panes.remove(&pane_id)
Expand All @@ -176,18 +176,18 @@ impl FloatingPanes {
.get_mut(&pane_id)
.map(|p| p.hold(exit_status, is_first_run, run_command));
}
pub fn get(&self, pane_id: &PaneId) -> Option<&Box<dyn Pane>> {
pub fn get(&self, pane_id: &PaneId) -> Option<&Box<dyn PaneTrait>> {
self.panes.get(pane_id)
}
pub fn get_mut(&mut self, pane_id: &PaneId) -> Option<&mut Box<dyn Pane>> {
pub fn get_mut(&mut self, pane_id: &PaneId) -> Option<&mut Box<dyn PaneTrait>> {
self.panes.get_mut(pane_id)
}
pub fn get_active_pane(&self, client_id: ClientId) -> Option<&Box<dyn Pane>> {
pub fn get_active_pane(&self, client_id: ClientId) -> Option<&Box<dyn PaneTrait>> {
self.active_panes
.get(&client_id)
.and_then(|active_pane_id| self.panes.get(active_pane_id))
}
pub fn get_active_pane_mut(&mut self, client_id: ClientId) -> Option<&mut Box<dyn Pane>> {
pub fn get_active_pane_mut(&mut self, client_id: ClientId) -> Option<&mut Box<dyn PaneTrait>> {
self.active_panes
.get(&client_id)
.and_then(|active_pane_id| self.panes.get_mut(active_pane_id))
Expand Down Expand Up @@ -672,7 +672,7 @@ impl FloatingPanes {
.collect();

// find the most recently active pane
let mut next_active_pane_candidates: Vec<(&PaneId, &Box<dyn Pane>)> = self
let mut next_active_pane_candidates: Vec<(&PaneId, &Box<dyn PaneTrait>)> = self
.panes
.iter()
.filter(|(_p_id, p)| p.selectable())
Expand Down Expand Up @@ -736,10 +736,10 @@ impl FloatingPanes {
self.active_panes.remove(&client_id, &mut self.panes);
self.set_force_render();
}
pub fn get_pane(&self, pane_id: PaneId) -> Option<&Box<dyn Pane>> {
pub fn get_pane(&self, pane_id: PaneId) -> Option<&Box<dyn PaneTrait>> {
self.panes.get(&pane_id)
}
pub fn get_pane_mut(&mut self, pane_id: PaneId) -> Option<&mut Box<dyn Pane>> {
pub fn get_pane_mut(&mut self, pane_id: PaneId) -> Option<&mut Box<dyn PaneTrait>> {
self.panes.get_mut(&pane_id)
}
pub fn get_pane_id_at(
Expand Down Expand Up @@ -771,7 +771,7 @@ impl FloatingPanes {
&mut self,
position: &Position,
search_selectable: bool,
) -> Option<&mut Box<dyn Pane>> {
) -> Option<&mut Box<dyn PaneTrait>> {
self.get_pane_id_at(position, search_selectable)
.unwrap()
.and_then(|pane_id| self.panes.get_mut(&pane_id))
Expand Down Expand Up @@ -836,13 +836,13 @@ impl FloatingPanes {
pub fn get_active_pane_id(&self, client_id: ClientId) -> Option<PaneId> {
self.active_panes.get(&client_id).copied()
}
pub fn get_panes(&self) -> impl Iterator<Item = (&PaneId, &Box<dyn Pane>)> {
pub fn get_panes(&self) -> impl Iterator<Item = (&PaneId, &Box<dyn PaneTrait>)> {
self.panes.iter()
}
pub fn visible_panes_count(&self) -> usize {
self.panes.len()
}
pub fn drain(&mut self) -> BTreeMap<PaneId, Box<dyn Pane>> {
pub fn drain(&mut self) -> BTreeMap<PaneId, Box<dyn PaneTrait>> {
self.z_indices.clear();
self.desired_pane_positions.clear();
match self.panes.iter().next().map(|(pid, _p)| *pid) {
Expand Down
21 changes: 21 additions & 0 deletions zellij-server/src/panes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ mod search;
mod terminal_pane;
mod tiled_panes;

use std::ops::Deref;

pub use active_panes::*;
pub use alacritty_functions::*;
pub use floating_panes::*;
Expand All @@ -22,3 +24,22 @@ pub use sixel::*;
pub(crate) use terminal_character::*;
pub use terminal_pane::*;
pub use tiled_panes::*;

use crate::tab::PaneTrait;

enum Pane {
TerminalPane(TerminalPane),
PluginPane(PluginPane),
}

// Deref trait for Lazy use of common methods as defined in the trait
impl Deref for Pane {
type Target = impl PaneTrait;

fn deref(&self) -> &Self::Target {
match self {
Pane::TerminalPane(term) => term,
Pane::PluginPane(plugin) => plugin,
}
}
}
4 changes: 2 additions & 2 deletions zellij-server/src/panes/plugin_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::panes::{
};
use crate::plugins::PluginInstruction;
use crate::pty::VteBytes;
use crate::tab::{AdjustedInput, Pane};
use crate::tab::{AdjustedInput, PaneTrait};
use crate::ui::{
loading_indication::LoadingIndication,
pane_boundaries_frame::{FrameParams, PaneFrame},
Expand Down Expand Up @@ -162,7 +162,7 @@ impl PluginPane {
}
}

impl Pane for PluginPane {
impl PaneTrait for PluginPane {
// FIXME: These position and size things should all be moved to default trait implementations,
// with something like a get_pos_and_sz() method underpinning all of them. Alternatively and
// preferably, just use an enum and not a trait object
Expand Down
4 changes: 2 additions & 2 deletions zellij-server/src/panes/terminal_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::panes::{
terminal_character::{render_first_run_banner, TerminalCharacter, EMPTY_TERMINAL_CHARACTER},
};
use crate::pty::VteBytes;
use crate::tab::{AdjustedInput, Pane};
use crate::tab::{AdjustedInput, PaneTrait};
use crate::ClientId;
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -141,7 +141,7 @@ pub struct TerminalPane {
arrow_fonts: bool,
}

impl Pane for TerminalPane {
impl PaneTrait for TerminalPane {
fn x(&self) -> usize {
self.get_x()
}
Expand Down
Loading