1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#[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,
/// Fingerprint of the text cells under the image when it was last sent
/// (see [`Self::needs_emit`]).
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;
}
/// Whether the post-frame blit must send the image again, given a
/// fingerprint of the text cells beneath it this frame. A picture is
/// megabytes of escape sequence, so it goes out only when it could be
/// missing from the screen: never shown, replaced, moved, wiped, or
/// written over since it was sent (the cells beneath it changed). Every
/// other frame, a keystroke or a cursor blink, sends nothing, which is
/// what keeps an image cheap over SSH (#682).
pub fn needs_emit(&self, under: u64) -> bool {
!self.displayed || self.under != Some(under)
}
/// Record that the image was sent over cells with fingerprint `under`.
pub fn mark_sent_over(&mut self, under: u64) {
self.displayed = true;
self.under = Some(under);
}
/// Forget what was sent: the next frame sends the image again. For a
/// screen wipe, or a frame that held the image back (a modal over it).
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>,
/// Cell positions of the icons painted on the most recent emit. Compared
/// against the next frame's positions so the flush can detect a layout
/// shift (terminal resize, or any change that recenters the bar) and
/// evict iTerm2's stale OSC-1337 image layer before re-emitting.
last_positions: Vec<(u16, u16)>,
/// Fingerprint of the cells around each icon when it was last sent, in
/// `last_positions` order: iTerm2 only evicts an icon under traffic next
/// to it, so the keepalive re-sends only icons whose ring changed (#682).
neighbourhood: Vec<u64>,
/// What each icon slot last sent: its cell and a hash of its image.
/// A dirty flush re-sends only slots that differ (a badge count or a
/// hover swap), not the whole bar; empty means send everything (#682).
sent: Vec<((u16, u16), u64)>,
/// Whether the side bar was open on the last frame drawn, `None` before
/// the first: the selected view's icon is pilled only while it is
/// (#1845), so a flip by any route re-sends the icons.
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;
}
/// Whether the icons should be re-emitted this frame. Always true on a
/// dirty (view switch, resize). The 2-second keepalive re-emit only applies
/// when `allow_keepalive` is set: it exists to outlast iTerm2's image-cache
/// eviction under SGR traffic, but sixel cells live in the normal buffer and
/// are never overwritten in the untouched activity-bar column, so re-emitting
/// them just risks a repaint flicker — the sixel path passes `false`.
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());
}
/// Whether the cells around icon `i` changed since it was sent.
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;
}
/// The screen was wiped or the icons evicted: send them all next time.
pub fn forget_sent(&mut self) {
self.sent.clear();
}
/// Record whether the side bar is open this frame, marking the icons
/// dirty when it opened or closed since the last one. The first frame
/// only records: nothing has been sent yet to go stale.
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;
}
}
/// Whether the dirty flag (a full re-send) is up.
pub fn dirty(&self) -> bool {
self.dirty
}
/// Where the icons were last painted.
pub fn last_positions(&self) -> &[(u16, u16)] {
&self.last_positions
}
/// Arm a one-shot `terminal.clear()` (consumed by the main loop's clear
/// chain) so iTerm2 evicts the cached OSC-1337 icon bitmaps. Needed
/// whenever a reflow could leave a stale image layer that a plain SGR
/// re-emit would stack a fresh copy on top of (the doubled-icon ghost).
pub fn request_clear(&mut self) {
self.clear.request();
}
pub fn consume_clear(&mut self) -> bool {
self.clear.consume()
}
/// True when the icons were emitted before at a *different* set of cells.
/// A first emit (or one after `forget_positions`) reports `false` so the
/// caller paints rather than looping on an eviction it doesn't need.
///
/// Only an icon that vanished or shifted counts: its old image would be
/// left behind. One that newly appeared (a count badge showing up when a
/// file turns dirty) moves nothing, and treating it as a move wiped the
/// whole screen on iTerm2 on the first keystroke into a file (#682).
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 {
/// Set when a pop-up that pictures must make way for opens or closes:
/// F1, Quick Open, the palette and both symbol pickers, the process,
/// directory and branch pickers, the input prompt and the list picker.
/// The main loop only asks whether any of them fired, so one latch
/// serves them all.
pub popup_clear: ClearLatch,
/// Whether any listed modal pop-up (`App::popup_open`) was open on the last
/// image-clear check (#2419). Detecting transitions here guarantees every
/// modal in `Popup::ALL` evicts Kitty-layer graphics on open and re-arms
/// them on close without each opener remembering to touch `popup_clear`.
pub last_popup_open: bool,
/// Inline-image overlay state, one slot per editor split column
/// (`[0]` = left pane, `[1]` = right pane). When the editor is not
/// split only `[0]` is used and `[1]` stays disabled. Keyed by
/// physical side so the OSC-1337 coordinates stay stable across a
/// focus swap (a focus swap never moves a group's column).
pub editor: [ImageOverlay<super::EditorImageLayout>; 2],
pub welcome: ImageOverlay<super::WelcomeLayout>,
/// The active terminal pane's newest inline image (captured OSC 1337
/// imgcat output), anchored to its grid line.
pub terminal_image: ImageOverlay<super::TerminalImageLayout>,
/// The markdown preview's topmost visible local image (#176).
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};
/// #682: iTerm2's icon keepalive waits for traffic next to the icons.
#[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"
);
}
/// #682: a large image is sent once and then only when it could be
/// missing: replaced, moved, written over, wiped or held back.
#[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");
}
}