refactor(tab): simplify logic for scrolling active pane (#1184)

This commit is contained in:
Thomas Linford 2022-03-06 19:50:33 +01:00 committed by GitHub
parent 520dee5426
commit c5eea7bd91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -887,6 +887,20 @@ impl Tab {
self.get_active_pane_id(client_id) self.get_active_pane_id(client_id)
.and_then(|ap| self.panes.get(&ap).map(Box::as_ref)) .and_then(|ap| self.panes.get(&ap).map(Box::as_ref))
} }
pub fn get_active_pane_mut(&mut self, client_id: ClientId) -> Option<&mut Box<dyn Pane>> {
self.get_active_pane_id(client_id)
.and_then(|ap| self.panes.get_mut(&ap))
}
pub fn get_active_pane_or_floating_pane_mut(
&mut self,
client_id: ClientId,
) -> Option<&mut Box<dyn Pane>> {
if self.floating_panes.panes_are_visible() && self.floating_panes.has_active_panes() {
self.floating_panes.get_active_pane_mut(client_id)
} else {
self.get_active_pane_mut(client_id)
}
}
fn get_active_pane_id(&self, client_id: ClientId) -> Option<PaneId> { fn get_active_pane_id(&self, client_id: ClientId) -> Option<PaneId> {
// TODO: why do we need this? // TODO: why do we need this?
self.active_panes.get(&client_id).copied() self.active_panes.get(&client_id).copied()
@ -2200,210 +2214,75 @@ impl Tab {
} }
} }
pub fn scroll_active_terminal_up(&mut self, client_id: ClientId) { pub fn scroll_active_terminal_up(&mut self, client_id: ClientId) {
if self.floating_panes.panes_are_visible() && self.floating_panes.has_active_panes() { if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) {
self.floating_panes active_pane.scroll_up(1, client_id);
.get_active_pane_mut(client_id)
.map(|active_pane| active_pane.scroll_up(1, client_id));
} else {
self.active_panes
.get(&client_id)
.and_then(|active_pane_id| self.panes.get_mut(active_pane_id))
.map(|active_pane| active_pane.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) {
if self.floating_panes.panes_are_visible() && self.floating_panes.has_active_panes() { if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) {
self.floating_panes active_pane.scroll_down(1, client_id);
.get_active_pane_mut(client_id) if !active_pane.is_scrolled() {
.and_then(|active_pane| { if let PaneId::Terminal(raw_fd) = active_pane.pid() {
active_pane.scroll_down(1, client_id); self.process_pending_vte_events(raw_fd);
if !active_pane.is_scrolled() { }
if let PaneId::Terminal(raw_fd) = active_pane.pid() { }
return Some(raw_fd);
}
}
None
})
.map(|raw_fd| self.process_pending_vte_events(raw_fd));
} else {
self.active_panes
.get(&client_id)
.and_then(|active_pane_id| self.panes.get_mut(active_pane_id))
.and_then(|active_pane| {
active_pane.scroll_down(1, client_id);
if !active_pane.is_scrolled() {
if let PaneId::Terminal(raw_fd) = active_pane.pid() {
return Some(raw_fd);
}
}
None
})
.map(|raw_fd| self.process_pending_vte_events(raw_fd));
} }
} }
pub fn scroll_active_terminal_up_page(&mut self, client_id: ClientId) { pub fn scroll_active_terminal_up_page(&mut self, client_id: ClientId) {
if self.floating_panes.panes_are_visible() && self.floating_panes.has_active_panes() { if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) {
self.floating_panes // prevent overflow when row == 0
.get_active_pane_mut(client_id) let scroll_rows = active_pane.rows().max(1) - 1;
.map(|active_pane| { active_pane.scroll_up(scroll_rows, client_id);
// prevent overflow when row == 0
let scroll_rows = active_pane.rows().max(1) - 1;
active_pane.scroll_up(scroll_rows, client_id);
});
} else {
self.active_panes
.get(&client_id)
.and_then(|active_pane_id| self.panes.get_mut(active_pane_id))
.map(|active_pane| {
// prevent overflow when row == 0
let scroll_rows = active_pane.get_content_rows();
active_pane.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) {
if self.floating_panes.panes_are_visible() && self.floating_panes.has_active_panes() { if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) {
self.floating_panes let scroll_rows = active_pane.get_content_rows();
.get_active_pane_mut(client_id) active_pane.scroll_down(scroll_rows, client_id);
.and_then(|active_pane| { if !active_pane.is_scrolled() {
let scroll_rows = active_pane.get_content_rows(); if let PaneId::Terminal(raw_fd) = active_pane.pid() {
active_pane.scroll_down(scroll_rows, client_id); self.process_pending_vte_events(raw_fd);
if !active_pane.is_scrolled() { }
if let PaneId::Terminal(raw_fd) = active_pane.pid() { }
return Some(raw_fd);
}
}
None
})
.map(|raw_fd| self.process_pending_vte_events(raw_fd));
} else {
self.active_panes
.get(&client_id)
.and_then(|active_pane_id| self.panes.get_mut(active_pane_id))
.and_then(|active_pane| {
let scroll_rows = active_pane.get_content_rows();
active_pane.scroll_down(scroll_rows, client_id);
if !active_pane.is_scrolled() {
if let PaneId::Terminal(raw_fd) = active_pane.pid() {
return Some(raw_fd);
}
}
None
})
.map(|raw_fd| self.process_pending_vte_events(raw_fd));
} }
} }
pub fn scroll_active_terminal_up_half_page(&mut self, client_id: ClientId) { pub fn scroll_active_terminal_up_half_page(&mut self, client_id: ClientId) {
if self.floating_panes.panes_are_visible() && self.floating_panes.has_active_panes() { if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) {
self.floating_panes // prevent overflow when row == 0
.get_active_pane_mut(client_id) let scroll_rows = (active_pane.rows().max(1) - 1) / 2;
.map(|active_pane| { active_pane.scroll_up(scroll_rows, client_id);
// prevent overflow when row == 0
let scroll_rows = (active_pane.rows().max(1) - 1) / 2;
active_pane.scroll_up(scroll_rows, client_id);
});
} else {
self.active_panes
.get(&client_id)
.and_then(|active_pane_id| self.panes.get_mut(active_pane_id))
.map(|active_pane| {
// prevent overflow when row == 0
let scroll_rows = (active_pane.rows().max(1) - 1) / 2;
active_pane.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) {
if self.floating_panes.panes_are_visible() && self.floating_panes.has_active_panes() { if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) {
self.floating_panes let scroll_rows = (active_pane.rows().max(1) - 1) / 2;
.get_active_pane_mut(client_id) active_pane.scroll_down(scroll_rows, client_id);
.and_then(|active_pane| { if !active_pane.is_scrolled() {
let scroll_rows = (active_pane.rows().max(1) - 1) / 2; if let PaneId::Terminal(raw_fd) = active_pane.pid() {
active_pane.scroll_down(scroll_rows, client_id); self.process_pending_vte_events(raw_fd);
if !active_pane.is_scrolled() { }
if let PaneId::Terminal(raw_fd) = active_pane.pid() { }
return Some(raw_fd);
}
}
None
})
.map(|raw_fd| self.process_pending_vte_events(raw_fd));
} else {
self.active_panes
.get(&client_id)
.and_then(|active_pane_id| self.panes.get_mut(active_pane_id))
.and_then(|active_pane| {
let scroll_rows = (active_pane.rows().max(1) - 1) / 2;
active_pane.scroll_down(scroll_rows, client_id);
if !active_pane.is_scrolled() {
if let PaneId::Terminal(raw_fd) = active_pane.pid() {
return Some(raw_fd);
}
}
None
})
.map(|raw_fd| self.process_pending_vte_events(raw_fd));
} }
} }
pub fn scroll_active_terminal_to_bottom(&mut self, client_id: ClientId) { pub fn scroll_active_terminal_to_bottom(&mut self, client_id: ClientId) {
if self.floating_panes.panes_are_visible() && self.floating_panes.has_active_panes() { if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) {
self.floating_panes active_pane.clear_scroll();
.get_active_pane_mut(client_id) if !active_pane.is_scrolled() {
.and_then(|active_pane| { if let PaneId::Terminal(raw_fd) = active_pane.pid() {
active_pane.clear_scroll(); self.process_pending_vte_events(raw_fd);
if !active_pane.is_scrolled() { }
if let PaneId::Terminal(raw_fd) = active_pane.pid() { }
return Some(raw_fd);
}
}
None
})
.map(|raw_fd| self.process_pending_vte_events(raw_fd));
} else {
self.active_panes
.get(&client_id)
.and_then(|active_pane_id| self.panes.get_mut(active_pane_id))
.and_then(|active_pane| {
active_pane.clear_scroll();
if !active_pane.is_scrolled() {
if let PaneId::Terminal(raw_fd) = active_pane.pid() {
return Some(raw_fd);
}
}
None
})
.map(|raw_fd| self.process_pending_vte_events(raw_fd));
} }
} }
pub fn clear_active_terminal_scroll(&mut self, client_id: ClientId) { pub fn clear_active_terminal_scroll(&mut self, client_id: ClientId) {
// TODO: is this a thing? // TODO: is this a thing?
if self.floating_panes.panes_are_visible() && self.floating_panes.has_active_panes() { if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) {
self.floating_panes active_pane.clear_scroll();
.get_active_pane_mut(client_id) if !active_pane.is_scrolled() {
.and_then(|active_pane| { if let PaneId::Terminal(raw_fd) = active_pane.pid() {
active_pane.clear_scroll(); self.process_pending_vte_events(raw_fd);
if !active_pane.is_scrolled() { }
if let PaneId::Terminal(raw_fd) = active_pane.pid() { }
return Some(raw_fd);
}
}
None
})
.map(|raw_fd| self.process_pending_vte_events(raw_fd));
} else {
self.active_panes
.get(&client_id)
.and_then(|active_pane_id| self.panes.get_mut(active_pane_id))
.and_then(|active_pane| {
active_pane.clear_scroll();
if !active_pane.is_scrolled() {
if let PaneId::Terminal(raw_fd) = active_pane.pid() {
return Some(raw_fd);
}
}
None
})
.map(|raw_fd| self.process_pending_vte_events(raw_fd));
} }
} }
pub fn scroll_terminal_up(&mut self, point: &Position, lines: usize, client_id: ClientId) { pub fn scroll_terminal_up(&mut self, point: &Position, lines: usize, client_id: ClientId) {