use std::sync::atomic::Ordering;
use std::time::{Duration, Instant};
use ratatui::layout::{Position, Rect};
use tokio::sync::mpsc::UnboundedSender;
use super::super::types::{
ActivePlanEntry, ActivityState, InlineEvent, InlineListSelection, ListOverlayRequest, LocalAgentEntry,
LocalAgentKind, ModalOverlayRequest, OverlayRequest, WizardOverlayRequest,
};
use super::mouse_selection::MouseSelectionState;
use super::reflow::is_info_box_line;
use super::status_requires_shimmer;
use super::{
ActiveOverlay, InlinePromptSuggestionState, MouseDragTarget, Session, SuggestedPromptState, TranscriptFlash,
action::Action,
modal::{ModalListState, ModalSearchState, ModalState, WizardModalState},
};
use crate::tui::config::constants::ui;
use crate::tui::options::FullscreenInteractionSettings;
const COPY_NOTIFICATION_DURATION: Duration = Duration::from_secs(2);
const TRANSCRIPT_FLASH_DURATION: Duration = Duration::from_millis(1500);
const COPY_FAILURE_NOTIFICATION_TEXT: &str = "Copy failed";
const ACTION_REQUIRED_STATUS_TEXT: &str = "Action required";
const APPROVAL_REQUIRED_STATUS_TEXT: &str = "Approval required";
const INPUT_REQUIRED_STATUS_TEXT: &str = "Input required";
const ACTIVE_PTY_STATUS_TEXT: &str = "Running PTY command...";
impl Session {
pub(crate) fn mark_task_panel_content_dirty(&mut self) {
self.mark_dirty();
}
pub(crate) fn set_task_panel_progress_label(&mut self, label: Option<String>) {
self.terminal_title_task_progress = label;
self.mark_dirty();
}
pub(crate) fn clear_inline_prompt_suggestion(&mut self) {
if self.inline_prompt_suggestion.suggestion.is_none() {
return;
}
self.inline_prompt_suggestion = InlinePromptSuggestionState::default();
self.mark_dirty();
}
pub(crate) fn copy_input_selection_to_clipboard(&mut self) -> bool {
let Some(selected) = self.input_manager.selected_text() else {
return false;
};
let char_count = selected.chars().count();
if self.input_manager.copy_selected_text_to_clipboard() {
self.show_copy_notification(char_count);
} else {
self.show_copy_failure_notification();
}
true
}
pub(crate) fn copy_text_to_clipboard(&mut self, text: &str) {
if text.is_empty() {
return;
}
let char_count = text.chars().count();
if MouseSelectionState::copy_to_clipboard(text) {
self.show_copy_notification(char_count);
} else {
self.show_copy_failure_notification();
}
}
pub(crate) fn clear_mouse_selection(&mut self) -> bool {
let had_selection = self.mouse_selection.has_selection || self.mouse_selection.is_selecting;
if !had_selection {
return false;
}
self.mouse_selection.clear();
self.mouse_drag_target = MouseDragTarget::None;
self.cancel_drag_auto_scroll();
self.mark_dirty();
true
}
pub(crate) fn clear_suggested_prompt_state(&mut self) {
if !self.suggested_prompt_state.active {
return;
}
self.suggested_prompt_state = SuggestedPromptState::default();
self.mark_dirty();
}
pub(crate) fn next_revision(&mut self) -> u64 {
self.line_revision_counter = self.line_revision_counter.wrapping_add(1);
self.line_revision_counter
}
pub(crate) fn should_exit(&self) -> bool {
self.should_exit
}
pub(crate) fn request_exit(&mut self) {
self.should_exit = true;
}
pub(crate) fn take_redraw(&mut self) -> bool {
self.render_state.take_redraw()
}
pub(crate) fn mark_visual_dirty(&mut self) {
self.render_state.request_redraw();
}
pub(crate) fn request_full_clear(&mut self) {
self.render_state.request_full_clear();
}
pub(crate) fn mark_dirty(&mut self) {
self.render_state.request_redraw();
self.header_lines_cache = None;
self.header_block_title_cache = None;
self.header_height_cache.clear();
self.input_render_cache = None;
self.queued_inputs_preview_cache = None;
self.subprocess_entries_preview_cache = None;
}
pub(crate) fn mark_transcript_line_dirty(&mut self, index: usize) {
let index = self.reflow_dirty_index(index);
self.first_dirty_line = match self.first_dirty_line {
Some(current) => Some(current.min(index)),
None => Some(index),
};
self.record_transcript_change(index);
self.render_state.request_redraw();
self.invalidate_transcript_viewport();
}
pub(crate) fn invalidate_header_cache(&mut self) {
self.header_lines_cache = None;
self.header_block_title_cache = None;
self.header_height_cache.clear();
self.input_render_cache = None;
self.render_state.request_redraw();
}
pub(crate) fn invalidate_sidebar_cache(&mut self) {
self.queued_inputs_preview_cache = None;
self.subprocess_entries_preview_cache = None;
self.render_state.request_redraw();
}
pub(crate) fn set_local_agents(&mut self, entries: Vec<LocalAgentEntry>) {
if self.local_agents != entries {
self.program_status.children(&entries);
self.local_agents = entries;
self.invalidate_sidebar_cache();
}
}
pub(crate) fn set_active_plan(&mut self, entry: Option<ActivePlanEntry>) {
if self.active_plan != entry {
self.active_plan = entry;
self.invalidate_sidebar_cache();
}
}
pub(crate) fn has_local_agents(&self) -> bool {
self.local_agents
.iter()
.any(|entry| matches!(entry.kind, LocalAgentKind::Delegated | LocalAgentKind::ExecSession))
}
pub(crate) fn set_local_agents_drawer_visible(&mut self, visible: bool) {
if self.local_agents_drawer_visible != visible {
self.local_agents_drawer_visible = visible;
self.mark_dirty();
}
}
pub(crate) fn set_transcript_area(&mut self, area: Option<Rect>) {
if self.areas.transcript() != area {
self.sticky_prompt_target = None;
}
self.areas.set_transcript(area);
}
pub(crate) fn transcript_area(&self) -> Option<Rect> {
self.areas.transcript()
}
pub(crate) fn set_input_area(&mut self, area: Option<Rect>) {
self.areas.set_input(area);
}
pub(crate) fn input_area(&self) -> Option<Rect> {
self.areas.input()
}
pub(crate) fn set_bottom_panel_area(&mut self, area: Option<Rect>) {
self.areas.set_bottom_panel(area);
}
pub(crate) fn bottom_panel_area(&self) -> Option<Rect> {
self.areas.bottom_panel()
}
pub(crate) fn set_modal_list_area(&mut self, area: Option<Rect>) {
self.areas.set_modal_list(area);
}
pub(crate) fn modal_list_area(&self) -> Option<Rect> {
self.areas.modal_list()
}
pub(crate) fn set_modal_text_areas(&mut self, areas: Vec<Rect>) {
self.areas.set_modal_text(areas);
}
pub(crate) fn modal_text_areas(&self) -> &[Rect] {
self.areas.modal_text()
}
pub(crate) fn set_modal_link_targets(&mut self, targets: Vec<super::TranscriptFileLinkTarget>) {
self.modal_link_targets = targets;
}
#[cfg(test)]
pub(crate) fn modal_link_targets(&self) -> &[super::TranscriptFileLinkTarget] {
&self.modal_link_targets
}
pub(crate) fn input_owner(&self) -> super::input_manager::InputOwner {
use super::input_manager::InputOwner;
if self.has_active_overlay() {
InputOwner::Overlay
} else if self.input_enabled && !self.activity_state.is_busy() {
InputOwner::Composer
} else {
InputOwner::Runtime
}
}
pub(crate) fn input_enabled(&self) -> bool {
self.input_owner() == super::input_manager::InputOwner::Composer
}
pub(crate) fn set_input_enabled(&mut self, enabled: bool) {
if let Some(ActiveOverlay::Modal(state)) = self.active_overlay.as_mut() {
state.restore_input = enabled;
}
self.input_enabled = enabled && !self.has_active_overlay();
self.input_render_cache = None;
}
pub(crate) fn image_input_enabled(&self) -> bool {
self.image_input_enabled
}
pub(crate) fn input_compact_mode(&self) -> bool {
self.input_compact_mode
}
pub(crate) fn set_input_compact_mode(&mut self, enabled: bool) {
self.input_compact_mode = enabled;
self.input_render_cache = None;
}
pub(crate) fn set_cursor_visible(&mut self, visible: bool) {
self.cursor_visible = visible;
}
pub(crate) fn current_transcript_revision(&self) -> u64 {
self.line_revision_counter
}
pub(crate) fn invalidate_transcript_viewport(&mut self) {
self.visible_lines_cache = None;
self.sticky_prompt_target = None;
}
pub(crate) fn request_transcript_clear(&mut self) {
self.transcript_clear_required = true;
}
pub(crate) fn set_fullscreen_active(&mut self, active: bool) {
self.fullscreen.active = active;
}
pub(crate) fn set_fullscreen_interaction(&mut self, config: FullscreenInteractionSettings) {
self.fullscreen.interaction = config;
}
pub(crate) fn handle_tick(&mut self) {
let animate_progress = self.appearance.should_animate_progress_status();
self.step_drag_auto_scroll();
let mut animation_updated = self.progress.tick();
if animate_progress && self.thinking_spinner.is_active && self.thinking_spinner.update() {
animation_updated = true;
self.mark_thinking_run_starts_dirty();
}
let shimmer_active = animate_progress
&& (self.is_shimmer_active() || self.background_status_shimmer_active() || self.progress.is_animated());
if shimmer_active {
let status_text = self.animation_status_text().to_string();
self.shimmer_state.hold_on_text_change(&status_text);
if self.shimmer_state.update() {
animation_updated = true;
}
}
if let Some(until) = self.scroll_cursor_steady_until
&& Instant::now() >= until
{
self.scroll_cursor_steady_until = None;
self.render_state.request_redraw();
}
if let Some(until) = self.copy_notification_until
&& Instant::now() >= until
{
self.copy_notification_until = None;
self.copy_notification_chars = 0;
self.render_state.request_redraw();
}
if self
.transcript_flash
.as_ref()
.is_some_and(|flash| Instant::now() >= flash.expires_at)
{
self.transcript_flash = None;
self.render_state.request_redraw();
}
if self.last_shimmer_active && !shimmer_active {
self.render_state.request_redraw();
}
self.last_shimmer_active = shimmer_active;
if animation_updated {
self.render_state.request_redraw();
}
}
pub(crate) fn needs_animation_tick(&self) -> bool {
if !self.terminal_visible {
return false;
}
if self.progress.is_animated() && self.appearance.should_animate_progress_status() {
return true;
}
if self.drag_auto_scroll.is_some() {
return true;
}
if self.scroll_cursor_steady_until.is_some()
|| self.copy_notification_until.is_some()
|| self.transcript_flash.is_some()
{
return true;
}
if self.last_shimmer_active {
return true;
}
if !self.appearance.should_animate_progress_status() {
return false;
}
self.thinking_spinner.is_active || self.is_shimmer_active() || self.background_status_shimmer_active()
}
pub(crate) fn is_terminal_visible(&self) -> bool {
self.terminal_visible
}
#[cfg_attr(not(vendored_crossterm), allow(dead_code))]
pub(crate) fn apply_terminal_visibility_report(&mut self, visible: bool) {
if self.terminal_visible == visible {
return;
}
self.terminal_visible = visible;
if visible {
self.mark_visual_dirty();
}
}
pub(crate) fn show_copy_notification(&mut self, char_count: usize) {
self.copy_notification_chars = char_count;
self.show_copy_result_notification(false);
}
pub(crate) fn show_copy_failure_notification(&mut self) {
self.show_copy_result_notification(true);
}
fn show_copy_result_notification(&mut self, failed: bool) {
self.copy_notification_until = Some(Instant::now() + COPY_NOTIFICATION_DURATION);
self.copy_notification_failed = failed;
self.render_state.request_redraw();
}
pub(crate) fn format_copy_notification(char_count: usize) -> String {
let unit = if char_count == 1 { "char" } else { "chars" };
format!("copied {char_count} {unit} to clipboard")
}
pub(crate) fn copy_notification_text(&self) -> Option<String> {
self.copy_notification_until.filter(|until| Instant::now() < *until)?;
if self.copy_notification_failed {
Some(COPY_FAILURE_NOTIFICATION_TEXT.to_string())
} else {
Some(Self::format_copy_notification(self.copy_notification_chars))
}
}
fn overlay_attention_status_text(&self) -> Option<&'static str> {
if let Some(modal) = self.modal_state() {
let normalized_title = modal.title.trim().to_ascii_lowercase();
if normalized_title.contains("input required") {
Some(INPUT_REQUIRED_STATUS_TEXT)
} else if normalized_title.contains("approval") || normalized_title.contains("permission") {
Some(APPROVAL_REQUIRED_STATUS_TEXT)
} else {
Some(ACTION_REQUIRED_STATUS_TEXT)
}
} else if self.wizard_overlay().is_some() || self.has_active_overlay() {
Some(ACTION_REQUIRED_STATUS_TEXT)
} else {
None
}
}
pub(crate) fn status_left_text(&self) -> Option<&str> {
let active_pty_status = self.active_pty_status_text();
if self.activity_state.is_stage() {
if let Some(left) = self.input_status_left.as_deref() {
let trimmed = left.trim();
if !trimmed.is_empty() && status_requires_shimmer(trimmed) {
return Some(trimmed);
}
}
if let Some(status) = active_pty_status {
return Some(status);
}
return self.activity_state.status();
}
if self.activity_state.status().is_none()
&& active_pty_status.is_some()
&& !self.input_status_left.as_deref().is_some_and(status_requires_shimmer)
{
return active_pty_status;
}
self.activity_state
.status()
.or(self.input_status_left.as_deref())
.map(str::trim)
.filter(|value: &&str| !value.is_empty())
.or_else(|| self.overlay_attention_status_text())
.or(active_pty_status)
}
pub(crate) fn status_right_text(&self) -> Option<&str> {
self.input_status_right
.as_deref()
.map(str::trim)
.filter(|value| !value.is_empty())
}
fn animation_status_text(&self) -> &str {
if self.activity_state.is_stage() {
if let Some(left) = self.input_status_left.as_deref()
&& !left.trim().is_empty()
&& status_requires_shimmer(left)
{
return left;
}
self.active_pty_status_text().unwrap_or("")
} else {
self.status_left_text().unwrap_or("")
}
}
pub(crate) fn is_running_activity(&self) -> bool {
if self.activity_state.is_busy() {
return true;
}
if matches!(self.activity_state, ActivityState::Blocked) {
return false;
}
let status = self.animation_status_text();
self.appearance.should_animate_progress_status()
&& status != ACTIVE_PTY_STATUS_TEXT
&& status_requires_shimmer(status)
}
pub(crate) fn active_pty_session_count(&self) -> usize {
self.active_pty_sessions
.as_ref()
.map(|counter| counter.load(Ordering::Relaxed))
.unwrap_or(0)
}
pub(crate) fn has_active_foreground_pty(&self) -> bool {
self.active_pty_session_count() > 0
}
pub(crate) fn set_background_activity_count(&mut self, count: usize) {
if self.background_activity_count != count {
self.background_activity_count = count;
self.mark_dirty();
}
}
pub(crate) fn set_background_finished_count(&mut self, count: usize) {
if self.background_finished_count != count {
self.background_finished_count = count;
self.mark_dirty();
}
}
pub(crate) fn has_background_activity(&self) -> bool {
self.background_activity_count > 0
}
pub(crate) fn has_background_history(&self) -> bool {
self.background_finished_count > 0
}
pub(crate) fn set_background_indicator_hits(&mut self, hits: Vec<Rect>) {
if self.background_indicator_hits != hits {
self.background_indicator_hits = hits;
self.mark_dirty();
}
}
pub(crate) fn background_indicator_hits(&self) -> &[Rect] {
&self.background_indicator_hits
}
pub(crate) fn background_indicator_contains(&self, column: u16, row: u16) -> bool {
let pos = Position { x: column, y: row };
self.background_indicator_hits.iter().any(|area| area.contains(pos))
}
pub(crate) fn background_activity_status_text(&self) -> Option<String> {
if self.has_background_activity() {
return Some(format!(
"Running {} background task{}...",
self.background_activity_count,
if self.background_activity_count == 1 { "" } else { "s" }
));
}
self.has_background_history().then(|| {
format!(
"{} agent{} finished",
self.background_finished_count,
if self.background_finished_count == 1 { "" } else { "s" }
)
})
}
pub(crate) fn background_header_badge_text(&self) -> Option<String> {
if self.has_background_activity() {
return Some(format!("• {} bg", self.background_activity_count));
}
self.has_background_history()
.then(|| format!("✓ {} done", self.background_finished_count))
}
pub(crate) fn background_shortcut_label(&self) -> &str {
self.primary_binding_label(Action::BackgroundOperation).unwrap_or("Ctrl+B")
}
pub(crate) fn foreground_pty_background_hint(&self) -> Option<String> {
self.has_active_foreground_pty()
.then(|| format!("{} background", self.background_shortcut_label()))
}
fn active_pty_status_text(&self) -> Option<&'static str> {
self.has_active_foreground_pty().then_some(ACTIVE_PTY_STATUS_TEXT)
}
fn status_indicator_active(&self) -> bool {
self.activity_state.is_busy()
|| (!matches!(self.activity_state, ActivityState::Blocked)
&& status_requires_shimmer(self.animation_status_text()))
}
pub(crate) fn has_status_spinner(&self) -> bool {
self.appearance.should_animate_progress_status() && self.status_indicator_active()
}
pub(crate) fn is_shimmer_active(&self) -> bool {
self.has_status_spinner() || self.thinking_spinner.is_active
}
pub(crate) fn loading_activity_active(&self) -> bool {
vtcode_commons::ui_protocol::LoadingActivity {
blocked: matches!(self.activity_state, ActivityState::Blocked),
turn: self.status_indicator_active(),
thinking: self.thinking_spinner.is_active,
progress: self.progress.is_animated(),
background: self.background_status_shimmer_active(),
}
.is_active()
}
pub(crate) fn title_attention_required(&self) -> bool {
if matches!(self.activity_state, ActivityState::Blocked) {
return true;
}
if let Some(modal) = self.modal_state() {
if modal.attention_required() {
return true;
}
let normalized_title = modal.title.trim().to_ascii_lowercase();
if normalized_title.contains("approval")
|| normalized_title.contains("permission")
|| normalized_title.contains("input required")
{
return true;
}
}
self.wizard_overlay().is_some()
}
pub(crate) fn background_status_shimmer_active(&self) -> bool {
self.has_background_activity() && !self.progress.is_active()
}
pub(crate) fn use_steady_cursor(&self) -> bool {
if !self.appearance.should_animate_progress_status() {
self.scroll_cursor_steady_until.is_some()
} else {
self.is_shimmer_active() || self.scroll_cursor_steady_until.is_some()
}
}
pub(crate) fn mark_scrolling(&mut self) {
let steady_duration = Duration::from_millis(ui::TUI_SCROLL_CURSOR_STEADY_MS);
if steady_duration.is_zero() {
self.scroll_cursor_steady_until = None;
} else {
self.scroll_cursor_steady_until = Some(Instant::now() + steady_duration);
}
}
pub(crate) fn mark_line_dirty(&mut self, index: usize) {
self.sticky_prompt_target = None;
let index = self.reflow_dirty_index(index);
self.first_dirty_line = match self.first_dirty_line {
Some(current) => Some(current.min(index)),
None => Some(index),
};
self.record_transcript_change(index);
self.mark_dirty();
}
pub(crate) fn record_transcript_change(&mut self, line_idx: usize) {
self.last_change_line_idx = Some(line_idx);
self.clamp_tracked_change();
}
pub(crate) fn arm_transcript_flash(&mut self, line_idx: usize) {
self.transcript_flash = Some(TranscriptFlash {
line_idx,
expires_at: Instant::now() + TRANSCRIPT_FLASH_DURATION,
});
self.render_state.request_redraw();
}
pub(crate) fn shift_tracked_change_after_eviction(&mut self, remove_count: usize) {
if remove_count == 0 {
return;
}
if let Some(idx) = self.last_change_line_idx {
self.last_change_line_idx = idx.checked_sub(remove_count);
}
self.transcript_flash = None;
self.clamp_tracked_change();
}
fn clamp_tracked_change(&mut self) {
let len = self.lines.len();
self.last_change_line_idx = match self.last_change_line_idx {
Some(_) if len == 0 => None,
Some(idx) if idx >= len => Some(len - 1),
other => other,
};
if self.transcript_flash.as_ref().is_some_and(|flash| flash.line_idx >= len) {
self.transcript_flash = None;
}
}
fn reflow_dirty_index(&mut self, index: usize) -> usize {
let index = self.grouped_message_start(index);
if let Some(cache) = self.transcript_cache.as_mut() {
cache.invalidate_message(index);
}
index
}
fn grouped_message_start(&self, index: usize) -> usize {
let Some(line) = self.lines.get(index) else {
return index;
};
if !is_info_box_line(line) {
return index;
}
let kind = line.kind;
let mut start = index;
while start > 0 && self.lines[start - 1].kind == kind && is_info_box_line(&self.lines[start - 1]) {
start -= 1;
}
start
}
pub(crate) fn ensure_prompt_style_color(&mut self) {
if self.prompt_style.color.is_none() {
self.prompt_style.color = self.theme.primary.or(self.theme.foreground);
}
}
pub(crate) fn clear_screen(&mut self) {
self.lines.clear();
self.leading_user_prompt_truncated = false;
self.collapsed_pastes.clear();
self.thinking_runs.clear();
self.user_scrolled = false;
self.pending_new_messages = 0;
self.last_change_line_idx = None;
self.transcript_flash = None;
self.scroll_manager.set_offset(0);
self.invalidate_transcript_cache();
self.invalidate_scroll_metrics();
self.render_state.request_full_clear();
self.mark_dirty();
}
pub(crate) fn toggle_logs(&mut self) {
self.show_logs = !self.show_logs;
self.invalidate_scroll_metrics();
self.mark_dirty();
}
}