resize: maintain ratio when changing constraints Before this change, changing the constraints (e.g. by resizing a window) would have the following behavior: - Increasing the max constraint would not change the absolute size of w1, i.e. float.Pos would be constant. - Decreasing the max constraint would rapidly shrink the Ratio to zero. With this change, the ratio is constant across changes in the constraints. Increasing the constraint will lead to an absolutely larger w1 and w2, and decreasing the constraint will lead to an absolutely smaller w1 and w2, while maintaining a constant ratio. Signed-off-by: Dominik Honnef <dominik@honnef.co>
1 files changed, 6 insertions(+), 1 deletions(-) M component/resizer.go
M component/resizer.go => component/resizer.go +6 -1
@@ 27,10 27,15 @@ func (rs *Resize) Layout(gtx layout.Context, w1, w2, handle layout.Widget) layou // Compute the first widget's max width/height. rs.float.Length = rs.Axis.Convert(gtx.Constraints.Max).X rs.float.Pos = int(rs.Ratio * float32(rs.float.Length)) oldPos := rs.float.Pos m := op.Record(gtx.Ops) dims := rs.float.Layout(gtx, rs.Axis, handle) c := m.Stop() rs.Ratio = float32(rs.float.Pos) / float32(rs.float.Length) if rs.float.Pos != oldPos { // We update rs.Ratio conditionally to avoid cumulating rounding errors when changing the constraints instead of // dragging the handle. rs.Ratio = float32(rs.float.Pos) / float32(rs.float.Length) } return layout.Flex{ Axis: rs.Axis, }.Layout(gtx,