feat: tmux 动态 resize(iPadOS 拖拽窗口/旋转自适应)
- ContentView 观测 onChange(geo.size)/onAppear → controller.containerResized - containerResized: 点尺寸 × displayScale ÷ cell 像素 = 目标 cols×rows,去重 + debounce(200ms) → refresh-client -C WxH → tmux 重排回 %layout-change → 更新 pane frame - cell 像素来自 raw 终端/pane surface metrics(InMemoryTerminalViewport.cellWidthPixels); surface resize 回调只 noteCellPixels 不驱动 tmux(防反馈环,单向数据流) - 验证:容器 1032x1280@2x cell16x35→129x73,tmux 根 rect 精确采纳,2 pane 复用不重建 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -14,12 +14,15 @@ final class TmuxPaneSurface: ObservableObject, Identifiable {
|
||||
/// 初始 `capture-pane` 应答到达前丢弃该 pane 的 %output(避免与快照重复);tmux 单线程串行保证正确。
|
||||
var awaitingCapture = true
|
||||
|
||||
init(id: TmuxPaneID, cols: Int, rows: Int, onInput: @escaping @Sendable (Data) -> Void) {
|
||||
init(id: TmuxPaneID, cols: Int, rows: Int,
|
||||
onInput: @escaping @Sendable (Data) -> Void,
|
||||
onMetrics: @escaping @Sendable (Int, Int) -> Void) {
|
||||
self.id = id
|
||||
self.grid = (cols, rows)
|
||||
let session = InMemoryTerminalSession(
|
||||
write: { data in onInput(data) }, // 输入直接绑到本 pane → send-keys -t %self
|
||||
resize: { _ in } // tmux 模式:surface resize 不反向驱动 tmux(防反馈环)
|
||||
// tmux 模式:surface resize 只回报 cell 像素供换算,绝不反向驱动 tmux(防反馈环)。
|
||||
resize: { vp in onMetrics(Int(vp.cellWidthPixels), Int(vp.cellHeightPixels)) }
|
||||
)
|
||||
let state = TerminalViewState()
|
||||
state.configuration = TerminalSurfaceOptions(backend: .inMemory(session))
|
||||
@@ -60,6 +63,9 @@ final class TmuxController: ObservableObject {
|
||||
var onExit: (() -> Void)?
|
||||
|
||||
private var clientCols = 80, clientRows = 24
|
||||
private var cellWpx = 0, cellHpx = 0 // cell 设备像素(换算容器点尺寸→格子)
|
||||
private var lastSentCols = 0, lastSentRows = 0
|
||||
private var resizeDebounce: Task<Void, Never>?
|
||||
|
||||
// control-mode 命令应答 FIFO 匹配。
|
||||
private var attachAcked = false
|
||||
@@ -73,6 +79,30 @@ final class TmuxController: ObservableObject {
|
||||
/// 由 model 在 attach 前告知全屏格子(tmux 据此 refresh-client -C 布局)。
|
||||
func setClientSize(cols: Int, rows: Int) { clientCols = max(1, cols); clientRows = max(1, rows) }
|
||||
|
||||
/// cell 设备像素(来自 raw 终端或 pane surface metrics)。
|
||||
func setCellPixels(w: Int, h: Int) { if w > 0 { cellWpx = w }; if h > 0 { cellHpx = h } }
|
||||
/// pane surface metrics 回报(仅刷新 cell 缓存,绝不反向驱动 tmux)。
|
||||
func noteCellPixels(w: Int, h: Int) { setCellPixels(w: w, h: h) }
|
||||
|
||||
/// 容器尺寸变化(iPadOS 拖拽窗口/旋转/分屏):换算总格子 → 去重 + debounce → refresh-client -C。
|
||||
/// 单向数据流:尺寸只来自"容器点尺寸 × displayScale ÷ cell 像素",绝不来自 surface resize 回调(防反馈环)。
|
||||
func containerResized(widthPt: CGFloat, heightPt: CGFloat, scale: CGFloat) {
|
||||
guard cellWpx > 0, cellHpx > 0, widthPt > 0, heightPt > 0, scale > 0 else { return }
|
||||
let cols = max(1, Int(widthPt * scale / CGFloat(cellWpx)))
|
||||
let rows = max(1, Int(heightPt * scale / CGFloat(cellHpx)))
|
||||
guard cols != lastSentCols || rows != lastSentRows else { return }
|
||||
lastSentCols = cols; lastSentRows = rows
|
||||
clientCols = cols; clientRows = rows
|
||||
NSLog("TMUXDBG containerResized pt=\(Int(widthPt))x\(Int(heightPt)) scale=\(scale) cell=\(cellWpx)x\(cellHpx) -> \(cols)x\(rows)")
|
||||
resizeDebounce?.cancel()
|
||||
resizeDebounce = Task { [weak self] in
|
||||
try? await Task.sleep(nanoseconds: 200_000_000) // 合并拖拽期间的连续变化
|
||||
guard !Task.isCancelled, let self, self.attachAcked else { return }
|
||||
NSLog("TMUXDBG refresh-client -C \(cols)x\(rows)")
|
||||
self.sendCommand("refresh-client -C \(cols)x\(rows)") // tmux 重排 → 回 %layout-change → 更新 frame
|
||||
}
|
||||
}
|
||||
|
||||
var activeWindow: TmuxWindow? {
|
||||
guard let id = activeWindowID else { return windows.first }
|
||||
return windowByID[id]
|
||||
@@ -139,6 +169,8 @@ final class TmuxController: ObservableObject {
|
||||
}
|
||||
win.fullLayout = fullTree
|
||||
win.visibleLayout = visTree // 单次发布 → SwiftUI 单帧更新,无闪烁
|
||||
let rr = fullTree.root.rect
|
||||
NSLog("TMUXDBG layout win=@\(w.raw) root=\(rr.width)x\(rr.height) panes=\(fullTree.paneIDs.count)")
|
||||
if win.activePane == nil || !newPanes.contains(win.activePane!) {
|
||||
win.activePane = fullTree.paneIDs.first
|
||||
}
|
||||
@@ -155,9 +187,11 @@ final class TmuxController: ObservableObject {
|
||||
paneToWindow[p] = w
|
||||
guard let win = windowByID[w] else { return }
|
||||
if let existing = surfaceByPane[p] { existing.grid = (cols, rows); return } // 复用,不重建
|
||||
let surface = TmuxPaneSurface(id: p, cols: cols, rows: rows) { [weak self] data in
|
||||
self?.sendKeys(pane: p, data: data)
|
||||
}
|
||||
let surface = TmuxPaneSurface(
|
||||
id: p, cols: cols, rows: rows,
|
||||
onInput: { [weak self] data in self?.sendKeys(pane: p, data: data) },
|
||||
onMetrics: { [weak self] w, h in Task { @MainActor in self?.noteCellPixels(w: w, h: h) } }
|
||||
)
|
||||
surfaceByPane[p] = surface
|
||||
win.panes[p] = surface
|
||||
// 抓初始屏(capture %end 后放行 %output)。
|
||||
|
||||
Reference in New Issue
Block a user