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
//! Every pop-up croft draws over the middle of the screen, in one list
//! (#2062, card 9).
//!
//! Code that must act differently while a pop-up is open used to keep its
//! own list of `is_some()` checks, and the lists drifted apart. Six of them
//! decided whether pictures are held back, and between them they missed 16
//! pop-ups: on iTerm2 and Sixel a picture was drawn over the agent's
//! approval dialog, every confirm, the file prompt, the connect dialog and
//! Settings (#2659). A new pop-up is now added here, once, and every
//! picture waits for it.
//!
//! Menus, tooltips, notices, hover cards, the find bars and the tour caption
//! open next to what they belong to, so they are not listed: a picture
//! yields to one of those only where it lands on the picture. Which pictures
//! that is, the frame tells: [`PictureCover`] notes the cells under every
//! picture just before the frame draws its pop-ups, and again after, and a
//! picture whose cells changed in between has something drawn over it
//! (#2663).
use super::App;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
/// A pop-up drawn over the middle of the screen.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Popup {
/// New File, New Folder, Rename and Save As.
Prompt,
LocalOpenConfirm,
DiscardConfirm,
RevertHunkConfirm,
DiscardAllConfirm,
StashDropConfirm,
UnsavedScmConfirm,
ReplaceAllConfirm,
BroadcastConfirm,
TerminalPasteConfirm,
RunBlockConfirm,
UnsavedConfirm,
LossySaveConfirm,
FileFinder,
CommandPalette,
GoToSymbol,
WorkspaceSymbols,
ProcessPicker,
ZoxideJump,
CommandHistory,
BranchPicker,
SettingsEditor,
InputPrompt,
ListPicker,
ShortcutsModal,
ConnectDialog,
/// An agent's edit proposal.
Approval,
/// The unsupported-terminal warning at startup.
TerminalWarning,
/// A long message, an error above all, shown whole.
MessageCard,
}
impl Popup {
/// Every pop-up, in the order the frame paints them. Nothing reads the
/// order yet.
pub(super) const ALL: [Popup; 29] = [
Popup::Prompt,
Popup::LocalOpenConfirm,
Popup::DiscardConfirm,
Popup::RevertHunkConfirm,
Popup::DiscardAllConfirm,
Popup::StashDropConfirm,
Popup::UnsavedScmConfirm,
Popup::ReplaceAllConfirm,
Popup::BroadcastConfirm,
Popup::TerminalPasteConfirm,
Popup::RunBlockConfirm,
Popup::UnsavedConfirm,
Popup::LossySaveConfirm,
Popup::FileFinder,
Popup::CommandPalette,
Popup::GoToSymbol,
Popup::WorkspaceSymbols,
Popup::ProcessPicker,
Popup::ZoxideJump,
Popup::CommandHistory,
Popup::BranchPicker,
Popup::SettingsEditor,
Popup::InputPrompt,
Popup::ListPicker,
Popup::ShortcutsModal,
Popup::ConnectDialog,
Popup::Approval,
Popup::TerminalWarning,
Popup::MessageCard,
];
/// Whether this pop-up is open: the state its painter draws from.
pub(super) fn is_open(self, app: &App) -> bool {
match self {
Popup::Prompt => app.prompt.is_some(),
Popup::LocalOpenConfirm => app.pending_local_open.is_some(),
Popup::DiscardConfirm => app.pending_discard.is_some(),
Popup::RevertHunkConfirm => app.pending_revert_hunk.is_some(),
Popup::DiscardAllConfirm => app.pending_discard_all,
Popup::StashDropConfirm => app.pending_stash_drop.is_some(),
Popup::UnsavedScmConfirm => app.pending_unsaved_scm.is_some(),
Popup::ReplaceAllConfirm => app.pending_replace_all.is_some(),
Popup::BroadcastConfirm => app.pending_broadcast_enable,
Popup::TerminalPasteConfirm => app.pending_terminal_paste.is_some(),
Popup::RunBlockConfirm => app.pending_run_block.is_some(),
Popup::UnsavedConfirm => app.pending_unsaved.is_some(),
Popup::LossySaveConfirm => app.pending_lossy_save.is_some(),
Popup::FileFinder => app.file_finder.is_some(),
Popup::CommandPalette => app.command_palette.is_some(),
Popup::GoToSymbol => app.go_to_symbol.is_some(),
Popup::WorkspaceSymbols => app.workspace_symbols.is_some(),
Popup::ProcessPicker => app.process_picker.is_some(),
Popup::ZoxideJump => app.zoxide_jump.is_some(),
Popup::CommandHistory => app.command_history_popup.is_some(),
Popup::BranchPicker => app.branch_picker.is_some(),
Popup::SettingsEditor => app.settings_editor.is_some(),
Popup::InputPrompt => app.input_prompt.is_some(),
Popup::ListPicker => app.list_picker.is_some(),
Popup::ShortcutsModal => app.shortcuts_modal.is_some(),
Popup::ConnectDialog => app.connect_dialog.is_some(),
Popup::Approval => app.approval_ui.is_some(),
Popup::TerminalWarning => app.pending_terminal_warning,
Popup::MessageCard => app.message_card.is_some(),
}
}
/// Whether the pop-up handles the mouse itself: a picker's rows and the
/// click outside that closes it, or a dialog's buttons. One that does
/// not takes every mouse event while it is open, so a click, the wheel
/// or the pointer never reaches what it covers (#862, #2674). The New
/// File box handles none: a click in the status bar or on a layout icon
/// acted behind it (#2678 review). The match has no catch-all: a new
/// pop-up must say which it is.
pub(super) fn handles_mouse(self) -> bool {
match self {
Popup::FileFinder
| Popup::CommandPalette
| Popup::GoToSymbol
| Popup::WorkspaceSymbols
| Popup::ProcessPicker
| Popup::ZoxideJump
| Popup::CommandHistory
| Popup::BranchPicker
| Popup::InputPrompt
| Popup::ListPicker
| Popup::ShortcutsModal
| Popup::ConnectDialog => true,
Popup::Prompt
| Popup::LocalOpenConfirm
| Popup::DiscardConfirm
| Popup::RevertHunkConfirm
| Popup::DiscardAllConfirm
| Popup::StashDropConfirm
| Popup::UnsavedScmConfirm
| Popup::ReplaceAllConfirm
| Popup::BroadcastConfirm
| Popup::TerminalPasteConfirm
| Popup::RunBlockConfirm
| Popup::UnsavedConfirm
| Popup::LossySaveConfirm
| Popup::SettingsEditor
| Popup::Approval
| Popup::TerminalWarning
| Popup::MessageCard => false,
}
}
}
impl App {
/// Whether every picture is held back this frame: a pop-up is open.
/// iTerm2 and Sixel have no layer for pictures, so a picture sent after
/// the frame would wipe the part of a pop-up it overlaps (#2659); Kitty
/// keeps pictures below the text and loses nothing by the wait.
pub(super) fn pictures_held_back(&self) -> bool {
self.popup_open()
}
/// Whether an open pop-up takes every mouse event, as one that handles
/// no mouse of its own does (#2674). Settings opens an input prompt over
/// itself to edit a value, and gives it the keys while it is up (see
/// `handle_key_inner`); it gives it the mouse too, so a click outside the
/// prompt closes it as anywhere else (#2678 review).
pub(super) fn popup_takes_the_mouse(&self) -> bool {
Popup::ALL.iter().any(|&p| {
!p.handles_mouse()
&& p.is_open(self)
&& !(p == Popup::SettingsEditor && self.input_prompt.is_some())
})
}
/// The pop-ups open now, in the list's order. An Esc that closed one
/// changes this, so Zen Mode's Esc Esc does not count it (#2679).
pub(super) fn open_popups(&self) -> Vec<Popup> {
Popup::ALL.into_iter().filter(|p| p.is_open(self)).collect()
}
/// Whether one of the pop-ups drawn over the middle of the screen is
/// open. It has the keys, so no pane's caret shows through it (#2669).
pub(super) fn popup_open(&self) -> bool {
Popup::ALL.iter().any(|p| p.is_open(self))
}
/// Whether the picture at `spot` waits this frame: a pop-up is open, or
/// something the frame drew after the panes landed on the picture.
pub(super) fn picture_waits(&self, spot: PictureSpot) -> bool {
self.pictures_held_back() || self.picture_cover.covered.contains(&spot)
}
/// Where each picture sits this frame, from the places the panes
/// recorded while they painted. A picture not on screen has no spot or
/// an empty one, which nothing can cover.
pub(super) fn picture_spots(&self) -> Vec<(PictureSpot, Rect)> {
let o = &self.overlays;
let mut spots = Vec::with_capacity(9);
for side in 0..2 {
if let Some(l) = o.editor[side].layout() {
let r = Rect::new(l.cell_x, l.cell_y, l.cell_w, l.cell_h);
spots.push((PictureSpot::Editor(side), r));
}
}
if let Some(l) = o.terminal_image.layout() {
let r = Rect::new(l.cell_x, l.cell_y, l.cell_w, l.cell_h);
spots.push((PictureSpot::Terminal, r));
}
if let Some(l) = o.markdown_image.layout() {
let r = Rect::new(l.cell_x, l.cell_y, l.cell_w, l.cell_h);
spots.push((PictureSpot::Markdown, r));
}
spots.push((PictureSpot::Minimap, self.minimap.img_rect));
if let Some(l) = o.welcome.layout() {
let r = Rect::new(l.cell_x, l.cell_y, l.cell_w, l.cell_h);
spots.push((PictureSpot::Welcome, r));
}
if let Some((x, y)) = self.run_debug.last_icon_cell {
use crate::widgets::run_debug::{RUN_DEBUG_ICON_CELLS_H, RUN_DEBUG_ICON_CELLS_W};
let r = Rect::new(x, y, RUN_DEBUG_ICON_CELLS_W, RUN_DEBUG_ICON_CELLS_H);
spots.push((PictureSpot::RunDebugIcon, r));
}
if let Some((x, y)) = self.problems_badge_cell {
let w = crate::iterm2_inline::count_badge_cells_w(self.problems_badge_count);
spots.push((PictureSpot::ProblemsBadge, Rect::new(x, y, w, 1)));
}
spots.push((
PictureSpot::SourceControlHero,
self.source_control.last_hero_area,
));
if let Some((x, y)) = self.remote.last_image_cell {
use crate::widgets::remote::{SSH_EMPTY_STATE_CELLS_H, SSH_EMPTY_STATE_CELLS_W};
let r = Rect::new(x, y, SSH_EMPTY_STATE_CELLS_W, SSH_EMPTY_STATE_CELLS_H);
spots.push((PictureSpot::SshIllustration, r));
}
spots
}
/// Called just before the frame draws its pop-ups.
pub(super) fn note_pictures_before_popups(&mut self, buf: &Buffer) {
let spots = self.picture_spots();
self.picture_cover.note_before(buf, spots);
}
/// Called once the frame has drawn its last pop-up.
pub(super) fn note_pictures_after_popups(&mut self, buf: &Buffer) {
self.picture_cover.note_after(buf);
}
}
/// A place croft sends a picture to.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum PictureSpot {
/// An image or PDF in the left (0) or right (1) editor group.
Editor(usize),
Terminal,
Markdown,
Minimap,
Welcome,
RunDebugIcon,
ProblemsBadge,
SourceControlHero,
SshIllustration,
}
/// What lay under each picture before the frame drew its pop-ups, and which
/// pictures something was then drawn over (#2663).
#[derive(Default)]
pub(super) struct PictureCover {
before: Vec<(PictureSpot, Rect, u64)>,
covered: Vec<PictureSpot>,
/// Spots covered on the last image-clear check (#2419), preserved across
/// `start_frame` so covering or uncovering a picture with a non-modal
/// overlay triggers one Kitty eviction / re-emit cycle without flickering
/// on steady-state frames.
last_covered: Vec<PictureSpot>,
}
impl PictureCover {
/// Forget the last frame's answer; a frame that never reaches its
/// pop-ups covers nothing.
pub(super) fn start_frame(&mut self) {
self.before.clear();
self.covered.clear();
}
/// Whether the set of covered picture spots changed since the last check
/// (#2419). While a modal popup already holds every picture back
/// (`suppressed`), treat the non-modal cover set as empty so opening a
/// modal does not trigger a redundant post-draw redraw after its pre-draw
/// eviction.
pub(super) fn take_cover_changed(&mut self, suppressed: bool) -> bool {
let effective: &[PictureSpot] = if suppressed { &[] } else { &self.covered };
if self.last_covered.as_slice() == effective {
return false;
}
self.last_covered.clear();
self.last_covered.extend_from_slice(effective);
true
}
fn note_before(&mut self, buf: &Buffer, spots: Vec<(PictureSpot, Rect)>) {
self.before.clear();
self.covered.clear();
for (spot, r) in spots {
if r.width > 0 && r.height > 0 {
let print =
super::inline_images::cells_fingerprint(buf, r.x, r.y, r.width, r.height);
self.before.push((spot, r, print));
}
}
}
fn note_after(&mut self, buf: &Buffer) {
self.covered = self
.before
.iter()
.filter(|(_, r, print)| {
super::inline_images::cells_fingerprint(buf, r.x, r.y, r.width, r.height) != *print
})
.map(|(spot, _, _)| *spot)
.collect();
}
}