summaryrefslogtreecommitdiff
path: root/internal/tui/flamegraph/model.go
blob: 7fbed6cbc05cd8391e543b30942dda8d705ae1d8 (plain)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
package flamegraph

import (
	"cmp"
	"fmt"
	"image/color"
	"slices"
	"strings"
	"time"

	coreflamegraph "ior/internal/flamegraph"
	common "ior/internal/tui/common"

	"charm.land/bubbles/v2/key"
	"charm.land/bubbles/v2/textinput"
	tea "charm.land/bubbletea/v2"
)

// snapshotNode aliases the live trie's snapshot type so the TUI can consume
// trees directly via SnapshotTree() without paying for a JSON marshal+unmarshal
// round-trip. The JSON tags on SnapshotNode keep the legacy SnapshotJSON path
// working unchanged.
type snapshotNode = coreflamegraph.SnapshotNode

type animTickMsg struct{}

// flameViewCacheKey captures the View() inputs that determine the rendered
// output. When two consecutive calls produce the same key, the cached content
// string is reused instead of re-running RenderTerminalView.
type flameViewCacheKey struct {
	version       uint64
	selectedIdx   int
	width         int
	height        int
	framesLen     int
	matchCount    int
	visibleCount  int
	searchQuery   string
	statusMessage string
	zoomPath      string
	searchActive  bool
	showHelp      bool
	paused        bool
	isDark        bool
}

type flameViewCache struct {
	key     flameViewCacheKey
	content string
	valid   bool
}

// flameSnapshotReadyMsg carries the result of a background snapshot+layout
// job. It is emitted by RefreshFromLiveTrieCmd and consumed by Update so the
// Bubble Tea goroutine can swap in the new state without blocking on JSON or
// frame layout work.
type flameSnapshotReadyMsg struct {
	version      uint64
	layoutWidth  int
	layoutHeight int
	zoomPath     string
	snapshot     *snapshotNode
	zoomRoot     *snapshotNode
	targetFrames []tuiFrame
	ancestry     frameAncestry
	globalTotal  uint64
}

const animFrameDuration = 33 * time.Millisecond
const flameKeyDebugEnabled = false

// driveWindow defines how recently a key must have been pressed to count as
// "user is actively driving". While inside this window, the flamegraph defers
// snapshot refresh and skips animation so keystrokes land without waiting on
// JSON+layout work or a 1-second animation chain.
const driveWindow = 250 * time.Millisecond

// LiveTrieSource is the minimal trie contract needed by the flamegraph TUI model.
// SnapshotJSON is retained for tests and external callers; SnapshotTree is the
// fast path used by the model's background refresh.
type LiveTrieSource interface {
	Fields() []string
	CountField() string
	Reconfigure([]string) error
	SetCountField(string) error
	Reset()
	Version() uint64
	SnapshotJSON() ([]byte, uint64)
	SnapshotTree() (*snapshotNode, uint64)
}

type zoomState struct {
	path                string
	previousSelectedIdx int
	lineWidth           int
}

type flameKeyMap struct {
	MoveShallower key.Binding
	MoveDeeper    key.Binding
	PrevSibling   key.Binding
	NextSibling   key.Binding
	JumpTop       key.Binding
	JumpRoot      key.Binding
	ZoomIn        key.Binding
	ZoomUndo      key.Binding
	ZoomReset     key.Binding
}

func defaultFlameKeyMap() flameKeyMap {
	return flameKeyMap{
		MoveShallower: key.NewBinding(key.WithKeys("j", "down")),
		MoveDeeper:    key.NewBinding(key.WithKeys("k", "up")),
		PrevSibling:   key.NewBinding(key.WithKeys("h", "left")),
		NextSibling:   key.NewBinding(key.WithKeys("l", "right")),
		JumpTop:       key.NewBinding(key.WithKeys("pgup", "pageup")),
		JumpRoot:      key.NewBinding(key.WithKeys("pgdown", "pgdn", "pagedown")),
		ZoomIn:        key.NewBinding(key.WithKeys("enter")),
		ZoomUndo:      key.NewBinding(key.WithKeys("backspace", "u", "esc")),
		ZoomReset:     key.NewBinding(),
	}
}

// Model is the Bubble Tea model for the TUI flamegraph tab.
type Model struct {
	liveTrie    LiveTrieSource
	lastVersion uint64
	snapshot    *snapshotNode
	globalTotal uint64

	// refreshInFlight is true while a background snapshot+layout job is
	// running. It coalesces flameTickMsg dispatches so we never queue more
	// than one snapshot rebuild concurrently.
	refreshInFlight bool

	frames       []tuiFrame
	targetFrames []tuiFrame
	ancestry     frameAncestry
	width        int
	height       int

	selectedIdx   int
	zoomStack     []zoomState
	zoomRoot      *snapshotNode
	zoomPath      string
	zoomLineWidth int

	searchActive  bool
	searchInput   textinput.Model
	searchQuery   string
	matchIndices  map[int]bool
	filterVisible map[int]bool
	subtreeSet    map[int]bool
	showHelp      bool
	statusMessage string
	lastKeyDebug  string

	fieldPresets [][]string
	fieldIndex   int
	countField   string

	animation AnimationState
	animating bool
	paused    bool

	// lastKeyAt records when the user most recently pressed a key. While the
	// user is actively driving the view (lastKeyAt within driveWindow ago),
	// the background snapshot refresh is suppressed and snapshot-ready
	// messages snap directly to target frames without animating. This keeps
	// keystrokes feeling instant under heavy event load.
	lastKeyAt time.Time

	// viewCache memoizes the last rendered string keyed on the inputs that
	// produce it. Bubble Tea may call View() multiple times per state change;
	// caching avoids re-running RenderTerminalView when nothing visible has
	// moved. Lives behind a pointer so the value-receiver View() can update it.
	viewCache *flameViewCache
	// hasNavigableSnapshot flips once we have at least one selectable non-root frame.
	hasNavigableSnapshot bool
	isDark               bool
	keys                 flameKeyMap
}

// tuiFrame stores one terminal flamegraph frame cell.
type tuiFrame struct {
	Name    string
	Col     int
	Row     int
	Width   int
	Total   uint64
	Percent float64
	Fill    color.Color
	Depth   int
	Path    string
}

// NewModel constructs a flamegraph tab model with default state.
func NewModel(liveTrie LiveTrieSource) Model {
	searchInput := textinput.New()
	searchInput.Prompt = "/"
	searchInput.CharLimit = 0
	searchInput.SetWidth(32)
	searchInput.SetStyles(textinput.DefaultStyles(true))

	m := Model{
		liveTrie:      liveTrie,
		matchIndices:  make(map[int]bool),
		filterVisible: make(map[int]bool),
		subtreeSet:    make(map[int]bool),
		searchInput:   searchInput,
		viewCache:     &flameViewCache{},
		fieldPresets: [][]string{
			{"comm", "tracepoint", "path"},
			{"path", "tracepoint", "comm"},
			{"tracepoint", "comm", "path"},
			{"pid", "tracepoint", "path"},
			{"comm", "path", "tracepoint"},
		},
		isDark:     true,
		keys:       defaultFlameKeyMap(),
		animation:  NewAnimationState(30, 6.0, 1.0),
		countField: "count",
	}
	m.syncFieldPresetToTrie()
	m.syncCountFieldToTrie()
	return m
}

// Init starts the flamegraph model.
func (m Model) Init() tea.Cmd {
	return nil
}

// Update handles incoming messages.
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {
	case animTickMsg:
		if !m.animating {
			return m, nil
		}
		m.animating = m.animation.Tick(0)
		m.frames = m.animation.CurrentFrames()
		m.clampSelection()
		m.subtreeSet = subtreeSetUsingAncestry(m.frames, m.selectedIdx, m.ancestry, m.subtreeSet)
		return m, m.animationTickCmd()
	case flameSnapshotReadyMsg:
		return m.handleSnapshotReady(msg)
	case tea.WindowSizeMsg:
		m.width = msg.Width
		m.height = msg.Height
		m.rebuildFrames(true)
		return m, m.animationTickCmd()
	case tea.MouseClickMsg:
		_ = m.handleMouseClick(msg)
		return m, nil
	case tea.KeyPressMsg:
		// Stamp every keypress so RefreshFromLiveTrieCmd and the
		// snapshot-ready handler can detect that the user is actively driving
		// the view and defer / unanimate accordingly.
		m.lastKeyAt = time.Now()
		if m.searchActive {
			return m.handleSearchInput(msg)
		}
		return m.handleKeyNavigation(msg)
	}
	return m, nil
}

// handleSearchInput processes key events while search mode is active.
// Handles escape (cancel search), enter (apply query), and delegates
// all other keys to the text input component for in-place editing.
func (m Model) handleSearchInput(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
	handled := false
	switch msg.String() {
	case "esc":
		handled = true
		m.clearSearch()
		m.recordKeyDebug(msg, handled, false)
		return m, nil
	case "enter":
		handled = true
		m.applySearchQuery(m.searchInput.Value())
		m.searchActive = false
		m.searchInput.Blur()
		m.recordKeyDebug(msg, handled, false)
		return m, nil
	}
	var cmd tea.Cmd
	m.searchInput, cmd = m.searchInput.Update(msg)
	_ = cmd
	m.recordKeyDebug(msg, true, false)
	return m, nil
}

// handleKeyNavigation processes navigation key events when search is not active.
// Delegates mode-toggle and zoom actions to handleModeKey, movement actions to
// handleMovementKey, then updates the subtree highlight when selection changes.
func (m Model) handleKeyNavigation(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
	prev := m.selectedIdx
	handled := m.handleModeKey(msg)
	if !handled {
		handled = m.handleMovementKey(msg)
	}
	if m.selectedIdx != prev {
		m.subtreeSet = subtreeSetUsingAncestry(m.frames, m.selectedIdx, m.ancestry, m.subtreeSet)
	}
	m.recordKeyDebug(msg, handled, m.selectedIdx != prev)
	return m, nil
}

// handleModeKey dispatches search, pause, zoom, and view-toggle key actions.
// Returns true when a key was handled so handleKeyNavigation can skip movement.
func (m *Model) handleModeKey(msg tea.KeyPressMsg) bool {
	switch {
	case isSearchOpenKey(msg):
		m.openSearch()
	case isNextMatchKey(msg):
		m.jumpMatch(1)
	case isPrevMatchKey(msg):
		m.jumpMatch(-1)
	case isPauseKey(msg):
		m.togglePause()
	case isResetBaselineKey(msg):
		m.resetBaseline()
	case isCycleOrderKey(msg):
		m.cycleFieldOrder()
	case isCycleMetricKey(msg):
		m.toggleCountField()
	case isHelpToggleKey(msg):
		m.toggleHelp()
	case isZoomInKey(msg, m.keys):
		m.zoomIn()
	case isZoomUndoKey(msg, m.keys):
		m.zoomUndo()
	case isZoomResetKey(msg, m.keys):
		m.zoomReset()
	default:
		return false
	}
	return true
}

// handleMovementKey dispatches directional and jump key actions.
// Returns true when a key was handled, false otherwise.
func (m *Model) handleMovementKey(msg tea.KeyPressMsg) bool {
	switch {
	case isMoveShallowerKey(msg, m.keys):
		m.moveVerticalWithFallback(-1, 1, -1)
	case isMoveDeeperKey(msg, m.keys):
		m.moveVerticalWithFallback(1, -1, 1)
	case isPrevSiblingKey(msg, m.keys):
		m.moveSibling(-1)
	case isNextSiblingKey(msg, m.keys):
		m.moveSibling(1)
	case isJumpTopKey(msg, m.keys):
		m.jumpToTop()
	case isJumpRootKey(msg, m.keys):
		m.jumpToRoot()
	default:
		return false
	}
	return true
}

// handleSnapshotReady applies the result of a background snapshot+layout job.
// Discards the result if viewport or zoom changed while the job was in flight
// (the next tick will dispatch a fresh refresh), or if the user paused after a
// snapshot already exists. Always clears refreshInFlight so subsequent ticks
// can dispatch the next refresh.
func (m Model) handleSnapshotReady(msg flameSnapshotReadyMsg) (tea.Model, tea.Cmd) {
	m.refreshInFlight = false
	if msg.snapshot == nil {
		return m, nil
	}
	if msg.layoutWidth != m.width || msg.layoutHeight != m.height || msg.zoomPath != m.zoomPath {
		return m, nil
	}
	if m.paused && m.snapshot != nil {
		return m, nil
	}

	prevPath := ""
	if len(m.frames) > 0 && m.selectedIdx >= 0 && m.selectedIdx < len(m.frames) {
		prevPath = m.frames[m.selectedIdx].Path
	}

	m.snapshot = msg.snapshot
	m.globalTotal = msg.globalTotal
	m.zoomRoot = msg.zoomRoot
	m.lastVersion = msg.version
	// Snap directly to target frames while the user is actively pressing keys
	// — animation would just add latency on top of the work the user wants to
	// see. Animation resumes on the next refresh after the drive window
	// expires.
	animate := !m.userDriving()
	m.applyTargetFrames(msg.targetFrames, msg.ancestry, prevPath, animate)
	if !m.animating {
		return m, nil
	}
	return m, m.animationTickCmd()
}

// userDriving reports whether the user has pressed a key within the recent
// drive window. Used to skip snapshot refresh and animation while keystrokes
// are arriving.
func (m Model) userDriving() bool {
	if m.lastKeyAt.IsZero() {
		return false
	}
	return time.Since(m.lastKeyAt) < driveWindow
}

// ConsumesKey reports whether the flamegraph should handle a key press before
// dashboard- or app-level shortcuts.
func (m Model) ConsumesKey(msg tea.KeyPressMsg) bool {
	if m.searchActive {
		return true
	}
	switch {
	case isSearchOpenKey(msg),
		isNextMatchKey(msg),
		isPrevMatchKey(msg),
		isPauseKey(msg),
		isResetBaselineKey(msg),
		isCycleOrderKey(msg),
		isCycleMetricKey(msg),
		isHelpToggleKey(msg):
		return true
	case isZoomInKey(msg, m.keys),
		isZoomUndoKey(msg, m.keys),
		isZoomResetKey(msg, m.keys),
		isMoveShallowerKey(msg, m.keys),
		isMoveDeeperKey(msg, m.keys),
		isPrevSiblingKey(msg, m.keys),
		isNextSiblingKey(msg, m.keys),
		isJumpTopKey(msg, m.keys),
		isJumpRootKey(msg, m.keys):
		return true
	default:
		return false
	}
}

// View renders the flamegraph viewport. Caches the rendered string keyed on
// the inputs that affect output; skips the cache while animating (frames
// change every 33 ms anyway, so cache hits are impossible).
func (m Model) View() tea.View {
	if !m.animating && m.viewCache != nil {
		key := m.currentViewCacheKey()
		if m.viewCache.valid && m.viewCache.key == key {
			return tea.NewView(m.viewCache.content)
		}
		content := m.renderViewContent()
		m.viewCache.key = key
		m.viewCache.content = content
		m.viewCache.valid = true
		return tea.NewView(content)
	}
	return tea.NewView(m.renderViewContent())
}

// renderViewContent assembles the flamegraph string. Pure function over Model
// state — pulled out so View() can decide whether to memoize the result.
func (m Model) renderViewContent() string {
	extraLines := 1 // selection status line
	if m.showHelp {
		extraLines++
	}
	renderHeight := m.height - extraLines
	if renderHeight < 3 {
		renderHeight = 3
	}

	content := RenderTerminalView(m.frames, m.width, renderHeight, m.selectedIdx, m.subtreeSet, m.matchIndices, m.filterVisible, m.globalTotal, m.countFieldLabel(), m.isDark, m.searchActive, m.searchQuery)
	content = replaceHeaderLine(content, m.toolbarLine())
	if m.searchActive {
		content = replaceFooterLine(content, m.searchFooter())
	}
	if m.snapshot != nil && len(m.frames) == 0 {
		content = common.PanelStyle.Render(fmt.Sprintf("Flame: snapshot v%d has no visible frames", m.lastVersion))
	}
	content += "\n" + m.selectionStatusLine()
	if m.showHelp {
		content += "\n" + m.helpOverlay()
	}
	return content
}

// currentViewCacheKey snapshots every Model field that influences View()
// output. If any of these differ between successive View() invocations, the
// cache misses and the content is rebuilt.
func (m Model) currentViewCacheKey() flameViewCacheKey {
	return flameViewCacheKey{
		version:       m.lastVersion,
		selectedIdx:   m.selectedIdx,
		width:         m.width,
		height:        m.height,
		framesLen:     len(m.frames),
		matchCount:    len(m.matchIndices),
		visibleCount:  len(m.filterVisible),
		searchQuery:   m.searchQuery,
		statusMessage: m.statusMessage,
		zoomPath:      m.zoomPath,
		searchActive:  m.searchActive,
		showHelp:      m.showHelp,
		paused:        m.paused,
		isDark:        m.isDark,
	}
}

// SetLiveTrie updates the data source used by the flamegraph model.
func (m *Model) SetLiveTrie(liveTrie LiveTrieSource) {
	m.liveTrie = liveTrie
	m.syncFieldPresetToTrie()
	m.syncCountFieldToTrie()
	m.lastVersion = 0
	m.snapshot = nil
	m.globalTotal = 0
	m.selectedIdx = 0
	m.frames = nil
	m.targetFrames = nil
	m.zoomStack = nil
	m.zoomRoot = nil
	m.zoomPath = ""
	m.zoomLineWidth = 0
	m.subtreeSet = make(map[int]bool)
	m.filterVisible = make(map[int]bool)
	m.ancestry = frameAncestry{}
	m.animation = NewAnimationState(30, 6.0, 1.0)
	m.animating = false
	m.hasNavigableSnapshot = false
}

func (m *Model) syncFieldPresetToTrie() {
	if m.liveTrie == nil {
		m.fieldIndex = 0
		return
	}
	fields := m.liveTrie.Fields()
	if len(fields) == 0 {
		m.fieldIndex = 0
		return
	}
	for idx, preset := range m.fieldPresets {
		if slices.Equal(preset, fields) {
			m.fieldIndex = idx
			return
		}
	}
	custom := slices.Clone(fields)
	m.fieldPresets = append([][]string{custom}, m.fieldPresets...)
	m.fieldIndex = 0
}

func (m *Model) syncCountFieldToTrie() {
	if m.liveTrie == nil {
		m.countField = "count"
		return
	}
	field := strings.TrimSpace(m.liveTrie.CountField())
	if field == "" {
		field = "count"
	}
	m.countField = field
}

// RefreshFromLiveTrie loads a new snapshot synchronously and returns true when
// a new snapshot was applied. Retained as a simple facade for tests; the
// production TUI now uses RefreshFromLiveTrieCmd to do the heavy lifting on a
// background goroutine.
func (m *Model) RefreshFromLiveTrie() bool {
	if m.liveTrie == nil {
		return false
	}
	// Once a snapshot exists, paused mode must freeze it regardless of current
	// navigability so selection and percentages remain stable.
	if m.paused && m.snapshot != nil {
		return false
	}
	version := m.liveTrie.Version()
	if version == m.lastVersion && m.snapshot != nil {
		return false
	}

	tree, version := m.liveTrie.SnapshotTree()
	if tree == nil {
		return false
	}
	m.snapshot = tree
	m.globalTotal = snapshotTotal(m.snapshot)
	if m.zoomPath != "" {
		m.zoomRoot = findNodeByPath(m.snapshot, m.zoomPath)
	} else {
		m.zoomRoot = nil
	}
	m.rebuildFrames(true)
	m.lastVersion = version
	return true
}

// RefreshFromLiveTrieCmd returns a tea.Cmd that fetches a snapshot, lays out
// frames, and builds the ancestry index on a background goroutine, then
// dispatches a flameSnapshotReadyMsg back to the Bubble Tea Update loop.
//
// Returns nil if no refresh is needed: no live trie configured, paused with an
// existing snapshot, version unchanged, another refresh already in flight, or
// the user is actively pressing keys. Skipping while driving keeps keystrokes
// responsive — the next idle tick picks up the latest version.
//
// Coalescing via refreshInFlight ensures we never queue more than one
// background job at a time. Newer ticks just no-op until the in-flight result
// lands, and the version gate then catches the freshest state.
func (m *Model) RefreshFromLiveTrieCmd() tea.Cmd {
	if m.liveTrie == nil {
		return nil
	}
	if m.paused && m.snapshot != nil {
		return nil
	}
	if m.refreshInFlight {
		return nil
	}
	if m.userDriving() && m.snapshot != nil {
		return nil
	}
	version := m.liveTrie.Version()
	if version == m.lastVersion && m.snapshot != nil {
		return nil
	}
	m.refreshInFlight = true

	// Capture fields the goroutine needs. Avoids reading Model fields
	// concurrently with the Update goroutine.
	liveTrie := m.liveTrie
	width := m.width
	height := m.height
	zoomPath := m.zoomPath
	return func() tea.Msg {
		tree, ver := liveTrie.SnapshotTree()
		if tree == nil {
			return flameSnapshotReadyMsg{version: ver, layoutWidth: width, layoutHeight: height, zoomPath: zoomPath}
		}
		var zoomRoot *snapshotNode
		layoutRoot := tree
		rootPath := ""
		if zoomPath != "" {
			zoomRoot = findNodeByPath(tree, zoomPath)
			if zoomRoot != nil {
				layoutRoot = zoomRoot
				rootPath = zoomPath
			}
		}
		targetFrames := buildTerminalLayoutWithPath(layoutRoot, width, height, rootPath)
		if zoomPath != "" {
			targetFrames = applyZoomLineage(targetFrames, tree, zoomPath, width)
		}
		ancestry := buildFrameAncestry(targetFrames)
		return flameSnapshotReadyMsg{
			version:      ver,
			layoutWidth:  width,
			layoutHeight: height,
			zoomPath:     zoomPath,
			snapshot:     tree,
			zoomRoot:     zoomRoot,
			targetFrames: targetFrames,
			ancestry:     ancestry,
			globalTotal:  snapshotTotal(tree),
		}
	}
}

// LastVersion returns the latest snapshot version loaded into the model.
func (m Model) LastVersion() uint64 {
	return m.lastVersion
}

// HasSnapshot reports whether the flamegraph model has loaded at least one snapshot.
func (m Model) HasSnapshot() bool {
	return m.snapshot != nil
}

// AnimationCmd returns a frame animation tick command when animation is active.
func (m Model) AnimationCmd() tea.Cmd {
	return m.animationTickCmd()
}

// Paused reports whether live refresh is paused.
func (m Model) Paused() bool {
	return m.paused
}

// SetViewport updates model render dimensions.
func (m *Model) SetViewport(width, height int) {
	if m.width == width && m.height == height {
		return
	}
	m.width = width
	m.height = height
	m.rebuildFrames(true)
}

// SetDarkMode sets the active color theme mode.
func (m *Model) SetDarkMode(isDark bool) {
	m.isDark = isDark
	m.searchInput.SetStyles(textinput.DefaultStyles(isDark))
}

func (m *Model) rebuildFrames(animate bool) {
	prevPath := ""
	if len(m.frames) > 0 && m.selectedIdx >= 0 && m.selectedIdx < len(m.frames) {
		prevPath = m.frames[m.selectedIdx].Path
	}

	var root *snapshotNode
	rootPath := ""
	if m.zoomRoot != nil {
		root = m.zoomRoot
		rootPath = m.zoomPath
	} else {
		root = m.snapshot
	}
	targetFrames := buildTerminalLayoutWithPath(root, m.width, m.height, rootPath)
	if m.zoomPath != "" {
		targetFrames = m.withZoomLineage(targetFrames)
	}
	ancestry := buildFrameAncestry(targetFrames)
	m.applyTargetFrames(targetFrames, ancestry, prevPath, animate)
}

// applyTargetFrames installs a prebuilt frame layout and ancestry index,
// optionally animating from the previous frames. Shared between the
// synchronous rebuildFrames path and the async snapshot-ready handler so
// post-swap state stays consistent (animation kickoff, selection restore,
// filter recompute, subtree highlight).
func (m *Model) applyTargetFrames(targetFrames []tuiFrame, ancestry frameAncestry, prevPath string, animate bool) {
	m.targetFrames = targetFrames
	m.ancestry = ancestry
	m.animation.SetTargets(m.targetFrames)
	if animate && len(m.frames) > 0 && !m.animation.Settled() {
		m.animating = true
		m.frames = m.animation.CurrentFrames()
	} else {
		m.animating = false
		m.frames = append(m.frames[:0], m.targetFrames...)
	}
	if len(m.frames) > 1 {
		m.hasNavigableSnapshot = true
	}
	m.restoreSelectionByPath(prevPath)
	m.clampSelection()
	m.recomputeFilterState()
	m.ensureSelectionNavigable()
	m.ensureSelectionVisible()
	m.subtreeSet = subtreeSetUsingAncestry(m.frames, m.selectedIdx, m.ancestry, m.subtreeSet)
}

func (m *Model) restoreSelectionByPath(path string) {
	if path == "" || len(m.frames) == 0 {
		return
	}
	if idx := m.frameIndexByPath(path); idx >= 0 {
		m.selectedIdx = idx
		return
	}
	for idx, frame := range m.frames {
		if hasPathBoundaryPrefix(path, frame.Path) || hasPathBoundaryPrefix(frame.Path, path) {
			m.selectedIdx = idx
			return
		}
	}
}

func (m Model) frameIndexByPath(path string) int {
	for idx, frame := range m.frames {
		if frame.Path == path {
			return idx
		}
	}
	return -1
}

func (m *Model) zoomIn() {
	if len(m.frames) == 0 || m.snapshot == nil {
		m.statusMessage = "Zoom unavailable: no frame selected"
		return
	}
	m.clampSelection()
	selectedPath := m.frames[m.selectedIdx].Path
	if selectedPath == m.currentRootPath() {
		m.statusMessage = "Zoom unchanged: selected frame is current view root"
		return
	}
	prevRootPath := m.zoomPath
	if !m.setZoomPath(selectedPath) {
		m.statusMessage = "Zoom failed: selected node is unavailable"
		return
	}
	m.zoomStack = append(m.zoomStack, zoomState{path: prevRootPath})
	m.statusMessage = "Zoom: " + compactFramePath(selectedPath)
}

func (m *Model) zoomUndo() {
	if len(m.zoomStack) == 0 || m.snapshot == nil {
		m.statusMessage = "Zoom undo unavailable"
		return
	}
	lastIdx := len(m.zoomStack) - 1
	last := m.zoomStack[lastIdx]
	m.zoomStack = m.zoomStack[:lastIdx]
	if !m.setZoomPath(last.path) {
		m.statusMessage = "Zoom undo unavailable"
		return
	}
	if m.zoomPath == "" {
		m.statusMessage = "Zoom: root"
		return
	}
	m.statusMessage = "Zoom: " + compactFramePath(m.zoomPath)
}

func (m *Model) zoomReset() {
	if m.zoomRoot == nil && len(m.zoomStack) == 0 {
		m.statusMessage = "Zoom already at root"
		return
	}
	m.zoomRoot = nil
	m.zoomPath = ""
	m.zoomStack = nil
	m.zoomLineWidth = 0
	m.rebuildFrames(false)
	m.statusMessage = "Zoom reset to root"
}

func (m *Model) moveVertical(delta int) {
	if len(m.frames) == 0 {
		return
	}
	m.clampSelection()
	m.ensureSelectionNavigable()
	current := m.frames[m.selectedIdx]
	targetDepth := current.Depth + delta
	targets := m.framesAtDepth(targetDepth)
	if len(targets) == 0 {
		return
	}
	best := targets[0]
	bestDist := abs(m.frames[best].Col - current.Col)
	for _, idx := range targets[1:] {
		dist := abs(m.frames[idx].Col - current.Col)
		if dist < bestDist {
			best = idx
			bestDist = dist
		}
	}
	m.selectedIdx = best
}

func (m *Model) moveVerticalWithFallback(primaryDelta, fallbackDelta, traversalDelta int) {
	before := m.selectedIdx
	m.moveVertical(primaryDelta)
	if m.selectedIdx == before && fallbackDelta != 0 {
		m.moveVertical(fallbackDelta)
	}
	if m.selectedIdx == before && traversalDelta != 0 {
		m.moveTraversal(traversalDelta)
	}
}

func (m *Model) moveSibling(delta int) {
	if len(m.frames) == 0 {
		return
	}
	before := m.selectedIdx
	m.clampSelection()
	m.ensureSelectionNavigable()
	current := m.frames[m.selectedIdx]
	siblings := m.framesAtDepth(current.Depth)
	if len(siblings) <= 1 {
		m.moveTraversal(delta)
		return
	}
	pos := indexOf(siblings, m.selectedIdx)
	if pos < 0 {
		m.moveTraversal(delta)
		return
	}
	next := pos + delta
	if next < 0 {
		next = 0
	}
	if next >= len(siblings) {
		next = len(siblings) - 1
	}
	m.selectedIdx = siblings[next]
	if m.selectedIdx == before {
		m.moveTraversal(delta)
	}
}

func (m *Model) jumpToTop() {
	if len(m.frames) == 0 {
		return
	}
	m.clampSelection()
	m.ensureSelectionNavigable()

	include := m.navigableFrameSet()
	currentCol := m.frames[m.selectedIdx].Col
	bestIdx := -1
	bestDepth := -1
	bestDist := int(^uint(0) >> 1)

	for idx, frame := range m.frames {
		if include != nil && !include[idx] {
			continue
		}
		dist := abs(frame.Col - currentCol)
		if frame.Depth > bestDepth {
			bestDepth = frame.Depth
			bestIdx = idx
			bestDist = dist
			continue
		}
		if frame.Depth == bestDepth {
			if dist < bestDist || (dist == bestDist && frame.Col < m.frames[bestIdx].Col) {
				bestIdx = idx
				bestDist = dist
			}
		}
	}
	if bestIdx >= 0 {
		m.selectedIdx = bestIdx
	}
}

func (m *Model) jumpToRoot() {
	if len(m.frames) == 0 {
		return
	}
	m.clampSelection()
	m.ensureSelectionNavigable()

	rootPath := m.currentRootPath()
	if rootPath != "" {
		if idx := m.frameIndexByPath(rootPath); idx >= 0 {
			if !m.filterActive() || m.frameNavigable(idx) {
				m.selectedIdx = idx
				return
			}
		}
	}

	include := m.navigableFrameSet()
	currentCol := m.frames[m.selectedIdx].Col
	bestIdx := -1
	bestDepth := int(^uint(0) >> 1)
	bestDist := int(^uint(0) >> 1)
	for idx, frame := range m.frames {
		if include != nil && !include[idx] {
			continue
		}
		dist := abs(frame.Col - currentCol)
		if frame.Depth < bestDepth {
			bestDepth = frame.Depth
			bestDist = dist
			bestIdx = idx
			continue
		}
		if frame.Depth == bestDepth {
			if dist < bestDist || (dist == bestDist && frame.Col < m.frames[bestIdx].Col) {
				bestDist = dist
				bestIdx = idx
			}
		}
	}
	if bestIdx >= 0 {
		m.selectedIdx = bestIdx
	}
}

func framesAtDepth(frames []tuiFrame, depth int) []int {
	return framesAtDepthFiltered(frames, depth, nil)
}

func framesAtDepthFiltered(frames []tuiFrame, depth int, include map[int]bool) []int {
	if depth < 0 {
		return nil
	}
	indices := make([]int, 0)
	for idx, frame := range frames {
		if include != nil && !include[idx] {
			continue
		}
		if frame.Depth == depth {
			indices = append(indices, idx)
		}
	}
	slices.SortFunc(indices, func(a, b int) int {
		return cmp.Compare(frames[a].Col, frames[b].Col)
	})
	return indices
}

func indexOf(values []int, target int) int {
	for idx, value := range values {
		if value == target {
			return idx
		}
	}
	return -1
}

func (m *Model) clampSelection() {
	if len(m.frames) == 0 {
		m.selectedIdx = 0
		return
	}
	if m.selectedIdx < 0 {
		m.selectedIdx = 0
	}
	if m.selectedIdx >= len(m.frames) {
		m.selectedIdx = len(m.frames) - 1
	}
}

func abs(v int) int {
	if v < 0 {
		return -v
	}
	return v
}

func (m Model) animationTickCmd() tea.Cmd {
	if !m.animating {
		return nil
	}
	return tea.Tick(animFrameDuration, func(time.Time) tea.Msg { return animTickMsg{} })
}

func (m Model) currentRootPath() string {
	if m.zoomPath != "" {
		return m.zoomPath
	}
	if len(m.frames) == 0 {
		return ""
	}
	return m.frames[0].Path
}

func (m Model) filterActive() bool {
	return strings.TrimSpace(m.searchQuery) != ""
}

func (m Model) navigableFrameSet() map[int]bool {
	if !m.filterActive() {
		return nil
	}
	return m.filterVisible
}

func (m Model) framesAtDepth(depth int) []int {
	return framesAtDepthFiltered(m.frames, depth, m.navigableFrameSet())
}

func (m Model) frameNavigable(idx int) bool {
	if idx < 0 || idx >= len(m.frames) {
		return false
	}
	if !m.filterActive() {
		return true
	}
	return m.filterVisible[idx]
}

func (m *Model) ensureSelectionNavigable() {
	if len(m.frames) == 0 {
		m.selectedIdx = 0
		return
	}
	m.clampSelection()
	if m.frameNavigable(m.selectedIdx) {
		return
	}

	if len(m.matchIndices) > 0 {
		for _, idx := range orderedMatchIndices(m.matchIndices) {
			if m.frameNavigable(idx) {
				m.selectedIdx = idx
				return
			}
		}
	}

	for idx := range m.frames {
		if m.frameNavigable(idx) {
			m.selectedIdx = idx
			return
		}
	}
}

func (m *Model) recordKeyDebug(msg tea.KeyPressMsg, handled, moved bool) {
	if !flameKeyDebugEnabled {
		return
	}
	keyID := keyString(msg)
	if keyID == "" {
		keyID = fmt.Sprintf("code:%d", msg.Code)
	}
	sel := "-"
	selIdx := m.selectedIdx
	if len(m.frames) > 0 && m.selectedIdx >= 0 && m.selectedIdx < len(m.frames) {
		sel = compactFramePath(m.frames[m.selectedIdx].Path)
	}
	m.lastKeyDebug = fmt.Sprintf("dbg frames=%d idx=%d key=%q code=%d handled=%t moved=%t sel=%s", len(m.frames), selIdx, keyID, msg.Code, handled, moved, sel)
}

func (m *Model) moveTraversal(delta int) {
	if len(m.frames) == 0 || delta == 0 {
		return
	}
	order := m.visibleTraversalOrder()
	if len(order) == 0 {
		return
	}
	pos := indexOf(order, m.selectedIdx)
	if pos < 0 {
		pos = 0
	}
	next := pos + delta
	if next < 0 {
		next = 0
	}
	if next >= len(order) {
		next = len(order) - 1
	}
	m.selectedIdx = order[next]
}

func (m Model) visibleTraversalOrder() []int {
	indices := make([]int, 0, len(m.frames))
	include := m.navigableFrameSet()
	for idx := range m.frames {
		if include != nil && !include[idx] {
			continue
		}
		indices = append(indices, idx)
	}
	slices.SortFunc(indices, func(a, b int) int {
		left := m.frames[a]
		right := m.frames[b]
		if left.Depth != right.Depth {
			return cmp.Compare(left.Depth, right.Depth)
		}
		if left.Col != right.Col {
			return cmp.Compare(left.Col, right.Col)
		}
		if left.Row != right.Row {
			return cmp.Compare(left.Row, right.Row)
		}
		return cmp.Compare(a, b)
	})
	return indices
}

func keyString(msg tea.KeyPressMsg) string {
	if s := msg.String(); s != "" {
		return s
	}
	return msg.Text
}

func isSearchOpenKey(msg tea.KeyPressMsg) bool { return keyString(msg) == "/" }
func isNextMatchKey(msg tea.KeyPressMsg) bool  { return keyString(msg) == "n" }
func isPrevMatchKey(msg tea.KeyPressMsg) bool  { return keyString(msg) == "N" }
func isPauseKey(msg tea.KeyPressMsg) bool {
	k := keyString(msg)
	return k == " " || k == "space" || msg.Code == tea.KeySpace
}
func isResetBaselineKey(msg tea.KeyPressMsg) bool {
	return keyString(msg) == "r"
}
func isCycleOrderKey(msg tea.KeyPressMsg) bool { return keyString(msg) == "o" }
func isCycleMetricKey(msg tea.KeyPressMsg) bool {
	return keyString(msg) == "b"
}
func isHelpToggleKey(msg tea.KeyPressMsg) bool { return keyString(msg) == "?" }

func isZoomInKey(msg tea.KeyPressMsg, keys flameKeyMap) bool {
	return key.Matches(msg, keys.ZoomIn) || msg.Code == tea.KeyEnter || strings.EqualFold(keyString(msg), "enter")
}

func isZoomUndoKey(msg tea.KeyPressMsg, keys flameKeyMap) bool {
	return key.Matches(msg, keys.ZoomUndo) || msg.Code == tea.KeyBackspace || msg.Code == tea.KeyEsc
}

func isZoomResetKey(msg tea.KeyPressMsg, keys flameKeyMap) bool {
	return key.Matches(msg, keys.ZoomReset)
}

func isMoveShallowerKey(msg tea.KeyPressMsg, keys flameKeyMap) bool {
	k := keyString(msg)
	return key.Matches(msg, keys.MoveShallower) || msg.Code == tea.KeyDown || keyMatchesDirection(k, "down", 'B')
}

func isMoveDeeperKey(msg tea.KeyPressMsg, keys flameKeyMap) bool {
	k := keyString(msg)
	return key.Matches(msg, keys.MoveDeeper) || msg.Code == tea.KeyUp || keyMatchesDirection(k, "up", 'A')
}

func isPrevSiblingKey(msg tea.KeyPressMsg, keys flameKeyMap) bool {
	k := keyString(msg)
	return key.Matches(msg, keys.PrevSibling) || msg.Code == tea.KeyLeft || keyMatchesDirection(k, "left", 'D')
}

func isNextSiblingKey(msg tea.KeyPressMsg, keys flameKeyMap) bool {
	k := keyString(msg)
	return key.Matches(msg, keys.NextSibling) || msg.Code == tea.KeyRight || keyMatchesDirection(k, "right", 'C')
}

func isJumpTopKey(msg tea.KeyPressMsg, keys flameKeyMap) bool {
	k := strings.ToLower(keyString(msg))
	return key.Matches(msg, keys.JumpTop) || msg.Code == tea.KeyPgUp || k == "pgup" || k == "pageup"
}

func isJumpRootKey(msg tea.KeyPressMsg, keys flameKeyMap) bool {
	k := strings.ToLower(keyString(msg))
	return key.Matches(msg, keys.JumpRoot) || msg.Code == tea.KeyPgDown || k == "pgdown" || k == "pgdn" || k == "pagedown"
}

func keyMatchesDirection(keyName, plain string, ansiFinal byte) bool {
	if keyName == plain || strings.HasSuffix(keyName, "+"+plain) {
		return true
	}
	return isArrowEscapeSequence(keyName, ansiFinal)
}

func isArrowEscapeSequence(value string, ansiFinal byte) bool {
	body, ok := strings.CutPrefix(value, "\x1b")
	if !ok || len(body) < 2 {
		return false
	}
	switch body[0] {
	case '[':
		return body[len(body)-1] == ansiFinal
	case 'O':
		return len(body) == 2 && body[1] == ansiFinal
	default:
		return false
	}
}

func (m Model) visibleRowOffset() int {
	if len(m.frames) == 0 {
		return 0
	}
	availableRows := m.height - 2 // toolbar + status
	if availableRows <= 0 {
		return 0
	}
	maxRow := maxFrameRowForSet(m.frames, m.navigableFrameSet())
	if maxRow+1 <= availableRows {
		return 0
	}
	return maxRow + 1 - availableRows
}

func (m *Model) ensureSelectionVisible() {
	if len(m.frames) == 0 {
		return
	}
	m.clampSelection()
	m.ensureSelectionNavigable()
	if !m.frameNavigable(m.selectedIdx) {
		return
	}
	rowOffset := m.visibleRowOffset()
	selected := m.frames[m.selectedIdx]
	if selected.Row >= rowOffset {
		return
	}

	bestIdx := -1
	bestScore := int(^uint(0) >> 1)
	for idx, frame := range m.frames {
		if !m.frameNavigable(idx) {
			continue
		}
		if frame.Row < rowOffset {
			continue
		}
		score := abs(frame.Row-rowOffset)*1000 + abs(frame.Col-selected.Col)
		if score < bestScore {
			bestIdx = idx
			bestScore = score
		}
	}
	if bestIdx >= 0 {
		m.selectedIdx = bestIdx
	}
}

func (m *Model) handleMouseClick(msg tea.MouseClickMsg) bool {
	if msg.Button != tea.MouseLeft {
		return false
	}
	idx := m.frameIndexAt(msg.X, msg.Y)
	if idx < 0 {
		return false
	}
	clickedPath := m.frames[idx].Path
	currentRoot := m.currentRootPath()
	if clickedPath == currentRoot {
		m.selectedIdx = idx
		m.subtreeSet = subtreeSetUsingAncestry(m.frames, m.selectedIdx, m.ancestry, m.subtreeSet)
		return true
	}
	if m.zoomPath != "" && hasPathBoundaryPrefix(currentRoot, clickedPath) {
		if !m.setZoomPath(clickedPath) {
			return false
		}
		m.zoomStack = buildZoomStack(clickedPath)
	} else {
		prevRootPath := m.zoomPath
		if !m.setZoomPath(clickedPath) {
			return false
		}
		m.zoomStack = append(m.zoomStack, zoomState{path: prevRootPath})
	}
	if sel := m.frameIndexByPath(clickedPath); sel >= 0 {
		m.selectedIdx = sel
	}
	m.subtreeSet = subtreeSetUsingAncestry(m.frames, m.selectedIdx, m.ancestry, m.subtreeSet)
	m.statusMessage = "Zoom: " + compactFramePath(clickedPath)
	return true
}

func (m *Model) setZoomPath(path string) bool {
	if m.snapshot == nil {
		return false
	}
	rootPath := m.rootSnapshotPath()
	if path == "" || path == rootPath {
		m.zoomRoot = nil
		m.zoomPath = ""
		m.zoomLineWidth = 0
		m.rebuildFrames(false)
		return true
	}
	target := findNodeByPath(m.snapshot, path)
	if target == nil {
		return false
	}
	m.zoomRoot = target
	m.zoomPath = path
	m.zoomLineWidth = 0
	m.rebuildFrames(false)
	return true
}

func (m Model) rootSnapshotPath() string {
	if m.snapshot != nil {
		return frameName(m.snapshot.Name, 0)
	}
	if len(m.frames) > 0 {
		return m.frames[0].Path
	}
	return ""
}

func buildZoomStack(path string) []zoomState {
	parts := strings.Split(path, pathSeparator)
	if len(parts) <= 1 {
		return nil
	}
	stack := []zoomState{{path: ""}}
	for idx := 1; idx < len(parts)-1; idx++ {
		stack = append(stack, zoomState{path: strings.Join(parts[:idx+1], pathSeparator)})
	}
	return stack
}

// frameIndexAt returns the index of the frame rendered at terminal coordinates
// (x, y), or -1 if no frame occupies that cell. Delegates boundary validation
// to frameCoordToTargetRow and frame scanning to findFrameAtRow.
func (m Model) frameIndexAt(x, y int) int {
	if len(m.frames) == 0 || m.width <= 0 || m.height <= 0 {
		return -1
	}
	if x < 0 || x >= m.width || y < 0 {
		return -1
	}

	extraLines := 1 // selection status line
	if m.showHelp {
		extraLines++
	}
	renderHeight := m.height - extraLines
	if renderHeight < 3 {
		renderHeight = 3
	}
	availableRows := renderHeight - 2 // flame toolbar + frame-status line
	if availableRows < 1 {
		return -1
	}

	// Row 0 is flame toolbar, rows 1..availableRows are bars, last row is status.
	if y < 1 || y > availableRows {
		return -1
	}

	targetRow := m.frameCoordToTargetRow(y-1, availableRows)
	if targetRow < 0 {
		return -1
	}
	return findFrameAtRow(m.frames, targetRow, x, m.width)
}

// frameCoordToTargetRow converts a data-area row offset (0-based, after
// stripping the toolbar row) into the logical frame row index. Returns -1 when
// the coordinate falls in the top padding area above the first visible row.
func (m Model) frameCoordToTargetRow(dataRow, availableRows int) int {
	maxRow := maxFrameRowForSet(m.frames, nil)
	barHeight := computeBarHeight(availableRows, maxRow+1, maxBarVisualHeight)
	visibleDepthRows := availableRows / barHeight
	if visibleDepthRows < 1 {
		visibleDepthRows = 1
	}
	rowOffset := 0
	if maxRow+1 > visibleDepthRows {
		rowOffset = maxRow + 1 - visibleDepthRows
	}
	renderedRows := (maxRow - rowOffset + 1) * barHeight
	padTop := 0
	if renderedRows < availableRows {
		padTop = availableRows - renderedRows
	}
	if dataRow < padTop {
		return -1
	}
	depthFromTop := (dataRow - padTop) / barHeight
	return maxRow - depthFromTop
}

// findFrameAtRow scans frames for the narrowest one that occupies logical row
// targetRow and contains pixel column x within [0, width). Returning the
// narrowest frame resolves overlap between wide parent and narrow child bars.
func findFrameAtRow(frames []tuiFrame, targetRow, x, width int) int {
	best := -1
	bestWidth := int(^uint(0) >> 1)
	for idx, frame := range frames {
		if frame.Row != targetRow || frame.Col >= width {
			continue
		}
		right := min(width, frame.Col+frame.Width)
		if x < frame.Col || x >= right {
			continue
		}
		if frame.Width < bestWidth {
			best = idx
			bestWidth = frame.Width
		}
	}
	return best
}

func (m Model) withZoomLineage(frames []tuiFrame) []tuiFrame {
	return applyZoomLineage(frames, m.snapshot, m.zoomPath, m.width)
}

// applyZoomLineage prepends the zoom path's ancestors to a zoomed frame
// layout. Extracted as a free function so the async snapshot refresh can
// reuse it on a background goroutine without referencing Model state directly.
func applyZoomLineage(frames []tuiFrame, snapshot *snapshotNode, zoomPath string, width int) []tuiFrame {
	if len(frames) == 0 || snapshot == nil {
		return frames
	}
	parts := strings.Split(zoomPath, pathSeparator)
	if len(parts) <= 1 {
		return frames
	}

	rowShift := len(parts) - 1
	out := make([]tuiFrame, 0, len(frames)+len(parts))
	for _, frame := range frames {
		if frame.Path == zoomPath {
			continue
		}
		frame.Row += rowShift
		frame.Depth += rowShift
		out = append(out, frame)
	}

	rootTotal := snapshotTotal(snapshot)
	for depth := range parts {
		path := strings.Join(parts[:depth+1], pathSeparator)
		node := findNodeByPath(snapshot, path)
		total := uint64(0)
		if node != nil {
			total = snapshotTotal(node)
		}
		percent := 0.0
		if rootTotal > 0 {
			percent = 100 * float64(total) / float64(rootTotal)
		}
		name := parts[depth]
		out = append(out, tuiFrame{
			Name:    name,
			Col:     0,
			Row:     depth,
			Width:   width,
			Total:   total,
			Percent: percent,
			Fill:    terminalFrameColor(name),
			Depth:   depth,
			Path:    path,
		})
	}
	return out
}