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
//! Helper methods for applying a parsed tmux layout to an existing tab.
//!
//! These four methods are called exclusively from
//! `layout::apply_layout_to_existing_tab` and handle each of the four
//! reconciliation cases:
//!
//! 1. `handle_same_pane_layout_update` — same pane set, geometry only changed
//! 2. `handle_pane_removal` — some panes were closed
//! 3. `handle_pane_addition` — new panes split in
//! 4. `handle_full_layout_recreation` — complete replacement or complex delta
use crate::app::window_state::WindowState;
use crate::tmux::TmuxWindowId;
use super::layout::BoundsInfo;
impl WindowState {
/// Case 1: Same tmux pane IDs — only layout geometry changed.
pub(super) fn handle_same_pane_layout_update(
&mut self,
tab_id: crate::tab::TabId,
parsed_layout: &crate::tmux::TmuxLayout,
bounds_info: BoundsInfo,
) {
crate::debug_info!(
"TMUX",
"Layout change with same panes - preserving terminals, updating layout"
);
let Some(tab) = self.tab_manager.get_tab_mut(tab_id) else {
return;
};
if let Some(pm) = tab.pane_manager_mut() {
let mappings = self.tmux_state.tab_mappings(tab_id);
pm.update_layout_from_tmux(parsed_layout, &mappings);
pm.recalculate_bounds();
if let Some((_, _, _, _, cell_width, cell_height, _)) = bounds_info {
pm.resize_all_terminals(cell_width, cell_height);
}
// A same-panes layout change can be another client's resize of
// the very panes we mirror: adopt the daemon-side leaf geometry
// after the local-metrics recompute so it wins.
pm.resize_pane_terminals_from_layout(parsed_layout, &mappings);
}
self.focus_state.needs_redraw = true;
}
/// Case 2: Some panes closed — remove them from native tree, update layout.
pub(super) fn handle_pane_removal(
&mut self,
tab_id: crate::tab::TabId,
parsed_layout: &crate::tmux::TmuxLayout,
panes_to_keep: &std::collections::HashSet<crate::tmux::TmuxPaneId>,
panes_to_remove: &[crate::tmux::TmuxPaneId],
bounds_info: BoundsInfo,
) {
crate::debug_info!(
"TMUX",
"Layout change: keeping {:?}, removing {:?}",
panes_to_keep,
panes_to_remove
);
let current_focused = self
.tmux_state
.tmux_session
.as_ref()
.and_then(|s| s.focused_pane());
let focused_pane_removed = current_focused
.map(|fp| panes_to_remove.contains(&fp))
.unwrap_or(false);
let Some(tab) = self.tab_manager.get_tab_mut(tab_id) else {
return;
};
if let Some(pm) = tab.pane_manager_mut() {
for tmux_pane_id in panes_to_remove {
if let Some((_, native_pane_id)) = self.tmux_state.tmux_pane_owner(*tmux_pane_id) {
crate::debug_info!(
"TMUX",
"Removing native pane {} for closed tmux pane %{}",
native_pane_id,
tmux_pane_id
);
pm.close_pane(native_pane_id);
}
}
let kept_mappings: std::collections::HashMap<_, _> = self
.tmux_state
.tab_mappings(tab_id)
.iter()
.filter(|(tmux_id, _)| panes_to_keep.contains(tmux_id))
.map(|(k, v)| (*k, *v))
.collect();
pm.update_layout_from_tmux(parsed_layout, &kept_mappings);
pm.recalculate_bounds();
if let Some((_, _, _, _, cell_width, cell_height, _)) = bounds_info {
pm.resize_all_terminals(cell_width, cell_height);
}
// Survivors adopt the daemon-side geometry, same as the
// same-panes case: the closing pane's resize and ours both
// land in this layout string.
pm.resize_pane_terminals_from_layout(parsed_layout, &kept_mappings);
}
// Update mappings - remove closed panes
for tmux_pane_id in panes_to_remove {
self.tmux_state.remove_tmux_pane_mapping(*tmux_pane_id);
}
// The daemon sends no agent-roster removal push, so this
// reconciliation is the close signal for the cache too — without
// it, a closed pane's entry lingers until the next attach fill.
#[cfg(feature = "mux")]
self.tmux_state.agent_roster.remove_panes(panes_to_remove);
if focused_pane_removed && let Some(new_focus) = panes_to_keep.iter().next().copied() {
crate::debug_info!(
"TMUX",
"Focused pane was removed, updating tmux session focus to %{}",
new_focus
);
if let Some(session) = &mut self.tmux_state.tmux_session {
session.set_focused_pane(Some(new_focus));
}
}
crate::debug_info!(
"TMUX",
"After pane removal, mappings: {:?}",
self.tmux_state.tmux_pane_owners
);
self.focus_state.needs_redraw = true;
self.request_redraw();
}
/// Case 3: New panes added (split) while existing panes remain.
pub(super) fn handle_pane_addition(
&mut self,
tab_id: crate::tab::TabId,
parsed_layout: &crate::tmux::TmuxLayout,
panes_to_keep: &std::collections::HashSet<crate::tmux::TmuxPaneId>,
panes_to_add: &[crate::tmux::TmuxPaneId],
bounds_info: BoundsInfo,
) {
crate::debug_info!(
"TMUX",
"Layout change: keeping {:?}, adding {:?}",
panes_to_keep,
panes_to_add
);
let Some(tab) = self.tab_manager.get_tab_mut(tab_id) else {
return;
};
if let Some(pm) = tab.pane_manager_mut() {
let existing_mappings: std::collections::HashMap<_, _> = panes_to_keep
.iter()
.filter_map(|tmux_id| {
self.tmux_state
.tmux_pane_owner(*tmux_id)
.map(|(_, native_id)| (*tmux_id, native_id))
})
.collect();
match pm.rebuild_from_tmux_layout(
parsed_layout,
&existing_mappings,
panes_to_add,
&self.config.load(),
std::sync::Arc::clone(&self.runtime),
) {
Ok(new_mappings) => {
self.tmux_state.set_tab_pane_mappings(tab_id, &new_mappings);
crate::debug_info!(
"TMUX",
"Rebuilt layout with {} panes: {:?}",
new_mappings.len(),
new_mappings
);
if let Some((_, _, _, _, cell_width, cell_height, _)) = bounds_info {
pm.resize_all_terminals(cell_width, cell_height);
}
}
Err(e) => {
crate::debug_error!("TMUX", "Failed to rebuild layout: {}", e);
}
}
}
// Panes born from a layout change must carry the session-logging
// attachment when logging is active (mirrors fire their output
// callback from `process_mux_output`).
if let Some(tab) = self.tab_manager.get_tab_mut(tab_id) {
tab.attach_session_logging_to_panes();
}
self.request_pane_refresh(panes_to_add);
crate::debug_info!(
"TMUX",
"After pane addition, mappings: {:?}",
self.tmux_state.tmux_pane_owners
);
self.focus_state.needs_redraw = true;
self.request_redraw();
}
/// Case 4: Full layout recreation — complete replacement or complex delta.
pub(super) fn handle_full_layout_recreation(
&mut self,
tab_id: crate::tab::TabId,
window_id: TmuxWindowId,
parsed_layout: &crate::tmux::TmuxLayout,
pane_ids: &[crate::tmux::TmuxPaneId],
bounds_info: BoundsInfo,
) {
let existing_tmux_ids: std::collections::HashSet<_> =
self.tmux_state.tab_tmux_pane_ids(tab_id);
let new_tmux_ids: std::collections::HashSet<crate::tmux::TmuxPaneId> =
pane_ids.iter().copied().collect();
crate::debug_info!(
"TMUX",
"Full layout recreation: existing={:?}, new={:?}",
existing_tmux_ids,
new_tmux_ids
);
let Some(tab) = self.tab_manager.get_tab_mut(tab_id) else {
return;
};
if let Some(pm) = tab.pane_manager_mut() {
match pm.set_from_tmux_layout(
parsed_layout,
&self.config.load(),
std::sync::Arc::clone(&self.runtime),
) {
Ok(pane_mappings) => {
crate::debug_info!("TMUX", "Storing pane mappings: {:?}", pane_mappings);
self.tmux_state
.set_tab_pane_mappings(tab_id, &pane_mappings);
crate::debug_info!(
"TMUX",
"Applied tmux layout to tab {}: {} pane mappings created",
tab_id,
pane_mappings.len()
);
// Resize pane terminals to their actual display sizes so that
// when tmux sends %output the content fills each pane correctly.
// NOTE: must happen before tab.tmux is accessed so that pm's
// mutable borrow of tab ends here (NLL: last use of pm).
if let Some((_, _, _, _, cell_width, cell_height, _)) = bounds_info {
pm.resize_all_terminals(cell_width, cell_height);
}
if !pane_ids.is_empty() && tab.tmux.tmux_pane_id.is_none() {
tab.tmux.tmux_pane_id = Some(pane_ids[0]);
}
tab.attach_session_logging_to_panes();
self.request_pane_refresh(pane_ids);
self.focus_state.needs_redraw = true;
}
Err(e) => {
crate::debug_error!(
"TMUX",
"Failed to apply tmux layout to tab {}: {}",
tab_id,
e
);
if !pane_ids.is_empty() && tab.tmux.tmux_pane_id.is_none() {
tab.tmux.tmux_pane_id = Some(pane_ids[0]);
}
}
}
} else {
// No pane manager - use legacy routing
if !pane_ids.is_empty() && tab.tmux.tmux_pane_id.is_none() {
tab.tmux.tmux_pane_id = Some(pane_ids[0]);
crate::debug_info!(
"TMUX",
"Set tab {} tmux_pane_id to %{} for output routing (no pane manager)",
tab_id,
pane_ids[0]
);
}
}
let _ = window_id; // used only in debug messages above
}
}