#[derive(Default)]
pub struct ClearLatch {
requested: bool,
}
impl ClearLatch {
pub fn request(&mut self) {
self.requested = true;
}
pub fn consume(&mut self) -> bool {
let requested = self.requested;
self.requested = false;
requested
}
}
pub struct ImageOverlay<L> {
image: Option<String>,
layout: Option<L>,
displayed: bool,
dirty: bool,
clear: ClearLatch,
under: Option<u64>,
}
impl<L> Default for ImageOverlay<L> {
fn default() -> Self {
Self {
image: None,
layout: None,
displayed: false,
dirty: false,
clear: ClearLatch::default(),
under: None,
}
}
}
impl<L: PartialEq> ImageOverlay<L> {
pub fn layout_matches(&self, desired: &L) -> bool {
self.layout.as_ref() == Some(desired)
}
pub fn layout(&self) -> Option<&L> {
self.layout.as_ref()
}
pub fn set(&mut self, image: String, layout: L) {
self.image = Some(image);
self.layout = Some(layout);
self.under = None;
}
pub fn set_image(&mut self, image: String) {
self.image = Some(image);
self.under = None;
}
pub fn set_layout(&mut self, layout: L) {
self.layout = Some(layout);
self.under = None;
}
pub fn needs_emit(&self, under: u64) -> bool {
!self.displayed || self.under != Some(under)
}
pub fn mark_sent_over(&mut self, under: u64) {
self.displayed = true;
self.under = Some(under);
}
pub fn forget_sent(&mut self) {
self.under = None;
}
pub fn has_image(&self) -> bool {
self.image.is_some()
}
pub fn image(&self) -> Option<&str> {
self.image.as_deref()
}
pub fn mark_dirty(&mut self) {
self.dirty = true;
}
pub fn mark_hidden(&mut self) {
if self.displayed {
self.displayed = false;
self.dirty = true;
}
}
pub fn hide_and_request_clear(&mut self) {
if self.displayed {
self.displayed = false;
self.clear.request();
}
}
pub fn request_clear(&mut self) {
self.clear.request();
}
pub fn request_clear_if_displayed(&mut self) {
if self.displayed {
self.clear.request();
}
}
pub fn invalidate_layout(&mut self) {
self.layout = None;
self.under = None;
}
pub fn disable(&mut self) {
self.request_clear_if_displayed();
self.image = None;
self.layout = None;
}
pub fn consume_clear(&mut self) -> bool {
if self.clear.consume() {
self.displayed = false;
true
} else {
false
}
}
pub fn consume_clear_latch(&mut self) -> bool {
self.clear.consume()
}
pub fn consume_clear_or(&mut self, also: bool) -> bool {
if self.clear.consume() || (self.displayed && also) {
self.displayed = false;
true
} else {
false
}
}
pub fn payload(&self) -> Option<(&str, &L)> {
Some((self.image.as_deref()?, self.layout.as_ref()?))
}
pub fn emit_payload(&self) -> Option<(&str, &L)> {
if self.dirty { self.payload() } else { None }
}
pub fn mark_displayed(&mut self) {
self.displayed = true;
}
pub fn mark_emitted(&mut self) {
self.dirty = false;
self.displayed = true;
}
#[cfg(test)]
pub fn set_test_state(&mut self, image: Option<String>, layout: Option<L>, displayed: bool) {
self.image = image;
self.layout = layout;
self.displayed = displayed;
}
#[cfg(test)]
pub fn image_is_none(&self) -> bool {
self.image.is_none()
}
#[cfg(test)]
pub fn layout_is_none(&self) -> bool {
self.layout.is_none()
}
#[cfg(test)]
pub fn set_displayed(&mut self, v: bool) {
self.displayed = v;
}
#[cfg(test)]
pub fn set_dirty(&mut self, v: bool) {
self.dirty = v;
}
#[cfg(test)]
pub fn is_displayed(&self) -> bool {
self.displayed
}
#[cfg(test)]
pub fn is_dirty(&self) -> bool {
self.dirty
}
#[cfg(test)]
pub fn clear_requested(&self) -> bool {
self.clear.requested
}
}
#[derive(Default)]
pub struct CellOverlay {
image: Option<String>,
last_emitted: Option<(u16, u16)>,
clear: ClearLatch,
}
impl CellOverlay {
pub fn set_image(&mut self, image: String) {
self.image = Some(image);
}
pub fn clear_image(&mut self) {
self.image = None;
}
pub fn image(&self) -> Option<&str> {
self.image.as_deref()
}
pub fn has_image(&self) -> bool {
self.image.is_some()
}
pub fn last_emitted(&self) -> Option<(u16, u16)> {
self.last_emitted
}
pub fn mark_emitted_at(&mut self, cell: (u16, u16)) {
self.last_emitted = Some(cell);
}
pub fn clear_emitted(&mut self) {
self.last_emitted = None;
}
pub fn was_emitted(&self) -> bool {
self.last_emitted.is_some()
}
pub fn request_clear(&mut self) {
self.clear.request();
}
pub fn consume_clear(&mut self) -> bool {
self.clear.consume()
}
}
#[derive(Default)]
pub struct ActivityOverlay {
images: Option<super::ActivityBarImages>,
dirty: bool,
last_emit: Option<std::time::Instant>,
last_positions: Vec<(u16, u16)>,
neighbourhood: Vec<u64>,
sent: Vec<((u16, u16), u64)>,
side_bar_shown: Option<bool>,
clear: ClearLatch,
}
impl ActivityOverlay {
const KEEPALIVE: std::time::Duration = std::time::Duration::from_secs(2);
pub fn set_images(&mut self, images: super::ActivityBarImages) {
self.images = Some(images);
}
pub fn images(&self) -> Option<&super::ActivityBarImages> {
self.images.as_ref()
}
pub fn has_images(&self) -> bool {
self.images.is_some()
}
pub fn clear_images(&mut self) {
self.images = None;
}
pub fn mark_dirty(&mut self) {
self.dirty = true;
}
pub fn should_refresh(&self, allow_keepalive: bool) -> bool {
self.dirty
|| (allow_keepalive
&& self
.last_emit
.is_none_or(|t| t.elapsed() >= Self::KEEPALIVE))
}
pub fn mark_emitted(&mut self) {
self.dirty = false;
self.last_emit = Some(std::time::Instant::now());
}
pub fn neighbourhood_changed(&self, i: usize, around: &[u64]) -> bool {
self.neighbourhood.get(i) != around.get(i) || around.get(i).is_none()
}
pub fn set_neighbourhood(&mut self, around: Vec<u64>) {
self.neighbourhood = around;
}
pub fn was_sent(&self, cell: (u16, u16), image: u64) -> bool {
self.sent.contains(&(cell, image))
}
pub fn nothing_sent(&self) -> bool {
self.sent.is_empty()
}
pub fn set_sent(&mut self, sent: Vec<((u16, u16), u64)>) {
self.sent = sent;
}
pub fn forget_sent(&mut self) {
self.sent.clear();
}
pub fn note_side_bar_shown(&mut self, shown: bool) {
let last = self.side_bar_shown.replace(shown);
if last.is_some_and(|was| was != shown) {
self.dirty = true;
}
}
pub fn dirty(&self) -> bool {
self.dirty
}
pub fn last_positions(&self) -> &[(u16, u16)] {
&self.last_positions
}
pub fn request_clear(&mut self) {
self.clear.request();
}
pub fn consume_clear(&mut self) -> bool {
self.clear.consume()
}
pub fn positions_moved(&self, positions: &[(u16, u16)]) -> bool {
self.last_positions.iter().any(|p| !positions.contains(p))
}
pub fn store_positions(&mut self, positions: Vec<(u16, u16)>) {
self.last_positions = positions;
}
pub fn forget_positions(&mut self) {
self.last_positions.clear();
}
}
#[derive(Default)]
pub struct OverlayManager {
pub shortcuts_clear: ClearLatch,
pub file_finder_clear: ClearLatch,
pub command_palette_clear: ClearLatch,
pub process_picker_clear: ClearLatch,
pub zoxide_jump_clear: ClearLatch,
pub branch_picker_clear: ClearLatch,
pub input_prompt_clear: ClearLatch,
pub list_picker_clear: ClearLatch,
pub editor: [ImageOverlay<super::EditorImageLayout>; 2],
pub welcome: ImageOverlay<super::WelcomeLayout>,
pub terminal_image: ImageOverlay<super::TerminalImageLayout>,
pub markdown_image: ImageOverlay<super::MarkdownImageLayout>,
pub minimap: ImageOverlay<super::MinimapLayout>,
pub hero: ImageOverlay<super::WelcomeLayout>,
pub ssh: ImageOverlay<()>,
pub run_debug: CellOverlay,
pub problems_badge: CellOverlay,
pub activity: ActivityOverlay,
}
#[cfg(test)]
mod tests {
use super::{ActivityOverlay, ImageOverlay};
#[test]
fn the_icon_keepalive_waits_for_the_cells_around_the_icons_to_change() {
let mut a = ActivityOverlay::default();
assert!(a.neighbourhood_changed(0, &[1, 1]), "never sent");
a.set_neighbourhood(vec![1, 1]);
assert!(
!a.neighbourhood_changed(0, &[1, 1]),
"idle: nothing to outlast"
);
assert!(
!a.neighbourhood_changed(0, &[1, 2]),
"only the other icon's ring changed"
);
assert!(
a.neighbourhood_changed(1, &[1, 2]),
"this icon's ring was repainted"
);
}
#[test]
fn an_image_is_sent_again_only_when_it_could_be_missing() {
let mut o: ImageOverlay<u8> = ImageOverlay::default();
o.set(String::from("img"), 1);
assert!(o.needs_emit(7), "never shown");
o.mark_sent_over(7);
assert!(!o.needs_emit(7), "a keystroke elsewhere sends nothing");
assert!(o.needs_emit(8), "the cells beneath it were written over");
o.mark_sent_over(8);
o.set_layout(2);
assert!(o.needs_emit(8), "moved");
o.mark_sent_over(8);
o.forget_sent();
assert!(o.needs_emit(8), "the screen was wiped");
o.mark_sent_over(8);
o.request_clear();
assert!(o.consume_clear());
assert!(o.needs_emit(8), "cleared images are shown again");
}
}