|
| 1 | +package dev.nucleusframework.window.tao |
| 2 | + |
| 3 | +import kotlinx.coroutines.channels.Channel |
| 4 | +import kotlinx.coroutines.channels.trySendBlocking |
| 5 | +import kotlinx.coroutines.flow.conflate |
| 6 | +import kotlinx.coroutines.flow.consumeAsFlow |
| 7 | +import org.jetbrains.compose.devtools.api.WindowsState |
| 8 | +import org.jetbrains.compose.reload.agent.orchestration |
| 9 | +import org.jetbrains.compose.reload.agent.sendAsync |
| 10 | +import org.jetbrains.compose.reload.core.WindowId |
| 11 | +import org.jetbrains.compose.reload.orchestration.OrchestrationMessage |
| 12 | +import kotlin.math.roundToInt |
| 13 | + |
| 14 | +/** |
| 15 | + * Concrete [TaoHotReloadBridge], loaded reflectively by [TaoHotReloadIntegration] |
| 16 | + * only when Compose Hot Reload is active (`compose.reload.isActive == "true"`), |
| 17 | + * which is also when the hot-reload artifacts land on the runtime classpath. |
| 18 | + * |
| 19 | + * Mirrors `org.jetbrains.compose.reload.jvm.startWindowManager` (the AWT-based |
| 20 | + * tracker in `hot-reload-runtime-jvm`), but driven by Tao window callbacks |
| 21 | + * instead of `java.awt.Window` listeners — the Tao backend has no AWT windows |
| 22 | + * for the agent's auto-instrumentation to attach to. |
| 23 | + * |
| 24 | + * Coordinates are converted from Tao's **physical** pixels to the **logical** |
| 25 | + * pixels the dev-tools sidecar expects (matching AWT's `window.x/width`, which |
| 26 | + * `startWindowManager` sends verbatim). Fractional HiDPI scales may drift a |
| 27 | + * few pixels; integer scales are exact. |
| 28 | + * |
| 29 | + * Version-skew safety: this class compiles against the catalog's hot-reload |
| 30 | + * version, but the runtime classes come from whatever agent the user's Compose |
| 31 | + * plugin launched — and the orchestration API is not binary-stable across |
| 32 | + * releases (e.g. `WindowsState.WindowState` gained a `title` parameter in |
| 33 | + * 1.2.0 and dropped the old constructor). Every entry point is guarded: the |
| 34 | + * first [LinkageError] (or any other failure) permanently disables tracking |
| 35 | + * for that window, so the sidecar merely stops following it — the app itself |
| 36 | + * must never be taken down by dev tooling. |
| 37 | + */ |
| 38 | +internal class TaoHotReloadBridgeImpl : TaoHotReloadBridge { |
| 39 | + |
| 40 | + override fun trackWindow(window: TaoWindow, title: String?, alwaysOnTop: Boolean) { |
| 41 | + // [title] is unused: WindowsState.WindowState has no title field in the |
| 42 | + // hot-reload version bundled by Compose 1.11.x. Kept in the bridge |
| 43 | + // surface so the call sites don't change when a newer API gains one. |
| 44 | + val windowId = WindowId.create() |
| 45 | + // Conflated channel: coalesce bursts of move/resize events (GTK fires |
| 46 | + // one per pixel of a drag) into a single WindowsState update, exactly |
| 47 | + // like the AWT tracker's `Channel.CONFLATED` + `conflate()`. |
| 48 | + val windowState = Channel<WindowsState.WindowState?>(Channel.CONFLATED) |
| 49 | + |
| 50 | + // One-shot kill switch: flipped on the first failure inside a Tao |
| 51 | + // callback (see class KDoc). Closing the channel also ends the pump. |
| 52 | + var broken = false |
| 53 | + |
| 54 | + fun guarded(block: () -> Unit) { |
| 55 | + if (broken) return |
| 56 | + try { |
| 57 | + block() |
| 58 | + } catch (t: Throwable) { |
| 59 | + broken = true |
| 60 | + windowState.close() |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Pump channel → orchestration WindowsState. Runs on the orchestration |
| 65 | + // task's dispatcher (off the Tao main thread). `null` removes the |
| 66 | + // window (minimize/hide/destroy) — matches AWT's `broadcastGone`. |
| 67 | + orchestration.subtask { |
| 68 | + windowState.consumeAsFlow().conflate().collect { state -> |
| 69 | + orchestration.update(WindowsState) { current -> |
| 70 | + val windows = if (state == null) { |
| 71 | + current.windows - windowId |
| 72 | + } else { |
| 73 | + current.windows + (windowId to state) |
| 74 | + } |
| 75 | + WindowsState(windows) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + fun toLogical(physical: Int): Int = (physical / window.scaleFactor).roundToInt() |
| 81 | + |
| 82 | + /** Reads the live outer rect and pushes a conflated state update. */ |
| 83 | + fun broadcastActiveState() { |
| 84 | + val bounds = window.outerBoundsPx() ?: return |
| 85 | + // [x, y, width, height] physical px → logical. |
| 86 | + val x = toLogical(bounds[0].toInt()) |
| 87 | + val y = toLogical(bounds[1].toInt()) |
| 88 | + val w = toLogical(bounds[2].toInt()) |
| 89 | + val h = toLogical(bounds[3].toInt()) |
| 90 | + if (w <= 0 || h <= 0) return |
| 91 | + windowState.trySendBlocking( |
| 92 | + WindowsState.WindowState( |
| 93 | + x = x, y = y, width = w, height = h, |
| 94 | + isAlwaysOnTop = alwaysOnTop, |
| 95 | + ), |
| 96 | + ) |
| 97 | + OrchestrationMessage.ApplicationWindowPositioned( |
| 98 | + windowId, x, y, w, h, isAlwaysOnTop = alwaysOnTop, |
| 99 | + ).sendAsync() |
| 100 | + } |
| 101 | + |
| 102 | + fun broadcastGone() { |
| 103 | + windowState.trySendBlocking(null) |
| 104 | + OrchestrationMessage.ApplicationWindowGone(windowId).sendAsync() |
| 105 | + } |
| 106 | + |
| 107 | + // Initial state: outerBoundsPx is null until the window is mapped, so |
| 108 | + // the first meaningful broadcast happens via onResized (fires right |
| 109 | + // after onWindowReady with the mapped size). Still try once in case |
| 110 | + // geometry is already available. |
| 111 | + guarded { broadcastActiveState() } |
| 112 | + |
| 113 | + window.onMoved { _, _ -> guarded { broadcastActiveState() } } |
| 114 | + window.onResized { _, _ -> guarded { broadcastActiveState() } } |
| 115 | + window.onFocusChanged { focused -> |
| 116 | + if (focused) { |
| 117 | + guarded { |
| 118 | + // Sidecar brings itself toFront() on this. |
| 119 | + OrchestrationMessage.ApplicationWindowGainedFocus(windowId).sendAsync() |
| 120 | + broadcastActiveState() |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + window.onMinimizedChanged { minimized -> |
| 125 | + guarded { if (minimized) broadcastGone() else broadcastActiveState() } |
| 126 | + } |
| 127 | + window.onDestroyed { guarded { broadcastGone() } } |
| 128 | + } |
| 129 | +} |
0 commit comments