Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ihbwbm opened this issue Nov 1, 2024 · 2 comments

Comments

@ihbwbm
Copy link

ihbwbm commented Nov 1, 2024

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
Copy link
Owner

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

@ShaunLawrie
Copy link
Owner

I'm still stuck on this one and hope to figure it out soon. The layout renderer has given me loads of headaches trying to get the image rendering to work without breaking it recently so I'm still using this hack solution myself 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants