Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions Terminal.Gui/View/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,8 @@ public virtual Rect Frame {
set {
_frame = new Rect (value.X, value.Y, Math.Max (value.Width, 0), Math.Max (value.Height, 0));
if (IsInitialized || LayoutStyle == LayoutStyle.Absolute) {
TextFormatter.Size = GetSizeNeededForTextAndHotKey ();
LayoutFrames ();
TextFormatter.Size = GetSizeNeededForTextAndHotKey ();
SetNeedsLayout ();
SetNeedsDisplay ();
}
Expand Down Expand Up @@ -676,7 +676,6 @@ public virtual Rect Bounds {
value.Size.Height + Margin.Thickness.Vertical + Border.Thickness.Vertical + Padding.Thickness.Vertical
)
);
;
}
}

Expand Down Expand Up @@ -869,8 +868,8 @@ Width is Dim.DimAbsolute &&
/// will not fit.</returns>
public bool SetMinWidthHeight ()
{
if (IsInitialized && GetMinimumBounds (out Size size)) {
Bounds = new Rect (Bounds.Location, size);
if (GetMinimumBounds (out Size size)) {
_frame = new Rect (_frame.Location, size);
return true;
}
return false;
Expand Down Expand Up @@ -1006,8 +1005,7 @@ void SetInitialProperties (ustring text, Rect rect, LayoutStyle layoutStyle = La

Text = text == null ? ustring.Empty : text;
LayoutStyle = layoutStyle;
var r = rect.IsEmpty ? TextFormatter.CalcRect (0, 0, text, direction) : rect;
Frame = r;
Frame = rect.IsEmpty ? TextFormatter.CalcRect (0, 0, text, direction) : rect;
OnResizeNeeded ();

CreateFrames ();
Expand Down Expand Up @@ -1050,15 +1048,14 @@ protected virtual void OnResizeNeeded ()
var w = _width is Dim.DimAbsolute ? _width.Anchor (0) : _frame.Width;
var h = _height is Dim.DimAbsolute ? _height.Anchor (0) : _frame.Height;
// BUGBUG: v2 - ? - If layoutstyle is absolute, this overwrites the current frame h/w with 0. Hmmm...
// This is needed for DimAbsolute values by setting the frame before LayoutSubViews.
_frame = new Rect (new Point (actX, actY), new Size (w, h)); // Set frame, not Frame!


}
//// BUGBUG: I think these calls are redundant or should be moved into just the AutoSize case
if (IsInitialized || LayoutStyle == LayoutStyle.Absolute) {
TextFormatter.Size = GetSizeNeededForTextAndHotKey ();
LayoutFrames ();
SetMinWidthHeight ();
LayoutFrames ();
TextFormatter.Size = GetSizeNeededForTextAndHotKey ();
SetNeedsLayout ();
SetNeedsDisplay ();
}
Expand Down Expand Up @@ -1624,6 +1621,7 @@ public virtual void OnAdded (SuperViewChangedEventArgs e)
{
var view = e.Child;
view.IsAdded = true;
view.OnResizeNeeded ();
view._x ??= view._frame.X;
view._y ??= view._frame.Y;
view._width ??= view._frame.Width;
Expand Down Expand Up @@ -2723,6 +2721,7 @@ internal virtual void LayoutFrames ()
if (Margin == null) return; // CreateFrames() has not been called yet

if (Margin.Frame.Size != Frame.Size) {
Margin._frame = new Rect (Point.Empty, Frame.Size);
Margin.X = 0;
Margin.Y = 0;
Margin.Width = Frame.Size.Width;
Expand All @@ -2734,6 +2733,7 @@ internal virtual void LayoutFrames ()

var border = Margin.Thickness.GetInside (Margin.Frame);
if (border != Border.Frame) {
Border._frame = new Rect (new Point (border.Location.X, border.Location.Y), border.Size);
Border.X = border.Location.X;
Border.Y = border.Location.Y;
Border.Width = border.Size.Width;
Expand All @@ -2745,6 +2745,7 @@ internal virtual void LayoutFrames ()

var padding = Border.Thickness.GetInside (Border.Frame);
if (padding != Padding.Frame) {
Padding._frame = new Rect (new Point (padding.Location.X, padding.Location.Y), padding.Size);
Padding.X = padding.Location.X;
Padding.Y = padding.Location.Y;
Padding.Width = padding.Size.Width;
Expand Down Expand Up @@ -2833,16 +2834,10 @@ public virtual ustring Text {
get => _text;
set {
_text = value;
if (IsInitialized) {
SetHotKey ();
UpdateTextFormatterText ();
//TextFormatter.Format ();
OnResizeNeeded ();
}

// BUGBUG: v2 - This is here as a HACK until we fix the unit tests to not check a view's dims until
// after it's been initialized. See #2450
SetHotKey ();
UpdateTextFormatterText ();
//TextFormatter.Format ();
OnResizeNeeded ();

#if DEBUG
if (_text != null && string.IsNullOrEmpty (Id)) {
Expand Down Expand Up @@ -2927,11 +2922,8 @@ public virtual VerticalTextAlignment VerticalTextAlignment {
public virtual TextDirection TextDirection {
get => TextFormatter.Direction;
set {
if (!IsInitialized) {
TextFormatter.Direction = value;
} else {
UpdateTextDirection (value);
}
UpdateTextDirection (value);
TextFormatter.Direction = value;
}
}

Expand Down Expand Up @@ -3324,6 +3316,7 @@ protected override void Dispose (bool disposing)
Margin?.Dispose ();
Margin = null;
Border?.Dispose ();
Border = null;
Padding?.Dispose ();
Padding = null;

Expand Down Expand Up @@ -3366,7 +3359,6 @@ public virtual void BeginInit ()

// TODO: Figure out why ScrollView and other tests fail if this call is put here
// instead of the constructor.
OnResizeNeeded ();
//InitializeFrames ();

} else {
Expand All @@ -3389,6 +3381,7 @@ public virtual void BeginInit ()
public void EndInit ()
{
IsInitialized = true;
OnResizeNeeded ();
if (_subviews != null) {
foreach (var view in _subviews) {
if (!view.IsInitialized) {
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/Toplevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ internal View GetLocationThatFits (Toplevel top, int targetX, int targetY,
}

// BUGBUG: v2 hack for now
var mfLength = top.Border.Thickness.Top > 0 ? 2 : 1;
var mfLength = top.Border?.Thickness.Top > 0 ? 2 : 1;
if (top.Frame.Width <= maxWidth) {
nx = Math.Max (targetX, 0);
nx = nx + top.Frame.Width > maxWidth ? Math.Max (maxWidth - top.Frame.Width, 0) : nx;
Expand Down
71 changes: 65 additions & 6 deletions UnitTests/Dialogs/DialogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void Location_When_Not_Application_Top_Not_Default ()
var iterations = -1;
Application.Iteration += () => {
iterations++;

if (iterations == 0) {
var d = new Dialog () {
X = 5,
Expand Down Expand Up @@ -200,12 +200,12 @@ public void Location_When_Not_Application_Top_Not_Default ()
Application.RequestStop ();
}
};

Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (20, 10);
Application.Run ();
}

[Fact]
[AutoInitShutdown]
public void ButtonAlignment_One ()
Expand Down Expand Up @@ -511,10 +511,10 @@ public void ButtonAlignment_Four_On_Too_Small_Width ()
var btn4Text = "never";
var btn4 = $"{d.LeftBracket} {btn4Text} {d.RightBracket}";
var buttonRow = string.Empty;

var width = 30;
d.SetBufferSize (width, 1);

// Default - Center
buttonRow = $"{d.VLine}es ] {btn2} {btn3} [ neve{d.VLine}";
(runstate, var dlg) = RunButtonTestDialog (title, width, Dialog.ButtonAlignments.Center, new Button (btn1Text), new Button (btn2Text), new Button (btn3Text), new Button (btn4Text));
Expand Down Expand Up @@ -937,6 +937,65 @@ public void Dialog_In_Window_With_Size_One_Button_Aligns ()
// Application.Run (win);
// Application.Shutdown ();
// }
}

[Fact, AutoInitShutdown]
public void Dialog_In_Window_With_TexxtField_And_Button_AnchorEnd ()
{
((FakeDriver)Application.Driver).SetBufferSize (20, 5);

var win = new Window ();

int iterations = 0;
Application.Iteration += () => {
if (++iterations > 2) {
Application.RequestStop ();
}
};

win.Loaded += (s, a) => {
var dlg = new Dialog () { Width = 18, Height = 3 };
Button btn = null;
btn = new Button ("Ok") {
X = Pos.AnchorEnd () - Pos.Function (Btn_Width)
};
int Btn_Width ()
{
return (btn?.Bounds.Width) ?? 0;
}
var tf = new TextField ("01234567890123456789") {
Width = Dim.Fill (1) - Dim.Function (Btn_Width)
};

dlg.Loaded += (s, a) => {
Application.Refresh ();
Assert.Equal (new Rect (10, 0, 6, 1), btn.Frame);
Assert.Equal (new Rect (0, 0, 6, 1), btn.Bounds);
var expected = @"
┌──────────────────┐
│┌────────────────┐│
││23456789 [ Ok ]││
│└────────────────┘│
└──────────────────┘";
_ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);

dlg.SetNeedsLayout ();
dlg.LayoutSubviews ();
Application.Refresh ();
Assert.Equal (new Rect (10, 0, 6, 1), btn.Frame);
Assert.Equal (new Rect (0, 0, 6, 1), btn.Bounds);
expected = @"
┌──────────────────┐
│┌────────────────┐│
││23456789 [ Ok ]││
│└────────────────┘│
└──────────────────┘";
_ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
};
dlg.Add (btn, tf);

Application.Run (dlg);
};
Application.Run (win);
}
}
}
Loading