diff --git a/Tools/windows/Tests/ValidationRunner.Tests.ps1 b/Tools/windows/Tests/ValidationRunner.Tests.ps1 index d8f24448..357f962b 100644 --- a/Tools/windows/Tests/ValidationRunner.Tests.ps1 +++ b/Tools/windows/Tests/ValidationRunner.Tests.ps1 @@ -1,5 +1,96 @@ +param( + [switch]$DialogSheet768Only +) + $ErrorActionPreference = "Stop" +function Test-DialogSheet768SourceContract([string] $source) { + if ($source -notmatch 'function Write-UiaDialogSheet768Observation\(' -or + $source -notmatch 'function Test-UiaDialogSheet768Geometry\(' -or + $source -notmatch 'UIA_DIALOG_SHEET_768_EVIDENCE=' -or + @([regex]::Matches( + $source, + 'Write-UiaDialogSheet768Observation ' + )).Count -lt 3) { + throw "RED: UIA live gate lacks the three-surface owned geometry and PrintWindow conformance probe for #768" + } + foreach ($required in @( + '"rename-loop" "initial" $false', + '"delete-loop" "confirmation" $false', + '"new-loop" "main-no-overflow" $false', + '"new-loop" "goal-overflow" $true' + )) { + if (-not $source.Contains($required)) { + throw "RED: UIA live gate omitted #768 scene contract $required" + } + } + if (@([regex]::Matches( + $source, '\[GraphCodeUiaGateState\]::IsDirectChild\(' + )).Count -lt 2 -or + @([regex]::Matches($source, 'Save-UiaShellWindowCapture ')).Count -lt 2) { + throw "RED: UIA #768 probe lacks exact native hierarchy or both PrintWindow captures" + } + $tokens = $null + $errors = $null + $ast = [Management.Automation.Language.Parser]::ParseInput( + $source, [ref]$tokens, [ref]$errors) + if ($errors.Count -ne 0) { throw "RED: UIA live gate #768 geometry source does not parse" } + $geometry = $ast.Find({ + param($node) + $node -is [Management.Automation.Language.FunctionDefinitionAst] -and + $node.Name -ceq "Test-UiaDialogSheet768Geometry" + }, $true) + if ($null -eq $geometry) { throw "RED: UIA #768 geometry decision is missing" } + . ([scriptblock]::Create($geometry.Extent.Text)) + $valid = [pscustomobject]@{ + expectedActionIds = @(2, 1) + expectedDpi = 96 + expectedOverflow = $true + dpi = 96 + ownerBounds = @(0, 0, 800, 600) + hostBounds = @(100, 50, 700, 550) + hostClient = @(0, 0, 600, 500) + viewportBounds = @(100, 50, 700, 486) + contentBounds = @(100, 20, 690, 920) + actions = @( + [pscustomobject]@{ id = 2; offscreen = $false; bounds = @(120, 500, 220, 534) }, + [pscustomobject]@{ id = 1; offscreen = $false; bounds = @(560, 500, 680, 534) } + ) + } + $decision = Test-UiaDialogSheet768Geometry $valid + if (-not $decision.accepted -or -not $decision.overflow) { + throw "RED: UIA #768 geometry accepted-control fixture was rejected" + } + $mutations = @( + @{ name = "dpi"; expected = "dpi-mismatch"; apply = { param($value) $value.dpi = 144 } }, + @{ name = "owner"; expected = "host-outside-owner"; apply = { param($value) $value.hostBounds = @(201, 50, 801, 550) } }, + @{ name = "content"; expected = "content-displaced"; apply = { param($value) $value.contentBounds = @(1000, 20, 1010, 920) } }, + @{ name = "frame"; expected = "host-client-frame-mismatch"; apply = { param($value) $value.hostClient = @(0, 0, 580, 500) } }, + @{ name = "actions"; expected = "footer-action-order-mismatch"; apply = { param($value) $value.actions[0].id = 7 } }, + @{ name = "offscreen"; expected = "footer-offscreen"; apply = { param($value) $value.actions[1].offscreen = $true } }, + @{ name = "footer"; expected = "footer-overlaps-viewport"; apply = { param($value) $value.actions[0].bounds = @(120, 480, 220, 514) } }, + @{ name = "overflow"; expected = "unexpected-overflow"; apply = { param($value) $value.expectedOverflow = $false } } + ) + foreach ($mutation in $mutations) { + $candidate = [Management.Automation.PSSerializer]::Deserialize( + [Management.Automation.PSSerializer]::Serialize($valid)) + & $mutation.apply $candidate + $result = Test-UiaDialogSheet768Geometry $candidate + if ($result.accepted -or $result.reason -cne $mutation.expected) { + throw "RED: UIA #768 geometry $($mutation.name) mutation was not refused as $($mutation.expected)" + } + } +} + +if ($DialogSheet768Only) { + $repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..\..\..") + Test-DialogSheet768SourceContract ( + Get-Content -LiteralPath (Join-Path $repoRoot "Tools\windows\uia-live-gate.ps1") -Raw + ) + Write-Host "ValidationRunner.Tests.ps1 DialogSheet768: PASS; executed=9" + exit 0 +} + function Test-MultiProjectProtocolContracts([string] $stubSource, [string] $gateSource, [string] $stubPath, [string] $pwsh) { foreach ($source in @($stubSource, $gateSource)) { $errors = $null @@ -3684,6 +3775,7 @@ try { if ($uiaErrors.Count -ne 0) { throw "RED: UIA live gate source does not parse" } + Test-DialogSheet768SourceContract $uiaLiveGateSource if ($uiaLiveGateSource -notmatch '\[switch\]\$CycleHierarchyOnly' -or $uiaLiveGateSource -notmatch 'PostFixtureMutation\(\$shellWindow, 23\)' -or $uiaLiveGateSource -notmatch 'UIA_CYCLE_HIERARCHY_EVIDENCE=' -or diff --git a/Tools/windows/ValidationRunner/Ui/NativeUiSuite.cs b/Tools/windows/ValidationRunner/Ui/NativeUiSuite.cs index f570e7d2..d84c3131 100644 --- a/Tools/windows/ValidationRunner/Ui/NativeUiSuite.cs +++ b/Tools/windows/ValidationRunner/Ui/NativeUiSuite.cs @@ -331,7 +331,11 @@ void Lookup(string id, IntPtr parent, int controlId, IntPtr expected) => IntPtr viewport = Create("GraphCodeSheetViewport", prompt); IntPtr content = Create("GraphCodeWindowsDialogContent", viewport); IntPtr edit = Create("EDIT", content, 9904); - Lookup("ui.native.lookup.named-direct-viewport", prompt, 9904, edit); + Check("ui.native.lookup.named-direct-viewport", () => ActivationCases.Require( + GraphCodeUiaGateState.ControlById(prompt, 9904) == edit && + GraphCodeUiaGateState.IsDirectChild(prompt, viewport) && + GraphCodeUiaGateState.IsDirectChild(viewport, content), + "RED: prompt lookup or native direct-child ownership differs")); Check("ui.native.lookup.hidden-write-read", () => ActivationCases.Require( !PromptLookupFixture.IsWindowVisible(prompt) && GraphCodeUiaGateState.SetEditTextById(prompt, 9904, "owned fixture") && @@ -366,7 +370,10 @@ void Lookup(string id, IntPtr parent, int controlId, IntPtr expected) => IntPtr deepViewport = Create("GraphCodeSheetViewport", wrapper); IntPtr deepContent = Create("GraphCodeWindowsDialogContent", deepViewport); Create("EDIT", deepContent, 9904); - Lookup("ui.native.lookup.direct-viewport-required", deepRoot, 9904, IntPtr.Zero); + Check("ui.native.lookup.direct-viewport-required", () => ActivationCases.Require( + GraphCodeUiaGateState.ControlById(deepRoot, 9904) == IntPtr.Zero && + !GraphCodeUiaGateState.IsDirectChild(deepRoot, deepViewport), + "RED: prompt lookup accepted a viewport below the required direct child")); IntPtr nativeRoot = NewRoot("GraphCodeSheetHost"); IntPtr nativeViewport = Create("GraphCodeSheetViewport", nativeRoot); IntPtr nativeContent = Create("GraphCodeNativeForm", nativeViewport); diff --git a/Tools/windows/ValidationRunner/Ui/Production/GraphCodeUiaGateState.cs b/Tools/windows/ValidationRunner/Ui/Production/GraphCodeUiaGateState.cs index 794fb9fd..47462d36 100644 --- a/Tools/windows/ValidationRunner/Ui/Production/GraphCodeUiaGateState.cs +++ b/Tools/windows/ValidationRunner/Ui/Production/GraphCodeUiaGateState.cs @@ -534,6 +534,9 @@ public static bool IsControlOwnedBy(IntPtr parent, IntPtr control, int controlId return parent != IntPtr.Zero && control != IntPtr.Zero && GetDlgCtrlID(control) == controlId && GetAncestor(control, 2) == parent; } + public static bool IsDirectChild(IntPtr parent, IntPtr child) { + return parent != IntPtr.Zero && child != IntPtr.Zero && GetAncestor(child, 1) == parent; + } public static int ControlIdOf(IntPtr control) { return control == IntPtr.Zero ? 0 : GetDlgCtrlID(control); } diff --git a/Tools/windows/uia-live-gate.ps1 b/Tools/windows/uia-live-gate.ps1 index 1a21e3c5..ab94628f 100644 --- a/Tools/windows/uia-live-gate.ps1 +++ b/Tools/windows/uia-live-gate.ps1 @@ -973,6 +973,244 @@ function Assert-UiaOwnedSheet( return $identity } +function Test-UiaDialogSheet768Geometry($observation) { + $reject = { + param([string] $reason) + return [pscustomobject]@{ accepted = $false; reason = $reason } + } + if ($null -eq $observation) { return & $reject "observation-missing" } + if (-not ($observation.dpi -is [int] -or $observation.dpi -is [uint32] -or + $observation.dpi -is [long]) -or [long]$observation.dpi -le 0) { + return & $reject "dpi-invalid" + } + if ([long]$observation.expectedDpi -gt 0 -and + [long]$observation.dpi -ne [long]$observation.expectedDpi) { + return & $reject "dpi-mismatch" + } + foreach ($name in @("ownerBounds", "hostBounds", "hostClient", "viewportBounds", "contentBounds")) { + $value = $observation.$name + if ($null -eq $value -or @($value).Count -ne 4) { return & $reject "$name-missing" } + foreach ($coordinate in @($value)) { + $number = [double]$coordinate + if ([double]::IsNaN($number) -or [double]::IsInfinity($number)) { + return & $reject "$name-nonfinite" + } + } + if ([double]$value[2] -le [double]$value[0] -or + [double]$value[3] -le [double]$value[1]) { + return & $reject "$name-empty" + } + } + $owner = @($observation.ownerBounds) + $hostRect = @($observation.hostBounds) + $client = @($observation.hostClient) + $viewport = @($observation.viewportBounds) + $content = @($observation.contentBounds) + if ([Math]::Abs(([double]$client[2] - [double]$client[0]) - + ([double]$hostRect[2] - [double]$hostRect[0])) -gt 1 -or + [Math]::Abs(([double]$client[3] - [double]$client[1]) - + ([double]$hostRect[3] - [double]$hostRect[1])) -gt 1) { + return & $reject "host-client-frame-mismatch" + } + if ($hostRect[0] -lt $owner[0] -or $hostRect[1] -lt $owner[1] -or + $hostRect[2] -gt $owner[2] -or $hostRect[3] -gt $owner[3]) { + return & $reject "host-outside-owner" + } + if ($viewport[0] -lt $hostRect[0] - 1 -or $viewport[1] -lt $hostRect[1] - 1 -or + $viewport[2] -gt $hostRect[2] + 1 -or $viewport[3] -gt $hostRect[3] + 1) { + return & $reject "viewport-outside-host" + } + if ($content[0] -lt $viewport[0] - 1 -or $content[2] -gt $viewport[2] + 1 -or + $content[1] -gt $viewport[1] + 1 -or $content[3] -lt $viewport[3] - 1) { + return & $reject "content-displaced" + } + $actions = @($observation.actions) + if ($actions.Count -lt 2) { return & $reject "footer-actions-missing" } + $expectedActionIds = @($observation.expectedActionIds) + if ($expectedActionIds.Count -ne $actions.Count) { + return & $reject "footer-action-count-mismatch" + } + for ($index = 0; $index -lt $expectedActionIds.Count; $index++) { + if ([int]$actions[$index].id -ne [int]$expectedActionIds[$index]) { + return & $reject "footer-action-order-mismatch" + } + } + $footerTop = [int]::MaxValue + foreach ($action in $actions) { + if ($null -eq $action -or @($action.bounds).Count -ne 4) { + return & $reject "footer-bounds-missing" + } + if ($action.offscreen -isnot [bool] -or $action.offscreen) { + return & $reject "footer-offscreen" + } + $bounds = @($action.bounds) + if ($bounds[2] -le $bounds[0] -or $bounds[3] -le $bounds[1] -or + $bounds[0] -lt $hostRect[0] - 1 -or $bounds[1] -lt $hostRect[1] - 1 -or + $bounds[2] -gt $hostRect[2] + 1 -or $bounds[3] -gt $hostRect[3] + 1) { + return & $reject "footer-outside-host" + } + $footerTop = [Math]::Min($footerTop, [int]$bounds[1]) + } + if ($viewport[3] -gt $footerTop + 1) { return & $reject "footer-overlaps-viewport" } + if ($null -ne $observation.expectedOverflow) { + $actualOverflow = ([double]$content[3] - [double]$content[1]) -gt + ([double]$viewport[3] - [double]$viewport[1]) + 1 + if ($actualOverflow -ne [bool]$observation.expectedOverflow) { + return & $reject $(if ([bool]$observation.expectedOverflow) { + "expected-overflow-missing" + } else { + "unexpected-overflow" + }) + } + } + return [pscustomobject]@{ + accepted = $true + reason = "owned-geometry" + overflow = ([double]$content[3] - [double]$content[1]) -gt + ([double]$viewport[3] - [double]$viewport[1]) + 1 + } +} + +function Write-UiaDialogSheet768Observation( + $element, + [int] $processId, + [IntPtr] $owner, + [string] $title, + [string] $contentClass, + [int[]] $actionIds, + [string] $surface, + [string] $state, + [Nullable[bool]] $expectedOverflow = $null, + [long] $expectedDpi = 0 +) { + $identity = Assert-UiaOwnedSheet $element $processId $owner $title $contentClass ` + "$surface $state" + $sheetHandle = [IntPtr]$element.Current.NativeWindowHandle + $directChildren = @($element.FindAll( + [System.Windows.Automation.TreeScope]::Children, + [System.Windows.Automation.Condition]::TrueCondition + )) + $viewportElements = @($directChildren | Where-Object { + $handle = [IntPtr]$_.Current.NativeWindowHandle + $handle -ne [IntPtr]::Zero -and + [GraphCodeUiaGateState]::WindowClass($handle) -ceq "GraphCodeSheetViewport" + }) + Require ($viewportElements.Count -eq 1) ` + "$surface $state did not expose exactly one direct GraphCodeSheetViewport child" + $viewport = [IntPtr]$viewportElements[0].Current.NativeWindowHandle + Require ([GraphCodeUiaGateState]::WindowProcessId($viewport) -eq $processId -and + [GraphCodeUiaGateState]::IsDirectChild($sheetHandle, $viewport)) ` + "$surface $state viewport lost exact native host ownership" + $contentElements = @($viewportElements[0].FindAll( + [System.Windows.Automation.TreeScope]::Children, + [System.Windows.Automation.Condition]::TrueCondition + ) | Where-Object { + $handle = [IntPtr]$_.Current.NativeWindowHandle + $handle -ne [IntPtr]::Zero -and + [GraphCodeUiaGateState]::WindowClass($handle) -ceq $contentClass + }) + Require ($contentElements.Count -eq 1) ` + "$surface $state did not expose exactly one direct $contentClass content child" + $content = [IntPtr]$contentElements[0].Current.NativeWindowHandle + Require ([GraphCodeUiaGateState]::WindowProcessId($content) -eq $processId -and + [GraphCodeUiaGateState]::IsDirectChild($viewport, $content)) ` + "$surface $state content lost exact native viewport ownership" + $actions = [Collections.Generic.List[object]]::new() + foreach ($id in $actionIds) { + $matches = @($directChildren | Where-Object { + $handle = [IntPtr]$_.Current.NativeWindowHandle + $handle -ne [IntPtr]::Zero -and + [GraphCodeUiaGateState]::ControlIdOf($handle) -eq $id + }) + Require ($matches.Count -eq 1) ` + "$surface $state did not expose footer action $id exactly once as a direct host child" + $actionHandle = [IntPtr]$matches[0].Current.NativeWindowHandle + Require ([GraphCodeUiaGateState]::WindowProcessId($actionHandle) -eq $processId) ` + "$surface $state footer action $id changed process ownership" + $actions.Add([ordered]@{ + id = $id + handle = $actionHandle.ToInt64() + name = [string]$matches[0].Current.Name + enabled = [bool]$matches[0].Current.IsEnabled + offscreen = [bool]$matches[0].Current.IsOffscreen + bounds = @([GraphCodeUiaGateState]::WindowBounds($actionHandle)) + }) + } + $hostSnapshot = [CapNative]::WindowSnapshot($sheetHandle, $processId) + $ownerSnapshot = [CapNative]::WindowSnapshot($owner, $processId) + $observation = [ordered]@{ + expectedActionIds = @($actionIds) + expectedDpi = $expectedDpi + expectedOverflow = $expectedOverflow + dpi = [long]$hostSnapshot.Dpi + ownerBounds = @([GraphCodeUiaGateState]::WindowBounds($owner)) + hostBounds = @($hostSnapshot.OuterBounds) + hostClient = @($hostSnapshot.ClientBounds) + viewportBounds = @([GraphCodeUiaGateState]::WindowBounds($viewport)) + contentBounds = @([GraphCodeUiaGateState]::WindowBounds($content)) + actions = $actions.ToArray() + } + $geometry = Test-UiaDialogSheet768Geometry $observation + Require $geometry.accepted ` + "$surface $state geometry rejected: $($geometry.reason); observation=$($observation | ConvertTo-Json -Depth 6 -Compress)" + Require ($ownerSnapshot.Dpi -eq $hostSnapshot.Dpi) ` + "$surface $state crossed owner/sheet DPI values $($ownerSnapshot.Dpi)/$($hostSnapshot.Dpi)" + Require ($null -ne $script:uiaAttributionDirectory -and + (Test-Path -LiteralPath $script:uiaAttributionDirectory -PathType Container)) ` + "$surface $state has no owned capture directory" + $stem = ("768-{0}-{1}" -f $surface, $state) -replace '[^A-Za-z0-9]+', '-' + $sheetPath = Assert-UiaSandboxPath $script:uiaAttributionDirectory ` + (Join-Path $script:uiaAttributionDirectory "$stem-sheet.png") + $ownerPath = Assert-UiaSandboxPath $script:uiaAttributionDirectory ` + (Join-Path $script:uiaAttributionDirectory "$stem-owner.png") + $sheetCapture = Save-UiaShellWindowCapture $sheetHandle $sheetPath + $ownerCapture = Save-UiaShellWindowCapture $owner $ownerPath + Require ($sheetCapture -ceq "saved" -and $ownerCapture -ceq "saved") ` + "$surface $state PrintWindow capture failed: sheet=$sheetCapture owner=$ownerCapture" + $sheetFile = Get-Item -LiteralPath $sheetPath -ErrorAction Stop + $ownerFile = Get-Item -LiteralPath $ownerPath -ErrorAction Stop + Require ($sheetFile.Length -gt 0 -and $ownerFile.Length -gt 0) ` + "$surface $state produced an empty PrintWindow capture" + $repoRoot = Resolve-Path (Join-Path $PSScriptRoot "..\..") + $record = [ordered]@{ + schemaVersion = 1 + surface = $surface + state = $state + identity = $identity + hierarchy = [ordered]@{ + host = $sheetHandle.ToInt64() + viewport = $viewport.ToInt64() + content = $content.ToInt64() + contentClass = $contentClass + } + geometry = $observation + geometryDecision = $geometry + captures = [ordered]@{ + owner = [ordered]@{ + path = $ownerFile.FullName + bytes = $ownerFile.Length + sha256 = (Get-FileHash -LiteralPath $ownerPath -Algorithm SHA256).Hash + } + sheet = [ordered]@{ + path = $sheetFile.FullName + bytes = $sheetFile.Length + sha256 = (Get-FileHash -LiteralPath $sheetPath -Algorithm SHA256).Hash + } + } + sources = [ordered]@{ + framework = (Get-FileHash (Join-Path $repoRoot ` + "graphcode-windows\src\SheetFramework.zig") -Algorithm SHA256).Hash + nativeDialogs = (Get-FileHash (Join-Path $repoRoot ` + "graphcode-windows\src\WindowsNativeDialogs.zig") -Algorithm SHA256).Hash + nativeForms = (Get-FileHash (Join-Path $repoRoot ` + "graphcode-windows\src\NativeForms.zig") -Algorithm SHA256).Hash + } + } + $json = $record | ConvertTo-Json -Depth 10 -Compress + Write-Host ("UIA_DIALOG_SHEET_768_EVIDENCE=" + $json) + return $record +} + function Wait-UiaOwnedSheetByContentClass( [int] $processId, [IntPtr] $owner, @@ -6784,6 +7022,20 @@ try { $sidebarNodeForm = Wait-UiaOwnedSheetByContentClass $process.Id $shellWindow ` "GraphCodeNativeForm" "Create or edit node" "project-row New Loop node form" Require ($null -ne $sidebarNodeForm) "project-row New Loop did not open the node form" + Require ([GraphCodeUiaGateState]::PostCommand( + [IntPtr]$sidebarNodeForm.Current.NativeWindowHandle, 9604 + )) "New Loop sheet rejected the Main type command" + Start-Sleep -Milliseconds 150 + $null = Write-UiaDialogSheet768Observation $sidebarNodeForm $process.Id $shellWindow ` + "Create or edit node" "GraphCodeNativeForm" @(2, 1) ` + "new-loop" "main-no-overflow" $false + Require ([GraphCodeUiaGateState]::PostCommand( + [IntPtr]$sidebarNodeForm.Current.NativeWindowHandle, 9602 + )) "New Loop sheet rejected the Goal type command" + Start-Sleep -Milliseconds 150 + $null = Write-UiaDialogSheet768Observation $sidebarNodeForm $process.Id $shellWindow ` + "Create or edit node" "GraphCodeNativeForm" @(2, 1) ` + "new-loop" "goal-overflow" $true $templatesButton = $sidebarNodeForm.FindFirst( [System.Windows.Automation.TreeScope]::Descendants, (New-Object System.Windows.Automation.PropertyCondition( @@ -8973,6 +9225,9 @@ try { Require ($null -ne $renameDialog) "Rename command did not open its native sheet" $renameSheetIdentity = Assert-UiaOwnedSheet $renameDialog $process.Id $shellWindow ` $fixtureRenameTitle "GraphCodeWindowsDialogContent" "Rename fixture sheet" + $null = Write-UiaDialogSheet768Observation $renameDialog $process.Id $shellWindow ` + $fixtureRenameTitle "GraphCodeWindowsDialogContent" @(9808, 9800) ` + "rename-loop" "initial" $false $renameGraphBefore = @(Get-DirectChildren $graph $rawWalker | Where-Object { $_.Current.AutomationId -match '^canvas-card-' -and $_.Current.Name -eq "UIA loop A" }) @@ -9122,6 +9377,9 @@ try { Require ($null -ne $deleteLoopDialog) "Delete Loop did not open its custom sheet" $deleteLoopIdentity = Assert-UiaOwnedSheet $deleteLoopDialog $process.Id $shellWindow ` $deleteLoopTitle "GraphCodeWindowsDialogContent" "Delete Loop custom sheet" + $null = Write-UiaDialogSheet768Observation $deleteLoopDialog $process.Id $shellWindow ` + $deleteLoopTitle "GraphCodeWindowsDialogContent" @(9808, 9800) ` + "delete-loop" "confirmation" $false $deleteLoopElements = @($deleteLoopDialog.FindAll( [System.Windows.Automation.TreeScope]::Descendants, [System.Windows.Automation.Condition]::TrueCondition diff --git a/graphcode-windows/src/NativeForms.zig b/graphcode-windows/src/NativeForms.zig index 94b7f5bc..541ef1c5 100644 --- a/graphcode-windows/src/NativeForms.zig +++ b/graphcode-windows/src/NativeForms.zig @@ -2948,6 +2948,11 @@ fn attachmentsVisible(state: *const DialogState) bool { return state.kind == .node and !NewLoopSheet.isComposite(state.values[1]); } +fn attachmentSectionHeight(state: *const DialogState) i32 { + if (!attachmentsVisible(state) or state.attachment_count == 0) return 0; + return scaled(state, attachment_section_height); +} + /// Which node field a `[image #N]` placeholder is inserted into/removed from — the /// one free-text field actually shown for the loop type currently selected. Composite /// ("proactive") loops have no such field, matching macOS hiding attachments entirely @@ -3015,7 +3020,7 @@ fn createButtonLabelled(hwnd: c.HWND, state: *const DialogState, text: []const u } fn layoutAttachmentsSection(hwnd: c.HWND, state: *DialogState, top: i32) void { - const shown = attachmentsVisible(state); + const shown = attachmentSectionHeight(state) != 0; const margin = scaled(state, form_margin); const width = formContentWidth(hwnd, state); const button_width = scaled(state, 132); @@ -3024,7 +3029,11 @@ fn layoutAttachmentsSection(hwnd: c.HWND, state: *DialogState, top: i32) void { const viewport = formViewportHeight(hwnd, state); layoutContentControl(state.attachment_label, margin, top, width, scaled(state, form_label_height), shown, viewport); layoutContentControl(state.attachment_listbox, margin, top + scaled(state, 26), list_width, scaled(state, 106), shown, viewport); - layoutContentControl(state.attachment_attach_button, margin + list_width + gap, top + scaled(state, 26), button_width, scaled(state, 32), shown, viewport); + if (shown) { + layoutContentControl(state.attachment_attach_button, margin + list_width + gap, top + scaled(state, 26), button_width, scaled(state, 32), true, viewport); + } else if (!attachmentsVisible(state)) { + layoutContentControl(state.attachment_attach_button, 0, 0, 0, 0, false, viewport); + } layoutContentControl(state.attachment_remove_button, margin + list_width + gap, top + scaled(state, 66), button_width, scaled(state, 32), shown, viewport); layoutContentControl(state.attachment_help, margin, top + scaled(state, 140), width, scaled(state, form_help_height), shown, viewport); } @@ -3169,7 +3178,10 @@ fn attachFiles(hwnd: c.HWND, state: *DialogState) void { added = true; insertAttachmentToken(state, state.attachment_count); } - if (added) refreshAttachmentListbox(state); + if (added) { + refreshAttachmentListbox(state); + refreshSheetLayout(state); + } } fn appendAttachment(state: *DialogState, source_path: []const u8) DraftAttachments.IngestError!void { @@ -3201,6 +3213,7 @@ fn removeSelectedAttachment(state: *DialogState) void { removeAttachmentToken(state, index + 1); removeAttachmentAt(state, index); refreshAttachmentListbox(state); + refreshSheetLayout(state); } fn removeAttachmentAt(state: *DialogState, index: usize) void { @@ -3502,7 +3515,7 @@ fn layoutForm(hwnd: c.HWND, state: *DialogState) void { } if (state.kind == .node) { layoutAttachmentsSection(hwnd, state, y); - if (attachmentsVisible(state)) y += scaled(state, attachment_section_height); + y += attachmentSectionHeight(state); } if (showsRecap(state.kind)) { layoutContentControl(state.recap, margin, y, width, scaled(state, 40), true, formViewportHeight(hwnd, state)); @@ -3590,7 +3603,7 @@ fn layoutNodeForm(state: *DialogState) void { } layoutAttachmentsSection(hwnd, state, y); - if (attachmentsVisible(state)) y += scaled(state, attachment_section_height); + y += attachmentSectionHeight(state); layoutContentControl(state.runs_as_caption, margin, y, width, scaled(state, 20), true, clientHeight(hwnd)); y += scaled(state, 28); @@ -3615,11 +3628,33 @@ fn layoutNodeForm(state: *DialogState) void { fn layoutNodeRow(state: *DialogState, index: usize, x: i32, y: i32, width: i32) void { const help = nodeFieldHelp(index); - const input_width = if (index == 7) @max(scaled(state, 120), width - scaled(state, 86)) else width; + const compact_attachment_action = if (briefFieldIndex(state)) |brief| + brief == index and state.attachment_count == 0 + else + false; + const attachment_button_width = scaled(state, 132); + const action_gap = scaled(state, 12); + const input_width = if (index == 7) + @max(scaled(state, 120), width - scaled(state, 86)) + else if (compact_attachment_action) + @max(scaled(state, 120), width - attachment_button_width - action_gap) + else + width; layoutContentControl(state.labels[index], x, y, width, scaled(state, form_label_height), true, clientHeight(state.content_root)); _ = c.ShowWindow(state.edits[index], c.SW_SHOW); layoutFieldControl(state.edits[index], state, fieldBounds(state, x, y + scaled(state, 26), input_width), inputControlHeight(state, state.input_kinds[index])); layoutContentControl(state.helps[index], x, y + scaled(state, 62), width, scaled(state, form_help_height), help.len != 0, clientHeight(state.content_root)); + if (compact_attachment_action) { + layoutContentControl( + state.attachment_attach_button, + x + input_width + action_gap, + y + scaled(state, 26), + attachment_button_width, + scaled(state, 32), + true, + clientHeight(state.content_root), + ); + } if (index == 7) { layoutContentControl(state.done_check_test, x + width - scaled(state, 76), y + scaled(state, 26), scaled(state, 76), scaled(state, 32), true, clientHeight(state.content_root)); layoutContentControl(state.done_check_outcome, x + width - scaled(state, 230), y + scaled(state, 62), scaled(state, 150), scaled(state, 20), true, clientHeight(state.content_root)); @@ -3662,7 +3697,7 @@ fn contentHeight(state: *const DialogState) i32 { for (0..state.field_count) |index| { if (state.visible[index]) y += rowHeight(state, index); } - if (state.kind == .node and attachmentsVisible(state)) y += scaled(state, attachment_section_height); + if (state.kind == .node) y += attachmentSectionHeight(state); if (showsRecap(state.kind)) y += scaled(state, 40); return y + scaled(state, 16); } @@ -3685,7 +3720,7 @@ fn nodeContentHeight(state: *const DialogState) i32 { } else { height += 84 * 4; } - if (attachmentsVisible(state)) height += attachment_section_height; + if (attachmentSectionHeight(state) != 0) height += attachment_section_height; return height + 28 + 72 + 58 + 16; } @@ -3711,6 +3746,10 @@ fn revealField(state: *DialogState, index: usize) void { } fn revealAttachments(state: *DialogState) void { + if (state.attachment_count == 0) { + if (briefFieldIndex(state)) |index| revealField(state, index); + return; + } var top = scaled(state, form_fields_top); for (0..state.field_count) |index| if (state.visible[index]) { top += rowHeight(state, index); @@ -6147,6 +6186,13 @@ fn initHiddenForm(owner: c.HWND, state: *DialogState, host: *SheetFramework.Host try host.relayout(); } +fn controlBoundsIn(control: c.HWND, parent: c.HWND) !c.RECT { + var rect: c.RECT = undefined; + if (c.GetWindowRect(control, &rect) == 0) return error.TestControlBoundsUnavailable; + _ = c.MapWindowPoints(null, parent, @ptrCast(&rect), 2); + return rect; +} + test "dialog framework: short promotion sizes from content" { var state = DialogState{ .allocator = std.testing.allocator, @@ -6840,6 +6886,66 @@ test "new loop: all five layouts keep the SheetFramework footer fixed at every D } } +test "new loop: empty Main fits the canonical 96 DPI owner without scrolling" { + var state = DialogState{ + .allocator = std.testing.allocator, + .kind = .node, + .parent = null, + .dpi = 96, + }; + state.values[1] = @constCast("sketch"); + configureFields(&state); + var input = formLayoutInput(&state); + input.owner = .{ .x = 8, .y = 51, .width = 1028, .height = 729 }; + const form = SheetFramework.layout(input); + + try std.testing.expect(!form.scroll.visible); + try std.testing.expect(form.scroll.extent <= form.scroll.page); + try std.testing.expectEqual(@as(i32, 620), form.scroll.extent); + try std.testing.expectEqual(@as(i32, 620), form.scroll.page); +} + +test "new loop: empty attachment action stays beside the brief and expands on demand" { + const owner = try hiddenNativeControlHost(); + defer _ = c.DestroyWindow(owner); + var state = DialogState{ + .allocator = std.testing.allocator, + .kind = .node, + .parent = owner, + .dpi = 96, + }; + defer freeValues(&state); + try initializeNodeDraft(&state, .{ + .title = "", + .loop_type = "sketch", + .first_instruction = "", + }); + var host: SheetFramework.Host = .{}; + defer host.deinit(); + try initHiddenForm(owner, &state, &host); + + try std.testing.expect(c.GetWindowLongPtrW(state.attachment_attach_button, c.GWL_STYLE) & c.WS_VISIBLE != 0); + try std.testing.expect(c.GetWindowLongPtrW(state.attachment_remove_button, c.GWL_STYLE) & c.WS_VISIBLE == 0); + try std.testing.expect(c.GetWindowLongPtrW(state.attachment_listbox, c.GWL_STYLE) & c.WS_VISIBLE == 0); + const edit_bounds = try controlBoundsIn(state.edits[4], state.content_root); + const attach_bounds = try controlBoundsIn(state.attachment_attach_button, state.content_root); + try std.testing.expect(edit_bounds.right <= attach_bounds.left); + try std.testing.expect(attachmentSectionHeight(&state) == 0); + + selectTile(&state, choiceIndex(.loop_type, "goalBased")); + refreshSheetLayout(&state); + try std.testing.expect(c.GetWindowLongPtrW(state.attachment_attach_button, c.GWL_STYLE) & c.WS_VISIBLE != 0); + try std.testing.expect(c.IsChild(state.content_root, state.attachment_attach_button) != 0); + + state.attachment_count = 1; + refreshSheetLayout(&state); + try std.testing.expect(attachmentSectionHeight(&state) == scaled(&state, attachment_section_height)); + try std.testing.expect(c.GetWindowLongPtrW(state.attachment_attach_button, c.GWL_STYLE) & c.WS_VISIBLE != 0); + try std.testing.expect(c.GetWindowLongPtrW(state.attachment_remove_button, c.GWL_STYLE) & c.WS_VISIBLE != 0); + try std.testing.expect(c.GetWindowLongPtrW(state.attachment_listbox, c.GWL_STYLE) & c.WS_VISIBLE != 0); + state.attachment_count = 0; +} + test "new loop: type switch resets scroll and exposes stable Goal fields from the top" { const owner = try hiddenNativeControlHost(); defer _ = c.DestroyWindow(owner); @@ -6850,8 +6956,12 @@ test "new loop: type switch resets scroll and exposes stable Goal fields from th .loop_type = "sketch", .first_instruction = "Capture the current change", }); + state.attachment_count = 1; var host: SheetFramework.Host = .{}; - defer host.deinit(); + defer { + state.attachment_count = 0; + host.deinit(); + } try initHiddenForm(owner, &state, &host); try std.testing.expect(host.current_layout.scroll.max_offset > 0); try host.setScrollOffset(host.current_layout.scroll.max_offset); @@ -7073,6 +7183,7 @@ test "tile rows reserve full teaching-tile height while other rows stay compact" state.visible[1] = true; state.visible[2] = true; state.values[1] = @constCast("sketch"); + state.attachment_count = 1; try std.testing.expectEqual(@as(i32, form_row_height), rowHeight(&state, 0)); try std.testing.expectEqual(@as(i32, tile_row_height), rowHeight(&state, 1)); try std.testing.expectEqual(@as(i32, form_row_height), rowHeight(&state, 2));