Resize_to_layout!() does not remove margins around plots in GridLayout

I use resize_to_layout!() to remove whatever margins around the plot in Figure after plotting. For example, the following code

using CairoMakie

fig = Figure()
fig[2,1] = Axis(fig, aspect=DataAspect())
hm = heatmap!(rand(1000,100))
fig[1,1] = Colorbar(fig, hm, vertical=false)
rowsize!(fig.layout, 2, Aspect(1,0.1))

resize_to_layout!(fig)
display(fig)

creates this plot without margins around the plot:

However, if I plot inside GridLayout, resize_to_layout!() does not work. For example, the following code,

using CairoMakie

fig = Figure()
fig[1,1] = layout = GridLayout()
layout[2,1] = Axis(fig, aspect=DataAspect())
hm = heatmap!(rand(1000,100))
layout[1,1] = Colorbar(fig, hm, vertical=false)
rowsize!(layout, 2, Aspect(1,0.1))

resize_to_layout!(fig)
display(fig)

which is nearly identical to the earlier one except for the use of GridLayout(), produces a figure with large top and bottom margins:

How can I remove these margins when using GridLayout()?

Probably GridLayoutBase needs to be improved here, if it doesn’t infer the size through the nesting (probably because of the Aspect). Although aspect constraints are resolved differently than other sizes so it could be that it’s not possible.

Thanks for the insights! Do you have any suggestions? I would like to use nested GridLayout because I wanted to put several heatmap and Colorbar pairs, each pair residing in its dedicated GridLayout, but if this makes the margin control difficult, maybe I should put all heatmaps and Colorbars in a single GridLayout.

Probably easier to do it in a single one, yes. At least for now. In principle you could first call resize_to_layout like logic on the inner GridLayout so it has a fixed size and then on the outer one.

How can I get the inner GridLayout? I tried things like fig[1,1].layout, but it seems to return the outermost GridLayout.

You should be able to get it via content(fig[1, 1]) I think, if it’s the only object there. Otherwise contents