question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Layout subviews does not apply `Width` changes to `Bounds`

See original GitHub issue

Describe the bug Changes to Width are only reflected in Bounds after calling LayoutSubviews on super which seems fine. But subsequent changes to Width are not applied to Bounds even after calling LayoutSubviews again.

If there is a workaround or this is expected behaviour let me know and I can apply to the tests in TerminalGuiDesigner. This is a common use case in the designer (e.g. drag resizing a view) so there are several tests that fail because of it. I notice that Frame is updated but don’t want to re-write all my tests to just check Frame instead without understanding if that is correct.

To Reproduce

[Fact, AutoInitShutdown]
public void TestResize ()
{
	var w = new Window ();
	var v = new View { Width = 8, Height = 8 };

	w.Add (v);

	Assert.Equal (0, v.Bounds.Width);

	w.LayoutSubviews ();

	Assert.Equal (8, v.Bounds.Width);

	v.Width = 10;

	w.LayoutSubviews ();

	// Fails here 
	Assert.Equal (10, v.Bounds.Width);
}

Expected behavior Calling LayoutSubviews should always update the Bounds to respect the Width/Height

Issue Analytics

  • State:open
  • Created 4 months ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
tigcommented, May 26, 2023

In my opinion if a Pos/Dim are initialized with absolute values it may be considered before IsInitialized. With others types I agree forcing to be set after IsInitialized when the SuperView bounds are knowing.

I believe you and I are aligned on this.

I had created this issue, IIRC, specifically so we can fix this: https://github.com/gui-cs/Terminal.Gui/issues/2465

0reactions
BDispcommented, May 25, 2023

Here is the reason why Frame did change but not Bounds, if (LayoutStyle == LayoutStyle.Computed && !IsInitialized):

		public virtual Rect Bounds {
			get {
#if DEBUG
				if (LayoutStyle == LayoutStyle.Computed && !IsInitialized) {
					Debug.WriteLine ($"WARNING: Bounds is being accessed before the View has been initialized. This is likely a bug. View: {this}");
				}
#endif // DEBUG
				var frameRelativeBounds = Padding?.Thickness.GetInside (Padding.Frame) ?? new Rect (default, Frame.Size);
				return new Rect (default, frameRelativeBounds.Size);
			}
			set {
				// BUGBUG: Margin etc.. can be null (if typeof(Frame))
				Frame = new Rect (Frame.Location,
					new Size (
						value.Size.Width + Margin.Thickness.Horizontal + Border.Thickness.Horizontal + Padding.Thickness.Horizontal,
						value.Size.Height + Margin.Thickness.Vertical + Border.Thickness.Vertical + Padding.Thickness.Vertical
						)
					);
			}
		}

So, if you need calculated Bounds you need to run BeginInit/EndInit on the SuperView.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Subview's bounds are zero in layoutSubviews() - ios
If no layout updates are pending, this method exits without modifying the layout or calling any layout-related callbacks.
Read more >
Subview is not resizing properly | Apple Developer Forums
I have a view with an image. When the image is clicked there is a modal popup of that image that is "full...
Read more >
Autolayout Fundamental - Manasa M P - Medium
When a view's bounds change, that view automatically resizes its subviews according to each subview's autoresizing mask. The default value of ...
Read more >
UIView Frames and Bounds
All UIViews have frame and bounds properties which define their dimensions. The similarity of these properties can cause some confusion when ...
Read more >
Frame vs Bounds in iOS: Implementing A Visual ...
Find out what is the difference between frame and bounds in iOS through the implementation of a small app that explains everything visually....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found