Public/WindowControl.ps1

# ---------------------------------------------------------------------------
# Window control - Hide / Show (via mockable wrappers)
# ---------------------------------------------------------------------------

function Hide-TTWindow {
    <#
    .SYNOPSIS
        Hide a terminal window (removes from taskbar, still running).
    .DESCRIPTION
        Hides a terminal window using the Win32 ShowWindow API. The process
        continues running in the background. Use Show-TTWindow to restore it.
    .PARAMETER Id
        Session ID (prefix match supported).
    .EXAMPLE
        Hide-TTWindow -Id 'abc12345'
        Hides the terminal window for the specified session.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)][string]$Id
    )
    Initialize-NativeHelper
    $session = @(Get-TTSession -Id $Id -IncludeHidden)
    if (-not $session -or @($session).Count -ne 1) { Write-Warning "Session not found or ambiguous."; return }
    $session = @($session)[0]

    $hwnd = Get-TTWindowHandle -Pid $session.Pid
    if ($hwnd -ne [IntPtr]::Zero) {
        $SW_HIDE = 0
        Invoke-TTShowWindow -Handle $hwnd -CmdShow $SW_HIDE | Out-Null
        $sessions = @(Read-JsonFile $script:SessionsFile)
        $target = $sessions | Where-Object { $_.Id -eq $session.Id }
        if ($target) {
            @($target)[0].Hidden = $true
            @($target)[0] | Add-Member -NotePropertyName HiddenWindowHandle -NotePropertyValue $hwnd.ToInt64() -Force
            Write-JsonFile $script:SessionsFile $sessions
        }
        Write-Verbose "Window hidden for session $($session.Id)."
    }
    else {
        Write-Warning "Could not find window handle for session $($session.Id)."
    }
}

function Show-TTWindow {
    <#
    .SYNOPSIS
        Show a previously hidden terminal window.
    .DESCRIPTION
        Restores a terminal window that was hidden via Hide-TTWindow. Re-resolves
        the window handle from the live process and brings it to the foreground.
    .PARAMETER Id
        Session ID (prefix match supported).
    .EXAMPLE
        Show-TTWindow -Id 'abc12345'
        Restores and brings the hidden window to the foreground.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)][string]$Id
    )
    Initialize-NativeHelper
    $sessions = @(Read-JsonFile $script:SessionsFile)
    $target = $sessions | Where-Object { $_.Id -like "$Id*" }
    if (-not $target -or @($target).Count -ne 1) { Write-Warning "Session not found or ambiguous."; return }
    $target = @($target)[0]

    # Always re-resolve HWND from live process first (stale HWNDs fail after reboot/WT restart)
    $hwnd = Get-TTWindowHandle -Pid $target.Pid
    if ($hwnd -eq [IntPtr]::Zero -and $target.HiddenWindowHandle) {
        # Fall back to stored handle only if live resolution fails
        $hwnd = [IntPtr]::new([long]$target.HiddenWindowHandle)
    }

    if ($hwnd -ne [IntPtr]::Zero) {
        $SW_SHOW = 5
        Invoke-TTShowWindow -Handle $hwnd -CmdShow $SW_SHOW | Out-Null
        Invoke-TTSetForegroundWindow -Handle $hwnd | Out-Null
        $target.Hidden = $false
        Write-JsonFile $script:SessionsFile $sessions
        Write-Verbose "Window restored for session $($target.Id)."
    }
    else {
        Write-Warning "Could not restore window for session $($target.Id)."
    }
}