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
//! Post-render action dispatch for the render pipeline.
//!
//! `update_post_render_state` consumes the `PostRenderActions` collected
//! during `submit_gpu_frame` and dispatches them to the appropriate handlers
//! (tab bar, clipboard, search, AI inspector, tmux, shader install, etc.).
use super::{PostRenderActions, ShaderInstallPrompt};
use crate::app::window_state::WindowState;
use crate::close_confirmation_ui::CloseConfirmAction;
use crate::command_history_ui::CommandHistoryAction;
use crate::paste_special_ui::PasteSpecialAction;
use crate::profile_drawer_ui::ProfileDrawerAction;
use crate::quit_confirmation_ui::QuitConfirmAction;
use crate::remote_shell_install_ui::{RemoteShellInstallAction, RemoteShellInstallUI};
use crate::shader_install_ui::ShaderInstallResponse;
use crate::ssh_connect_ui::SshConnectAction;
use crate::tmux_session_picker_ui::SessionPickerAction;
impl WindowState {
/// Handle all actions collected during the render pass and finalize frame timing.
pub(super) fn update_post_render_state(&mut self, actions: PostRenderActions) {
let PostRenderActions {
clipboard,
command_history,
paste_special,
session_picker,
tab_action,
shader_install,
integrations,
search,
inspector,
profile_drawer,
close_confirm,
quit_confirm,
remote_install,
ssh_connect,
save_config,
} = actions;
// Persist config if any render-pass handler requested it (e.g., "Skip This Version").
if save_config && let Err(e) = self.save_config_debounced() {
log::error!("Failed to save config after render action: {}", e);
}
// Sync AI Inspector panel width after the render pass.
// This catches drag-resize changes that update self.overlay_ui.ai_inspector.width during show().
// Done here to avoid borrow conflicts with the renderer block above.
self.sync_ai_inspector_width();
// Handle tab bar actions collected during egui rendering.
// If egui didn't detect a tab click but a focus-click landed on a known
// tab (stored in pending_focus_tab_switch), apply the switch now as a
// fallback. This covers the case where egui's pointer state was stale
// when the window was unfocused and clicked_by() didn't fire.
let effective_tab_action = if tab_action != crate::tab_bar_ui::TabBarAction::None {
self.focus_state.pending_focus_tab_switch = None;
tab_action
} else if let Some(tab_id) = self.focus_state.pending_focus_tab_switch.take() {
crate::tab_bar_ui::TabBarAction::SwitchTo(tab_id)
} else {
tab_action
};
self.handle_tab_bar_action_after_render(effective_tab_action);
// Handle clipboard actions collected during egui rendering
self.handle_clipboard_history_action_after_render(clipboard);
// Handle command history actions collected during egui rendering
match command_history {
CommandHistoryAction::Insert(command) => {
self.paste_text(&command);
log::info!(
"Inserted command from history: {}",
&command[..command.len().min(60)]
);
}
CommandHistoryAction::None => {}
}
// Handle close confirmation dialog actions
match close_confirm {
CloseConfirmAction::Close { tab_id, pane_id } => {
// Route through the proper cleanup path so session-undo capture,
// tab-bar resize, alert sounds, and is_shutting_down are all handled.
self.tab_manager.switch_to(tab_id);
if let Some(pane_id) = pane_id {
// Focus the confirmed pane then close it via the normal path.
if let Some(tab) = self.tab_manager.active_tab_mut()
&& let Some(pm) = tab.pane_manager_mut()
{
pm.focus_pane(pane_id);
}
let was_last = self.close_focused_pane_immediately();
if was_last {
self.is_shutting_down = true;
}
log::info!("Force-closed pane {} in tab {}", pane_id, tab_id);
} else {
let was_last = self.close_current_tab_immediately();
if was_last {
self.is_shutting_down = true;
}
log::info!("Force-closed tab {}", tab_id);
}
}
CloseConfirmAction::Cancel => {
// User cancelled - do nothing, dialog already hidden
log::debug!("Close confirmation cancelled");
}
CloseConfirmAction::None => {}
}
// Handle quit confirmation dialog actions
match quit_confirm {
QuitConfirmAction::Quit => {
// User confirmed quit - proceed with shutdown
log::info!("Quit confirmed by user");
self.perform_shutdown();
}
QuitConfirmAction::Cancel => {
log::debug!("Quit confirmation cancelled");
}
QuitConfirmAction::None => {}
}
// Handle remote shell integration install action
match remote_install {
RemoteShellInstallAction::Install => {
// Send the install command via paste_text() which uses the same
// code path as Cmd+V paste — handles bracketed paste mode and
// correctly forwards through SSH sessions.
let command = RemoteShellInstallUI::install_command();
// paste_text appends \r internally via term.paste()
self.paste_text(&format!("{}\n", command));
self.request_redraw();
}
RemoteShellInstallAction::Cancel => {
self.request_redraw();
}
RemoteShellInstallAction::None => {}
}
// Handle SSH Quick Connect actions
match ssh_connect {
SshConnectAction::Connect {
host,
profile_override: _,
} => {
// Build SSH command and write it to the active terminal's PTY
let args = host.ssh_args();
let ssh_cmd = format!("ssh {}\n", args.join(" "));
if let Some(tab) = self.tab_manager.active_tab()
&& let Ok(term) = tab.terminal.try_write()
{
let _ = term.write_str(&ssh_cmd);
}
log::info!(
"SSH Quick Connect: connecting to {}",
host.connection_string()
);
self.request_redraw();
}
SshConnectAction::Cancel => {
self.request_redraw();
}
SshConnectAction::None => {}
}
// Handle paste special actions collected during egui rendering
match paste_special {
PasteSpecialAction::Paste(content) => {
self.paste_text(&content);
log::debug!("Pasted transformed text ({} chars)", content.len());
}
PasteSpecialAction::None => {}
}
// Handle search actions collected during egui rendering
match search {
crate::search::SearchAction::ScrollToMatch(offset) => {
self.set_scroll_target(offset);
self.focus_state.needs_redraw = true;
self.request_redraw();
}
crate::search::SearchAction::Close => {
self.focus_state.needs_redraw = true;
self.request_redraw();
}
crate::search::SearchAction::None => {}
}
// Handle AI Inspector actions collected during egui rendering
self.handle_inspector_action_after_render(inspector);
// Handle tmux session picker actions collected during egui rendering
// Uses gateway mode: writes tmux commands to existing PTY instead of spawning process
match session_picker {
SessionPickerAction::Attach(session_name) => {
crate::debug_info!(
"TMUX",
"Session picker: attaching to '{}' via gateway",
session_name
);
if let Err(e) = self.attach_tmux_gateway(&session_name) {
log::error!("Failed to attach to tmux session '{}': {}", session_name, e);
self.show_toast(format!("Failed to attach: {}", e));
} else {
crate::debug_info!("TMUX", "Gateway initiated for session '{}'", session_name);
self.show_toast(format!("Connecting to session '{}'...", session_name));
}
self.focus_state.needs_redraw = true;
}
SessionPickerAction::CreateNew(name) => {
crate::debug_info!(
"TMUX",
"Session picker: creating new session {:?} via gateway",
name
);
if let Err(e) = self.initiate_tmux_gateway(name.as_deref()) {
log::error!("Failed to create tmux session: {}", e);
crate::debug_error!("TMUX", "Failed to initiate gateway: {}", e);
self.show_toast(format!("Failed to create session: {}", e));
} else {
let msg = match name {
Some(ref n) => format!("Creating session '{}'...", n),
None => "Creating new tmux session...".to_string(),
};
crate::debug_info!("TMUX", "Gateway initiated: {}", msg);
self.show_toast(msg);
}
self.focus_state.needs_redraw = true;
}
SessionPickerAction::None => {}
}
// Check for shader installation completion from background thread
if let Some(ref rx) = self.overlay_ui.shader_install_receiver
&& let Ok(result) = rx.try_recv()
{
match result {
Ok(count) => {
log::info!("Successfully installed {} shaders", count);
self.overlay_ui
.shader_install_ui
.set_success(&format!("Installed {} shaders!", count));
// Update config to mark as installed
self.config.shader_install_prompt = ShaderInstallPrompt::Installed;
if let Err(e) = self.save_config_debounced() {
log::error!("Failed to save config after shader install: {}", e);
}
}
Err(e) => {
log::error!("Failed to install shaders: {}", e);
self.overlay_ui.shader_install_ui.set_error(&e);
}
}
self.overlay_ui.shader_install_receiver = None;
self.focus_state.needs_redraw = true;
}
// Handle shader install responses
match shader_install {
ShaderInstallResponse::Install => {
log::info!("User requested shader installation");
self.overlay_ui
.shader_install_ui
.set_installing("Downloading shaders...");
self.focus_state.needs_redraw = true;
// Spawn installation in background thread so UI can show progress
let (tx, rx) = std::sync::mpsc::channel();
self.overlay_ui.shader_install_receiver = Some(rx);
std::thread::spawn(move || {
let result = crate::shader_install_ui::install_shaders_headless();
let _ = tx.send(result);
});
// Request redraw so the spinner shows
self.request_redraw();
}
ShaderInstallResponse::Never => {
log::info!("User declined shader installation (never ask again)");
self.overlay_ui.shader_install_ui.hide();
// Update config to never ask again
self.config.shader_install_prompt = ShaderInstallPrompt::Never;
if let Err(e) = self.save_config_debounced() {
log::error!("Failed to save config after declining shaders: {}", e);
}
}
ShaderInstallResponse::Later => {
log::info!("User deferred shader installation");
self.overlay_ui.shader_install_ui.hide();
// Config remains "ask" - will prompt again on next startup
}
ShaderInstallResponse::None => {}
}
// Handle integrations welcome dialog responses
self.handle_integrations_response(&integrations);
// Handle profile drawer actions
match profile_drawer {
ProfileDrawerAction::OpenProfile(id) => {
self.open_profile(id);
}
ProfileDrawerAction::ManageProfiles => {
// Open settings window to Profiles tab instead of terminal-embedded modal
self.overlay_state.open_settings_window_requested = true;
self.overlay_state.open_settings_profiles_tab = true;
}
ProfileDrawerAction::None => {}
}
if let Some(start) = self.debug.render_start {
let total = start.elapsed();
if total.as_millis() > 10 {
log::debug!(
"TIMING: AbsoluteTotal={:.2}ms (from function start to end)",
total.as_secs_f64() * 1000.0
);
}
}
}
}