use super::App;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Popup {
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,
Approval,
TerminalWarning,
}
impl Popup {
pub(super) const ALL: [Popup; 28] = [
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,
];
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,
}
}
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 => false,
}
}
}
impl App {
pub(super) fn pictures_held_back(&self) -> bool {
self.popup_open()
}
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())
})
}
pub(super) fn open_popups(&self) -> Vec<Popup> {
Popup::ALL.into_iter().filter(|p| p.is_open(self)).collect()
}
pub(super) fn popup_open(&self) -> bool {
Popup::ALL.iter().any(|p| p.is_open(self))
}
pub(super) fn picture_waits(&self, spot: PictureSpot) -> bool {
self.pictures_held_back() || self.picture_cover.covered.contains(&spot)
}
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
}
pub(super) fn note_pictures_before_popups(&mut self, buf: &Buffer) {
let spots = self.picture_spots();
self.picture_cover.note_before(buf, spots);
}
pub(super) fn note_pictures_after_popups(&mut self, buf: &Buffer) {
self.picture_cover.note_after(buf);
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum PictureSpot {
Editor(usize),
Terminal,
Markdown,
Minimap,
Welcome,
RunDebugIcon,
ProblemsBadge,
SourceControlHero,
SshIllustration,
}
#[derive(Default)]
pub(super) struct PictureCover {
before: Vec<(PictureSpot, Rect, u64)>,
covered: Vec<PictureSpot>,
}
impl PictureCover {
pub(super) fn start_frame(&mut self) {
self.before.clear();
self.covered.clear();
}
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();
}
}