ShaunLawrie / PwshSpectreConsole

👻 PwshSpectreConsole is a PowerShell wrapper for the awesome Spectre.Console library
https://pwshspectreconsole.com/
MIT License
114 stars 7 forks source link

[Question] Get the Width/Height of a layout element after it's rendered to fit the available space #55

Open ihbwbm opened 5 days ago

ihbwbm commented 5 days ago

Greetings!

Is there a way to gracefully get the width/height of an expanded panel element in a layout that has already been rendered inside Invoke-SpectreLive to fit the available space?

As an example provided on the site with the files: image

I've been trying to retrieve the height "Preview" panel once it's expanded, but I could not find an okay method of getting it besides getting the [console]::WindowHeight and then subtracting the height of the header panel.

Tried to somehow get it using Get-SpectreRenderableSize but to no avail. Accessing the panel using something like $layout['name'] yields no result as it scales to the whole console.

If there's nothing to render in the panel, it prints a placeholder, that contains the needed size values, but I cant figure out how to get them, e.g.: image

Any help would be greatly appreciated <3

ShaunLawrie commented 4 days ago

This has been bamboozling me too I couldn't find a way to get those measurements it shows in the placeholder panels 😆 At the moment subtracting the height of the header panel like you've done from the console height is the best option. To force the header to not expand and stay stuck at it's minimum size you should be able to explicitly set the minimum height you want and force the ratio to favour the bottom half so much that the top can't expand to take up any additional height.

$headerHeight = 5
$layout = New-SpectreLayout -Name "root" -Rows @(
    # Row 1
    (New-SpectreLayout -Name "title" -MinimumSize $headerHeight -Ratio 1 -Data ("HEADER" | Format-SpectrePanel -Expand)),
    # Row 2 with a ratio of 999 (the default ratio weighting is 1)
    (New-SpectreLayout -Name "messages" -Ratio 999 -Data ("CONTENT" |  Format-SpectrePanel -Expand))
)

As for a real fix... the item size can't be determined until its parent is rendered so I'm going to need to do some weird stuff to accurately get the height. I've had a dig through the spectre.console source and found I can implement the same logic used to generate the sizes so they're accessible via the powershell module but it's going to be a bit of work 😅 thanks for asking the question as it's motivated me to work it out. https://github.com/spectreconsole/spectre.console/blob/main/src/Spectre.Console/Widgets/Layout/Layout.cs#L266